diff options
author | Allan Wang <me@allanwang.ca> | 2017-06-21 21:10:18 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2017-06-21 21:10:18 -0700 |
commit | 621c083b4ee1a81c67665ebf4b442f0ea98b5d4a (patch) | |
tree | dfed2a9fedc185aee4315f71e5a993bd1c374438 /library/src/main/kotlin/ca | |
parent | 5c1a82b943e148ef6109b1d1ec65f89198458735 (diff) | |
download | kau-621c083b4ee1a81c67665ebf4b442f0ea98b5d4a.tar.gz kau-621c083b4ee1a81c67665ebf4b442f0ea98b5d4a.tar.bz2 kau-621c083b4ee1a81c67665ebf4b442f0ea98b5d4a.zip |
Remapped kprefs to support sub items
Diffstat (limited to 'library/src/main/kotlin/ca')
8 files changed, 150 insertions, 33 deletions
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 f2f045b..7c4efee 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt @@ -6,6 +6,9 @@ import android.support.v7.app.AppCompatActivity import android.support.v7.widget.RecyclerView import android.support.v7.widget.Toolbar import android.view.View +import android.view.animation.AnimationUtils +import android.widget.FrameLayout +import android.widget.ViewAnimator import ca.allanwang.kau.R import ca.allanwang.kau.kpref.items.KPrefItemCore import ca.allanwang.kau.utils.bindView @@ -14,13 +17,35 @@ import ca.allanwang.kau.utils.statusBarColor import ca.allanwang.kau.views.RippleCanvas import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter -abstract class KPrefActivity : AppCompatActivity() { +abstract class KPrefActivity : AppCompatActivity(), KPrefActivityContract { - lateinit var adapter: FastItemAdapter<KPrefItemCore> - val recycler: RecyclerView by bindView(R.id.kau_recycler) + val adapter: FastItemAdapter<KPrefItemCore> + get() = recycler.adapter as FastItemAdapter<KPrefItemCore> + val recycler: RecyclerView + get() = prefHolder.currentView as RecyclerView + lateinit var baseRecycler: RecyclerView val bgCanvas: RippleCanvas by bindView(R.id.kau_ripple) val toolbarCanvas: RippleCanvas by bindView(R.id.kau_toolbar_ripple) val toolbar: Toolbar by bindView(R.id.kau_toolbar) + val prefHolder: ViewAnimator by bindView(R.id.kau_holder) + private lateinit var globalOptions: GlobalOptions + + /** + * Core attribute builder that is consistent throughout all items + * Leave blank to use defaults + */ + abstract fun kPrefCoreAttributes(): CoreAttributeContract.() -> Unit + + init { + setup() + } + + private fun setup() { + val core = CoreAttributeBuilder() + val builder = kPrefCoreAttributes() + core.builder() + globalOptions = GlobalOptions(core, this) + } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -30,15 +55,42 @@ abstract class KPrefActivity : AppCompatActivity() { with(supportActionBar!!) { setDisplayHomeAsUpEnabled(true) setDisplayShowHomeEnabled(true) - toolbar.setNavigationOnClickListener { - onBackPressed() - } + toolbar.setNavigationOnClickListener { onBackPressed() } } window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION statusBarColor = 0x30000000 toolbarCanvas.set(resolveColor(R.attr.colorPrimary)) bgCanvas.set(resolveColor(android.R.attr.colorBackground)) - adapter = recycler.setKPrefAdapter(onCreateKPrefs(savedInstanceState)) + with(prefHolder) { + animateFirstView = false + inAnimation = AnimationUtils.loadAnimation(context, R.anim.kau_slide_in_right) + outAnimation = AnimationUtils.loadAnimation(context, R.anim.kau_slide_out_left) + } + showNextPrefs(onCreateKPrefs(savedInstanceState)) + baseRecycler = recycler + } + + override fun showNextPrefs(builder: KPrefAdapterBuilder.() -> Unit) { + val rv = RecyclerView(this).apply { + layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT) + setKPrefAdapter(globalOptions, builder) + } + with(prefHolder) { + inAnimation = AnimationUtils.loadAnimation(context, R.anim.kau_slide_in_right) + outAnimation = AnimationUtils.loadAnimation(context, R.anim.kau_slide_out_left) + addView(rv) + showNext() + } + } + + override fun showPrevPrefs() { + val current = prefHolder.currentView + with(prefHolder) { + inAnimation = AnimationUtils.loadAnimation(context, R.anim.kau_slide_in_left) + outAnimation = AnimationUtils.loadAnimation(context, R.anim.kau_slide_out_right) + showPrevious() + removeView(current) + } } fun reload(vararg index: Int) { @@ -56,5 +108,16 @@ abstract class KPrefActivity : AppCompatActivity() { abstract fun onCreateKPrefs(savedInstanceState: Bundle?): KPrefAdapterBuilder.() -> Unit + override fun onBackPressed() { + if (!backPress()) super.onBackPressed() + } + + fun backPress(): Boolean { + if (baseRecycler != recycler) { + showPrevPrefs() + return true + } + return false + } } 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 75c24bc..53cba99 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt @@ -16,11 +16,11 @@ import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter /** * Base extension that will register the layout manager and adapter with the given items */ -fun RecyclerView.setKPrefAdapter(builder: KPrefAdapterBuilder.() -> Unit): FastItemAdapter<KPrefItemCore> { +fun RecyclerView.setKPrefAdapter(globalOptions: GlobalOptions, builder: KPrefAdapterBuilder.() -> Unit): FastItemAdapter<KPrefItemCore> { layoutManager = LinearLayoutManager(context) val adapter = FastItemAdapter<KPrefItemCore>() adapter.withOnClickListener { v, _, item, _ -> item.onClick(v, v.findViewById(R.id.kau_pref_inner_content)) } - val items = KPrefAdapterBuilder() + val items = KPrefAdapterBuilder(globalOptions) builder.invoke(items) adapter.add(items.list) this.adapter = adapter @@ -43,22 +43,32 @@ class CoreAttributeBuilder : CoreAttributeContract { override var accentColor: (() -> Int)? = null } +interface KPrefActivityContract { + fun showNextPrefs(builder: KPrefAdapterBuilder.() -> Unit) + fun showPrevPrefs() +} + + +class GlobalOptions(core: CoreAttributeContract, activity: KPrefActivityContract +) : CoreAttributeContract by core, KPrefActivityContract by activity + + /** * Builder for kpref items * Contains DSLs for every possible item * The arguments are all the mandatory values plus an optional builder housing all the possible configurations * The mandatory values are final so they cannot be edited in the builder */ -class KPrefAdapterBuilder : CoreAttributeContract by CoreAttributeBuilder() { +class KPrefAdapterBuilder(internal val globalOptions: GlobalOptions) { fun header(@StringRes title: Int) - = list.add(KPrefHeader(KPrefItemCore.CoreBuilder(this, title))) + = list.add(KPrefHeader(KPrefItemCore.CoreBuilder(globalOptions, title))) fun checkbox(@StringRes title: Int, getter: (() -> Boolean), setter: ((value: Boolean) -> Unit), builder: KPrefItemBase.BaseContract<Boolean>.() -> Unit = {}) - = list.add(KPrefCheckbox(KPrefItemBase.BaseBuilder<Boolean>(this, title, getter, setter) + = list.add(KPrefCheckbox(KPrefItemBase.BaseBuilder(globalOptions, title, getter, setter) .apply { builder() })) @@ -66,16 +76,19 @@ class KPrefAdapterBuilder : CoreAttributeContract by CoreAttributeBuilder() { getter: (() -> Int), setter: ((value: Int) -> Unit), builder: KPrefColorPicker.KPrefColorContract.() -> Unit = {}) - = list.add(KPrefColorPicker(KPrefColorPicker.KPrefColorBuilder(this, title, getter, setter) + = list.add(KPrefColorPicker(KPrefColorPicker.KPrefColorBuilder(globalOptions, title, getter, setter) .apply { builder() })) fun <T> text(@StringRes title: Int, getter: (() -> T), setter: ((value: T) -> Unit), builder: KPrefText.KPrefTextContract<T>.() -> Unit = {}) - = list.add(KPrefText<T>(KPrefText.KPrefTextBuilder<T>(this, title, getter, setter) + = list.add(KPrefText<T>(KPrefText.KPrefTextBuilder<T>(globalOptions, title, getter, setter) .apply { builder() })) - internal val list: MutableList<KPrefItemCore> = mutableListOf() + fun subItems(@StringRes title:Int, + itemBuilder:KPrefAdapterBuilder.()->Unit) + = list.add(KPrefSubItems(KPrefSubItems.KPrefSubItemsBuilder(globalOptions, title, itemBuilder))) + internal val list: MutableList<KPrefItemCore> = mutableListOf() }
\ No newline at end of file diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt index 2ec0a34..b22c4b3 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt @@ -7,6 +7,7 @@ import ca.allanwang.kau.dialogs.color.ColorBuilder import ca.allanwang.kau.dialogs.color.ColorContract import ca.allanwang.kau.dialogs.color.colorPickerDialog import ca.allanwang.kau.kpref.CoreAttributeContract +import ca.allanwang.kau.kpref.GlobalOptions /** * Created by Allan Wang on 2017-06-07. @@ -57,11 +58,11 @@ class KPrefColorPicker(val builder: KPrefColorContract) : KPrefItemBase<Int>(bui /** * Default implementation of [KPrefColorContract] */ - class KPrefColorBuilder(attributes: CoreAttributeContract, + class KPrefColorBuilder(globalOptions: GlobalOptions, override var titleRes: Int, getter: () -> Int, setter: (value: Int) -> Unit - ) : KPrefColorContract, BaseContract<Int> by BaseBuilder<Int>(attributes, titleRes, getter, setter), + ) : KPrefColorContract, BaseContract<Int> by BaseBuilder<Int>(globalOptions, titleRes, getter, setter), ColorContract by ColorBuilder() { override var showPreview: Boolean = true } diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt index 76fa01e..949ae59 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt @@ -4,6 +4,7 @@ import android.support.annotation.CallSuper import android.view.View import ca.allanwang.kau.R import ca.allanwang.kau.kpref.CoreAttributeContract +import ca.allanwang.kau.kpref.GlobalOptions import ca.allanwang.kau.utils.resolveDrawable /** @@ -72,11 +73,11 @@ abstract class KPrefItemBase<T>(val base: BaseContract<T>) : KPrefItemCore(base) /** * Default implementation of [BaseContract] */ - class BaseBuilder<T>(attributes: CoreAttributeContract, + class BaseBuilder<T>(globalOptions: GlobalOptions, titleRes: Int, override val getter: () -> T, override val setter: (value: T) -> Unit - ) : CoreContract by CoreBuilder(attributes, titleRes), BaseContract<T> { + ) : CoreContract by CoreBuilder(globalOptions, titleRes), BaseContract<T> { override var enabler: () -> Boolean = { true } override var onClick: ((itemView: View, innerContent: View?, item: KPrefItemBase<T>) -> Boolean)? = null override var onDisabledClick: ((itemView: View, innerContent: View?, item: KPrefItemBase<T>) -> Boolean)? = null diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt index 2d3c714..faf451f 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt @@ -11,7 +11,7 @@ import android.widget.ImageView import android.widget.LinearLayout import android.widget.TextView import ca.allanwang.kau.R -import ca.allanwang.kau.kpref.CoreAttributeContract +import ca.allanwang.kau.kpref.GlobalOptions import ca.allanwang.kau.utils.* import com.mikepenz.fastadapter.items.AbstractItem import com.mikepenz.iconics.typeface.IIcon @@ -39,12 +39,12 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem<KPrefItemCor if (core.iicon != null) icon?.visible()?.setIcon(core.iicon, 24) else icon?.gone() innerFrame?.removeAllViews() - val textColor = core.attributes.textColor?.invoke() + val textColor = core.globalOptions.textColor?.invoke() if (textColor != null) { title.setTextColor(textColor) desc?.setTextColor(textColor) } - val accentColor = core.attributes.accentColor?.invoke() + val accentColor = core.globalOptions.accentColor?.invoke() if (accentColor != null) { icon?.drawable?.setTint(accentColor) } @@ -70,7 +70,7 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem<KPrefItemCor * Core values for all kpref items */ interface CoreContract { - val attributes: CoreAttributeContract + val globalOptions: GlobalOptions val titleRes: Int var descRes: Int var iicon: IIcon? @@ -79,7 +79,7 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem<KPrefItemCor /** * Default impementation of [CoreContract] */ - class CoreBuilder(override val attributes: CoreAttributeContract, + class CoreBuilder(override val globalOptions: GlobalOptions, override val titleRes: Int) : CoreContract { override var descRes: Int = -1 override var iicon: IIcon? = null 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 new file mode 100644 index 0000000..c8a9fda --- /dev/null +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefSubItems.kt @@ -0,0 +1,44 @@ +package ca.allanwang.kau.kpref.items + +import android.view.View +import ca.allanwang.kau.R +import ca.allanwang.kau.kpref.GlobalOptions +import ca.allanwang.kau.kpref.KPrefAdapterBuilder + +/** + * Created by Allan Wang on 2017-06-14. + * + * Text preference + * Holds a textview to display data on the right + * This is still a generic preference + * + */ +class KPrefSubItems(val builder: KPrefSubItemsBuilder) : KPrefItemCore(builder) { + + override fun onClick(itemView: View, innerContent: View?): Boolean { + builder.globalOptions.showNextPrefs(builder.itemBuilder) + return true + } + + override fun getLayoutRes(): Int = R.layout.kau_preference + + override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) {} + /** + * Extension of the base contract with an optional text getter + */ + interface KPrefSubItemsContract : CoreContract { + val itemBuilder: KPrefAdapterBuilder.() -> Unit + } + + /** + * Default implementation of [KPrefTextContract] + */ + class KPrefSubItemsBuilder( + globalOptions: GlobalOptions, + titleRes: Int, + override val itemBuilder: KPrefAdapterBuilder.() -> Unit + ) : KPrefSubItemsContract, CoreContract by CoreBuilder(globalOptions, titleRes) + + override fun getType(): Int = R.id.kau_item_pref_checkbox + +}
\ No newline at end of file diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt index 0daa054..b93dbed 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt @@ -4,6 +4,7 @@ import android.view.View import android.widget.TextView import ca.allanwang.kau.R import ca.allanwang.kau.kpref.CoreAttributeContract +import ca.allanwang.kau.kpref.GlobalOptions import ca.allanwang.kau.utils.toast /** @@ -39,11 +40,11 @@ class KPrefText<T>(val builder: KPrefTextContract<T>) : KPrefItemBase<T>(builder * Default implementation of [KPrefTextContract] */ class KPrefTextBuilder<T>( - attributes: CoreAttributeContract, + globalOptions: GlobalOptions, titleRes: Int, getter: () -> T, setter: (value: T) -> Unit - ) : KPrefTextContract<T>, BaseContract<T> by BaseBuilder<T>(attributes, titleRes, getter, setter) { + ) : KPrefTextContract<T>, BaseContract<T> by BaseBuilder<T>(globalOptions, titleRes, getter, setter) { override var textGetter: (T) -> String? = { it?.toString() } } diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt index 72b8573..c64220c 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt @@ -6,6 +6,7 @@ import android.support.annotation.StringRes import android.support.design.widget.Snackbar import android.view.View import android.view.ViewGroup +import android.widget.FrameLayout import android.widget.ImageView import android.widget.TextView import com.mikepenz.iconics.IconicsDrawable @@ -34,13 +35,6 @@ fun View.isVisible(): Boolean = visibility == View.VISIBLE fun View.isInvisible(): Boolean = visibility == View.INVISIBLE fun View.isGone(): Boolean = visibility == View.GONE -fun View.matchParent() { - with(layoutParams) { - height = ViewGroup.LayoutParams.MATCH_PARENT - width = ViewGroup.LayoutParams.MATCH_PARENT - } -} - fun View.snackbar(text: String, duration: Int = Snackbar.LENGTH_LONG, builder: (Snackbar) -> Unit = {}) { val snackbar = Snackbar.make(this, text, duration) builder.invoke(snackbar) |