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 --- .../main/kotlin/ca/allanwang/kau/kotlin/LazyUi.kt | 6 +++ .../ca/allanwang/kau/swipe/SwipeBackLayout.kt | 50 ++++++++++++++-------- 2 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyUi.kt (limited to 'core') 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) -- cgit v1.2.3