aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-12-27 00:16:09 -0500
committerGitHub <noreply@github.com>2017-12-27 00:16:09 -0500
commitbbaddee2ed4c5f2e534683d9687392d254c37d67 (patch)
treecb7bd886194f207cddb2f654e659acbaa65ee078
parent23b9f24503130f1f3d29f0ab7a33ea3a08e0c064 (diff)
downloadkau-bbaddee2ed4c5f2e534683d9687392d254c37d67.tar.gz
kau-bbaddee2ed4c5f2e534683d9687392d254c37d67.tar.bz2
kau-bbaddee2ed4c5f2e534683d9687392d254c37d67.zip
Feature/fast adapter (#119)
* Update fast adapter implementation * Add repeated click listener
-rw-r--r--adapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt8
-rw-r--r--adapter/src/main/kotlin/ca/allanwang/kau/adapters/RepeatedClickListener.kt53
-rw-r--r--docs/Changelog.md1
-rw-r--r--gradle.properties2
-rw-r--r--mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityBase.kt11
-rw-r--r--mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityOverlayBase.kt2
-rw-r--r--mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt2
-rw-r--r--sample/src/main/res/xml/kau_changelog.xml1
8 files changed, 66 insertions, 14 deletions
diff --git a/adapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt b/adapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt
index 206c66b..dae2862 100644
--- a/adapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt
+++ b/adapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt
@@ -13,10 +13,4 @@ import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter
* Add kotlin's generic syntax to better support out types
*/
fun <T : IItem<*, *>> fastAdapter(vararg adapter: IAdapter<out T>) =
- FastAdapter.with<T, IAdapter<out T>>(adapter.toList())!!
-
-/**
- * Helper to get an [IAdapter] directly from a [FastItemAdapter]
- */
-fun <T : IItem<*, *>> fastAdapter(adapter: IAdapter<out T>, fastAdapter: FastItemAdapter<out T>) =
- fastAdapter(adapter, fastAdapter.itemAdapter) \ No newline at end of file
+ FastAdapter.with<T, IAdapter<out T>>(adapter.toList())!! \ No newline at end of file
diff --git a/adapter/src/main/kotlin/ca/allanwang/kau/adapters/RepeatedClickListener.kt b/adapter/src/main/kotlin/ca/allanwang/kau/adapters/RepeatedClickListener.kt
new file mode 100644
index 0000000..d864a55
--- /dev/null
+++ b/adapter/src/main/kotlin/ca/allanwang/kau/adapters/RepeatedClickListener.kt
@@ -0,0 +1,53 @@
+package ca.allanwang.kau.adapters
+
+import android.support.annotation.IntRange
+import android.view.View
+import com.mikepenz.fastadapter.FastAdapter
+import com.mikepenz.fastadapter.IAdapter
+import com.mikepenz.fastadapter.IItem
+import com.mikepenz.fastadapter.listeners.OnClickListener
+
+/**
+ * Created by Allan Wang on 26/12/17.
+ */
+fun <Item : IItem<*, *>> FastAdapter<Item>.withOnRepeatedClickListener(count: Int,
+ duration: Long,
+ event: OnClickListener<Item>) =
+ withOnClickListener(RepeatedClickListener(count, duration, event))
+
+/**
+ * Registers and skips each click until the designated [count] clicks are triggered,
+ * each within [duration] from each other.
+ * Only then will the [event] be fired, and everything will be reset.
+ */
+private class RepeatedClickListener<Item : IItem<*, *>>(
+ @IntRange(from = 1) val count: Int,
+ @IntRange(from = 1) val duration: Long,
+ val event: OnClickListener<Item>) : OnClickListener<Item> {
+
+ init {
+ if (count <= 0)
+ throw IllegalArgumentException("RepeatedClickListener's count must be > 1")
+ if (duration <= 0)
+ throw IllegalArgumentException("RepeatedClickListener's duration must be > 1L")
+ }
+
+ private var chain = 0
+ private var time = -1L
+
+ override fun onClick(v: View, adapter: IAdapter<Item>, item: Item, position: Int): Boolean {
+ val now = System.currentTimeMillis()
+ if (time - now < duration)
+ chain++
+ else
+ chain = 0
+ time = now
+ if (chain == count) {
+ chain = 0
+ time = -1
+ event.onClick(v, adapter, item, position)
+ return true
+ }
+ return false
+ }
+} \ No newline at end of file
diff --git a/docs/Changelog.md b/docs/Changelog.md
index 2d09855..238294e 100644
--- a/docs/Changelog.md
+++ b/docs/Changelog.md
@@ -1,6 +1,7 @@
# Changelog
## v3.6.0
+* :core: Create kotlin flyweight
* :core: Created BundleUtils
* :core: [Breaking] Refactored startActivity functions
* :kpref-activity: [Breaking] Simplified listener function parameters
diff --git a/gradle.properties b/gradle.properties
index 2dd7002..5c72b0e 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -25,7 +25,7 @@ ANDROID_SUPPORT_LIBS=27.0.2
VERSION_NAME=3.5.1.0
-KOTLIN=1.2.0
+KOTLIN=1.2.10
ABOUT_LIBRARIES=6.0.0
ANKO=0.10.3
BLURRY=2.1.1
diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityBase.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityBase.kt
index c3b6396..464c85e 100644
--- a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityBase.kt
+++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityBase.kt
@@ -48,14 +48,16 @@ abstract class MediaPickerActivityBase(
initializeRecycler(recycler)
- MediaItem.bindEvents(adapter)
- adapter.withSelectionListener({ _, _ -> selectionCount.text = adapter.selections.size.toString() })
+ MediaItem.bindEvents(adapter.fastAdapter)
+ adapter.fastAdapter.withSelectionListener { _, _ ->
+ selectionCount.text = adapter.fastAdapter.selections.size.toString()
+ }
fab.apply {
show()
setIcon(GoogleMaterial.Icon.gmd_send)
setOnClickListener {
- val selection = adapter.selectedItems
+ val selection = adapter.fastAdapter.selectedItems
if (selection.isEmpty()) {
toast(R.string.kau_no_items_selected)
} else {
@@ -88,7 +90,8 @@ abstract class MediaPickerActivityBase(
override fun onLoadFinished(loader: Loader<Cursor>?, data: Cursor?) {
super.onLoadFinished(loader, data)
- setToolbarScrollable((recycler.layoutManager as LinearLayoutManager).findLastCompletelyVisibleItemPosition() < adapter.getItemCount() - 1)
+ setToolbarScrollable((recycler.layoutManager as LinearLayoutManager)
+ .findLastCompletelyVisibleItemPosition() < adapter.adapterItemCount - 1)
}
override fun onStatusChange(loaded: Boolean) {
diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityOverlayBase.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityOverlayBase.kt
index 67f9577..9516eab 100644
--- a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityOverlayBase.kt
+++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityOverlayBase.kt
@@ -29,7 +29,7 @@ abstract class MediaPickerActivityOverlayBase(
super.onCreate(savedInstanceState)
setContentView(R.layout.kau_activity_image_picker_overlay)
initializeRecycler(recycler)
- MediaItemBasic.bindEvents(this, adapter)
+ MediaItemBasic.bindEvents(this, adapter.fastAdapter)
draggable.addExitListener(this, R.transition.kau_image_exit_bottom, R.transition.kau_image_exit_top)
draggable.setOnClickListener { finishAfterTransition() }
diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt
index 9bab4c4..af90fdc 100644
--- a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt
+++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt
@@ -109,7 +109,7 @@ abstract class MediaPickerCore<T : IItem<*, *>>(
private var hasPreloaded = false
private var prefetcher: Future<*>? = null
- val adapter = FastItemAdapter<T>()
+ val adapter = ItemAdapter<T>()
/**
* Further improve preloading by extending the layout space
diff --git a/sample/src/main/res/xml/kau_changelog.xml b/sample/src/main/res/xml/kau_changelog.xml
index f57f8c4..55e110d 100644
--- a/sample/src/main/res/xml/kau_changelog.xml
+++ b/sample/src/main/res/xml/kau_changelog.xml
@@ -7,6 +7,7 @@
-->
<version title="v3.6.0" />
+ <item text=":core: Create kotlin flyweight" />
<item text=":core: Created BundleUtils" />
<item text=":core: [Breaking] Refactored startActivity functions" />
<item text=":kpref-activity: [Breaking] Simplified listener function parameters" />