Add isInEditMode support for QkSwitch

This commit is contained in:
Moez Bhatti
2018-02-26 23:40:11 -05:00
parent b83f20882f
commit 0fc001c6f8
@@ -35,27 +35,31 @@ class QkSwitch @JvmOverloads constructor(context: Context, attrs: AttributeSet?
@Inject lateinit var colors: Colors @Inject lateinit var colors: Colors
init { init {
appComponent.inject(this) if (!isInEditMode) {
appComponent.inject(this)
}
} }
override fun onAttachedToWindow() { override fun onAttachedToWindow() {
super.onAttachedToWindow() super.onAttachedToWindow()
val states = arrayOf( if (!isInEditMode) {
intArrayOf(-android.R.attr.state_enabled), val states = arrayOf(
intArrayOf(android.R.attr.state_checked), intArrayOf(-android.R.attr.state_enabled),
intArrayOf()) intArrayOf(android.R.attr.state_checked),
intArrayOf())
Observables.combineLatest(colors.theme, colors.switchThumbEnabled, colors.switchThumbDisabled, Observables.combineLatest(colors.theme, colors.switchThumbEnabled, colors.switchThumbDisabled,
{ color, enabled, disabled -> intArrayOf(disabled, color, enabled) }) { color, enabled, disabled -> intArrayOf(disabled, color, enabled) })
.map { values -> ColorStateList(states, values) } .map { values -> ColorStateList(states, values) }
.autoDisposable(scope()) .autoDisposable(scope())
.subscribe { tintList -> thumbTintList = tintList } .subscribe { tintList -> thumbTintList = tintList }
Observables.combineLatest(colors.theme, colors.switchTrackEnabled, colors.switchTrackDisabled, Observables.combineLatest(colors.theme, colors.switchTrackEnabled, colors.switchTrackDisabled,
{ color, enabled, disabled -> intArrayOf(disabled, color.withAlpha(0x4D), enabled) }) { color, enabled, disabled -> intArrayOf(disabled, color.withAlpha(0x4D), enabled) })
.map { values -> ColorStateList(states, values) } .map { values -> ColorStateList(states, values) }
.autoDisposable(scope()) .autoDisposable(scope())
.subscribe { tintList -> trackTintList = tintList } .subscribe { tintList -> trackTintList = tintList }
}
} }
} }