diff options
Diffstat (limited to 'library/src/main/kotlin/ca/allanwang/kau/kpref/items')
5 files changed, 209 insertions, 0 deletions
diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt new file mode 100644 index 0000000..3c87571 --- /dev/null +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt @@ -0,0 +1,34 @@ +package ca.allanwang.kau.kpref.items + +import android.support.annotation.StringRes +import android.view.View +import android.widget.CheckBox +import ca.allanwang.kau.R +import com.mikepenz.iconics.typeface.IIcon + +/** + * Created by Allan Wang on 2017-06-07. + */ +class KPrefCheckbox(@StringRes title: Int, + @StringRes description: Int = -1, + iicon: IIcon? = null, + enabled: Boolean = true, + getter: () -> Boolean, + setter: (value: Boolean) -> Unit) : KPrefItemBase<Boolean>(title, description, iicon, enabled, getter, setter) { + + override fun onPostBindView(viewHolder: KPrefItemCore.ViewHolder) { + super.onPostBindView(viewHolder) + viewHolder.addInnerView(R.layout.kau_preference_checkbox) + (viewHolder[R.id.kau_pref_checkbox] as CheckBox).isChecked = pref + } + + override fun onClick(itemView: View): Boolean { + val checkbox = itemView.findViewById(R.id.kau_pref_checkbox) as CheckBox + pref = !pref + checkbox.isChecked = pref + return true + } + + 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/KPrefColorPicker.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt new file mode 100644 index 0000000..cca35b0 --- /dev/null +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt @@ -0,0 +1,35 @@ +package ca.allanwang.kau.kpref.items + +import android.support.annotation.StringRes +import android.view.View +import ca.allanwang.kau.R +import ca.allanwang.kau.dialogs.color.colorPickerDialog +import com.mikepenz.iconics.typeface.IIcon + +/** + * Created by Allan Wang on 2017-06-07. + */ +class KPrefColorPicker(@StringRes title: Int, + @StringRes description: Int = -1, + iicon: IIcon? = null, + enabled: Boolean = true, + getter: () -> Int, + setter: (value: Int) -> Unit) : KPrefItemBase<Int>(title, description, iicon, enabled, getter, setter) { + + override fun onPostBindView(viewHolder: KPrefItemCore.ViewHolder) { + super.onPostBindView(viewHolder) + //TODO add color circle view + } + + override fun onClick(itemView: View): Boolean { + itemView.context.colorPickerDialog { + titleRes = this@KPrefColorPicker.title + defaultColor = pref + colorCallbacks.add { pref = it } + }.show() + return true + } + + override fun getType(): Int = R.id.kau_item_pref_color_picker + +}
\ No newline at end of file diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt new file mode 100644 index 0000000..9c22469 --- /dev/null +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt @@ -0,0 +1,22 @@ +package ca.allanwang.kau.kpref.items + +import android.support.annotation.StringRes +import android.view.View +import ca.allanwang.kau.R + +/** + * Created by Allan Wang on 2017-06-07. + */ +class KPrefHeader(@StringRes title: Int) : KPrefItemCore(title = title) { + + override fun getLayoutRes(): Int = R.layout.kau_preference_header + + override fun onPostBindView(viewHolder: ViewHolder) { + viewHolder.itemView.isClickable = false + } + + override fun onClick(itemView: View): Boolean = true + + override fun getType() = R.id.kau_item_pref_header + +}
\ No newline at end of file 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 new file mode 100644 index 0000000..c86f3b6 --- /dev/null +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt @@ -0,0 +1,35 @@ +package ca.allanwang.kau.kpref.items + +import android.support.annotation.CallSuper +import android.support.annotation.StringRes +import android.util.Log +import ca.allanwang.kau.R +import com.mikepenz.iconics.typeface.IIcon + +/** + * Created by Allan Wang on 2017-06-05. + * + * Base class for pref setters that include the Shared Preference hooks + */ + +abstract class KPrefItemBase<T>(@StringRes title: Int, + @StringRes description: Int = -1, + iicon: IIcon? = null, + val enabled: Boolean = true, + private val getter: () -> T, + private val setter: (value: T) -> Unit) : KPrefItemCore(title, description, iicon) { + + var pref: T + get() = getter.invoke() + set(value) { + setter.invoke(value) + } + + @CallSuper + override fun onPostBindView(viewHolder: ViewHolder) { + viewHolder.itemView.isEnabled = enabled + viewHolder.itemView.alpha = if (enabled) 1.0f else 0.3f + } + + override final fun getLayoutRes(): Int = R.layout.kau_preference +}
\ No newline at end of file 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 new file mode 100644 index 0000000..8766234 --- /dev/null +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt @@ -0,0 +1,83 @@ +package ca.allanwang.kau.kpref.items + +import android.support.annotation.CallSuper +import android.support.annotation.IdRes +import android.support.annotation.LayoutRes +import android.support.annotation.StringRes +import android.support.v7.widget.RecyclerView +import android.view.LayoutInflater +import android.view.View +import android.widget.ImageView +import android.widget.LinearLayout +import android.widget.TextView +import butterknife.ButterKnife +import ca.allanwang.kau.R +import ca.allanwang.kau.logging.SL +import ca.allanwang.kau.utils.* +import com.mikepenz.fastadapter.items.AbstractItem +import com.mikepenz.iconics.typeface.IIcon + +/** + * Created by Allan Wang on 2017-06-05. + * + * Core class containing nothing but the view items + */ + +abstract class KPrefItemCore(@StringRes val title: Int, + @StringRes val description: Int = -1, + val iicon: IIcon? = null) : AbstractItem<KPrefItemCore, KPrefItemCore.ViewHolder>() { + + override final fun getViewHolder(v: View) = ViewHolder(v) + + @CallSuper + override fun bindView(viewHolder: ViewHolder, payloads: List<Any>) { + super.bindView(viewHolder, payloads) + with(viewHolder) { + val context = itemView.context + title.text = context.string(this@KPrefItemCore.title) + if (description > 0) + desc?.visible()?.setText(description) + else + desc?.gone() + if (iicon != null) { + iconFrame?.visible() + icon?.setIcon(iicon, 48) + } else iconFrame?.gone() + onPostBindView(this) + } + } + + abstract fun onPostBindView(viewHolder: ViewHolder) + + abstract fun onClick(itemView: View): Boolean + + override fun unbindView(holder: ViewHolder) { + super.unbindView(holder) + with(holder) { + title.text = null + desc?.text = null + icon?.setImageDrawable(null) + innerFrame?.removeAllViews() + itemView.isEnabled = true + itemView.alpha = 1.0f + } + } + + class ViewHolder(v: View) : RecyclerView.ViewHolder(v) { + val title: TextView by bindView(R.id.kau_pref_title) + val desc: TextView? by bindOptionalView(R.id.kau_pref_desc) + val icon: ImageView? by bindOptionalView(R.id.kau_pref_icon) + val iconFrame: LinearLayout? by bindOptionalView(R.id.kau_pref_icon_frame) + val innerFrame: LinearLayout? by bindOptionalView(R.id.kau_pref_inner_frame) + + init { + ButterKnife.bind(v) + } + + fun addInnerView(@LayoutRes id: Int) { + LayoutInflater.from(innerFrame!!.context).inflate(id, innerFrame) + } + + operator fun get(@IdRes id: Int): View = itemView.findViewById(id) + } +}
\ No newline at end of file |