diff options
author | Allan Wang <me@allanwang.ca> | 2017-07-05 21:17:03 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2017-07-05 21:17:03 -0700 |
commit | 82a98786f301cd8e7fcafc44bf760f0434bd2eb2 (patch) | |
tree | fee179c415d719123cadb494bb84c2d5785a5657 /core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt | |
parent | 9bc520fc9041fed26ef965ef2420a0552ccc20d6 (diff) | |
download | kau-82a98786f301cd8e7fcafc44bf760f0434bd2eb2.tar.gz kau-82a98786f301cd8e7fcafc44bf760f0434bd2eb2.tar.bz2 kau-82a98786f301cd8e7fcafc44bf760f0434bd2eb2.zip |
Bring back swipe
Diffstat (limited to 'core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt')
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt index 92cdd7c..b24c7d8 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt @@ -2,11 +2,16 @@ package ca.allanwang.kau.swipe import android.app.Activity import ca.allanwang.kau.R -import ca.allanwang.kau.logging.KL +import ca.allanwang.kau.swipe.SwipeBackHelper.onDestroy import java.util.* -class SwipeBackException(message: String = "You Should call SwipeBackHelper.onCreate(activity) first") : RuntimeException(message) +class SwipeBackException(message: String = "You Should call kauSwipeOnCreate() first") : RuntimeException(message) +/** + * Singleton to hold our swipe stack + * All activity pages held with strong references, so it is crucial to call + * [onDestroy] whenever an activity should be disposed + */ object SwipeBackHelper { private val pageStack = Stack<SwipeBackPage>() @@ -16,7 +21,7 @@ object SwipeBackHelper { fun getCurrentPage(activity: Activity): SwipeBackPage = this[activity] - fun onCreate(activity: Activity, builder: SwipeBackPage.() -> Unit = {}) { + fun onCreate(activity: Activity, builder: SwipeBackContract.() -> Unit = {}) { val page = pageStack.firstOrNull { it.activity === activity } ?: pageStack.push(SwipeBackPage(activity).apply { builder() }) val startAnimation: Int = with(page.edgeFlag) { if (this and SWIPE_EDGE_LEFT > 0) R.anim.kau_slide_in_right @@ -30,7 +35,6 @@ object SwipeBackHelper { fun onPostCreate(activity: Activity) = this[activity].onPostCreate() fun onDestroy(activity: Activity) { - KL.d("Swipe destroy") val page: SwipeBackPage = this[activity] pageStack.remove(page) page.activity = null @@ -45,21 +49,24 @@ object SwipeBackHelper { } -fun Activity.kauSwipeOnCreate(builder: SwipeBackPage.() -> Unit = {}) = SwipeBackHelper.onCreate(this, builder) +/** + * The following are the activity bindings to add an activity to the stack + * onCreate, onPostCreate, and onDestroy are mandatory + * finish is there as a helper method to animate the transaction + */ +fun Activity.kauSwipeOnCreate(builder: SwipeBackContract.() -> Unit = {}) = SwipeBackHelper.onCreate(this, builder) + fun Activity.kauSwipeOnPostCreate() = SwipeBackHelper.onPostCreate(this) fun Activity.kauSwipeOnDestroy() = SwipeBackHelper.onDestroy(this) fun Activity.kauSwipeFinish() = SwipeBackHelper.finish(this) +/** + * Constants used for the swipe edge flags + */ const val SWIPE_EDGE_LEFT = ViewDragHelper.EDGE_LEFT const val SWIPE_EDGE_RIGHT = ViewDragHelper.EDGE_RIGHT const val SWIPE_EDGE_TOP = ViewDragHelper.EDGE_TOP -const val SWIPE_EDGE_BOTTOM = ViewDragHelper.EDGE_BOTTOM - -const val SWIPE_EDGE_HORIZONTAL = SWIPE_EDGE_LEFT or SWIPE_EDGE_RIGHT - -const val SWIPE_EDGE_VERTICAL = SWIPE_EDGE_TOP or SWIPE_EDGE_BOTTOM - -const val SWIPE_EDGE_ALL = SWIPE_EDGE_HORIZONTAL or SWIPE_EDGE_VERTICAL
\ No newline at end of file +const val SWIPE_EDGE_BOTTOM = ViewDragHelper.EDGE_BOTTOM
\ No newline at end of file |