aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2019-12-31 11:34:04 -0800
committerGitHub <noreply@github.com>2019-12-31 11:34:04 -0800
commit12ec13d1d406951a453d6c88142caa152fa9e351 (patch)
tree17e2d344469b4cb7af5750880630ff7da31a32b4
parent91da44158d9aab832317a16fc0e63f48f3e5e940 (diff)
parent5388aabf0b581a69405125ec0cdaf05ae2455133 (diff)
downloadkau-12ec13d1d406951a453d6c88142caa152fa9e351.tar.gz
kau-12ec13d1d406951a453d6c88142caa152fa9e351.tar.bz2
kau-12ec13d1d406951a453d6c88142caa152fa9e351.zip
Merge pull request #240 from AllanWang/lazyui
Add lazy ui
-rw-r--r--core-ui/src/main/kotlin/ca/allanwang/kau/ui/widgets/TextSlider.kt9
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyUi.kt6
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt50
-rw-r--r--kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefActivity.kt5
-rw-r--r--kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemBase.kt3
-rw-r--r--mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt3
6 files changed, 51 insertions, 25 deletions
diff --git a/core-ui/src/main/kotlin/ca/allanwang/kau/ui/widgets/TextSlider.kt b/core-ui/src/main/kotlin/ca/allanwang/kau/ui/widgets/TextSlider.kt
index 726f5b6..91e6b67 100644
--- a/core-ui/src/main/kotlin/ca/allanwang/kau/ui/widgets/TextSlider.kt
+++ b/core-ui/src/main/kotlin/ca/allanwang/kau/ui/widgets/TextSlider.kt
@@ -25,6 +25,7 @@ import android.view.animation.AnimationUtils
import android.widget.TextSwitcher
import android.widget.TextView
import androidx.core.widget.TextViewCompat
+import ca.allanwang.kau.kotlin.lazyUi
import ca.allanwang.kau.ui.R
import java.util.EmptyStackException
import java.util.Stack
@@ -66,10 +67,10 @@ class TextSlider @JvmOverloads constructor(
private val prevIn: Int,
private val prevOut: Int
) {
- val NEXT_IN: Animation by lazy { AnimationUtils.loadAnimation(context, nextIn) }
- val NEXT_OUT: Animation by lazy { AnimationUtils.loadAnimation(context, nextOut) }
- val PREV_IN: Animation by lazy { AnimationUtils.loadAnimation(context, prevIn) }
- val PREV_OUT: Animation by lazy { AnimationUtils.loadAnimation(context, prevOut) }
+ val NEXT_IN: Animation by lazyUi { AnimationUtils.loadAnimation(context, nextIn) }
+ val NEXT_OUT: Animation by lazyUi { AnimationUtils.loadAnimation(context, nextOut) }
+ val PREV_IN: Animation by lazyUi { AnimationUtils.loadAnimation(context, prevIn) }
+ val PREV_OUT: Animation by lazyUi { AnimationUtils.loadAnimation(context, prevOut) }
}
companion object {
diff --git a/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyUi.kt b/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyUi.kt
new file mode 100644
index 0000000..28994e4
--- /dev/null
+++ b/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyUi.kt
@@ -0,0 +1,6 @@
+package ca.allanwang.kau.kotlin
+
+/**
+ * Shortcut for unsynchronized lazy block
+ */
+fun <T> lazyUi(initializer: () -> T): Lazy<T> = lazy(LazyThreadSafetyMode.NONE, initializer) \ No newline at end of file
diff --git a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt
index b2d9a5f..4f9d54f 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt
@@ -105,21 +105,18 @@ internal class SwipeBackLayout @JvmOverloads constructor(
private var statusBarBase: Int = 0
private var navBarBase: Int = 0
- val chromeFadeListener: SwipeListener by lazy {
- object : SwipeListener {
- @SuppressLint("NewApi")
- override fun onScroll(percent: Float, px: Int, edgeFlag: Int) {
- if (!transitionSystemBars) return
- activity?.apply {
- statusBarColor = statusBarBase.adjustAlpha(scrimOpacity)
- navigationBarColor = navBarBase.adjustAlpha(scrimOpacity)
- }
+ private val chromeFadeListener: SwipeListener = object : SwipeListener {
+ override fun onScroll(percent: Float, px: Int, edgeFlag: Int) {
+ if (!transitionSystemBars) return
+ activity?.apply {
+ statusBarColor = statusBarBase.adjustAlpha(scrimOpacity)
+ navigationBarColor = navBarBase.adjustAlpha(scrimOpacity)
}
+ }
- override fun onEdgeTouch() {}
+ override fun onEdgeTouch() {}
- override fun onScrollToClose(edgeFlag: Int) {}
- }
+ override fun onScrollToClose(edgeFlag: Int) {}
}
private var inLayout: Boolean = false
@@ -135,7 +132,13 @@ internal class SwipeBackLayout @JvmOverloads constructor(
* We will verify that only one axis is used at a time
*/
set(value) {
- if (value !in arrayOf(SWIPE_EDGE_TOP, SWIPE_EDGE_BOTTOM, SWIPE_EDGE_LEFT, SWIPE_EDGE_RIGHT)) {
+ if (value !in arrayOf(
+ SWIPE_EDGE_TOP,
+ SWIPE_EDGE_BOTTOM,
+ SWIPE_EDGE_LEFT,
+ SWIPE_EDGE_RIGHT
+ )
+ ) {
throw IllegalArgumentException("Edge flag is not valid; use one of the SWIPE_EDGE_* values")
}
field = value
@@ -275,7 +278,12 @@ internal class SwipeBackLayout @JvmOverloads constructor(
xOffset = 0
yOffset = contentOffset
}
- contentView.layout(xOffset, yOffset, xOffset + contentView.measuredWidth, yOffset + contentView.measuredHeight)
+ contentView.layout(
+ xOffset,
+ yOffset,
+ xOffset + contentView.measuredWidth,
+ yOffset + contentView.measuredHeight
+ )
inLayout = false
}
@@ -364,13 +372,21 @@ internal class SwipeBackLayout @JvmOverloads constructor(
return if (!horizontal) 1 else 0
}
- override fun onViewPositionChanged(changedView: View, left: Int, top: Int, dx: Int, dy: Int) {
+ override fun onViewPositionChanged(
+ changedView: View,
+ left: Int,
+ top: Int,
+ dx: Int,
+ dy: Int
+ ) {
super.onViewPositionChanged(changedView, left, top, dx, dy)
val contentView = contentViewRef.get()
?: return KL.e { "KauSwipe cannot change view position as contentView is null; is onPostCreate called?" }
// make sure that we are using the proper axis
- scrollPercent = abs(if (horizontal) left.toFloat() / contentView.width
- else (top.toFloat() / contentView.height))
+ scrollPercent = abs(
+ if (horizontal) left.toFloat() / contentView.width
+ else (top.toFloat() / contentView.height)
+ )
contentOffset = if (horizontal) left else top
invalidate()
if (scrollPercent < scrollThreshold && !isScrollOverValid)
diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefActivity.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefActivity.kt
index 90827d3..818a770 100644
--- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefActivity.kt
+++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefActivity.kt
@@ -24,6 +24,7 @@ import ca.allanwang.kau.animators.KauAnimator
import ca.allanwang.kau.animators.SlideAnimatorAdd
import ca.allanwang.kau.animators.SlideAnimatorRemove
import ca.allanwang.kau.internal.KauBaseActivity
+import ca.allanwang.kau.kotlin.lazyUi
import ca.allanwang.kau.kpref.activity.items.KPrefItemCore
import ca.allanwang.kau.ui.views.RippleCanvas
import ca.allanwang.kau.utils.KAU_LEFT
@@ -53,13 +54,13 @@ abstract class KPrefActivity : KauBaseActivity(), KPrefActivityContract {
*/
var animate: Boolean = true
- private val recyclerAnimatorNext: KauAnimator by lazy {
+ private val recyclerAnimatorNext: KauAnimator by lazyUi {
KauAnimator(
SlideAnimatorAdd(KAU_RIGHT, itemDelayFactor = 0f),
SlideAnimatorRemove(KAU_LEFT, itemDelayFactor = 0f)
)
}
- private val recyclerAnimatorPrev: KauAnimator by lazy {
+ private val recyclerAnimatorPrev: KauAnimator by lazyUi {
KauAnimator(
SlideAnimatorAdd(KAU_LEFT, itemDelayFactor = 0f),
SlideAnimatorRemove(KAU_RIGHT, itemDelayFactor = 0f)
diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemBase.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemBase.kt
index 9197057..16363e2 100644
--- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemBase.kt
+++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemBase.kt
@@ -17,6 +17,7 @@ package ca.allanwang.kau.kpref.activity.items
import android.view.View
import androidx.annotation.CallSuper
+import ca.allanwang.kau.kotlin.lazyUi
import ca.allanwang.kau.kpref.activity.GlobalOptions
import ca.allanwang.kau.kpref.activity.KClick
import ca.allanwang.kau.kpref.activity.KPrefItemActions
@@ -59,7 +60,7 @@ abstract class KPrefItemBase<T>(protected val base: BaseContract<T>) : KPrefItem
val kclick = object : KClick<T> {
override val context = itemView.context
override val itemView = itemView
- override val innerView: View? by lazy { itemView.findViewById<View>(R.id.kau_pref_inner_content) }
+ override val innerView: View? by lazyUi { itemView.findViewById<View>(R.id.kau_pref_inner_content) }
override val item = this@KPrefItemBase
}
if (_enabled) {
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 a84df8a..e65cab2 100644
--- a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt
+++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt
@@ -38,6 +38,7 @@ import ca.allanwang.kau.animators.FadeScaleAnimatorAdd
import ca.allanwang.kau.animators.KauAnimator
import ca.allanwang.kau.internal.KauBaseActivity
import ca.allanwang.kau.kotlin.lazyContext
+import ca.allanwang.kau.kotlin.lazyUi
import ca.allanwang.kau.logging.KL
import ca.allanwang.kau.permissions.kauRequestPermissions
import ca.allanwang.kau.utils.dimenPixelSize
@@ -131,7 +132,7 @@ abstract class MediaPickerCore<T : GenericItem>(
/**
* Further improve preloading by extending the layout space
*/
- val extraSpace: Int by lazy { resources.displayMetrics.heightPixels }
+ val extraSpace: Int by lazyUi { resources.displayMetrics.heightPixels }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)