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.support.v7.widget.AppCompatTextView
import android.util.AttributeSet import android.util.AttributeSet
import com.moez.QKSMS.R import com.moez.QKSMS.R
import com.uber.autodispose.android.scope
import com.uber.autodispose.kotlin.autoDisposable
import common.di.appComponent import common.di.appComponent
import common.util.Colors import common.util.Colors
import common.util.FontProvider import common.util.FontProvider
import common.util.extensions.getColorCompat import common.util.extensions.getColorCompat
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import javax.inject.Inject import javax.inject.Inject
open class QkTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) 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 colors: Colors
@Inject lateinit var fontProvider: FontProvider @Inject lateinit var fontProvider: FontProvider
private var textColorDisposable: Disposable? = null
private var textColorObservable: Observable<Int>? = 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 { init {
if (!isInEditMode) { if (!isInEditMode) {
@@ -82,44 +67,29 @@ open class QkTextView @JvmOverloads constructor(context: Context, attrs: Attribu
else -> R.color.textPrimary else -> R.color.textPrimary
})) }))
} else { } 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() recycle()
} }
} }
fun setColor(colorAttr: Int) { override fun onAttachedToWindow() {
textColorObservable = when (colorAttr) { super.onAttachedToWindow()
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
}
}
private fun subscribeColorChanges() { textColorObservable
textColorDisposable = textColorObservable ?.autoDisposable(scope())
?.subscribe { color -> ?.subscribe { color ->
setTextColor(color) setTextColor(color)
setLinkTextColor(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 package presentation.feature.plus
import android.content.res.ColorStateList
import android.graphics.Typeface import android.graphics.Typeface
import android.os.Bundle import android.os.Bundle
import com.jakewharton.rxbinding2.view.clicks import com.jakewharton.rxbinding2.view.clicks
@@ -30,9 +31,9 @@ import common.util.FontProvider
import common.util.extensions.setBackgroundTint import common.util.extensions.setBackgroundTint
import common.util.extensions.setTint import common.util.extensions.setTint
import common.util.extensions.setVisible import common.util.extensions.setVisible
import io.reactivex.rxkotlin.Observables
import kotlinx.android.synthetic.main.qksms_plus_activity.* import kotlinx.android.synthetic.main.qksms_plus_activity.*
import presentation.common.base.QkActivity import presentation.common.base.QkActivity
import presentation.common.widget.QkTextView
import javax.inject.Inject import javax.inject.Inject
class PlusActivity : QkActivity<PlusViewModel>(), PlusView { class PlusActivity : QkActivity<PlusViewModel>(), PlusView {
@@ -81,6 +82,35 @@ class PlusActivity : QkActivity<PlusViewModel>(), PlusView {
donor.setBackgroundTint(color) donor.setBackgroundTint(color)
philanthropist.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) { override fun render(state: PlusState) {
@@ -91,31 +121,14 @@ class PlusActivity : QkActivity<PlusViewModel>(), PlusView {
val supportedSelected = state.currentPlan == BillingManager.UpgradeStatus.SUPPORTER val supportedSelected = state.currentPlan == BillingManager.UpgradeStatus.SUPPORTER
supporter.isSelected = supportedSelected supporter.isSelected = supportedSelected
supporterPrice.text = state.supporterPrice supporterPrice.text = state.supporterPrice
supporterName.setColor(getPrimaryTextColorAttr(supportedSelected))
supporterPrice.setColor(getPrimaryTextColorAttr(supportedSelected))
supporterPeriod.setColor(getSecondaryTextColorAttr(supportedSelected))
val donorSelected = state.currentPlan == BillingManager.UpgradeStatus.DONOR val donorSelected = state.currentPlan == BillingManager.UpgradeStatus.DONOR
donor.isSelected = donorSelected donor.isSelected = donorSelected
donorPrice.text = state.donorPrice donorPrice.text = state.donorPrice
donorName.setColor(getPrimaryTextColorAttr(donorSelected))
donorPrice.setColor(getPrimaryTextColorAttr(donorSelected))
donorPeriod.setColor(getSecondaryTextColorAttr(donorSelected))
val philanthropistSelected = state.currentPlan == BillingManager.UpgradeStatus.PHILANTHROPIST val philanthropistSelected = state.currentPlan == BillingManager.UpgradeStatus.PHILANTHROPIST
philanthropist.isSelected = philanthropistSelected philanthropist.isSelected = philanthropistSelected
philanthropistPrice.text = state.philanthropistPrice 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) { override fun initiatePurchaseFlow(billingManager: BillingManager, sku: String) {
@@ -152,7 +152,7 @@
android:textStyle="bold" android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:textColor="primary" /> tools:textColor="@color/textPrimary" />
<presentation.common.widget.QkTextView <presentation.common.widget.QkTextView
android:id="@+id/supporterPrice" android:id="@+id/supporterPrice"
@@ -164,8 +164,8 @@
app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/supporterName" 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 <presentation.common.widget.QkTextView
android:id="@+id/supporterPeriod" android:id="@+id/supporterPeriod"
@@ -176,7 +176,7 @@
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/supporterPrice" app:layout_constraintStart_toEndOf="@id/supporterPrice"
app:layout_constraintTop_toBottomOf="@id/supporterName" app:layout_constraintTop_toBottomOf="@id/supporterName"
app:textColor="secondary" /> tools:textColor="@color/textSecondary" />
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>
@@ -200,7 +200,7 @@
android:textStyle="bold" android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:textColor="primary" /> tools:textColor="@color/textPrimary" />
<presentation.common.widget.QkTextView <presentation.common.widget.QkTextView
android:id="@+id/donorPrice" android:id="@+id/donorPrice"
@@ -212,8 +212,8 @@
app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/donorName" 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 <presentation.common.widget.QkTextView
android:id="@+id/donorPeriod" android:id="@+id/donorPeriod"
@@ -224,7 +224,7 @@
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/donorPrice" app:layout_constraintStart_toEndOf="@id/donorPrice"
app:layout_constraintTop_toBottomOf="@id/donorName" app:layout_constraintTop_toBottomOf="@id/donorName"
app:textColor="secondary" /> tools:textColor="@color/textSecondary" />
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>
@@ -248,7 +248,7 @@
android:textStyle="bold" android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:textColor="primary" /> tools:textColor="@color/textPrimary" />
<presentation.common.widget.QkTextView <presentation.common.widget.QkTextView
android:id="@+id/philanthropistPrice" android:id="@+id/philanthropistPrice"
@@ -260,8 +260,8 @@
app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/philanthropistName" 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 <presentation.common.widget.QkTextView
android:id="@+id/philanthropistPeriod" android:id="@+id/philanthropistPeriod"
@@ -272,7 +272,7 @@
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/philanthropistPrice" app:layout_constraintStart_toEndOf="@id/philanthropistPrice"
app:layout_constraintTop_toBottomOf="@id/philanthropistName" app:layout_constraintTop_toBottomOf="@id/philanthropistName"
app:textColor="secondary" /> tools:textColor="@color/textSecondary" />
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>