Clean up QkTextView, set subscription colors using ColorStateList

This commit is contained in:
Moez Bhatti
2018-02-02 23:49:37 -05:00
parent 1e3bbd1b19
commit fcf5f2d6e8
3 changed files with 58 additions and 75 deletions
@@ -22,12 +22,13 @@ import android.content.Context
import android.support.v7.widget.AppCompatTextView
import android.util.AttributeSet
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.FontProvider
import common.util.extensions.getColorCompat
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import javax.inject.Inject
open class QkTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null)
@@ -45,23 +46,7 @@ open class QkTextView @JvmOverloads constructor(context: Context, attrs: Attribu
@Inject lateinit var colors: Colors
@Inject lateinit var fontProvider: FontProvider
private var textColorDisposable: Disposable? = null
private var textColorObservable: Observable<Int>? = null
set(value) {
if (field !== value) {
field = value
if (isAttachedToWindow) {
textColorDisposable?.let { disposable ->
if (!disposable.isDisposed) {
disposable.dispose()
}
}
subscribeColorChanges()
}
}
}
init {
if (!isInEditMode) {
@@ -82,44 +67,29 @@ open class QkTextView @JvmOverloads constructor(context: Context, attrs: Attribu
else -> R.color.textPrimary
}))
} else {
setColor(colorAttr)
textColorObservable = when (colorAttr) {
COLOR_PRIMARY -> colors.textPrimary
COLOR_SECONDARY -> colors.textSecondary
COLOR_TERTIARY -> colors.textTertiary
COLOR_PRIMARY_ON_THEME -> colors.textPrimaryOnTheme
COLOR_SECONDARY_ON_THEME -> colors.textSecondaryOnTheme
COLOR_TERTIARY_ON_THEME -> colors.textTertiaryOnTheme
else -> null
}
}
recycle()
}
}
fun setColor(colorAttr: Int) {
textColorObservable = when (colorAttr) {
COLOR_PRIMARY -> colors.textPrimary
COLOR_SECONDARY -> colors.textSecondary
COLOR_TERTIARY -> colors.textTertiary
COLOR_PRIMARY_ON_THEME -> colors.textPrimaryOnTheme
COLOR_SECONDARY_ON_THEME -> colors.textSecondaryOnTheme
COLOR_TERTIARY_ON_THEME -> colors.textTertiaryOnTheme
else -> null
}
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
private fun subscribeColorChanges() {
textColorDisposable = textColorObservable
textColorObservable
?.autoDisposable(scope())
?.subscribe { color ->
setTextColor(color)
setLinkTextColor(color)
}
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (textColorDisposable?.isDisposed != false) {
subscribeColorChanges()
}
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
textColorDisposable?.dispose()
}
}
@@ -18,6 +18,7 @@
*/
package presentation.feature.plus
import android.content.res.ColorStateList
import android.graphics.Typeface
import android.os.Bundle
import com.jakewharton.rxbinding2.view.clicks
@@ -30,9 +31,9 @@ import common.util.FontProvider
import common.util.extensions.setBackgroundTint
import common.util.extensions.setTint
import common.util.extensions.setVisible
import io.reactivex.rxkotlin.Observables
import kotlinx.android.synthetic.main.qksms_plus_activity.*
import presentation.common.base.QkActivity
import presentation.common.widget.QkTextView
import javax.inject.Inject
class PlusActivity : QkActivity<PlusViewModel>(), PlusView {
@@ -81,6 +82,35 @@ class PlusActivity : QkActivity<PlusViewModel>(), PlusView {
donor.setBackgroundTint(color)
philanthropist.setBackgroundTint(color)
}
val states = arrayOf(
intArrayOf(android.R.attr.state_selected),
intArrayOf(-android.R.attr.state_selected))
Observables
.combineLatest(colors.textPrimaryOnTheme, colors.textPrimary, { theme, textSecondary ->
ColorStateList(states, intArrayOf(theme, textSecondary))
})
.autoDisposable(scope())
.subscribe { colorStateList ->
supporterName.setTextColor(colorStateList)
supporterPrice.setTextColor(colorStateList)
donorName.setTextColor(colorStateList)
donorPrice.setTextColor(colorStateList)
philanthropistName.setTextColor(colorStateList)
philanthropistPrice.setTextColor(colorStateList)
}
Observables
.combineLatest(colors.textSecondaryOnTheme, colors.textSecondary, { theme, textSecondary ->
ColorStateList(states, intArrayOf(theme, textSecondary))
})
.autoDisposable(scope())
.subscribe { colorStateList ->
supporterPeriod.setTextColor(colorStateList)
donorPeriod.setTextColor(colorStateList)
philanthropistPeriod.setTextColor(colorStateList)
}
}
override fun render(state: PlusState) {
@@ -91,31 +121,14 @@ class PlusActivity : QkActivity<PlusViewModel>(), PlusView {
val supportedSelected = state.currentPlan == BillingManager.UpgradeStatus.SUPPORTER
supporter.isSelected = supportedSelected
supporterPrice.text = state.supporterPrice
supporterName.setColor(getPrimaryTextColorAttr(supportedSelected))
supporterPrice.setColor(getPrimaryTextColorAttr(supportedSelected))
supporterPeriod.setColor(getSecondaryTextColorAttr(supportedSelected))
val donorSelected = state.currentPlan == BillingManager.UpgradeStatus.DONOR
donor.isSelected = donorSelected
donorPrice.text = state.donorPrice
donorName.setColor(getPrimaryTextColorAttr(donorSelected))
donorPrice.setColor(getPrimaryTextColorAttr(donorSelected))
donorPeriod.setColor(getSecondaryTextColorAttr(donorSelected))
val philanthropistSelected = state.currentPlan == BillingManager.UpgradeStatus.PHILANTHROPIST
philanthropist.isSelected = philanthropistSelected
philanthropistPrice.text = state.philanthropistPrice
philanthropistName.setColor(getPrimaryTextColorAttr(philanthropistSelected))
philanthropistPrice.setColor(getPrimaryTextColorAttr(philanthropistSelected))
philanthropistPeriod.setColor(getSecondaryTextColorAttr(philanthropistSelected))
}
private fun getPrimaryTextColorAttr(selected: Boolean): Int {
return if (selected) QkTextView.COLOR_PRIMARY_ON_THEME else QkTextView.COLOR_PRIMARY
}
private fun getSecondaryTextColorAttr(selected: Boolean): Int {
return if (selected) QkTextView.COLOR_SECONDARY_ON_THEME else QkTextView.COLOR_SECONDARY
}
override fun initiatePurchaseFlow(billingManager: BillingManager, sku: String) {
@@ -152,7 +152,7 @@
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:textColor="primary" />
tools:textColor="@color/textPrimary" />
<presentation.common.widget.QkTextView
android:id="@+id/supporterPrice"
@@ -164,8 +164,8 @@
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/supporterName"
app:textColor="primary"
tools:text="$2.99" />
tools:text="$2.99"
tools:textColor="@color/textPrimary" />
<presentation.common.widget.QkTextView
android:id="@+id/supporterPeriod"
@@ -176,7 +176,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/supporterPrice"
app:layout_constraintTop_toBottomOf="@id/supporterName"
app:textColor="secondary" />
tools:textColor="@color/textSecondary" />
</android.support.constraint.ConstraintLayout>
@@ -200,7 +200,7 @@
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:textColor="primary" />
tools:textColor="@color/textPrimary" />
<presentation.common.widget.QkTextView
android:id="@+id/donorPrice"
@@ -212,8 +212,8 @@
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/donorName"
app:textColor="primary"
tools:text="$2.99" />
tools:text="$2.99"
tools:textColor="@color/textPrimary" />
<presentation.common.widget.QkTextView
android:id="@+id/donorPeriod"
@@ -224,7 +224,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/donorPrice"
app:layout_constraintTop_toBottomOf="@id/donorName"
app:textColor="secondary" />
tools:textColor="@color/textSecondary" />
</android.support.constraint.ConstraintLayout>
@@ -248,7 +248,7 @@
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:textColor="primary" />
tools:textColor="@color/textPrimary" />
<presentation.common.widget.QkTextView
android:id="@+id/philanthropistPrice"
@@ -260,8 +260,8 @@
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/philanthropistName"
app:textColor="primary"
tools:text="$2.99" />
tools:text="$2.99"
tools:textColor="@color/textPrimary" />
<presentation.common.widget.QkTextView
android:id="@+id/philanthropistPeriod"
@@ -272,7 +272,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/philanthropistPrice"
app:layout_constraintTop_toBottomOf="@id/philanthropistName"
app:textColor="secondary" />
tools:textColor="@color/textSecondary" />
</android.support.constraint.ConstraintLayout>