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
@@ -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.kotlin.autoDisposable
import common.di.appComponent
import io.reactivex.Observable
import common.util.extensions.pageScrolled
import kotlinx.android.synthetic.main.theme_picker_activity.*
import presentation.common.base.QkActivity
import javax.inject.Inject
@@ -32,9 +32,11 @@ import javax.inject.Inject
class ThemePickerActivity : QkActivity<ThemePickerViewModel>(), ThemePickerView {
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 themePagerAdapter: ThemePagerAdapter
override fun onCreate(savedInstanceState: Bundle?) {
appComponent.inject(this)
@@ -44,6 +46,10 @@ class ThemePickerActivity : QkActivity<ThemePickerViewModel>(), ThemePickerView
supportActionBar?.setDisplayHomeAsUpEnabled(true)
viewModel.bindView(this)
pager.offscreenPageLimit = 1
pager.adapter = themePagerAdapter
tabs.pager = pager
themeAdapter.data = colors.materialColors
materialColors.layoutManager = LinearLayoutManager(this)
@@ -52,9 +58,15 @@ class ThemePickerActivity : QkActivity<ThemePickerViewModel>(), ThemePickerView
colors.background
.autoDisposable(scope())
.subscribe { color -> window.decorView.setBackgroundColor(color) }
colors.theme
.autoDisposable(scope())
.subscribe { color -> rgbPicker.setBackgroundColor(color) }
}
override fun render(state: ThemePickerState) {
rgbPicker.alpha = state.rgbAlpha
themeAdapter.selectedColor = state.selectedColor
}
@@ -19,7 +19,6 @@
package presentation.feature.themepicker
data class ThemePickerState(
val selectedColor: Int = -1
val selectedColor: Int = -1,
val rgbAlpha: Float = 0f
)
@@ -18,11 +18,13 @@
*/
package presentation.feature.themepicker
import common.util.extensions.PageScroll
import io.reactivex.Observable
import presentation.common.base.QkView
interface ThemePickerView : QkView<ThemePickerState> {
val pageScrolledIntent: Observable<PageScroll>
val themeSelectedIntent: Observable<Int>
}
@@ -24,6 +24,7 @@ import common.di.appComponent
import common.util.Preferences
import io.reactivex.rxkotlin.plusAssign
import presentation.common.base.QkViewModel
import java.util.concurrent.TimeUnit
import javax.inject.Inject
class ThemePickerViewModel : QkViewModel<ThemePickerView, ThemePickerState>(ThemePickerState()) {
@@ -40,6 +41,12 @@ class ThemePickerViewModel : QkViewModel<ThemePickerView, ThemePickerState>(Them
override fun bindView(view: ThemePickerView) {
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
.autoDisposable(view.scope())
.subscribe { color -> prefs.theme.set(color) }