From 26421aa428c669f4123fca7094fff0e1d90b5387 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Wed, 30 Aug 2017 12:13:38 -0400 Subject: fix/mediapicker (#50) * Bring all glide request managers to one instance * Switch to test implementation * Check if parent is null for searchview * Ensure open close runs on ui thread * Make glide contract internal * Update changelog * Update version Update changelog for previous prs --- .../ca/allanwang/kau/searchview/SearchView.kt | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'searchview') 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 491fbe9..dabf4bc 100644 --- a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt +++ b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt @@ -5,7 +5,6 @@ import android.content.Context import android.content.res.ColorStateList import android.graphics.Color import android.support.annotation.ColorInt -import android.support.annotation.ColorRes import android.support.annotation.IdRes import android.support.transition.ChangeBounds import android.support.transition.TransitionManager @@ -215,7 +214,7 @@ class SearchView @JvmOverloads constructor( val adapter = FastItemAdapter() var menuItem: MenuItem? = null val isOpen: Boolean - get() = isAttachedToWindow && card.isVisible + get() = parent != null && card.isVisible /* * Ripple start points and search view offset @@ -306,7 +305,7 @@ class SearchView @JvmOverloads constructor( 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 { revealOpen(); true } shadow.setOnClickListener { revealClose() } this.menuItem = menuItem return this @@ -322,7 +321,8 @@ class SearchView @JvmOverloads constructor( menuItem = null } - private fun configureCoords(item: MenuItem) { + private fun configureCoords(item: MenuItem?) { + item ?: return if (parent !is ViewGroup) return val view = parentViewGroup.findViewById(item.itemId) ?: return val locations = IntArray(2) @@ -334,7 +334,7 @@ class SearchView @JvmOverloads constructor( override fun onPreDraw(): Boolean { view.viewTreeObserver.removeOnPreDrawListener(this) card.setMarginTop(menuY - card.height / 2) - return false + return true } }) } @@ -372,14 +372,15 @@ class SearchView @JvmOverloads constructor( } fun revealOpen() { - if (isOpen) return - post { + if (parent == null || isOpen) return + context.runOnUiThread { /** * The y component is relative to the cardView, but it hasn't been drawn yet so its own height is 0 * We therefore use half the menuItem height, which is a close approximation to our intended value * The cardView matches the parent's width, so menuX is correct */ - configs.openListener?.invoke(this) + configureCoords(menuItem) + configs.openListener?.invoke(this@SearchView) shadow.fadeIn() editText.showKeyboard() card.circularReveal(menuX, menuHalfHeight, duration = configs.revealDuration) { @@ -390,8 +391,8 @@ class SearchView @JvmOverloads constructor( } fun revealClose() { - if (!isOpen) return - post { + if (parent == null || !isOpen) return + context.runOnUiThread { shadow.fadeOut(duration = configs.transitionDuration) cardTransition { addEndListener { -- cgit v1.2.3 From b87c75d607956393ad3b07751eb59ccf41726863 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Wed, 11 Oct 2017 14:17:27 -0400 Subject: fix/misc (#81) * Remove jvmstatic, fixes #68 * Create HO logging * Remove double null boolean notation * Replace multi if else with when * Ignore case in setSpan, closes #82 --- .../main/kotlin/ca/allanwang/kau/about/FaqIItem.kt | 6 ++--- .../kotlin/ca/allanwang/kau/about/LibraryIItem.kt | 6 ++--- .../kotlin/ca/allanwang/kau/iitems/CardIItem.kt | 2 +- .../ca/allanwang/kau/colorpicker/CircleView.kt | 4 ---- .../kotlin/ca/allanwang/kau/ui/views/CutoutView.kt | 12 ++++++---- .../kotlin/ca/allanwang/kau/logging/KauLogger.kt | 28 +++++++++++++++++++++- .../allanwang/kau/permissions/PermissionManager.kt | 6 ++--- .../ca/allanwang/kau/swipe/SwipeBackLayout.kt | 18 ++++++++------ .../main/kotlin/ca/allanwang/kau/xml/Changelog.kt | 2 +- .../kotlin/ca/allanwang/kau/sample/MainActivity.kt | 2 +- .../ca/allanwang/kau/searchview/SearchItem.kt | 8 +++---- 11 files changed, 61 insertions(+), 33 deletions(-) (limited to 'searchview') diff --git a/about/src/main/kotlin/ca/allanwang/kau/about/FaqIItem.kt b/about/src/main/kotlin/ca/allanwang/kau/about/FaqIItem.kt index 629aa52..5595aed 100644 --- a/about/src/main/kotlin/ca/allanwang/kau/about/FaqIItem.kt +++ b/about/src/main/kotlin/ca/allanwang/kau/about/FaqIItem.kt @@ -1,5 +1,6 @@ package ca.allanwang.kau.about +import android.annotation.SuppressLint import android.support.v7.widget.RecyclerView import android.text.method.LinkMovementMethod import android.view.View @@ -22,9 +23,7 @@ class FaqIItem(val content: FaqItem) : KauIItem>) { + fun bindEvents(fastAdapter: FastAdapter>) { fastAdapter.withSelectable(false) .withEventHook(object : ClickEventHook>() { @@ -43,6 +42,7 @@ class FaqIItem(val content: FaqItem) : KauIItem?) { super.bindView(holder, payloads) with(holder) { diff --git a/about/src/main/kotlin/ca/allanwang/kau/about/LibraryIItem.kt b/about/src/main/kotlin/ca/allanwang/kau/about/LibraryIItem.kt index e50460e..88e6f9b 100644 --- a/about/src/main/kotlin/ca/allanwang/kau/about/LibraryIItem.kt +++ b/about/src/main/kotlin/ca/allanwang/kau/about/LibraryIItem.kt @@ -25,7 +25,7 @@ class LibraryIItem(val lib: Library) : KauIItem>) { + fun bindEvents(fastAdapter: FastAdapter>) { fastAdapter.withSelectable(false) .withOnClickListener { v, _, item, _ -> if (item !is LibraryIItem) false @@ -53,11 +53,11 @@ class LibraryIItem(val lib: Library) : KauIItem>) { + fun bindClickEvents(fastAdapter: FastAdapter>) { fastAdapter.withEventHook(object : ClickEventHook>() { override fun onBindMany(viewHolder: RecyclerView.ViewHolder): List? { return if (viewHolder is ViewHolder) listOf(viewHolder.card, viewHolder.button) else null diff --git a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/CircleView.kt b/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/CircleView.kt index d697c8b..ed98090 100644 --- a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/CircleView.kt +++ b/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/CircleView.kt @@ -194,7 +194,6 @@ class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet companion object { @ColorInt - @JvmStatic private fun translucentColor(color: Int): Int { val factor = 0.7f val alpha = Math.round(Color.alpha(color) * factor) @@ -205,7 +204,6 @@ class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet } @ColorInt - @JvmStatic fun shiftColor(@ColorInt color: Int, @FloatRange(from = 0.0, to = 2.0) by: Float): Int { if (by == 1f) return color @@ -215,11 +213,9 @@ class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet } @ColorInt - @JvmStatic fun shiftColorDown(@ColorInt color: Int): Int = shiftColor(color, 0.9f) @ColorInt - @JvmStatic fun shiftColorUp(@ColorInt color: Int): Int = shiftColor(color, 1.1f) } } \ No newline at end of file diff --git a/core-ui/src/main/kotlin/ca/allanwang/kau/ui/views/CutoutView.kt b/core-ui/src/main/kotlin/ca/allanwang/kau/ui/views/CutoutView.kt index 9e8ac11..fc03563 100644 --- a/core-ui/src/main/kotlin/ca/allanwang/kau/ui/views/CutoutView.kt +++ b/core-ui/src/main/kotlin/ca/allanwang/kau/ui/views/CutoutView.kt @@ -147,14 +147,16 @@ class CutoutView @JvmOverloads constructor( paint.textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, mid, metrics) val maxLineWidth = paint.measureText(text) - return if (high - low < precision) low - else if (maxLineWidth > targetWidth) getSingleLineTextSize(text, paint, targetWidth, low, mid, precision, metrics) - else if (maxLineWidth < targetWidth) getSingleLineTextSize(text, paint, targetWidth, mid, high, precision, metrics) - else mid + return when { + high - low < precision -> low + maxLineWidth > targetWidth -> getSingleLineTextSize(text, paint, targetWidth, low, mid, precision, metrics) + maxLineWidth < targetWidth -> getSingleLineTextSize(text, paint, targetWidth, mid, high, precision, metrics) + else -> mid + } } private fun createBitmap() { - if (!(cutout?.isRecycled ?: true)) + if (cutout?.isRecycled == false) cutout?.recycle() if (width == 0 || height == 0) return cutout = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) diff --git a/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt b/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt index 14655f0..61f0708 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt @@ -11,13 +11,33 @@ import android.util.Log * * Base logger class with a predefined tag * This may be extended by an object to effectively replace [Log] + * Almost everything is opened to make everything customizable */ open class KauLogger(val tag: String) { + /** + * Global toggle to enable the whole logger + */ open var enabled = true + + /** + * Global toggle to show private text + */ open var showPrivateText = false + + /** + * If both msg and priv msg are accepted, output the combined output + */ open var messageJoiner: (msg: String, privMsg: String) -> String = { msg, privMsg -> "$msg: $privMsg" } + /** + * Open hook to change the output of the logger (for instance, output to stdout rather than Android log files + * Does not use reference notation to avoid constructor leaks + */ + open var logFun: (priority: Int, message: String?, privateMessage: String?, t: Throwable?) -> Unit = { p, m, pm, t -> + logImpl(p, m, pm, t) + } + /** * Filter pass-through to decide what we wish to log * By default, we will ignore verbose and debug logs @@ -35,14 +55,20 @@ open class KauLogger(val tag: String) { showPrivateText = enable } - open fun log(priority: Int, message: String?, privateMessage: String?, t: Throwable? = null) { + private fun log(priority: Int, message: String?, privateMessage: String?, t: Throwable? = null) { if (!shouldLog(priority, message, privateMessage, t)) return logImpl(priority, message, privateMessage, t) } + /** + * Condition to pass to allow the input to be logged + */ protected open fun shouldLog(priority: Int, message: String?, privateMessage: String?, t: Throwable?): Boolean = enabled && filter(priority) + /** + * Base implementation of the Android logger + */ protected open fun logImpl(priority: Int, message: String?, privateMessage: String?, t: Throwable?) { val text = if (showPrivateText) { if (message == null) privateMessage diff --git a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt index 5fe0ddf..18f3e41 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt @@ -18,8 +18,8 @@ import java.lang.ref.WeakReference */ internal object PermissionManager { - var requestInProgress = false - val pendingResults: MutableList> by lazy { mutableListOf>() } + private var requestInProgress = false + private val pendingResults: MutableList> by lazy { mutableListOf>() } /** * Retrieve permissions requested in our manifest @@ -63,7 +63,7 @@ internal object PermissionManager { val iter = pendingResults.iterator() while (iter.hasNext()) { val action = iter.next().get() - if ((0 until count).any { action?.onResult(permissions[it], grantResults[it]) ?: true }) + if ((0 until count).any { action?.onResult(permissions[it], grantResults[it]) != false }) iter.remove() } if (pendingResults.isEmpty()) 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 c0875d1..065f4bb 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt @@ -349,7 +349,7 @@ internal class SwipeBackLayout @JvmOverloads constructor(context: Context, attrs listeners.forEach { it.get()?.onScroll(scrollPercent, contentOffset, edgeFlag) } if (scrollPercent >= 1) { - if (!(activity?.isFinishing ?: true)) { + if (activity?.isFinishing == false) { if (scrollPercent >= scrollThreshold && isScrollOverValid) { isScrollOverValid = false listeners.forEach { it.get()?.onScrollToClose(edgeFlag) } @@ -390,15 +390,19 @@ internal class SwipeBackLayout @JvmOverloads constructor(context: Context, attrs } override fun clampViewPositionHorizontal(child: View, left: Int, dx: Int): Int { - return if (edgeFlag == SWIPE_EDGE_RIGHT) Math.min(0, Math.max(left, -child.width)) - else if (edgeFlag == SWIPE_EDGE_LEFT) Math.min(child.width, Math.max(left, 0)) - else 0 + return when (edgeFlag) { + SWIPE_EDGE_RIGHT -> Math.min(0, Math.max(left, -child.width)) + SWIPE_EDGE_LEFT -> Math.min(child.width, Math.max(left, 0)) + else -> 0 + } } override fun clampViewPositionVertical(child: View, top: Int, dy: Int): Int { - return if (edgeFlag == SWIPE_EDGE_BOTTOM) Math.min(0, Math.max(top, -child.height)) - else if (edgeFlag == SWIPE_EDGE_TOP) Math.min(child.height, Math.max(top, 0)) - else 0 + return when (edgeFlag) { + SWIPE_EDGE_BOTTOM -> Math.min(0, Math.max(top, -child.height)) + SWIPE_EDGE_TOP -> Math.min(child.height, Math.max(top, 0)) + else -> 0 + } } } diff --git a/core/src/main/kotlin/ca/allanwang/kau/xml/Changelog.kt b/core/src/main/kotlin/ca/allanwang/kau/xml/Changelog.kt index 4bf1836..58f1ccc 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/xml/Changelog.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/xml/Changelog.kt @@ -87,7 +87,7 @@ internal enum class ChangelogType(val tag: String, val attr: String, @LayoutRes ITEM("item", "text", R.layout.kau_changelog_content); companion object { - @JvmStatic val values = values() + val values = values() } /** diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt index ca75ebb..482c911 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt @@ -277,7 +277,7 @@ class MainActivity : KPrefActivity() { } override fun onBackPressed() { - if (!(searchView?.onBackPressed() ?: false)) super.onBackPressed() + if (searchView?.onBackPressed() != true) super.onBackPressed() } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { diff --git a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt index 75d9b27..29341af 100644 --- a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt +++ b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt @@ -34,8 +34,8 @@ class SearchItem(val key: String, ) { companion object { - @JvmStatic var foregroundColor: Int = 0xdd000000.toInt() - @JvmStatic var backgroundColor: Int = 0xfffafafa.toInt() + var foregroundColor: Int = 0xdd000000.toInt() + var backgroundColor: Int = 0xfffafafa.toInt() } var styledContent: SpannableStringBuilder? = null @@ -44,7 +44,7 @@ class SearchItem(val key: String, * Highlight the subText if it is present in the content */ fun withHighlights(subText: String) { - val index = content.indexOf(subText) + val index = content.indexOf(subText, ignoreCase = true) if (index == -1) return styledContent = SpannableStringBuilder(content) styledContent!!.setSpan(StyleSpan(Typeface.BOLD), index, index + subText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) @@ -60,7 +60,7 @@ class SearchItem(val key: String, holder.container.setRippleBackground(foregroundColor, backgroundColor) holder.title.text = styledContent ?: content - if (description?.isNotBlank() ?: false) holder.desc.visible().text = description + if (description?.isNotBlank() == true) holder.desc.visible().text = description } override fun unbindView(holder: ViewHolder) { -- cgit v1.2.3