aboutsummaryrefslogtreecommitdiff
path: root/library/src
diff options
context:
space:
mode:
Diffstat (limited to 'library/src')
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt104
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt15
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/views/TextSwitcherThemed.kt23
-rw-r--r--library/src/main/res/anim/kau_slide_in_left.xml4
-rw-r--r--library/src/main/res/anim/kau_slide_out_left.xml4
-rw-r--r--library/src/main/res/layout/kau_activity_kpref.xml12
6 files changed, 128 insertions, 34 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 7c4efee..9653029 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt
@@ -2,33 +2,59 @@ package ca.allanwang.kau.kpref
import android.os.Bundle
import android.support.annotation.StringRes
+import android.support.v4.widget.TextViewCompat
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar
+import android.text.TextUtils
+import android.view.Gravity
import android.view.View
+import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.FrameLayout
+import android.widget.TextView
import android.widget.ViewAnimator
import ca.allanwang.kau.R
import ca.allanwang.kau.kpref.items.KPrefItemCore
+import ca.allanwang.kau.logging.KL
import ca.allanwang.kau.utils.bindView
import ca.allanwang.kau.utils.resolveColor
import ca.allanwang.kau.utils.statusBarColor
+import ca.allanwang.kau.utils.string
import ca.allanwang.kau.views.RippleCanvas
+import ca.allanwang.kau.views.TextSwitcherThemed
import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter
+import java.util.*
abstract class KPrefActivity : AppCompatActivity(), KPrefActivityContract {
val adapter: FastItemAdapter<KPrefItemCore>
+ @Suppress("UNCHECKED_CAST")
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 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<Pair<Int, Int>> = LinkedList()
+ private var index = -1
+ var animate: Boolean = true
+ val isRootPref: Boolean
+ get() = index <= 0
+
+ //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) }
+ private val SLIDE_IN_RIGHT_TITLE: Animation by lazy { AnimationUtils.loadAnimation(this, R.anim.kau_slide_in_right) }
+ private val SLIDE_OUT_LEFT_TITLE: Animation by lazy { AnimationUtils.loadAnimation(this, R.anim.kau_slide_out_left) }
+ private val SLIDE_OUT_RIGHT_TITLE: Animation by lazy { AnimationUtils.loadAnimation(this, R.anim.kau_slide_out_right) }
+
+ private val SLIDE_IN_LEFT_ITEMS: Animation by lazy { AnimationUtils.loadAnimation(this, R.anim.kau_slide_in_left) }
+ private val SLIDE_IN_RIGHT_ITEMS: Animation by lazy { AnimationUtils.loadAnimation(this, R.anim.kau_slide_in_right) }
+ private val SLIDE_OUT_LEFT_ITEMS: Animation by lazy { AnimationUtils.loadAnimation(this, R.anim.kau_slide_out_left) }
+ private val SLIDE_OUT_RIGHT_ITEMS: Animation by lazy { AnimationUtils.loadAnimation(this, R.anim.kau_slide_out_right) }
/**
* Core attribute builder that is consistent throughout all items
@@ -36,19 +62,9 @@ abstract class KPrefActivity : AppCompatActivity(), KPrefActivityContract {
*/
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)
+ //setup layout
setContentView(R.layout.kau_activity_kpref)
setSupportActionBar(toolbar)
if (supportActionBar != null)
@@ -56,41 +72,81 @@ abstract class KPrefActivity : AppCompatActivity(), KPrefActivityContract {
setDisplayHomeAsUpEnabled(true)
setDisplayShowHomeEnabled(true)
toolbar.setNavigationOnClickListener { onBackPressed() }
+ setDisplayShowTitleEnabled(false)
}
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))
- with(prefHolder) {
- animateFirstView = false
- inAnimation = AnimationUtils.loadAnimation(context, R.anim.kau_slide_in_right)
- outAnimation = AnimationUtils.loadAnimation(context, R.anim.kau_slide_out_left)
- }
+ 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))
- baseRecycler = recycler
+ with(toolbarTitle) {
+ setFactory {
+ TextView(this@KPrefActivity).apply {
+ //replica of toolbar title
+ gravity = Gravity.START
+ setSingleLine()
+ ellipsize = TextUtils.TruncateAt.END
+ TextViewCompat.setTextAppearance(this, R.style.TextAppearance_AppCompat_Title)
+ }
+ }
+ setCurrentText(string(R.string.kau_settings))
+ }
+ }
+
+ override fun onPostCreate(savedInstanceState: Bundle?) {
+ super.onPostCreate(savedInstanceState)
}
override fun showNextPrefs(builder: KPrefAdapterBuilder.() -> Unit) {
+ index++
val rv = RecyclerView(this).apply {
layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
- setKPrefAdapter(globalOptions, builder)
+ titles.addFirst(Pair(index, 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)
+ inAnimation = if (animate) SLIDE_IN_RIGHT_ITEMS else null
+ outAnimation = if (animate) SLIDE_OUT_LEFT_ITEMS else null
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))
+ }
+ }
}
override fun showPrevPrefs() {
+ if (index <= 0) {
+ KL.e("Cannot go back in KPrefActivity; already at index $index")
+ return
+ }
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)
+ inAnimation = if (animate) SLIDE_IN_LEFT_ITEMS else null
+ outAnimation = if (animate) SLIDE_OUT_RIGHT_ITEMS else null
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))
+ }
+ }
}
fun reload(vararg index: Int) {
@@ -113,7 +169,7 @@ abstract class KPrefActivity : AppCompatActivity(), KPrefActivityContract {
}
fun backPress(): Boolean {
- if (baseRecycler != recycler) {
+ if (!isRootPref) {
showPrevPrefs()
return true
}
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 53cba99..a6ab8cd 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt
@@ -15,8 +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
*/
-fun RecyclerView.setKPrefAdapter(globalOptions: GlobalOptions, builder: KPrefAdapterBuilder.() -> Unit): FastItemAdapter<KPrefItemCore> {
+fun RecyclerView.setKPrefAdapter(globalOptions: GlobalOptions, builder: KPrefAdapterBuilder.() -> Unit): Int {
layoutManager = LinearLayoutManager(context)
val adapter = FastItemAdapter<KPrefItemCore>()
adapter.withOnClickListener { v, _, item, _ -> item.onClick(v, v.findViewById(R.id.kau_pref_inner_content)) }
@@ -24,7 +25,7 @@ fun RecyclerView.setKPrefAdapter(globalOptions: GlobalOptions, builder: KPrefAda
builder.invoke(items)
adapter.add(items.list)
this.adapter = adapter
- return adapter
+ return items.toolbarTitleRes
}
/**
@@ -61,6 +62,8 @@ 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)))
@@ -86,9 +89,11 @@ class KPrefAdapterBuilder(internal val globalOptions: GlobalOptions) {
= list.add(KPrefText<T>(KPrefText.KPrefTextBuilder<T>(globalOptions, title, getter, setter)
.apply { builder() }))
- fun subItems(@StringRes title:Int,
- itemBuilder:KPrefAdapterBuilder.()->Unit)
- = list.add(KPrefSubItems(KPrefSubItems.KPrefSubItemsBuilder(globalOptions, title, itemBuilder)))
+ fun subItems(@StringRes title: Int,
+ itemBuilder: KPrefAdapterBuilder.() -> Unit,
+ builder: KPrefSubItems.KPrefSubItemsContract.() -> Unit)
+ = list.add(KPrefSubItems(KPrefSubItems.KPrefSubItemsBuilder(globalOptions, title, itemBuilder)
+ .apply { builder() }))
internal val list: MutableList<KPrefItemCore> = mutableListOf()
} \ No newline at end of file
diff --git a/library/src/main/kotlin/ca/allanwang/kau/views/TextSwitcherThemed.kt b/library/src/main/kotlin/ca/allanwang/kau/views/TextSwitcherThemed.kt
new file mode 100644
index 0000000..bdc529c
--- /dev/null
+++ b/library/src/main/kotlin/ca/allanwang/kau/views/TextSwitcherThemed.kt
@@ -0,0 +1,23 @@
+package ca.allanwang.kau.views
+
+import android.content.Context
+import android.util.AttributeSet
+import android.widget.TextSwitcher
+import android.widget.TextView
+
+/**
+ * Created by Allan Wang on 2017-06-21.
+ */
+class TextSwitcherThemed @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null
+) : TextSwitcher(context, attrs) {
+
+ var textColor: Int = -1
+ get() = field
+ set(value) {
+ field = value
+ if (value != -1) {
+ (getChildAt(0) as TextView).setTextColor(value)
+ (getChildAt(1) as TextView).setTextColor(value)
+ }
+ }
+} \ No newline at end of file
diff --git a/library/src/main/res/anim/kau_slide_in_left.xml b/library/src/main/res/anim/kau_slide_in_left.xml
index 9322741..828bdae 100644
--- a/library/src/main/res/anim/kau_slide_in_left.xml
+++ b/library/src/main/res/anim/kau_slide_in_left.xml
@@ -2,6 +2,6 @@
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_shortAnimTime"
- android:fromXDelta="0"
- android:toXDelta="100%p" />
+ android:fromXDelta="-100%p"
+ android:toXDelta="0" />
</set> \ No newline at end of file
diff --git a/library/src/main/res/anim/kau_slide_out_left.xml b/library/src/main/res/anim/kau_slide_out_left.xml
index 0f9fbce..6fab33e 100644
--- a/library/src/main/res/anim/kau_slide_out_left.xml
+++ b/library/src/main/res/anim/kau_slide_out_left.xml
@@ -2,6 +2,6 @@
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_shortAnimTime"
- android:fromXDelta="100%p"
- android:toXDelta="0" />
+ android:fromXDelta="0"
+ android:toXDelta="-100%p" />
</set> \ No newline at end of file
diff --git a/library/src/main/res/layout/kau_activity_kpref.xml b/library/src/main/res/layout/kau_activity_kpref.xml
index 390674f..acc1f76 100644
--- a/library/src/main/res/layout/kau_activity_kpref.xml
+++ b/library/src/main/res/layout/kau_activity_kpref.xml
@@ -21,7 +21,17 @@
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
+ app:layout_constraintTop_toTopOf="parent">
+
+ <ca.allanwang.kau.views.TextSwitcherThemed
+ android:id="@+id/kau_toolbar_text"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:gravity="center"
+ android:textAppearance="@style/TextAppearance.AppCompat.Title"
+ android:textColor="@android:color/white" />
+
+ </android.support.v7.widget.Toolbar>
<ca.allanwang.kau.views.RippleCanvas
android:id="@+id/kau_ripple"