From 5388aabf0b581a69405125ec0cdaf05ae2455133 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 31 Dec 2019 11:25:29 -0800 Subject: Add lazy ui --- .../ca/allanwang/kau/ui/widgets/TextSlider.kt | 9 ++-- .../main/kotlin/ca/allanwang/kau/kotlin/LazyUi.kt | 6 +++ .../ca/allanwang/kau/swipe/SwipeBackLayout.kt | 50 ++++++++++++++-------- .../allanwang/kau/kpref/activity/KPrefActivity.kt | 5 ++- .../kau/kpref/activity/items/KPrefItemBase.kt | 3 +- .../allanwang/kau/mediapicker/MediaPickerCore.kt | 3 +- 6 files changed, 51 insertions(+), 25 deletions(-) create mode 100644 core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyUi.kt 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 lazyUi(initializer: () -> T): Lazy = 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(protected val base: BaseContract) : KPrefItem val kclick = object : KClick { override val context = itemView.context override val itemView = itemView - override val innerView: View? by lazy { itemView.findViewById(R.id.kau_pref_inner_content) } + override val innerView: View? by lazyUi { itemView.findViewById(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( /** * 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) -- cgit v1.2.3