aboutsummaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
Diffstat (limited to 'core/src')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt (renamed from core/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt)14
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt44
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt2
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt18
4 files changed, 56 insertions, 22 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt b/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt
index 2fbecf5..14655f0 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt
@@ -16,6 +16,7 @@ open class KauLogger(val tag: String) {
open var enabled = true
open var showPrivateText = false
+ open var messageJoiner: (msg: String, privMsg: String) -> String = { msg, privMsg -> "$msg: $privMsg" }
/**
* Filter pass-through to decide what we wish to log
@@ -43,11 +44,14 @@ open class KauLogger(val tag: String) {
= enabled && filter(priority)
protected open fun logImpl(priority: Int, message: String?, privateMessage: String?, t: Throwable?) {
- var text = message ?: ""
- if (showPrivateText && privateMessage != null)
- text += "\n-\t$privateMessage"
- if (t != null) Log.e(tag, text, t)
- else if (text.isNotBlank()) Log.println(priority, tag, text)
+ val text = if (showPrivateText) {
+ if (message == null) privateMessage
+ else if (privateMessage == null) message
+ else messageJoiner(message, privateMessage)
+ } else message
+
+ if (t != null) Log.e(tag, text ?: "Error", t)
+ else if (!text.isNullOrBlank()) Log.println(priority, tag, text)
}
open fun v(text: String?, privateText: String? = null) = log(Log.VERBOSE, text, privateText)
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 0859ac5..bc909be 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt
@@ -7,8 +7,6 @@ import ca.allanwang.kau.logging.KL
import ca.allanwang.kau.swipe.SwipeBackHelper.onDestroy
import java.util.*
-internal 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
@@ -30,14 +28,10 @@ internal object SwipeBackHelper {
else -> R.anim.kau_slide_in_top
}
activity.overridePendingTransition(startAnimation, 0)
+ page.onPostCreate()
KL.v("KauSwipe onCreate ${activity.localClassName}")
}
- fun onPostCreate(activity: Activity) {
- this[activity]?.onPostCreate() ?: throw SwipeBackException()
- KL.v("KauSwipe onPostCreate ${activity.localClassName}")
- }
-
fun onDestroy(activity: Activity) {
val page: SwipeBackPage? = this[activity]
pageStack.kauRemoveIf { it.activityRef.get() == null || it === page }
@@ -55,17 +49,43 @@ internal object SwipeBackHelper {
}
/**
- * 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
+ * The creation binder, which adds the swipe functionality to an activity.
+ * Call this during [Activity.onCreate] after all views are added.
+ *
+ * Preferably, this should be the last line in the onCreate method.
+ * Note that this will also capture your statusbar color and nav bar color,
+ * so be sure to assign those beforehand if at all.
+ *
+ * Lastly, don't forget to call [kauSwipeOnDestroy] as well when the activity is destroyed.
*/
fun Activity.kauSwipeOnCreate(builder: SwipeBackContract.() -> Unit = {}) = SwipeBackHelper.onCreate(this, builder)
-fun Activity.kauSwipeOnPostCreate() = SwipeBackHelper.onPostCreate(this)
+/**
+ * Deprecated as onPostCreate seems unreliable.
+ * We will instead initialize everything during [kauSwipeOnCreate]
+ */
+@Deprecated(level = DeprecationLevel.WARNING, message = "All functionality has been moved to kauSwipeOnCreate")
+fun Activity.kauSwipeOnPostCreate() {
+}
+
+/**
+ * The unbinder, which removes our layouts, releases our weak references, and cleans our page stack
+ * Call this during [Activity.onDestroy]
+ *
+ * Given that our references are held weakly, we allow failures and missing pages to pass silently
+ * as they should be destroyed anyways.
+ *
+ * Don't forget to call [kauSwipeOnCreate] when the activity is created to enable swipe functionality.
+ */
fun Activity.kauSwipeOnDestroy() = SwipeBackHelper.onDestroy(this)
-fun Activity.kauSwipeFinish() = SwipeBackHelper.finish(this)
/**
+ * Helper function for activities to animate the finish transaction with a pseudo swipe
+ * The activity will automatically be finished afterwards
+ */
+fun Activity.kauSwipeFinish() = SwipeBackHelper.finish(this)
+
+/*
* Constants used for the swipe edge flags
*/
const val SWIPE_EDGE_LEFT = ViewDragHelper.EDGE_LEFT
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 51cd17f..c0875d1 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt
@@ -115,7 +115,7 @@ internal class SwipeBackLayout @JvmOverloads constructor(context: Context, attrs
*/
set(value) {
if (value !in arrayOf(SWIPE_EDGE_TOP, SWIPE_EDGE_BOTTOM, SWIPE_EDGE_LEFT, SWIPE_EDGE_RIGHT))
- throw SwipeBackException("Edge flag is not valid; use one of the SWIPE_EDGE_* values")
+ throw IllegalArgumentException("Edge flag is not valid; use one of the SWIPE_EDGE_* values")
field = value
horizontal = edgeFlag == SWIPE_EDGE_LEFT || edgeFlag == SWIPE_EDGE_RIGHT
dragHelper.setEdgeTrackingEnabled(value)
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt
index 2f3e9a5..9e0ad75 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt
@@ -7,10 +7,8 @@ import android.graphics.Canvas
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.os.Handler
-import android.os.Looper
import android.support.annotation.IntRange
import ca.allanwang.kau.R
-import ca.allanwang.kau.logging.KL
import java.math.RoundingMode
import java.text.DecimalFormat
@@ -30,13 +28,25 @@ annotation class KauUtils
get() = this * Resources.getSystem().displayMetrics.density
@KauUtils inline val Int.dpToPx: Int
- get() = (this * Resources.getSystem().displayMetrics.density).toInt()
+ get() = toFloat().dpToPx.toInt()
@KauUtils inline val Float.pxToDp: Float
get() = this / Resources.getSystem().displayMetrics.density
@KauUtils inline val Int.pxToDp: Int
- get() = (this / Resources.getSystem().displayMetrics.density).toInt()
+ get() = toFloat().pxToDp.toInt()
+
+@KauUtils inline val Float.dpToSp: Float
+ get() = this * Resources.getSystem().displayMetrics.scaledDensity
+
+@KauUtils inline val Int.dpToSp: Int
+ get() = toFloat().dpToSp.toInt()
+
+@KauUtils inline val Float.spToDp: Float
+ get() = this / Resources.getSystem().displayMetrics.scaledDensity
+
+@KauUtils inline val Int.spToDp: Int
+ get() = toFloat().spToDp.toInt()
/**
* Converts minute value to string