forked from upstream-mirrors/quik
Add support for themed Switches
This commit is contained in:
@@ -11,6 +11,7 @@ import com.moez.QKSMS.presentation.main.MainViewModel
|
||||
import com.moez.QKSMS.presentation.settings.SettingsActivity
|
||||
import com.moez.QKSMS.presentation.settings.SettingsViewModel
|
||||
import com.moez.QKSMS.presentation.view.AvatarView
|
||||
import com.moez.QKSMS.presentation.view.QkSwitch
|
||||
import com.moez.QKSMS.receiver.*
|
||||
import dagger.Component
|
||||
import javax.inject.Singleton
|
||||
@@ -35,6 +36,7 @@ interface AppComponent {
|
||||
fun inject(receiver: RemoteMessagingReceiver)
|
||||
|
||||
fun inject(view: AvatarView)
|
||||
fun inject(view: QkSwitch)
|
||||
|
||||
fun inject(viewModel: MainViewModel)
|
||||
fun inject(viewModel: ComposeViewModel)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.moez.QKSMS.common.util
|
||||
|
||||
import android.content.Context
|
||||
import com.moez.QKSMS.R
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.subjects.BehaviorSubject
|
||||
@@ -9,10 +11,15 @@ import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class ThemeManager @Inject constructor(prefs: Preferences) {
|
||||
class ThemeManager @Inject constructor(context: Context, prefs: Preferences) {
|
||||
|
||||
val color: Observable<Int>
|
||||
|
||||
val switchThumbEnabled: Observable<Int>
|
||||
val switchThumbDisabled: Observable<Int>
|
||||
val switchTrackEnabled: Observable<Int>
|
||||
val switchTrackDisabled: Observable<Int>
|
||||
|
||||
val bubbleColor = 0xFFFFFFFF.toInt()
|
||||
|
||||
val textPrimary = 0xFF49555F.toInt()
|
||||
@@ -31,6 +38,42 @@ class ThemeManager @Inject constructor(prefs: Preferences) {
|
||||
.filter { it.isNotEmpty() }
|
||||
.map { it.last() }
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
|
||||
switchThumbEnabled = prefs.dark.asObservable()
|
||||
.map { dark ->
|
||||
when (dark) {
|
||||
true -> R.color.switch_thumb_enabled_dark
|
||||
false -> R.color.switch_thumb_enabled_light
|
||||
}
|
||||
}
|
||||
.map { res -> context.resources.getColor(res) }
|
||||
|
||||
switchThumbDisabled = prefs.dark.asObservable()
|
||||
.map { dark ->
|
||||
when (dark) {
|
||||
true -> R.color.switch_thumb_disabled_dark
|
||||
false -> R.color.switch_thumb_disabled_light
|
||||
}
|
||||
}
|
||||
.map { res -> context.resources.getColor(res) }
|
||||
|
||||
switchTrackEnabled = prefs.dark.asObservable()
|
||||
.map { dark ->
|
||||
when (dark) {
|
||||
true -> R.color.switch_track_enabled_dark
|
||||
false -> R.color.switch_track_enabled_light
|
||||
}
|
||||
}
|
||||
.map { res -> context.resources.getColor(res) }
|
||||
|
||||
switchTrackDisabled = prefs.dark.asObservable()
|
||||
.map { dark ->
|
||||
when (dark) {
|
||||
true -> R.color.switch_track_disabled_dark
|
||||
false -> R.color.switch_track_disabled_light
|
||||
}
|
||||
}
|
||||
.map { res -> context.resources.getColor(res) }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,13 @@
|
||||
package com.moez.QKSMS.common.util.extensions
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Color
|
||||
import android.util.TypedValue
|
||||
|
||||
fun Int.dpToPx(context: Context): Int {
|
||||
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), context.resources.displayMetrics).toInt()
|
||||
}
|
||||
|
||||
fun Int.withAlpha(alpha: Int): Int {
|
||||
return Color.argb(0x4D, Color.red(this), Color.green(this), Color.blue(this))
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.moez.QKSMS.presentation.view
|
||||
|
||||
import android.content.Context
|
||||
import android.content.res.ColorStateList
|
||||
import android.support.v7.widget.SwitchCompat
|
||||
import android.util.AttributeSet
|
||||
import com.moez.QKSMS.common.di.AppComponentManager
|
||||
import com.moez.QKSMS.common.util.ThemeManager
|
||||
import com.moez.QKSMS.common.util.extensions.withAlpha
|
||||
import io.reactivex.disposables.CompositeDisposable
|
||||
import io.reactivex.rxkotlin.Observables
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import javax.inject.Inject
|
||||
|
||||
class QkSwitch @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : SwitchCompat(context, attrs) {
|
||||
|
||||
@Inject lateinit var themeManager: ThemeManager
|
||||
|
||||
private val disposables = CompositeDisposable()
|
||||
|
||||
init {
|
||||
AppComponentManager.appComponent.inject(this)
|
||||
}
|
||||
|
||||
override fun onAttachedToWindow() {
|
||||
super.onAttachedToWindow()
|
||||
|
||||
val states = arrayOf(
|
||||
intArrayOf(-android.R.attr.state_enabled),
|
||||
intArrayOf(android.R.attr.state_checked),
|
||||
intArrayOf())
|
||||
|
||||
disposables += Observables.combineLatest(themeManager.color, themeManager.switchThumbEnabled, themeManager.switchThumbDisabled,
|
||||
{ color, enabled, disabled -> intArrayOf(disabled, color, enabled) })
|
||||
.map { values -> ColorStateList(states, values) }
|
||||
.subscribe { tintList -> thumbTintList = tintList }
|
||||
|
||||
disposables += Observables.combineLatest(themeManager.color, themeManager.switchTrackEnabled, themeManager.switchTrackDisabled,
|
||||
{ color, enabled, disabled -> intArrayOf(disabled, color.withAlpha(0x4D), enabled) })
|
||||
.map { values -> ColorStateList(states, values) }
|
||||
.subscribe { tintList -> trackTintList = tintList }
|
||||
}
|
||||
|
||||
override fun onDetachedFromWindow() {
|
||||
super.onDetachedFromWindow()
|
||||
disposables.clear()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user