From d540934915da26ab2cec4c897e973be35e0bfe24 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 9 Sep 2018 11:59:24 -0400 Subject: Clean up kotterknife (#161) * Remove bindview implementations internally * Remove more bindView calls * Make config private in ElasticRecyclerActivity * Fix recyclerview * Update adapter * Improve swipe destroy and add direction to swipe finish --- .../ca/allanwang/kau/swipe/SwipeBackHelper.kt | 2 +- .../ca/allanwang/kau/swipe/SwipeBackLayout.kt | 13 ++- .../kotlin/ca/allanwang/kau/utils/ContextUtils.kt | 2 +- .../kotlin/ca/allanwang/kau/utils/Kotterknife.kt | 115 ++++++++++++--------- .../main/kotlin/ca/allanwang/kau/xml/Changelog.kt | 6 +- 5 files changed, 81 insertions(+), 57 deletions(-) (limited to 'core/src/main/kotlin/ca/allanwang') 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 4bc216d..41cd2e0 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt @@ -33,7 +33,7 @@ internal object SwipeBackHelper { } fun onDestroy(activity: Activity) { - val page: SwipeBackPage? = this[activity] + val page: SwipeBackPage? = this[activity] ?: return pageStack.kauRemoveIf { it.activityRef.get() == null || it === page } page?.activityRef?.clear() KL.v { "KauSwipe onDestroy ${activity.localClassName}" } 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 ae09c8a..a323e6c 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt @@ -207,9 +207,16 @@ internal class SwipeBackLayout @JvmOverloads constructor(context: Context, attrs override fun scrollToFinishActivity() { val contentView = contentViewRef.get() ?: return KL.e { "KauSwipe cannot scroll to finish as contentView is null. Is onPostCreate called?" } - val childWidth = contentView.width - val top = 0 - val left = childWidth + OVERSCROLL_DISTANCE + val swipeWidth = contentView.width + OVERSCROLL_DISTANCE + val swipeHeight = contentView.height + OVERSCROLL_DISTANCE + var top = 0 + var left = 0 + when (edgeFlag) { + SWIPE_EDGE_LEFT -> left = swipeWidth + SWIPE_EDGE_TOP -> top = swipeHeight + SWIPE_EDGE_RIGHT -> left = -swipeWidth + SWIPE_EDGE_BOTTOM -> top = -swipeHeight + } dragHelper.smoothSlideViewTo(contentView, left, top) invalidate() } diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt index fdee4d8..6568bf2 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt @@ -118,7 +118,7 @@ inline fun Context.animation(@AnimRes id: Int) = AnimationUtils.loadAnimation(th inline fun Context.plural(@PluralsRes id: Int, quantity: Number) = resources.getQuantityString(id, quantity.toInt(), quantity.toInt())!! //Attr retrievers -fun Context.resolveColor(@AttrRes attr: Int, fallback: Int = 0): Int { +fun Context.resolveColor(@AttrRes attr: Int, @ColorInt fallback: Int = 0): Int { val a = theme.obtainStyledAttributes(intArrayOf(attr)) try { return a.getColor(0, fallback) diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt index b0448d1..8c7c039 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt @@ -21,147 +21,133 @@ import kotlin.reflect.KProperty import android.support.v4.app.DialogFragment as SupportDialogFragment import android.support.v4.app.Fragment as SupportFragment +private const val DEPRECATION_MESSAGE = "Kotterknife will be removed in favour of the kotlin_android_extensions plugin" + +@Deprecated(DEPRECATION_MESSAGE) fun View.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Activity.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Dialog.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) -@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewResettable and resetting using KotterKnife.reset(this)", - ReplaceWith("bindViewResettable(id)"), - DeprecationLevel.WARNING) +@Deprecated(DEPRECATION_MESSAGE) fun DialogFragment.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) -@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewResettable and resetting using KotterKnife.reset(this)", - ReplaceWith("bindViewResettable(id)"), - DeprecationLevel.WARNING) +@Deprecated(DEPRECATION_MESSAGE) fun SupportDialogFragment.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) -@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewResettable and resetting using KotterKnife.reset(this)", - ReplaceWith("bindViewResettable(id)"), - DeprecationLevel.WARNING) +@Deprecated(DEPRECATION_MESSAGE) fun Fragment.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) -@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewResettable and resetting using KotterKnife.reset(this)", - ReplaceWith("bindViewResettable(id)"), - DeprecationLevel.WARNING) +@Deprecated(DEPRECATION_MESSAGE) fun SupportFragment.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun ViewHolder.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun View.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Activity.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Dialog.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) -@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewResettable and resetting using KotterKnife.reset(this)", - ReplaceWith("bindOptionalViewResettable(id)"), - DeprecationLevel.WARNING) +@Deprecated(DEPRECATION_MESSAGE) fun DialogFragment.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) -@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewResettable and resetting using KotterKnife.reset(this)", - ReplaceWith("bindOptionalViewResettable(id)"), - DeprecationLevel.WARNING) +@Deprecated(DEPRECATION_MESSAGE) fun SupportDialogFragment.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) -@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewResettable and resetting using KotterKnife.reset(this)", - ReplaceWith("bindOptionalViewResettable(id)"), - DeprecationLevel.WARNING) +@Deprecated(DEPRECATION_MESSAGE) fun Fragment.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) -@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewResettable and resetting using KotterKnife.reset(this)", - ReplaceWith("bindOptionalViewResettable(id)"), - DeprecationLevel.WARNING) +@Deprecated(DEPRECATION_MESSAGE) fun SupportFragment.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun ViewHolder.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun View.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Activity.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Dialog.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) -@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewsResettable and resetting using KotterKnife.reset(this)", - ReplaceWith("bindViewsResettable(*ids)"), - DeprecationLevel.WARNING) +@Deprecated(DEPRECATION_MESSAGE) fun DialogFragment.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) -@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewsResettable and resetting using KotterKnife.reset(this)", - ReplaceWith("bindViewsResettable(*ids)"), - DeprecationLevel.WARNING) +@Deprecated(DEPRECATION_MESSAGE) fun SupportDialogFragment.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) -@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewsResettable and resetting using KotterKnife.reset(this)", - ReplaceWith("bindViewsResettable(*ids)"), - DeprecationLevel.WARNING) +@Deprecated(DEPRECATION_MESSAGE) fun Fragment.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) -@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewsResettable and resetting using KotterKnife.reset(this)", - ReplaceWith("bindViewsResettable(*ids)"), - DeprecationLevel.WARNING) +@Deprecated(DEPRECATION_MESSAGE) fun SupportFragment.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun ViewHolder.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun View.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Activity.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Dialog.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) -@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewsResettable and resetting using KotterKnife.reset(this)", - ReplaceWith("bindOptionalViewsResettable(*ids)"), - DeprecationLevel.WARNING) +@Deprecated(DEPRECATION_MESSAGE) fun DialogFragment.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) -@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewsResettable and resetting using KotterKnife.reset(this)", - ReplaceWith("bindOptionalViewsResettable(*ids)"), - DeprecationLevel.WARNING) +@Deprecated(DEPRECATION_MESSAGE) fun SupportDialogFragment.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) -@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewsResettable and resetting using KotterKnife.reset(this)", - ReplaceWith("bindOptionalViewsResettable(*ids)"), - DeprecationLevel.WARNING) +@Deprecated(DEPRECATION_MESSAGE) fun Fragment.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) -@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewsResettable and resetting using KotterKnife.reset(this)", - ReplaceWith("bindOptionalViewsResettable(*ids)"), - DeprecationLevel.WARNING) +@Deprecated(DEPRECATION_MESSAGE) fun SupportFragment.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun ViewHolder.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) @@ -223,99 +209,131 @@ private open class Lazy(private val initializer: (T, KProperty<*>) * Credits to MichaelRocks */ +@Deprecated(DEPRECATION_MESSAGE) fun View.bindViewResettable(id: Int) : ReadOnlyProperty = requiredResettable(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Activity.bindViewResettable(id: Int) : ReadOnlyProperty = requiredResettable(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Dialog.bindViewResettable(id: Int) : ReadOnlyProperty = requiredResettable(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun DialogFragment.bindViewResettable(id: Int) : ReadOnlyProperty = requiredResettable(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun SupportDialogFragment.bindViewResettable(id: Int) : ReadOnlyProperty = requiredResettable(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Fragment.bindViewResettable(id: Int) : ReadOnlyProperty = requiredResettable(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun SupportFragment.bindViewResettable(id: Int) : ReadOnlyProperty = requiredResettable(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun ViewHolder.bindViewResettable(id: Int) : ReadOnlyProperty = requiredResettable(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun View.bindOptionalViewResettable(id: Int) : ReadOnlyProperty = optionalResettable(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Activity.bindOptionalViewResettable(id: Int) : ReadOnlyProperty = optionalResettable(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Dialog.bindOptionalViewResettable(id: Int) : ReadOnlyProperty = optionalResettable(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun DialogFragment.bindOptionalViewResettable(id: Int) : ReadOnlyProperty = optionalResettable(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun SupportDialogFragment.bindOptionalViewResettable(id: Int) : ReadOnlyProperty = optionalResettable(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Fragment.bindOptionalViewResettable(id: Int) : ReadOnlyProperty = optionalResettable(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun SupportFragment.bindOptionalViewResettable(id: Int) : ReadOnlyProperty = optionalResettable(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun ViewHolder.bindOptionalViewResettable(id: Int) : ReadOnlyProperty = optionalResettable(id, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun View.bindViewsResettable(vararg ids: Int) : ReadOnlyProperty> = requiredResettable(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Activity.bindViewsResettable(vararg ids: Int) : ReadOnlyProperty> = requiredResettable(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Dialog.bindViewsResettable(vararg ids: Int) : ReadOnlyProperty> = requiredResettable(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun DialogFragment.bindViewsResettable(vararg ids: Int) : ReadOnlyProperty> = requiredResettable(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun SupportDialogFragment.bindViewsResettable(vararg ids: Int) : ReadOnlyProperty> = requiredResettable(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Fragment.bindViewsResettable(vararg ids: Int) : ReadOnlyProperty> = requiredResettable(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun SupportFragment.bindViewsResettable(vararg ids: Int) : ReadOnlyProperty> = requiredResettable(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun ViewHolder.bindViewsResettable(vararg ids: Int) : ReadOnlyProperty> = requiredResettable(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun View.bindOptionalViewsResettable(vararg ids: Int) : ReadOnlyProperty> = optionalResettable(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Activity.bindOptionalViewsResettable(vararg ids: Int) : ReadOnlyProperty> = optionalResettable(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Dialog.bindOptionalViewsResettable(vararg ids: Int) : ReadOnlyProperty> = optionalResettable(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun DialogFragment.bindOptionalViewsResettable(vararg ids: Int) : ReadOnlyProperty> = optionalResettable(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun SupportDialogFragment.bindOptionalViewsResettable(vararg ids: Int) : ReadOnlyProperty> = optionalResettable(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun Fragment.bindOptionalViewsResettable(vararg ids: Int) : ReadOnlyProperty> = optionalResettable(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun SupportFragment.bindOptionalViewsResettable(vararg ids: Int) : ReadOnlyProperty> = optionalResettable(ids, viewFinder) +@Deprecated(DEPRECATION_MESSAGE) fun ViewHolder.bindOptionalViewsResettable(vararg ids: Int) : ReadOnlyProperty> = optionalResettable(ids, viewFinder) @@ -345,6 +363,7 @@ private class LazyResettable(initializer: (T, KProperty<*>) -> V) : } } +@Deprecated(DEPRECATION_MESSAGE) object Kotterknife { fun reset(target: Any) { KotterknifeRegistry.reset(target) 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 83b182f..28d51a4 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/xml/Changelog.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/xml/Changelog.kt @@ -11,8 +11,6 @@ import android.view.View import android.view.ViewGroup import android.widget.TextView import ca.allanwang.kau.R -import ca.allanwang.kau.utils.bindOptionalView -import ca.allanwang.kau.utils.bindView import ca.allanwang.kau.utils.materialDialog import ca.allanwang.kau.utils.use import com.afollestad.materialdialogs.MaterialDialog @@ -64,8 +62,8 @@ internal class ChangelogAdapter(val items: List>, @C override fun getItemCount() = items.size internal class ChangelogVH(itemView: View) : RecyclerView.ViewHolder(itemView) { - val text: TextView by bindView(R.id.kau_changelog_text) - val bullet: TextView? by bindOptionalView(R.id.kau_changelog_bullet) + val text: TextView = itemView.findViewById(R.id.kau_changelog_text) + val bullet: TextView? = itemView.findViewById(R.id.kau_changelog_bullet) } } -- cgit v1.2.3