Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Put everything in one file for ease of importing without gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
acolombo11 committed Jul 8, 2019
1 parent 8d04d50 commit 78be0b8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 36 deletions.
26 changes: 21 additions & 5 deletions minimap-view/src/main/java/eu/acolombo/minimap/MinimapView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import android.view.View
import androidx.core.view.doOnPreDraw
import androidx.recyclerview.widget.RecyclerView

var RecyclerView.minimap: MinimapView
get() = MinimapView(context).apply { setRecyclerView(this@minimap) }
set(value) = value.setRecyclerView(this@minimap)

class MinimapView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
View(context, attrs, defStyleAttr) {

Expand Down Expand Up @@ -120,11 +124,11 @@ class MinimapView @JvmOverloads constructor(context: Context, attrs: AttributeSe
}

private fun updateMapVisibility(scrollableView: View): Boolean {
if (!scrollableView.isBigger()) visibility = GONE
if (!scrollableView.isScrollable()) visibility = GONE
return visibility == VISIBLE
}

private fun View?.isBigger() = this != null && (scrollWidth > this.width || scrollHeight > this.height)
private fun View?.isScrollable() = this != null && (scrollWidth > this.width || scrollHeight > this.height)

private fun moveIndicator(dx: Int, dy: Int) = if (scaleFactor != 0f) {
indicatorX += dx / scaleFactor
Expand Down Expand Up @@ -189,6 +193,18 @@ class MinimapView @JvmOverloads constructor(context: Context, attrs: AttributeSe

}

var RecyclerView.minimap: MinimapView
get() = MinimapView(context).apply { setRecyclerView(this@minimap) }
set(value) = value.setRecyclerView(this@minimap)
// Extensions to have cleaner code in [setRecyclerView]
fun RecyclerView.addScrollListener(func: (dx: Int, dy: Int) -> Unit) {
addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
func(dx, dy)
}
})
}

fun View.addLayoutChangeListener(func: () -> Unit) {
addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
func()
}
}
31 changes: 0 additions & 31 deletions minimap-view/src/main/java/eu/acolombo/minimap/RecyclerViewExt.kt

This file was deleted.

0 comments on commit 78be0b8

Please sign in to comment.