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 +++-- docs/Changelog.md | 5 ++ .../kotlin/ca/allanwang/kau/sample/AnimActivity.kt | 8 +-- .../ca/allanwang/kau/sample/SwipeActivity.kt | 13 ++-- sample/src/main/res/xml/kau_changelog.xml | 11 +++- .../ca/allanwang/kau/searchview/SearchView.kt | 11 ++-- 10 files changed, 147 insertions(+), 113 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 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 diff --git a/docs/Changelog.md b/docs/Changelog.md index 0fe867a..a69171e 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -1,5 +1,10 @@ # Changelog +## v3.3.1 +* :core: Open up all logger functions +* :core: Deprecate kauSwipeOnPostCreate and move functionality to onCreate +* :searchview: Fix background tint + ## v3.3.0 * :core: Create debounce methods * :core: Create zip methods diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt index cd86d38..f052d97 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt @@ -10,7 +10,6 @@ import ca.allanwang.kau.permissions.kauRequestPermissions import ca.allanwang.kau.swipe.SWIPE_EDGE_LEFT import ca.allanwang.kau.swipe.kauSwipeOnCreate import ca.allanwang.kau.swipe.kauSwipeOnDestroy -import ca.allanwang.kau.swipe.kauSwipeOnPostCreate import ca.allanwang.kau.utils.fullLinearRecycler import ca.allanwang.kau.utils.startActivitySlideOut import ca.allanwang.kau.utils.toast @@ -49,14 +48,9 @@ class AnimActivity : KauBaseActivity() { } } - override fun onPostCreate(savedInstanceState: Bundle?) { - super.onPostCreate(savedInstanceState) - kauSwipeOnPostCreate() - } - override fun onDestroy() { - super.onDestroy() kauSwipeOnDestroy() + super.onDestroy() } override fun onBackPressed() { diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/SwipeActivity.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/SwipeActivity.kt index cba9ccd..da088cf 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/SwipeActivity.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/SwipeActivity.kt @@ -40,9 +40,6 @@ class SwipeActivity : KauBaseActivity() { it.setOnClickListener { startActivityWithEdge(swipeEdge) } } val flag = intent.getIntExtra(SWIPE_EDGE, -1) - kauSwipeOnCreate { - edgeFlag = flag - } toolbar.title = when (flag) { SWIPE_EDGE_LEFT -> "Left Edge Swipe" SWIPE_EDGE_RIGHT -> "Right Edge Swipe" @@ -57,16 +54,14 @@ class SwipeActivity : KauBaseActivity() { val bg = headerColor.darken(0.2f) container.setBackgroundColor(bg) navigationBarColor = bg - } - - override fun onPostCreate(savedInstanceState: Bundle?) { - super.onPostCreate(savedInstanceState) - kauSwipeOnPostCreate() + kauSwipeOnCreate { + edgeFlag = flag + } } override fun onDestroy() { - super.onDestroy() kauSwipeOnDestroy() + super.onDestroy() } override fun onBackPressed() { diff --git a/sample/src/main/res/xml/kau_changelog.xml b/sample/src/main/res/xml/kau_changelog.xml index 6008ae1..fd3113c 100644 --- a/sample/src/main/res/xml/kau_changelog.xml +++ b/sample/src/main/res/xml/kau_changelog.xml @@ -6,6 +6,14 @@ --> + + + + + + + + @@ -14,9 +22,6 @@ - - - diff --git a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt index cc56289..1376115 100644 --- a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt +++ b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt @@ -10,7 +10,6 @@ import android.support.transition.ChangeBounds import android.support.transition.TransitionManager import android.support.transition.TransitionSet import android.support.v7.widget.AppCompatEditText -import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView import android.text.Editable import android.text.TextWatcher @@ -153,7 +152,7 @@ class SearchView @JvmOverloads constructor( } if (SearchItem.backgroundColor != backgroundColor) { SearchItem.backgroundColor = backgroundColor - tintForeground(backgroundColor) + tintBackground(backgroundColor) } val icons = mutableListOf(navIcon to iconNav, clearIcon to iconClear) val extra = extraIcon @@ -291,11 +290,12 @@ class SearchView @JvmOverloads constructor( */ fun bind(menu: Menu, @IdRes id: Int, @ColorInt menuIconColor: Int = Color.WHITE, config: Configs.() -> Unit = {}): SearchView { config(config) - menuItem = menu.findItem(id) ?: throw IllegalArgumentException("Menu item with given id doesn't exist") - if (menuItem!!.icon == null) menuItem!!.icon = GoogleMaterial.Icon.gmd_search.toDrawable(context, 18, menuIconColor) + val menuItem = menu.findItem(id) ?: throw IllegalArgumentException("Menu item with given id doesn't exist") + if (menuItem.icon == null) menuItem.icon = GoogleMaterial.Icon.gmd_search.toDrawable(context, 18, menuIconColor) card.gone() - menuItem!!.setOnMenuItemClickListener { configureCoords(it); revealOpen(); true } + menuItem.setOnMenuItemClickListener { configureCoords(it); revealOpen(); true } shadow.setOnClickListener { revealClose() } + this.menuItem = menuItem return this } @@ -310,6 +310,7 @@ class SearchView @JvmOverloads constructor( } private fun configureCoords(item: MenuItem) { + if (parent !is ViewGroup) return val view = parentViewGroup.findViewById(item.itemId) ?: return val locations = IntArray(2) view.getLocationOnScreen(locations) -- cgit v1.2.3