diff options
author | Allan Wang <me@allanwang.ca> | 2017-06-27 00:29:57 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2017-06-27 00:29:57 -0700 |
commit | 1e5b600a5f6bbffa4dc8efa2991e4b833a8e4c15 (patch) | |
tree | 3f898b8141e7c1668df204eea58c856e08c0aa5e /library/src/main/kotlin | |
parent | 215021180431503c55845f9a9fb224abaa4973bb (diff) | |
download | kau-1e5b600a5f6bbffa4dc8efa2991e4b833a8e4c15.tar.gz kau-1e5b600a5f6bbffa4dc8efa2991e4b833a8e4c15.tar.bz2 kau-1e5b600a5f6bbffa4dc8efa2991e4b833a8e4c15.zip |
Update text slider and begin about libraries
Diffstat (limited to 'library/src/main/kotlin')
4 files changed, 154 insertions, 71 deletions
diff --git a/library/src/main/kotlin/ca/allanwang/kau/about/AboutActivity.kt b/library/src/main/kotlin/ca/allanwang/kau/about/AboutActivity.kt new file mode 100644 index 0000000..ce81c29 --- /dev/null +++ b/library/src/main/kotlin/ca/allanwang/kau/about/AboutActivity.kt @@ -0,0 +1,33 @@ +package ca.allanwang.kau.about + +import android.os.Bundle +import android.support.v7.app.AppCompatActivity +import android.support.v7.widget.RecyclerView +import android.support.v7.widget.Toolbar +import android.view.animation.Animation +import android.view.animation.AnimationUtils +import ca.allanwang.kau.R +import ca.allanwang.kau.utils.bindView +import ca.allanwang.kau.views.TextSlider + +/** + * Created by Allan Wang on 2017-06-26. + */ +open class AboutActivity : AppCompatActivity() { + + val toolbar: Toolbar by bindView(R.id.kau_toolbar) + val toolbarText: TextSlider by bindView(R.id.kau_toolbar_text) + val recycler: RecyclerView by bindView(R.id.kau_recycler) + + protected val SLIDE_IN_UP: Animation by lazy { AnimationUtils.loadAnimation(this, R.anim.kau_slide_in_top) } + protected val SLIDE_IN_DOWN: Animation by lazy { AnimationUtils.loadAnimation(this, R.anim.kau_slide_in_bottom) } + + protected val SLIDE_OUT_UP: Animation by lazy { AnimationUtils.loadAnimation(this, R.anim.kau_slide_out_top) } + protected val SLIDE_OUT_DOWN: Animation by lazy { AnimationUtils.loadAnimation(this, R.anim.kau_slide_out_bottom) } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.kau_activity_about) + + } +}
\ No newline at end of file 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 6103e50..96b1237 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt @@ -3,17 +3,13 @@ package ca.allanwang.kau.kpref import android.os.Bundle import android.support.annotation.StringRes import android.support.constraint.ConstraintLayout -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 @@ -23,9 +19,8 @@ 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 ca.allanwang.kau.views.TextSlider import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter -import java.util.* abstract class KPrefActivity : AppCompatActivity(), KPrefActivityContract { @@ -38,19 +33,14 @@ abstract class KPrefActivity : AppCompatActivity(), KPrefActivityContract { 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 toolbarTitle: TextSlider by bindView(R.id.kau_toolbar_text) val prefHolder: ViewAnimator by bindView(R.id.kau_holder) private lateinit var globalOptions: GlobalOptions - private val titleStack: Stack<Int> = Stack() var animate: Boolean = true - val isRootPref: Boolean - 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) } - 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) } + set(value) { + field = value + toolbarTitle.animate = value + } 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) } @@ -85,17 +75,6 @@ abstract class KPrefActivity : AppCompatActivity(), KPrefActivityContract { val builder = kPrefCoreAttributes() core.builder() globalOptions = GlobalOptions(core, this) - 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) - } - } - } showNextPrefs(R.string.kau_settings, onCreateKPrefs(savedInstanceState)) } @@ -106,7 +85,6 @@ abstract class KPrefActivity : AppCompatActivity(), KPrefActivityContract { 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) - titleStack.push(toolbarTitleRes) setKPrefAdapter(globalOptions, builder) } with(prefHolder) { @@ -115,18 +93,10 @@ abstract class KPrefActivity : AppCompatActivity(), KPrefActivityContract { addView(rv) showNext() } - with(toolbarTitle) { - inAnimation = if (animate) SLIDE_IN_RIGHT_TITLE else null - outAnimation = if (animate) SLIDE_OUT_LEFT_TITLE else null - setText(string(titleStack.peek())) - } + toolbarTitle.setNextText(string(toolbarTitleRes)) } override fun showPrevPrefs() { - if (titleStack.size <= 1) { - KL.e("Cannot go back in KPrefActivity; already at index ${titleStack.size - 1}") - return - } val current = prefHolder.currentView with(prefHolder) { inAnimation = if (animate) SLIDE_IN_LEFT_ITEMS else null @@ -135,12 +105,7 @@ abstract class KPrefActivity : AppCompatActivity(), KPrefActivityContract { removeView(current) adapter.notifyAdapterDataSetChanged() } - 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())) - } + toolbarTitle.setPrevText() } fun reload(vararg index: Int) { @@ -163,7 +128,7 @@ abstract class KPrefActivity : AppCompatActivity(), KPrefActivityContract { } fun backPress(): Boolean { - if (!isRootPref) { + if (!toolbarTitle.isRoot) { showPrevPrefs() return true } diff --git a/library/src/main/kotlin/ca/allanwang/kau/views/TextSlider.kt b/library/src/main/kotlin/ca/allanwang/kau/views/TextSlider.kt new file mode 100644 index 0000000..50b1848 --- /dev/null +++ b/library/src/main/kotlin/ca/allanwang/kau/views/TextSlider.kt @@ -0,0 +1,112 @@ +package ca.allanwang.kau.views + +import android.content.Context +import android.graphics.Color +import android.support.v4.widget.TextViewCompat +import android.text.TextUtils +import android.util.AttributeSet +import android.view.Gravity +import android.view.animation.Animation +import android.view.animation.AnimationUtils +import android.widget.TextSwitcher +import android.widget.TextView +import ca.allanwang.kau.R +import java.util.* + +/** + * Created by Allan Wang on 2017-06-21. + * + * Text switcher with global text color and embedded sliding animations + * Also has a stack to keep track of title changes + */ +class TextSlider @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null +) : TextSwitcher(context, attrs) { + + val titleStack: Stack<CharSequence?> = Stack() + + private val SLIDE_IN_LEFT: Animation by lazy { AnimationUtils.loadAnimation(context, R.anim.kau_slide_in_left) } + private val SLIDE_IN_RIGHT: Animation by lazy { AnimationUtils.loadAnimation(context, R.anim.kau_slide_in_right) } + private val SLIDE_IN_TOP: Animation by lazy { AnimationUtils.loadAnimation(context, R.anim.kau_slide_in_top) } + private val SLIDE_IN_BOTTOM: Animation by lazy { AnimationUtils.loadAnimation(context, R.anim.kau_slide_in_bottom) } + + private val SLIDE_OUT_LEFT: Animation by lazy { AnimationUtils.loadAnimation(context, R.anim.kau_slide_out_left) } + private val SLIDE_OUT_RIGHT: Animation by lazy { AnimationUtils.loadAnimation(context, R.anim.kau_slide_out_right) } + private val SLIDE_OUT_TOP: Animation by lazy { AnimationUtils.loadAnimation(context, R.anim.kau_slide_out_top) } + private val SLIDE_OUT_BOTTOM: Animation by lazy { AnimationUtils.loadAnimation(context, R.anim.kau_slide_out_bottom) } + + var animate: Boolean = true + var horizontal: Boolean = true + + var textColor: Int = Color.WHITE + get() = field + set(value) { + field = value + (getChildAt(0) as TextView).setTextColor(value) + (getChildAt(1) as TextView).setTextColor(value) + } + val isRoot: Boolean + get() = titleStack.size <= 1 + + override fun setText(text: CharSequence?) { + if ((currentView as TextView).text == text) return + super.setText(text) + } + + fun setTextSlideUp(text: CharSequence?) { + inAnimation = if (animate) SLIDE_IN_BOTTOM else null + outAnimation = if (animate) SLIDE_OUT_TOP else null + setText(text) + } + + fun setTextSlideDown(text: CharSequence?) { + inAnimation = if (animate) SLIDE_IN_TOP else null + outAnimation = if (animate) SLIDE_OUT_BOTTOM else null + setText(text) + } + + fun setTextSlideLeft(text: CharSequence?) { + inAnimation = if (animate) SLIDE_IN_RIGHT else null + outAnimation = if (animate) SLIDE_OUT_LEFT else null + setText(text) + } + + fun setTextSlideRight(text: CharSequence?) { + inAnimation = if (animate) SLIDE_IN_LEFT else null + outAnimation = if (animate) SLIDE_OUT_RIGHT else null + setText(text) + } + + override fun setCurrentText(text: CharSequence?) { + if (titleStack.isNotEmpty()) titleStack.pop() + titleStack.push(text) + super.setCurrentText(text) + } + + fun setNextText(text: CharSequence?) { + titleStack.push(text) + if (horizontal) setTextSlideLeft(text) else setTextSlideUp(text) + } + + /** + * Sets the text as the previous title + * No further checks are done, so be sure to verify with [isRoot] + */ + @Throws(EmptyStackException::class) + fun setPrevText() { + titleStack.pop() + val text = titleStack.peek() + if (horizontal) setTextSlideRight(text) else setTextSlideDown(text) + } + + init { + setFactory { + TextView(context).apply { + //replica of toolbar title + gravity = Gravity.START + setSingleLine() + ellipsize = TextUtils.TruncateAt.END + TextViewCompat.setTextAppearance(this, R.style.TextAppearance_AppCompat) + } + } + } +}
\ 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 deleted file mode 100644 index bef661e..0000000 --- a/library/src/main/kotlin/ca/allanwang/kau/views/TextSwitcherThemed.kt +++ /dev/null @@ -1,27 +0,0 @@ -package ca.allanwang.kau.views - -import android.content.Context -import android.graphics.Color -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 = Color.WHITE - get() = field - set(value) { - field = value - (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 |