From c2bdcc4c50d03678e2c9200b11dd96395be82652 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Thu, 22 Jun 2017 11:20:25 -0700 Subject: Inherit title from subitem title --- .../kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt | 43 +++++++++------------- .../kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt | 10 ++--- .../ca/allanwang/kau/kpref/items/KPrefSubItems.kt | 2 +- .../ca/allanwang/kau/views/TextSwitcherThemed.kt | 5 +++ 4 files changed, 27 insertions(+), 33 deletions(-) (limited to 'library/src/main/kotlin/ca/allanwang') diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt index 9653029..e4be59b 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt @@ -39,11 +39,10 @@ abstract class KPrefActivity : AppCompatActivity(), KPrefActivityContract { val toolbarTitle: TextSwitcherThemed by bindView(R.id.kau_toolbar_text) val prefHolder: ViewAnimator by bindView(R.id.kau_holder) private lateinit var globalOptions: GlobalOptions - private val titles: LinkedList> = LinkedList() - private var index = -1 + private val titleStack: Stack = Stack() var animate: Boolean = true val isRootPref: Boolean - get() = index <= 0 + get() = titleStack.size == 1 //we can't use the same animations for both views; otherwise the durations will sync private val SLIDE_IN_LEFT_TITLE: Animation by lazy { AnimationUtils.loadAnimation(this, R.anim.kau_slide_in_left) } @@ -79,13 +78,11 @@ abstract class KPrefActivity : AppCompatActivity(), KPrefActivityContract { toolbarCanvas.set(resolveColor(R.attr.colorPrimary)) bgCanvas.set(resolveColor(android.R.attr.colorBackground)) prefHolder.animateFirstView = false - titles.add(Pair(-1, R.string.kau_settings)) //setup prefs val core = CoreAttributeBuilder() val builder = kPrefCoreAttributes() core.builder() globalOptions = GlobalOptions(core, this) - showNextPrefs(onCreateKPrefs(savedInstanceState)) with(toolbarTitle) { setFactory { TextView(this@KPrefActivity).apply { @@ -96,19 +93,19 @@ abstract class KPrefActivity : AppCompatActivity(), KPrefActivityContract { TextViewCompat.setTextAppearance(this, R.style.TextAppearance_AppCompat_Title) } } - setCurrentText(string(R.string.kau_settings)) } + showNextPrefs(R.string.kau_settings, onCreateKPrefs(savedInstanceState)) } override fun onPostCreate(savedInstanceState: Bundle?) { super.onPostCreate(savedInstanceState) } - override fun showNextPrefs(builder: KPrefAdapterBuilder.() -> Unit) { - index++ + override fun showNextPrefs(@StringRes toolbarTitleRes: Int, builder: KPrefAdapterBuilder.() -> Unit) { val rv = RecyclerView(this).apply { layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT) - titles.addFirst(Pair(index, setKPrefAdapter(globalOptions, builder))) + titleStack.push(toolbarTitleRes) + setKPrefAdapter(globalOptions, builder) } with(prefHolder) { inAnimation = if (animate) SLIDE_IN_RIGHT_ITEMS else null @@ -116,18 +113,16 @@ abstract class KPrefActivity : AppCompatActivity(), KPrefActivityContract { addView(rv) showNext() } - if (titles.first().second != -1) { - with(toolbarTitle) { - inAnimation = if (animate) SLIDE_IN_RIGHT_TITLE else null - outAnimation = if (animate) SLIDE_OUT_LEFT_TITLE else null - setText(string(titles.first().second)) - } + with(toolbarTitle) { + inAnimation = if (animate) SLIDE_IN_RIGHT_TITLE else null + outAnimation = if (animate) SLIDE_OUT_LEFT_TITLE else null + setText(string(titleStack.peek())) } } override fun showPrevPrefs() { - if (index <= 0) { - KL.e("Cannot go back in KPrefActivity; already at index $index") + if (titleStack.size <= 1) { + KL.e("Cannot go back in KPrefActivity; already at index ${titleStack.size - 1}") return } val current = prefHolder.currentView @@ -137,15 +132,11 @@ abstract class KPrefActivity : AppCompatActivity(), KPrefActivityContract { showPrevious() removeView(current) } - index-- - titles.removeFirst() - val nextTitle = string(titles.first { it.first <= index && it.second != -1 }.second) - if (nextTitle != (toolbarTitle.currentView as TextView).text) { - with(toolbarTitle) { - inAnimation = if (animate) SLIDE_IN_LEFT_TITLE else null - outAnimation = if (animate) SLIDE_OUT_RIGHT_TITLE else null - setText(string(titles.first { it.first <= index && it.second != -1 }.second)) - } + titleStack.pop() + with(toolbarTitle) { + inAnimation = if (animate) SLIDE_IN_LEFT_TITLE else null + outAnimation = if (animate) SLIDE_OUT_RIGHT_TITLE else null + setText(string(titleStack.peek())) } } diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt index a6ab8cd..564898a 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt @@ -15,9 +15,9 @@ import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter /** * Base extension that will register the layout manager and adapter with the given items - * Returns toolbar title res + * Returns FastAdapter */ -fun RecyclerView.setKPrefAdapter(globalOptions: GlobalOptions, builder: KPrefAdapterBuilder.() -> Unit): Int { +fun RecyclerView.setKPrefAdapter(globalOptions: GlobalOptions, builder: KPrefAdapterBuilder.() -> Unit): FastItemAdapter { layoutManager = LinearLayoutManager(context) val adapter = FastItemAdapter() adapter.withOnClickListener { v, _, item, _ -> item.onClick(v, v.findViewById(R.id.kau_pref_inner_content)) } @@ -25,7 +25,7 @@ fun RecyclerView.setKPrefAdapter(globalOptions: GlobalOptions, builder: KPrefAda builder.invoke(items) adapter.add(items.list) this.adapter = adapter - return items.toolbarTitleRes + return adapter } /** @@ -45,7 +45,7 @@ class CoreAttributeBuilder : CoreAttributeContract { } interface KPrefActivityContract { - fun showNextPrefs(builder: KPrefAdapterBuilder.() -> Unit) + fun showNextPrefs(@StringRes toolbarTitleRes:Int, builder: KPrefAdapterBuilder.() -> Unit) fun showPrevPrefs() } @@ -62,8 +62,6 @@ class GlobalOptions(core: CoreAttributeContract, activity: KPrefActivityContract */ class KPrefAdapterBuilder(internal val globalOptions: GlobalOptions) { - var toolbarTitleRes: Int = -1 - fun header(@StringRes title: Int) = list.add(KPrefHeader(KPrefItemCore.CoreBuilder(globalOptions, title))) diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefSubItems.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefSubItems.kt index c8a9fda..8614cb6 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefSubItems.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefSubItems.kt @@ -16,7 +16,7 @@ import ca.allanwang.kau.kpref.KPrefAdapterBuilder class KPrefSubItems(val builder: KPrefSubItemsBuilder) : KPrefItemCore(builder) { override fun onClick(itemView: View, innerContent: View?): Boolean { - builder.globalOptions.showNextPrefs(builder.itemBuilder) + builder.globalOptions.showNextPrefs(builder.titleRes, builder.itemBuilder) return true } diff --git a/library/src/main/kotlin/ca/allanwang/kau/views/TextSwitcherThemed.kt b/library/src/main/kotlin/ca/allanwang/kau/views/TextSwitcherThemed.kt index c4de808..bef661e 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/views/TextSwitcherThemed.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/views/TextSwitcherThemed.kt @@ -19,4 +19,9 @@ class TextSwitcherThemed @JvmOverloads constructor(context: Context, attrs: Attr (getChildAt(0) as TextView).setTextColor(value) (getChildAt(1) as TextView).setTextColor(value) } + + override fun setText(text: CharSequence?) { + if ((currentView as TextView).text == text) return + super.setText(text) + } } \ No newline at end of file -- cgit v1.2.3