From 10617f02a95b162695ea068c0be6acceda74cf35 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 12 Aug 2017 10:09:01 -0700 Subject: Release 3.3.1 (#36) * Add open message joiner function * Update text extraction * Fix background tint * Rename logger file * Test codecov * Remove coverage * Enhancement/swipe (#35) * Merge swipe onPostCreate with swipe onCreate * Update samples and docs * Add deprecated method to maintain compatibility * Replace exception with illegal argument * Check if parent exists before configurations in searchview --- .../kotlin/ca/allanwang/kau/logging/KauLogger.kt | 76 ++++++++++++++++++++++ .../ca/allanwang/kau/logging/TimberLogger.kt | 72 -------------------- .../ca/allanwang/kau/swipe/SwipeBackHelper.kt | 44 +++++++++---- .../ca/allanwang/kau/swipe/SwipeBackLayout.kt | 2 +- .../main/kotlin/ca/allanwang/kau/utils/Utils.kt | 18 +++-- 5 files changed, 123 insertions(+), 89 deletions(-) create mode 100644 core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt delete mode 100644 core/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt (limited to 'core/src/main/kotlin/ca') diff --git a/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt b/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt new file mode 100644 index 0000000..14655f0 --- /dev/null +++ b/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt @@ -0,0 +1,76 @@ +@file:Suppress("NOTHING_TO_INLINE") + +package ca.allanwang.kau.logging + +import android.os.Looper +import android.util.Log + + +/** + * Created by Allan Wang on 2017-05-28. + * + * Base logger class with a predefined tag + * This may be extended by an object to effectively replace [Log] + */ +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 + * By default, we will ignore verbose and debug logs + * @returns {@code true} to log the message, {@code false} to ignore + */ + open var filter: (Int) -> Boolean = { it != Log.VERBOSE && it != Log.DEBUG } + + open fun disable(disable: Boolean = true): KauLogger { + enabled = !disable + return this + } + + open fun debug(enable: Boolean) { + filter = if (enable) { _ -> true } else { i -> i != Log.VERBOSE && i != Log.DEBUG } + showPrivateText = enable + } + + open fun log(priority: Int, message: String?, privateMessage: String?, t: Throwable? = null) { + if (!shouldLog(priority, message, privateMessage, t)) return + logImpl(priority, message, privateMessage, t) + } + + protected open fun shouldLog(priority: Int, message: String?, privateMessage: String?, t: Throwable?): Boolean + = enabled && filter(priority) + + protected open fun logImpl(priority: Int, message: String?, privateMessage: String?, t: Throwable?) { + 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) + open fun d(text: String?, privateText: String? = null) = log(Log.DEBUG, text, privateText) + open fun i(text: String?, privateText: String? = null) = log(Log.INFO, text, privateText) + open fun e(text: String?, privateText: String? = null) = log(Log.ERROR, text, privateText) + open fun a(text: String?, privateText: String? = null) = log(Log.ASSERT, text, privateText) + open fun e(t: Throwable?, text: String?, privateText: String? = null) = log(Log.ERROR, text, privateText, t) + open fun eThrow(text: String?) { + if (text != null) + e(Throwable(text), text) + } + + /** + * Log the looper + */ + open fun checkThread(id: Int) { + val name = Thread.currentThread().name + val status = if (Looper.myLooper() == Looper.getMainLooper()) "is" else "is not" + d("$id $status in the main thread - thread name: $name") + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt b/core/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt deleted file mode 100644 index 2fbecf5..0000000 --- a/core/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt +++ /dev/null @@ -1,72 +0,0 @@ -@file:Suppress("NOTHING_TO_INLINE") - -package ca.allanwang.kau.logging - -import android.os.Looper -import android.util.Log - - -/** - * Created by Allan Wang on 2017-05-28. - * - * Base logger class with a predefined tag - * This may be extended by an object to effectively replace [Log] - */ -open class KauLogger(val tag: String) { - - open var enabled = true - open var showPrivateText = false - - /** - * Filter pass-through to decide what we wish to log - * By default, we will ignore verbose and debug logs - * @returns {@code true} to log the message, {@code false} to ignore - */ - open var filter: (Int) -> Boolean = { it != Log.VERBOSE && it != Log.DEBUG } - - open fun disable(disable: Boolean = true): KauLogger { - enabled = !disable - return this - } - - open fun debug(enable: Boolean) { - filter = if (enable) { _ -> true } else { i -> i != Log.VERBOSE && i != Log.DEBUG } - showPrivateText = enable - } - - open fun log(priority: Int, message: String?, privateMessage: String?, t: Throwable? = null) { - if (!shouldLog(priority, message, privateMessage, t)) return - logImpl(priority, message, privateMessage, t) - } - - protected open fun shouldLog(priority: Int, message: String?, privateMessage: String?, t: Throwable?): Boolean - = 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) - } - - open fun v(text: String?, privateText: String? = null) = log(Log.VERBOSE, text, privateText) - open fun d(text: String?, privateText: String? = null) = log(Log.DEBUG, text, privateText) - open fun i(text: String?, privateText: String? = null) = log(Log.INFO, text, privateText) - open fun e(text: String?, privateText: String? = null) = log(Log.ERROR, text, privateText) - open fun a(text: String?, privateText: String? = null) = log(Log.ASSERT, text, privateText) - open fun e(t: Throwable?, text: String?, privateText: String? = null) = log(Log.ERROR, text, privateText, t) - open fun eThrow(text: String?) { - if (text != null) - e(Throwable(text), text) - } - - /** - * Log the looper - */ - open fun checkThread(id: Int) { - val name = Thread.currentThread().name - val status = if (Looper.myLooper() == Looper.getMainLooper()) "is" else "is not" - d("$id $status in the main thread - thread name: $name") - } -} \ No newline at end of file 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 -- cgit v1.2.3