Add pager to theme picker

This commit is contained in:
Moez Bhatti
2018-02-01 21:34:49 -05:00
parent 4b1034d908
commit 1e3bbd1b19
13 changed files with 267 additions and 8 deletions
@@ -64,6 +64,7 @@ interface AppComponent {
fun inject(view: AvatarView) fun inject(view: AvatarView)
fun inject(view: DetailedChipView) fun inject(view: DetailedChipView)
fun inject(view: PagerTitleView)
fun inject(view: PreferenceView) fun inject(view: PreferenceView)
fun inject(view: QkEditText) fun inject(view: QkEditText)
fun inject(view: QkSwitch) fun inject(view: QkSwitch)
@@ -28,4 +28,10 @@ fun Int.dpToPx(context: Context): Int {
fun Int.withAlpha(alpha: Int): Int { fun Int.withAlpha(alpha: Int): Int {
return Color.argb(alpha, Color.red(this), Color.green(this), Color.blue(this)) return Color.argb(alpha, Color.red(this), Color.green(this), Color.blue(this))
}
fun Int.forEach(action: (Int) -> Unit) {
for (index in 0 until this) {
action(index)
}
} }
@@ -20,11 +20,13 @@ package common.util.extensions
import android.content.Context import android.content.Context
import android.content.res.ColorStateList import android.content.res.ColorStateList
import android.support.v4.view.ViewPager
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.widget.EditText import android.widget.EditText
import android.widget.ImageView import android.widget.ImageView
import io.reactivex.Observable
fun EditText.showKeyboard() { fun EditText.showKeyboard() {
requestFocus() requestFocus()
@@ -58,4 +60,17 @@ fun View.setPadding(left: Int? = null, top: Int? = null, right: Int? = null, bot
fun View.setVisible(visible: Boolean, invisible: Int = View.GONE) { fun View.setVisible(visible: Boolean, invisible: Int = View.GONE) {
visibility = if (visible) View.VISIBLE else invisible visibility = if (visible) View.VISIBLE else invisible
}
data class PageScroll(val position: Int, val offset: Float)
fun ViewPager.pageScrolled(): Observable<PageScroll> {
return Observable.create<PageScroll> { emitter ->
addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels)
emitter.onNext(PageScroll(position, positionOffset))
}
})
}
} }
@@ -0,0 +1,97 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package presentation.common.widget
import android.content.Context
import android.content.res.ColorStateList
import android.support.v4.view.ViewPager
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.TextView
import com.moez.QKSMS.R
import com.uber.autodispose.android.scope
import com.uber.autodispose.kotlin.autoDisposable
import common.di.appComponent
import common.util.Colors
import common.util.extensions.forEach
import io.reactivex.rxkotlin.Observables
import kotlinx.android.synthetic.main.tab_view.view.*
import javax.inject.Inject
class PagerTitleView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : LinearLayout(context, attrs) {
@Inject lateinit var colors: Colors
var pager: ViewPager? = null
set(value) {
if (field !== value) {
field = value
recreate()
}
}
init {
if (!isInEditMode) appComponent.inject(this)
}
private fun recreate() {
removeAllViews()
pager?.adapter?.count?.forEach { position ->
val view = LayoutInflater.from(context).inflate(R.layout.tab_view, this, false)
view.label.text = pager?.adapter?.getPageTitle(position)
view.setOnClickListener { pager?.currentItem = position }
addView(view)
}
pager?.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
override fun onPageSelected(position: Int) {
childCount.forEach { index ->
getChildAt(index).isSelected = index == position
}
}
})
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
val states = arrayOf(
intArrayOf(android.R.attr.state_selected),
intArrayOf(-android.R.attr.state_selected))
Observables
.combineLatest(colors.theme, colors.textSecondary, { theme, textSecondary ->
ColorStateList(states, intArrayOf(theme, textSecondary))
})
.autoDisposable(scope())
.subscribe { colorStateList ->
childCount.forEach { index ->
(getChildAt(index) as? TextView)?.setTextColor(colorStateList)
}
}
colors.toolbarColor
.autoDisposable(scope())
.subscribe { color -> setBackgroundColor(color) }
}
}
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package presentation.feature.themepicker
import android.content.Context
import android.support.v4.view.PagerAdapter
import android.view.View
import android.view.ViewGroup
import com.moez.QKSMS.R
import javax.inject.Inject
class ThemePagerAdapter @Inject constructor(private val context: Context) : PagerAdapter() {
override fun instantiateItem(container: ViewGroup, position: Int): Any {
return when (position) {
1 -> container.findViewById(R.id.rgbPicker)
else -> container.findViewById(R.id.materialColors)
}
}
override fun getPageTitle(position: Int): CharSequence? {
return when (position) {
1 -> context.getString(R.string.theme_rgb)
else -> context.getString(R.string.theme_material)
}
}
override fun isViewFromObject(view: View, `object`: Any): Boolean {
return view == `object`
}
override fun getCount(): Int {
return 2
}
}
@@ -24,7 +24,7 @@ import com.moez.QKSMS.R
import com.uber.autodispose.android.lifecycle.scope import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable import com.uber.autodispose.kotlin.autoDisposable
import common.di.appComponent import common.di.appComponent
import io.reactivex.Observable import common.util.extensions.pageScrolled
import kotlinx.android.synthetic.main.theme_picker_activity.* import kotlinx.android.synthetic.main.theme_picker_activity.*
import presentation.common.base.QkActivity import presentation.common.base.QkActivity
import javax.inject.Inject import javax.inject.Inject
@@ -32,9 +32,11 @@ import javax.inject.Inject
class ThemePickerActivity : QkActivity<ThemePickerViewModel>(), ThemePickerView { class ThemePickerActivity : QkActivity<ThemePickerViewModel>(), ThemePickerView {
override val viewModelClass = ThemePickerViewModel::class override val viewModelClass = ThemePickerViewModel::class
override val themeSelectedIntent: Observable<Int> by lazy { themeAdapter.colorSelected } override val themeSelectedIntent by lazy { themeAdapter.colorSelected }
override val pageScrolledIntent by lazy { pager.pageScrolled() }
@Inject lateinit var themeAdapter: ThemeAdapter @Inject lateinit var themeAdapter: ThemeAdapter
@Inject lateinit var themePagerAdapter: ThemePagerAdapter
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
appComponent.inject(this) appComponent.inject(this)
@@ -44,6 +46,10 @@ class ThemePickerActivity : QkActivity<ThemePickerViewModel>(), ThemePickerView
supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true)
viewModel.bindView(this) viewModel.bindView(this)
pager.offscreenPageLimit = 1
pager.adapter = themePagerAdapter
tabs.pager = pager
themeAdapter.data = colors.materialColors themeAdapter.data = colors.materialColors
materialColors.layoutManager = LinearLayoutManager(this) materialColors.layoutManager = LinearLayoutManager(this)
@@ -52,9 +58,15 @@ class ThemePickerActivity : QkActivity<ThemePickerViewModel>(), ThemePickerView
colors.background colors.background
.autoDisposable(scope()) .autoDisposable(scope())
.subscribe { color -> window.decorView.setBackgroundColor(color) } .subscribe { color -> window.decorView.setBackgroundColor(color) }
colors.theme
.autoDisposable(scope())
.subscribe { color -> rgbPicker.setBackgroundColor(color) }
} }
override fun render(state: ThemePickerState) { override fun render(state: ThemePickerState) {
rgbPicker.alpha = state.rgbAlpha
themeAdapter.selectedColor = state.selectedColor themeAdapter.selectedColor = state.selectedColor
} }
@@ -19,7 +19,6 @@
package presentation.feature.themepicker package presentation.feature.themepicker
data class ThemePickerState( data class ThemePickerState(
val selectedColor: Int = -1,
val selectedColor: Int = -1 val rgbAlpha: Float = 0f
) )
@@ -18,11 +18,13 @@
*/ */
package presentation.feature.themepicker package presentation.feature.themepicker
import common.util.extensions.PageScroll
import io.reactivex.Observable import io.reactivex.Observable
import presentation.common.base.QkView import presentation.common.base.QkView
interface ThemePickerView : QkView<ThemePickerState> { interface ThemePickerView : QkView<ThemePickerState> {
val pageScrolledIntent: Observable<PageScroll>
val themeSelectedIntent: Observable<Int> val themeSelectedIntent: Observable<Int>
} }
@@ -24,6 +24,7 @@ import common.di.appComponent
import common.util.Preferences import common.util.Preferences
import io.reactivex.rxkotlin.plusAssign import io.reactivex.rxkotlin.plusAssign
import presentation.common.base.QkViewModel import presentation.common.base.QkViewModel
import java.util.concurrent.TimeUnit
import javax.inject.Inject import javax.inject.Inject
class ThemePickerViewModel : QkViewModel<ThemePickerView, ThemePickerState>(ThemePickerState()) { class ThemePickerViewModel : QkViewModel<ThemePickerView, ThemePickerState>(ThemePickerState()) {
@@ -40,6 +41,12 @@ class ThemePickerViewModel : QkViewModel<ThemePickerView, ThemePickerState>(Them
override fun bindView(view: ThemePickerView) { override fun bindView(view: ThemePickerView) {
super.bindView(view) super.bindView(view)
view.pageScrolledIntent
.throttleFirst(16, TimeUnit.MILLISECONDS)
.map { (position, positionOffset) -> if (position == 0) positionOffset * positionOffset else 1f }
.autoDisposable(view.scope())
.subscribe { alpha -> newState { it.copy(rgbAlpha = alpha) } }
view.themeSelectedIntent view.themeSelectedIntent
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { color -> prefs.theme.set(color) } .subscribe { color -> prefs.theme.set(color) }
+31
View File
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
~
~ This file is part of QKSMS.
~
~ QKSMS is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ QKSMS is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
-->
<presentation.common.widget.QkTextView android:id="@+id/label"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/ripple"
android:backgroundTint="@android:color/transparent"
android:gravity="center"
android:paddingEnd="12dp"
android:paddingStart="12dp"
android:textAllCaps="true"
android:textSize="14sp"
android:textStyle="bold" />
@@ -24,15 +24,37 @@
<include layout="@layout/toolbar" /> <include layout="@layout/toolbar" />
<presentation.common.widget.PagerTitleView
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:paddingEnd="12dp"
android:paddingStart="12dp" />
<FrameLayout <FrameLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="1"> android:layout_weight="1">
<android.support.v7.widget.RecyclerView <android.support.v4.view.ViewPager
android:id="@+id/materialColors" android:id="@+id/pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/materialColors"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/rgbPicker"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</android.support.v4.view.ViewPager>
<View <View
android:layout_width="match_parent" android:layout_width="match_parent"
+3
View File
@@ -120,6 +120,9 @@
<string name="settings_dialog_theme_title">Theme</string> <string name="settings_dialog_theme_title">Theme</string>
<string name="theme_material">Material</string>
<string name="theme_rgb">RGB</string>
<string name="notification_reply">Reply</string> <string name="notification_reply">Reply</string>
<string name="notification_read">Mark read</string> <string name="notification_read">Mark read</string>
+12
View File
@@ -49,6 +49,18 @@
<item name="textColor">tertiary</item> <item name="textColor">tertiary</item>
</style> </style>
<style name="TextTab">
<item name="android:layout_height">48dp</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:background">@drawable/ripple</item>
<item name="android:gravity">center</item>
<item name="android:paddingEnd">12sp</item>
<item name="android:paddingStart">12sp</item>
<item name="android:textAllCaps">true</item>
<item name="android:textSize">14sp</item>
<item name="android:textStyle">bold</item>
</style>
<style name="CollapsingToolbarCollapsedText"> <style name="CollapsingToolbarCollapsedText">
<item name="android:textSize">20sp</item> <item name="android:textSize">20sp</item>
</style> </style>