Remove Observables from Views

commit 6bb74ca5201cba2fc5de2db349bb7627c028b617
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Sun May 27 15:15:27 2018 -0400

    Fix crash when tapping on selected contact

commit f5a45e2a600a8d373160dcb0273bab232e6cc0c5
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Sun May 27 15:10:44 2018 -0400

    Add missing attributes for dialog themes

commit 2188adbe5948b9b71f816dad5fb115829ba57e30
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Sun May 27 14:59:39 2018 -0400

    Refresh settings activity when theme changed

commit a9541019c566a3a6e41e64ea8136f7fd50a6d364
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Sun May 27 14:56:33 2018 -0400

    Don't use themed attributes in widget

commit 4823a3034848efac624eb82856f692c7f9e775a5
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Sun May 27 14:29:00 2018 -0400

    Refresh adapter in conversation info when theme changed

commit 7ea5837b99c2b6eac547dad383515ee7b79ff6e4
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Fri May 25 18:27:08 2018 -0400

    Bundle theme colors into data class

commit 6a832fdde98622c5a252aca0cac3150a6eb0f554
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Fri May 25 17:16:21 2018 -0400

    Update conversation UI when theme changed

commit ef6948e80ed8734a7fefbec1b00f1b32e8209072
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Fri May 25 16:15:39 2018 -0400

    Fix avatar not respecting theme

commit 4ea21b6a9963f91d3b9cdaf0ba4749a6f24e8a74
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Fri May 25 15:52:40 2018 -0400

    Fix checkmark color in theme picker

commit 270a9d3037439f165a110c7d7dcaf7dd84dad29f
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Fri May 25 13:39:12 2018 -0400

    Update views in MainActivity when theme changed

commit cdc1187ccaee800e4305da5fab5d22220d04440f
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Thu May 24 23:36:17 2018 -0400

    Remove unnecessary programmatic creation of drawer item background

commit bc6fbfa75c66bb81db9591617f8c1de4c0f2746f
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Thu May 24 23:28:44 2018 -0400

    Temporarily disable RxJava2Debug

commit eae422be0bab8cedb3cb5ddf5f4ae61368ab5d1e
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Thu May 24 21:16:46 2018 -0400

    Remove observable usage inside of custom views

commit 2e65d55b9b283e74fb395d4828eb310395b4902b
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Thu May 24 02:27:26 2018 -0400

    Migrate ripple and text size to no longer use observables
This commit is contained in:
Moez Bhatti
2018-05-27 15:49:02 -04:00
parent e94296a5a4
commit 472a36ec3b
82 changed files with 752 additions and 1253 deletions
@@ -29,10 +29,9 @@ import com.moez.QKSMS.R
import common.base.QkAdapter
import common.base.QkViewHolder
import common.util.Colors
import common.util.extensions.resolveThemeColor
import common.util.extensions.setVisible
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.Observables
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.menu_list_item.view.*
@@ -65,17 +64,15 @@ class MenuItemAdapter @Inject constructor(private val context: Context, private
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.menu_list_item, parent, false)
val layoutInflater = LayoutInflater.from(parent.context)
val view = layoutInflater.inflate(R.layout.menu_list_item, parent, false)
val states = arrayOf(
intArrayOf(android.R.attr.state_selected),
intArrayOf(-android.R.attr.state_selected))
disposables += Observables
.combineLatest(colors.theme, colors.textTertiary, { theme, textSecondary ->
ColorStateList(states, intArrayOf(theme, textSecondary))
})
.subscribe { view.check.imageTintList = it }
val text = parent.context.resolveThemeColor(android.R.attr.textColorTertiary)
view.check.imageTintList = ColorStateList(states, intArrayOf(colors.theme().theme, text))
return QkViewHolder(view)
}
@@ -23,7 +23,6 @@ import android.app.Application
import android.support.text.emoji.EmojiCompat
import android.support.text.emoji.FontRequestEmojiCompatConfig
import android.support.v4.provider.FontRequest
import com.akaita.java.rxjava2debug.RxJava2Debug
import com.bugsnag.android.Bugsnag
import com.bugsnag.android.Configuration
import com.moez.QKSMS.BuildConfig
@@ -63,7 +62,7 @@ class QKApplication : Application(), HasActivityInjector {
projectPackages = packages
})
RxJava2Debug.enableRxJava2AssemblyTracking(packages)
//RxJava2Debug.enableRxJava2AssemblyTracking(packages)
Realm.init(this)
Realm.setDefaultConfiguration(RealmConfiguration.Builder()
@@ -1,9 +1,9 @@
package common
import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.support.annotation.StringRes
import android.support.v7.app.AlertDialog
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import common.util.extensions.dpToPx
@@ -23,7 +23,7 @@ class QkDialog @Inject constructor(private val context: Context, val adapter: Me
}
fun show(activity: Activity) {
val recyclerView = RecyclerView(context)
val recyclerView = RecyclerView(activity)
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = adapter
recyclerView.setPadding(top = 8.dpToPx(context), bottom = 8.dpToPx(context))
@@ -22,16 +22,20 @@ import android.annotation.SuppressLint
import android.app.ActivityManager
import android.arch.lifecycle.Lifecycle
import android.graphics.BitmapFactory
import android.os.Build
import android.os.Bundle
import android.view.View
import com.moez.QKSMS.R
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable
import common.util.Colors
import common.util.extensions.setBackgroundTint
import common.util.extensions.resolveThemeColor
import io.reactivex.Observable
import io.reactivex.rxkotlin.Observables
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.toolbar.*
import util.Preferences
import javax.inject.Inject
/**
@@ -43,6 +47,7 @@ import javax.inject.Inject
abstract class QkThemedActivity : QkActivity() {
@Inject lateinit var colors: Colors
@Inject lateinit var prefs: Preferences
/**
* In case the activity should be themed for a specific conversation, the selected conversation
@@ -55,40 +60,58 @@ abstract class QkThemedActivity : QkActivity() {
*/
protected val theme = threadId
.distinctUntilChanged()
.switchMap { threadId -> colors.themeForConversation(threadId) }
.switchMap { threadId -> colors.themeObservable(threadId) }
@SuppressLint("InlinedApi")
override fun onCreate(savedInstanceState: Bundle?) {
val night = prefs.night.get()
val black = prefs.black.get()
setTheme(getActivityThemeRes(night, black))
super.onCreate(savedInstanceState)
getAppThemeResourcesObservable()
// When certain preferences change, we need to recreate the activity
Observable.merge(
listOf(prefs.night, prefs.black, prefs.textSize, prefs.systemFont)
.map { it.asObservable().skip(1) })
.autoDisposable(scope())
.subscribe { res -> setTheme(res) }
.subscribe { recreate() }
colors.systemBarIcons
.autoDisposable(scope())
.subscribe { systemUiVisibility -> window.decorView.systemUiVisibility = systemUiVisibility }
// Set the color for the status bar icons
// If night mode, or no dark icons supported, use light icons
// If night mode and only dark status icons supported, use dark status icons
// If night mode and all dark icons supported, use all dark icons
window.decorView.systemUiVisibility = when {
night || Build.VERSION.SDK_INT < Build.VERSION_CODES.M -> 0
Build.VERSION.SDK_INT < Build.VERSION_CODES.O -> View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
else -> View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
}
colors.statusBar
.autoDisposable(scope())
.subscribe { color -> window.statusBarColor = color }
// Set the color for the overflow and navigation icon
val textTertiary = resolveThemeColor(android.R.attr.textColorTertiary)
toolbar?.overflowIcon = toolbar?.overflowIcon?.apply { setTint(textTertiary) }
toolbar?.navigationIcon = toolbar?.navigationIcon?.apply { setTint(textTertiary) }
colors.navigationBar
.autoDisposable(scope())
.subscribe { color -> window.navigationBarColor = color }
// Set the color for the recent apps title
val toolbarColor = resolveThemeColor(R.attr.colorPrimary)
val icon = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
val taskDesc = ActivityManager.TaskDescription(getString(R.string.app_name), icon, toolbarColor)
setTaskDescription(taskDesc)
}
override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState)
// Update the colours of the menu items
Observables.combineLatest(menu, theme, colors.textSecondary, { menu, theme, text ->
Observables.combineLatest(menu, theme, { menu, theme ->
val text = resolveThemeColor(android.R.attr.textColorSecondary)
(0 until menu.size())
.map { position -> menu.getItem(position) }
.forEach { menuItem ->
menuItem?.icon?.run {
setTint(when (menuItem.itemId) {
in getColoredMenuItems() -> theme
in getColoredMenuItems() -> theme.theme
else -> text
})
@@ -96,27 +119,6 @@ abstract class QkThemedActivity : QkActivity() {
}
}
}).autoDisposable(scope(Lifecycle.Event.ON_DESTROY)).subscribe()
colors.textTertiary
.doOnNext { color -> toolbar?.overflowIcon = toolbar?.overflowIcon?.apply { setTint(color) } }
.doOnNext { color -> toolbar?.navigationIcon = toolbar?.navigationIcon?.apply { setTint(color) } }
.autoDisposable(scope(Lifecycle.Event.ON_DESTROY))
.subscribe()
colors.popupThemeResource
.autoDisposable(scope(Lifecycle.Event.ON_DESTROY))
.subscribe { res -> toolbar?.popupTheme = res }
colors.toolbarColor
.doOnNext { color -> toolbar?.setBackgroundTint(color) }
.doOnNext { color ->
// Set the color for the recent apps title
val icon = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
val taskDesc = ActivityManager.TaskDescription(getString(R.string.app_name), icon, color)
setTaskDescription(taskDesc)
}
.autoDisposable(scope(Lifecycle.Event.ON_DESTROY))
.subscribe { color -> toolbar?.setBackgroundTint(color) }
}
open fun getColoredMenuItems(): List<Int> {
@@ -126,6 +128,10 @@ abstract class QkThemedActivity : QkActivity() {
/**
* This can be overridden in case an activity does not want to use the default themes
*/
open fun getAppThemeResourcesObservable() = colors.appThemeResources
open fun getActivityThemeRes(night: Boolean, black: Boolean) = when {
night && black -> R.style.AppThemeBlack
night && !black -> R.style.AppThemeDark
else -> R.style.AppThemeLight
}
}
+41 -223
View File
@@ -18,15 +18,11 @@
*/
package common.util
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.support.annotation.ColorRes
import android.view.View
import com.moez.QKSMS.R
import common.util.extensions.getColorCompat
import io.reactivex.Observable
import io.reactivex.rxkotlin.Observables
import util.Preferences
import javax.inject.Inject
import javax.inject.Singleton
@@ -34,6 +30,13 @@ import javax.inject.Singleton
@Singleton
class Colors @Inject constructor(private val context: Context, private val prefs: Preferences) {
data class Theme(val theme: Int, private val colors: Colors) {
val highlight by lazy { colors.highlightColorForTheme(theme) }
val textPrimary by lazy { colors.textPrimaryOnThemeForColor(theme) }
val textSecondary by lazy { colors.textSecondaryOnThemeForColor(theme) }
val textTertiary by lazy { colors.textTertiaryOnThemeForColor(theme) }
}
val materialColors = listOf(
listOf(0xffffebee, 0xffffcdd2, 0xffef9a9a, 0xffe57373, 0xffef5350, 0xfff44336, 0xffe53935, 0xffd32f2f, 0xffc62828, 0xffb71c1c),
listOf(0xffFCE4EC, 0xffF8BBD0, 0xffF48FB1, 0xffF06292, 0xffEC407A, 0xffE91E63, 0xffD81B60, 0xffC2185B, 0xffAD1457, 0xff880E4F),
@@ -59,228 +62,43 @@ class Colors @Inject constructor(private val context: Context, private val prefs
private val minimumContrastRatio = 2
// Cache these values so they don't need to be recalculated
private val primaryTextLuminance = measureLuminance(getColor(R.color.textPrimaryDark))
private val secondaryTextLuminance = measureLuminance(getColor(R.color.textSecondaryDark))
private val tertiaryTextLuminance = measureLuminance(getColor(R.color.textTertiaryDark))
private val primaryTextLuminance = measureLuminance(context.getColorCompat(R.color.textPrimaryDark))
private val secondaryTextLuminance = measureLuminance(context.getColorCompat(R.color.textSecondaryDark))
private val tertiaryTextLuminance = measureLuminance(context.getColorCompat(R.color.textTertiaryDark))
val theme = themeForConversation()
fun theme(threadId: Long = 0): Theme = Theme(prefs.theme(threadId).get(), this)
val appThemeResources = Observables.combineLatest(prefs.night.asObservable(), prefs.black.asObservable(),
{ night, black ->
when {
night && black -> R.style.AppThemeBlack
night && !black -> R.style.AppThemeDark
else -> R.style.AppThemeLight
}
})
val appDialogThemeResources = Observables.combineLatest(prefs.night.asObservable(), prefs.black.asObservable(),
{ night, black ->
when {
night && black -> R.style.AppThemeBlackDialog
night && !black -> R.style.AppThemeDarkDialog
else -> R.style.AppThemeLightDialog
}
})
val popupThemeResource = Observables.combineLatest(prefs.night.asObservable(), prefs.black.asObservable(),
{ night, black ->
when {
night && black -> R.style.PopupThemeBlack
night && !black -> R.style.PopupThemeDark
else -> R.style.PopupThemeLight
}
})
/**
* Returns the flags to be used for the system bars. This is used for tinting the icons
* according to the colour of the system bars (light or dark)
*
* If night mode, or no dark icons supported, use light icons
*
* If night mode and only dark status icons supported, use dark status icons
*
* If night mode and all dark icons supported, use all dark icons
*/
@SuppressLint("InlinedApi")
val systemBarIcons: Observable<Int> = prefs.night.asObservable()
.map { night ->
when {
night || Build.VERSION.SDK_INT < Build.VERSION_CODES.M -> 0
Build.VERSION.SDK_INT < Build.VERSION_CODES.O -> View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
else -> View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
}
}
.distinctUntilChanged()
val statusBar: Observable<Int> = Observables.combineLatest(prefs.night.asObservable(), prefs.black.asObservable(),
{ night, black ->
when {
night && black -> R.color.black
night && !black -> R.color.statusBarDark
Build.VERSION.SDK_INT < Build.VERSION_CODES.M -> R.color.black
else -> R.color.statusBarLight
}
})
.map { res -> getColor(res) }
.distinctUntilChanged()
val navigationBar: Observable<Int> = Observables.combineLatest(prefs.night.asObservable(), prefs.black.asObservable(),
{ night, black ->
when {
night && black -> R.color.black
night && !black -> R.color.backgroundDark
Build.VERSION.SDK_INT < Build.VERSION_CODES.O -> R.color.black
else -> R.color.white
}
})
.map { res -> getColor(res) }
.distinctUntilChanged()
val toolbarColor: Observable<Int> = Observables.combineLatest(prefs.night.asObservable(), prefs.black.asObservable(),
{ night, black ->
when {
night && black -> R.color.black
night && !black -> R.color.toolbarDark
else -> R.color.toolbarLight
}
})
.map { res -> getColor(res) }
.distinctUntilChanged()
val background: Observable<Int> = Observables.combineLatest(prefs.night.asObservable(), prefs.black.asObservable(),
{ night, black ->
when {
night && black -> R.color.black
night && !black -> R.color.backgroundDark
else -> R.color.white
}
})
.map { res -> getColor(res) }
.distinctUntilChanged()
val ripple: Observable<Int> = prefs.night.asObservable()
.map { night ->
when (night) {
true -> R.drawable.ripple_dark
false -> R.drawable.ripple
}
}
.distinctUntilChanged()
val field: Observable<Int> = prefs.night.asObservable()
.map { night -> if (night) R.color.fieldDark else R.color.fieldLight }
.map { res -> getColor(res) }
.distinctUntilChanged()
val composeBackground: Observable<Int> = Observables.combineLatest(prefs.night.asObservable(), prefs.black.asObservable(),
{ night, black ->
when {
night && black -> R.color.black
night && !black -> R.color.backgroundDark
else -> R.color.backgroundLight
}
})
.map { res -> getColor(res) }
.distinctUntilChanged()
val separator: Observable<Int> = prefs.night.asObservable()
.map { night -> if (night) R.color.separatorDark else R.color.separatorLight }
.map { res -> getColor(res) }
.distinctUntilChanged()
val textPrimary: Observable<Int> = prefs.night.asObservable()
.map { night -> if (night) R.color.textPrimaryDark else R.color.textPrimary }
.map { res -> getColor(res) }
.distinctUntilChanged()
val textSecondary: Observable<Int> = prefs.night.asObservable()
.map { night -> if (night) R.color.textSecondaryDark else R.color.textSecondary }
.map { res -> getColor(res) }
.distinctUntilChanged()
val textTertiary: Observable<Int> = prefs.night.asObservable()
.map { night -> if (night) R.color.textTertiaryDark else R.color.textTertiary }
.map { res -> getColor(res) }
.distinctUntilChanged()
val highlightColor: Observable<Int> = theme
.map { theme -> FloatArray(3).apply { Color.colorToHSV(theme, this) } }
.map { hsv -> hsv.apply { set(2, 0.75f) } } // 75% value
.map { hsv -> Color.HSVToColor(85, hsv) } // 33% alpha
val textPrimaryOnTheme = textPrimaryOnThemeForConversation()
val textSecondaryOnTheme = textSecondaryOnThemeForConversation()
val textTertiaryOnTheme = textTertiaryOnThemeForConversation()
val bubble: Observable<Int> = prefs.night.asObservable()
.map { night -> if (night) R.color.bubbleDark else R.color.bubbleLight }
.map { res -> getColor(res) }
.distinctUntilChanged()
val switchThumbEnabled: Observable<Int> = prefs.night.asObservable()
.map { night -> if (night) R.color.switchThumbEnabledDark else R.color.switchThumbEnabledLight }
.map { res -> getColor(res) }
.distinctUntilChanged()
val switchThumbDisabled: Observable<Int> = prefs.night.asObservable()
.map { night -> if (night) R.color.switchThumbDisabledDark else R.color.switchThumbDisabledLight }
.map { res -> getColor(res) }
.distinctUntilChanged()
val switchTrackEnabled: Observable<Int> = prefs.night.asObservable()
.map { night -> if (night) R.color.switchTrackEnabledDark else R.color.switchTrackEnabledLight }
.map { res -> getColor(res) }
.distinctUntilChanged()
val switchTrackDisabled: Observable<Int> = prefs.night.asObservable()
.map { night -> if (night) R.color.switchTrackDisabledDark else R.color.switchTrackDisabledLight }
.map { res -> getColor(res) }
.distinctUntilChanged()
fun themeForConversation(threadId: Long = 0): Observable<Int> = prefs.theme(threadId)
.asObservable()
.distinctUntilChanged()
fun textPrimaryOnThemeForConversation(threadId: Long = 0): Observable<Int> = themeForConversation(threadId)
.switchMap { color -> textPrimaryOnThemeForColor(color) }
fun textPrimaryOnThemeForColor(color: Int): Observable<Int> = Observable.just(color)
.map { theme -> measureLuminance(theme) }
.map { themeLuminance -> primaryTextLuminance / themeLuminance }
.map { contrastRatio -> contrastRatio < minimumContrastRatio }
.map { contrastRatio -> if (contrastRatio) R.color.textPrimary else R.color.textPrimaryDark }
.map { res -> getColor(res) }
.distinctUntilChanged()
fun textSecondaryOnThemeForConversation(threadId: Long = 0): Observable<Int> = themeForConversation(threadId)
.switchMap { color -> textSecondaryOnThemeForColor(color) }
fun textSecondaryOnThemeForColor(color: Int): Observable<Int> = Observable.just(color)
.map { theme -> measureLuminance(theme) }
.map { themeLuminance -> secondaryTextLuminance / themeLuminance }
.map { contrastRatio -> contrastRatio < minimumContrastRatio }
.map { contrastRatio -> if (contrastRatio) R.color.textSecondary else R.color.textSecondaryDark }
.map { res -> getColor(res) }
.distinctUntilChanged()
fun textTertiaryOnThemeForConversation(threadId: Long = 0): Observable<Int> = themeForConversation(threadId)
.switchMap { color -> textTertiaryOnThemeForColor(color) }
fun textTertiaryOnThemeForColor(color: Int): Observable<Int> = Observable.just(color)
.map { theme -> measureLuminance(theme) }
.map { themeLuminance -> tertiaryTextLuminance / themeLuminance }
.map { contrastRatio -> contrastRatio < minimumContrastRatio }
.map { contrastRatio -> if (contrastRatio) R.color.textTertiary else R.color.textTertiaryDark }
.map { res -> getColor(res) }
.distinctUntilChanged()
private fun getColor(@ColorRes res: Int): Int {
return context.resources.getColor(res)
fun themeObservable(threadId: Long = 0): Observable<Theme> {
return prefs.theme(threadId).asObservable()
.map { color -> Theme(color, this) }
}
fun highlightColorForTheme(theme: Int): Int = FloatArray(3)
.apply { Color.colorToHSV(theme, this) }
.let { hsv -> hsv.apply { set(2, 0.75f) } } // 75% value
.let { hsv -> Color.HSVToColor(85, hsv) } // 33% alpha
fun textPrimaryOnThemeForColor(color: Int): Int = color
.let { theme -> measureLuminance(theme) }
.let { themeLuminance -> primaryTextLuminance / themeLuminance }
.let { contrastRatio -> contrastRatio < minimumContrastRatio }
.let { contrastRatio -> if (contrastRatio) R.color.textPrimary else R.color.textPrimaryDark }
.let { res -> context.getColorCompat(res) }
fun textSecondaryOnThemeForColor(color: Int): Int = color
.let { theme -> measureLuminance(theme) }
.let { themeLuminance -> secondaryTextLuminance / themeLuminance }
.let { contrastRatio -> contrastRatio < minimumContrastRatio }
.let { contrastRatio -> if (contrastRatio) R.color.textSecondary else R.color.textSecondaryDark }
.let { res -> context.getColorCompat(res) }
fun textTertiaryOnThemeForColor(color: Int): Int = color
.let { theme -> measureLuminance(theme) }
.let { themeLuminance -> tertiaryTextLuminance / themeLuminance }
.let { contrastRatio -> contrastRatio < minimumContrastRatio }
.let { contrastRatio -> if (contrastRatio) R.color.textTertiary else R.color.textTertiaryDark }
.let { res -> context.getColorCompat(res) }
/**
* Measures the luminance value of a color to be able to measure the contrast ratio between two materialColors
* Based on https://stackoverflow.com/a/9733420
@@ -22,28 +22,15 @@ import android.content.Context
import android.graphics.Typeface
import android.support.v4.content.res.ResourcesCompat
import com.moez.QKSMS.R
import io.reactivex.Observable
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import timber.log.Timber
import util.Preferences
import util.extensions.Optional
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class FontProvider @Inject constructor(context: Context, prefs: Preferences) {
class FontProvider @Inject constructor(context: Context) {
val typeface: Observable<Optional<Typeface>> = prefs.systemFont.asObservable()
.distinctUntilChanged()
.switchMap { systemFont ->
when (systemFont) {
true -> Observable.just(Optional(null))
false -> lato
}
}
private val lato: Observable<Optional<Typeface>> = BehaviorSubject.create()
private var lato: Typeface? = null
private val pendingCallbacks = ArrayList<(Typeface) -> Unit>()
init {
ResourcesCompat.getFont(context, R.font.lato, object : ResourcesCompat.FontCallback() {
@@ -52,10 +39,16 @@ class FontProvider @Inject constructor(context: Context, prefs: Preferences) {
}
override fun onFontRetrieved(typeface: Typeface) {
val subject = lato as Subject<Optional<Typeface>>
subject.onNext(Optional(typeface))
lato = typeface
pendingCallbacks.forEach { lato?.run(it) }
pendingCallbacks.clear()
}
}, null)
}
fun getLato(callback: (Typeface) -> Unit) {
lato?.run(callback) ?: pendingCallbacks.add(callback)
}
}
@@ -110,7 +110,7 @@ class NotificationManagerImpl @Inject constructor(
val notification = NotificationCompat.Builder(context, getChannelIdForNotification(threadId))
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setColor(colors.themeForConversation(threadId).blockingFirst())
.setColor(colors.theme(threadId).theme)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setSmallIcon(R.drawable.ic_notification)
.setNumber(messages.size)
@@ -207,7 +207,7 @@ class NotificationManagerImpl @Inject constructor(
val notification = NotificationCompat.Builder(context, getChannelIdForNotification(threadId))
.setContentTitle(context.getString(R.string.notification_message_failed_title))
.setContentText(context.getString(R.string.notification_message_failed_text, conversation.getTitle()))
.setColor(colors.themeForConversation(threadId).blockingFirst())
.setColor(colors.theme(threadId).theme)
.setPriority(NotificationManagerCompat.IMPORTANCE_MAX)
.setSmallIcon(R.drawable.ic_notification_failed)
.setAutoCancel(true)
@@ -0,0 +1,160 @@
/*
* 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 common.util
import android.graphics.Typeface
import android.util.AttributeSet
import android.widget.TextView
import com.moez.QKSMS.R
import common.util.extensions.getColorCompat
import common.util.extensions.resolveThemeColor
import common.widget.QkEditText
import common.widget.QkTextView
import util.Preferences
import javax.inject.Inject
class TextViewStyler @Inject constructor(
private val prefs: Preferences,
private val colors: Colors,
private val fontProvider: FontProvider
) {
companion object {
const val COLOR_PRIMARY = 0
const val COLOR_SECONDARY = 1
const val COLOR_TERTIARY = 2
const val COLOR_PRIMARY_ON_THEME = 3
const val COLOR_SECONDARY_ON_THEME = 4
const val COLOR_TERTIARY_ON_THEME = 5
const val COLOR_THEME = 6
const val SIZE_PRIMARY = 0
const val SIZE_SECONDARY = 1
const val SIZE_TERTIARY = 2
const val SIZE_TOOLBAR = 3
}
fun applyAttributes(textView: TextView, attrs: AttributeSet?) {
textView.run {
when (this) {
is QkTextView -> context.obtainStyledAttributes(attrs, R.styleable.QkTextView)?.run {
val colorAttr = getInt(R.styleable.QkTextView_textColor, -1)
val textSizeAttr = getInt(R.styleable.QkTextView_textSize, -1)
applyAttributes(textView, colorAttr, textSizeAttr)
recycle()
}
is QkEditText -> context.obtainStyledAttributes(attrs, R.styleable.QkEditText)?.run {
val colorAttr = getInt(R.styleable.QkEditText_textColor, -1)
val textSizeAttr = getInt(R.styleable.QkEditText_textSize, -1)
applyAttributes(textView, colorAttr, textSizeAttr)
recycle()
}
}
if (!prefs.systemFont.get()) {
fontProvider.getLato { lato ->
setTypeface(lato, typeface?.style ?: Typeface.NORMAL)
}
}
}
}
private fun applyAttributes(textView: TextView, colorAttr: Int, textSizeAttr: Int) {
textView.run {
if (isInEditMode) {
setTextColor(context.getColorCompat(when (colorAttr) {
COLOR_PRIMARY -> R.color.textPrimary
COLOR_SECONDARY -> R.color.textSecondary
COLOR_TERTIARY -> R.color.textTertiary
COLOR_PRIMARY_ON_THEME -> R.color.textPrimaryDark
COLOR_SECONDARY_ON_THEME -> R.color.textSecondaryDark
COLOR_TERTIARY_ON_THEME -> R.color.textTertiaryDark
COLOR_THEME -> R.color.tools_theme
else -> R.color.textPrimary
}))
textSize = when (textSizeAttr) {
SIZE_PRIMARY -> 16f
SIZE_SECONDARY -> 14f
SIZE_TERTIARY -> 12f
SIZE_TOOLBAR -> 20f
else -> textSize
}
} else {
setTextColor(when (colorAttr) {
COLOR_PRIMARY -> context.resolveThemeColor(android.R.attr.textColorPrimary)
COLOR_SECONDARY -> context.resolveThemeColor(android.R.attr.textColorSecondary)
COLOR_TERTIARY -> context.resolveThemeColor(android.R.attr.textColorTertiary)
COLOR_PRIMARY_ON_THEME -> colors.theme().textPrimary
COLOR_SECONDARY_ON_THEME -> colors.theme().textSecondary
COLOR_TERTIARY_ON_THEME -> colors.theme().textTertiary
COLOR_THEME -> colors.theme().theme
else -> currentTextColor
})
setTextSize(textView, textSizeAttr)
}
}
}
/**
* @see SIZE_PRIMARY
* @see SIZE_SECONDARY
* @see SIZE_TERTIARY
* @see SIZE_TOOLBAR
*/
fun setTextSize(textView: TextView, textSizeAttr: Int) {
val textSizePref = prefs.textSize.get()
when (textSizeAttr) {
SIZE_PRIMARY -> textView.textSize = when (textSizePref) {
Preferences.TEXT_SIZE_SMALL -> 14f
Preferences.TEXT_SIZE_NORMAL -> 16f
Preferences.TEXT_SIZE_LARGE -> 18f
Preferences.TEXT_SIZE_LARGER -> 20f
else -> 16f
}
SIZE_SECONDARY -> textView.textSize = when (textSizePref) {
Preferences.TEXT_SIZE_SMALL -> 12f
Preferences.TEXT_SIZE_NORMAL -> 14f
Preferences.TEXT_SIZE_LARGE -> 16f
Preferences.TEXT_SIZE_LARGER -> 18f
else -> 14f
}
SIZE_TERTIARY -> textView.textSize = when (textSizePref) {
Preferences.TEXT_SIZE_SMALL -> 10f
Preferences.TEXT_SIZE_NORMAL -> 12f
Preferences.TEXT_SIZE_LARGE -> 14f
Preferences.TEXT_SIZE_LARGER -> 16f
else -> 12f
}
SIZE_TOOLBAR -> textView.textSize = when (textSizePref) {
Preferences.TEXT_SIZE_SMALL -> 18f
Preferences.TEXT_SIZE_NORMAL -> 20f
Preferences.TEXT_SIZE_LARGE -> 22f
Preferences.TEXT_SIZE_LARGER -> 26f
else -> 20f
}
}
}
}
@@ -22,6 +22,7 @@ import android.app.Activity
import android.content.Context
import android.support.annotation.StringRes
import android.support.v4.content.ContextCompat
import android.util.TypedValue
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
@@ -29,6 +30,23 @@ fun Context.getColorCompat(colorRes: Int): Int {
return ContextCompat.getColor(this, colorRes)
}
/**
* Tries to resolve a resource id from the current theme, based on the [attributeId]
*/
fun Context.resolveThemeAttribute(attributeId: Int, default: Int = 0): Int {
val outValue = TypedValue()
val wasResolved = theme.resolveAttribute(attributeId, outValue, true)
return if (wasResolved) outValue.resourceId else default
}
fun Context.resolveThemeColor(attributeId: Int, default: Int = 0): Int {
val outValue = TypedValue()
val wasResolved = theme.resolveAttribute(attributeId, outValue, true)
return if (wasResolved) getColorCompat(outValue.resourceId) else default
}
fun Context.makeToast(@StringRes res: Int, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, res, duration).show()
}
@@ -22,6 +22,7 @@ import android.content.Context
import android.content.res.ColorStateList
import android.graphics.PorterDuff
import android.os.Build
import android.support.v7.widget.RecyclerView
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
@@ -94,3 +95,15 @@ fun View.forwardTouches(parent: View) {
}
}
fun RecyclerView.scrapViews() {
val adapter = adapter
val layoutManager = layoutManager
this.adapter = null
this.layoutManager = null
this.adapter = adapter
this.layoutManager = layoutManager
adapter?.notifyDataSetChanged()
}
@@ -25,10 +25,7 @@ import android.telephony.PhoneNumberUtils
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import com.jakewharton.rxbinding2.view.clicks
import com.moez.QKSMS.R
import com.uber.autodispose.android.scope
import com.uber.autodispose.kotlin.autoDisposable
import common.Navigator
import common.util.Colors
import common.util.GlideApp
@@ -49,6 +46,16 @@ class AvatarView @JvmOverloads constructor(context: Context, attrs: AttributeSet
* This value can be changes if we should use the theme from a particular conversation
*/
var threadId: Long = 0
set(value) {
if (field == value) return
field = value
colors.theme(value).run {
setBackgroundTint(theme)
initial.setTextColor(textPrimary)
icon.setTint(textPrimary)
}
}
private var lookupKey: String? = null
private var name: String? = null
@@ -63,6 +70,16 @@ class AvatarView @JvmOverloads constructor(context: Context, attrs: AttributeSet
setBackgroundResource(R.drawable.circle)
clipToOutline = true
setOnClickListener {
if (lookupKey.isNullOrEmpty()) {
address?.let { address -> navigator.addContact(address) }
} else {
val uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey)
ContactsContract.QuickContact.showQuickContact(context, this@AvatarView, uri,
ContactsContract.QuickContact.MODE_MEDIUM, null)
}
}
}
fun setContact(recipient: Recipient?) {
@@ -85,37 +102,16 @@ class AvatarView @JvmOverloads constructor(context: Context, attrs: AttributeSet
updateView()
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (!isInEditMode) {
colors.themeForConversation(threadId)
.autoDisposable(scope())
.subscribe { color -> setBackgroundTint(color) }
colors.textPrimaryOnThemeForConversation(threadId)
.apply { initial.textColorObservable = this }
.autoDisposable(scope())
.subscribe { color -> icon.setTint(color) }
clicks()
.autoDisposable(scope())
.subscribe {
if (lookupKey.isNullOrEmpty()) {
address?.let { address -> navigator.addContact(address) }
} else {
val uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey)
ContactsContract.QuickContact.showQuickContact(context, this@AvatarView, uri,
ContactsContract.QuickContact.MODE_MEDIUM, null)
}
}
}
}
override fun onFinishInflate() {
super.onFinishInflate()
if (!isInEditMode) {
colors.theme(threadId).run {
setBackgroundTint(theme)
initial.setTextColor(textPrimary)
icon.setTint(textPrimary)
}
updateView()
}
}
@@ -30,8 +30,8 @@ import com.uber.autodispose.android.scope
import com.uber.autodispose.kotlin.autoDisposable
import common.util.Colors
import common.util.extensions.forEach
import common.util.extensions.resolveThemeColor
import injection.appComponent
import io.reactivex.rxkotlin.Observables
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.tab_view.view.*
@@ -90,24 +90,19 @@ class PagerTitleView @JvmOverloads constructor(context: Context, attrs: Attribut
intArrayOf(android.R.attr.state_selected),
intArrayOf(-android.R.attr.state_selected))
val theme = threadId
threadId
.distinctUntilChanged()
.switchMap { threadId -> colors.themeForConversation(threadId) }
Observables
.combineLatest(theme, colors.textSecondary, { theme, textSecondary ->
ColorStateList(states, intArrayOf(theme, textSecondary))
})
.switchMap { threadId -> colors.themeObservable(threadId) }
.map { theme ->
val textSecondary = context.resolveThemeColor(android.R.attr.textColorSecondary)
ColorStateList(states, intArrayOf(theme.theme, textSecondary))
}
.autoDisposable(scope())
.subscribe { colorStateList ->
childCount.forEach { index ->
(getChildAt(index) as? TextView)?.setTextColor(colorStateList)
}
}
colors.toolbarColor
.autoDisposable(scope())
.subscribe { color -> setBackgroundColor(color) }
}
}
@@ -25,20 +25,15 @@ import android.view.Gravity
import android.view.View
import android.widget.TextView
import com.moez.QKSMS.R
import com.uber.autodispose.android.scope
import com.uber.autodispose.kotlin.autoDisposable
import common.util.Colors
import common.util.extensions.getColorCompat
import common.util.extensions.resolveThemeAttribute
import common.util.extensions.resolveThemeColor
import common.util.extensions.setTint
import common.util.extensions.setVisible
import injection.appComponent
import kotlinx.android.synthetic.main.preference_view.view.*
import javax.inject.Inject
class PreferenceView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : LinearLayoutCompat(context, attrs) {
@Inject lateinit var colors: Colors
var title: String? = null
set(value) {
field = value
@@ -72,10 +67,13 @@ class PreferenceView @JvmOverloads constructor(context: Context, attrs: Attribut
}
View.inflate(context, R.layout.preference_view, this)
setBackgroundResource(R.drawable.ripple)
setBackgroundResource(context.resolveThemeAttribute(R.attr.selectableItemBackground))
orientation = HORIZONTAL
gravity = Gravity.CENTER_VERTICAL
val textSecondary = context.resolveThemeColor(android.R.attr.textColorSecondary)
icon.setTint(textSecondary)
context.obtainStyledAttributes(attrs, R.styleable.PreferenceView)?.run {
title = getString(R.styleable.PreferenceView_title)
summary = getString(R.styleable.PreferenceView_summary)
@@ -95,16 +93,4 @@ class PreferenceView @JvmOverloads constructor(context: Context, attrs: Attribut
}
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (isInEditMode) {
icon.setTint(context.getColorCompat(R.color.textSecondary))
} else {
colors.textSecondary
.autoDisposable(scope())
.subscribe { icon.setTint(it) }
}
}
}
@@ -19,7 +19,6 @@
package common.widget
import android.content.Context
import android.graphics.Typeface
import android.os.Build
import android.support.v13.view.inputmethod.EditorInfoCompat
import android.support.v13.view.inputmethod.InputConnectionCompat
@@ -31,22 +30,10 @@ import android.view.inputmethod.InputConnection
import android.view.inputmethod.InputConnectionWrapper
import android.widget.EditText
import com.google.android.mms.ContentType
import com.moez.QKSMS.R
import com.uber.autodispose.android.scope
import com.uber.autodispose.kotlin.autoDisposable
import common.util.Colors
import common.util.FontProvider
import common.widget.QkTextView.Companion.SIZE_PRIMARY
import common.widget.QkTextView.Companion.SIZE_SECONDARY
import common.widget.QkTextView.Companion.SIZE_TERTIARY
import common.widget.QkTextView.Companion.SIZE_TOOLBAR
import common.util.TextViewStyler
import injection.appComponent
import io.reactivex.Observable
import io.reactivex.rxkotlin.Observables
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import util.Preferences
import util.tryOrNull
import javax.inject.Inject
@@ -59,132 +46,19 @@ import javax.inject.Inject
*/
class QkEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : EditText(context, attrs) {
companion object {
const val COLOR_PRIMARY = 0
const val COLOR_SECONDARY = 1
const val COLOR_TERTIARY = 2
const val COLOR_PRIMARY_ON_THEME = 3
const val COLOR_SECONDARY_ON_THEME = 4
const val COLOR_TERTIARY_ON_THEME = 5
const val SIZE_PRIMARY = 0
const val SIZE_SECONDARY = 1
const val SIZE_TERTIARY = 2
const val SIZE_TOOLBAR = 3
}
@Inject lateinit var colors: Colors
@Inject lateinit var fontProvider: FontProvider
@Inject lateinit var prefs: Preferences
@Inject lateinit var textViewStyler: TextViewStyler
val backspaces: Subject<Unit> = PublishSubject.create()
val inputContentSelected: Subject<InputContentInfoCompat> = PublishSubject.create()
var supportsInputContent: Boolean = false
private var textColorObservable: Observable<Int>? = null
private var textColorHintObservable: Observable<Int>? = null
private var textSizeAttrSubject: Subject<Int> = BehaviorSubject.create()
init {
if (!isInEditMode) {
appComponent.inject(this)
context.obtainStyledAttributes(attrs, R.styleable.QkEditText)?.run {
val colorAttr = getInt(R.styleable.QkEditText_textColor, -1)
val colorHintAttr = getInt(R.styleable.QkEditText_textColorHint, -1)
val textSizeAttr = getInt(R.styleable.QkEditText_textSize, -1)
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
}
textColorHintObservable = when (colorHintAttr) {
COLOR_PRIMARY -> colors.textPrimary
COLOR_SECONDARY -> colors.textSecondary
COLOR_TERTIARY -> colors.textTertiary
else -> null
}
setTextSize(textSizeAttr)
recycle()
}
textViewStyler.applyAttributes(this, attrs)
}
}
/**
* @see SIZE_PRIMARY
* @see SIZE_SECONDARY
* @see SIZE_TERTIARY
* @see SIZE_TOOLBAR
*/
fun setTextSize(size: Int) {
textSizeAttrSubject.onNext(size)
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (isInEditMode) return
fontProvider.typeface
.autoDisposable(scope())
.subscribe { setTypeface(it.value, typeface?.style ?: Typeface.NORMAL) }
textColorObservable
?.autoDisposable(scope())
?.subscribe { color -> setTextColor(color) }
textColorHintObservable
?.autoDisposable(scope())
?.subscribe { color -> setHintTextColor(color) }
Observables
.combineLatest(prefs.textSize.asObservable(), textSizeAttrSubject, { textSizePref, textSizeAttr ->
when (textSizeAttr) {
SIZE_PRIMARY -> textSize = when (textSizePref) {
Preferences.TEXT_SIZE_SMALL -> 14f
Preferences.TEXT_SIZE_NORMAL -> 16f
Preferences.TEXT_SIZE_LARGE -> 18f
Preferences.TEXT_SIZE_LARGER -> 20f
else -> 16f
}
SIZE_SECONDARY -> textSize = when (textSizePref) {
Preferences.TEXT_SIZE_SMALL -> 12f
Preferences.TEXT_SIZE_NORMAL -> 14f
Preferences.TEXT_SIZE_LARGE -> 16f
Preferences.TEXT_SIZE_LARGER -> 18f
else -> 14f
}
SIZE_TERTIARY -> textSize = when (textSizePref) {
Preferences.TEXT_SIZE_SMALL -> 10f
Preferences.TEXT_SIZE_NORMAL -> 12f
Preferences.TEXT_SIZE_LARGE -> 14f
Preferences.TEXT_SIZE_LARGER -> 16f
else -> 12f
}
SIZE_TOOLBAR -> textSize = when (textSizePref) {
Preferences.TEXT_SIZE_SMALL -> 18f
Preferences.TEXT_SIZE_NORMAL -> 20f
Preferences.TEXT_SIZE_LARGE -> 22f
Preferences.TEXT_SIZE_LARGER -> 26f
else -> 20f
}
}
})
.autoDisposable(scope())
.subscribe()
}
override fun onCreateInputConnection(editorInfo: EditorInfo): InputConnection {
val inputConnection = object : InputConnectionWrapper(super.onCreateInputConnection(editorInfo), true) {
@@ -22,17 +22,18 @@ import android.content.Context
import android.content.res.ColorStateList
import android.support.v7.widget.SwitchCompat
import android.util.AttributeSet
import com.uber.autodispose.android.scope
import com.uber.autodispose.kotlin.autoDisposable
import com.moez.QKSMS.R
import common.util.Colors
import common.util.extensions.getColorCompat
import common.util.extensions.withAlpha
import injection.appComponent
import io.reactivex.rxkotlin.Observables
import util.Preferences
import javax.inject.Inject
class QkSwitch @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : SwitchCompat(context, attrs) {
@Inject lateinit var colors: Colors
@Inject lateinit var prefs: Preferences
init {
if (!isInEditMode) {
@@ -49,17 +50,26 @@ class QkSwitch @JvmOverloads constructor(context: Context, attrs: AttributeSet?
intArrayOf(android.R.attr.state_checked),
intArrayOf())
Observables.combineLatest(colors.theme, colors.switchThumbEnabled, colors.switchThumbDisabled,
{ color, enabled, disabled -> intArrayOf(disabled, color, enabled) })
.map { values -> ColorStateList(states, values) }
.autoDisposable(scope())
.subscribe { tintList -> thumbTintList = tintList }
val themeColor = colors.theme().theme
Observables.combineLatest(colors.theme, colors.switchTrackEnabled, colors.switchTrackDisabled,
{ color, enabled, disabled -> intArrayOf(disabled, color.withAlpha(0x4D), enabled) })
.map { values -> ColorStateList(states, values) }
.autoDisposable(scope())
.subscribe { tintList -> trackTintList = tintList }
val switchThumbEnabled: Int = prefs.night.get()
.let { night -> if (night) R.color.switchThumbEnabledDark else R.color.switchThumbEnabledLight }
.let { res -> context.getColorCompat(res) }
val switchThumbDisabled: Int = prefs.night.get()
.let { night -> if (night) R.color.switchThumbDisabledDark else R.color.switchThumbDisabledLight }
.let { res -> context.getColorCompat(res) }
val switchTrackEnabled: Int = prefs.night.get()
.let { night -> if (night) R.color.switchTrackEnabledDark else R.color.switchTrackEnabledLight }
.let { res -> context.getColorCompat(res) }
val switchTrackDisabled: Int = prefs.night.get()
.let { night -> if (night) R.color.switchTrackDisabledDark else R.color.switchTrackDisabledLight }
.let { res -> context.getColorCompat(res) }
thumbTintList = ColorStateList(states, intArrayOf(switchThumbDisabled, themeColor, switchThumbEnabled))
trackTintList = ColorStateList(states, intArrayOf(switchTrackDisabled, themeColor.withAlpha(0x4D), switchTrackEnabled))
}
}
}
@@ -19,178 +19,27 @@
package common.widget
import android.content.Context
import android.graphics.Typeface
import android.support.text.emoji.widget.EmojiAppCompatTextView
import android.util.AttributeSet
import com.moez.QKSMS.R
import com.uber.autodispose.android.scope
import com.uber.autodispose.kotlin.autoDisposable
import common.util.Colors
import common.util.FontProvider
import common.util.extensions.getColorCompat
import common.util.TextViewStyler
import injection.appComponent
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import io.reactivex.rxkotlin.Observables
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import util.Preferences
import javax.inject.Inject
open class QkTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null)
: EmojiAppCompatTextView(context, attrs) {
companion object {
const val COLOR_PRIMARY = 0
const val COLOR_SECONDARY = 1
const val COLOR_TERTIARY = 2
const val COLOR_PRIMARY_ON_THEME = 3
const val COLOR_SECONDARY_ON_THEME = 4
const val COLOR_TERTIARY_ON_THEME = 5
const val COLOR_THEME = 6
const val SIZE_PRIMARY = 0
const val SIZE_SECONDARY = 1
const val SIZE_TERTIARY = 2
const val SIZE_TOOLBAR = 3
}
@Inject lateinit var colors: Colors
@Inject lateinit var fontProvider: FontProvider
@Inject lateinit var prefs: Preferences
var textColorObservable: Observable<Int>? = null
set(value) {
field = value
updateSubscription()
}
private var textColorDisposable: Disposable? = null
set(value) {
field?.dispose()
field = value
}
private var textSizeAttrSubject: Subject<Int> = BehaviorSubject.create()
@Inject lateinit var textViewStyler: TextViewStyler
init {
if (!isInEditMode) {
appComponent.inject(this)
}
context.obtainStyledAttributes(attrs, R.styleable.QkTextView)?.run {
val colorAttr = getInt(R.styleable.QkTextView_textColor, -1)
val textSizeAttr = getInt(R.styleable.QkTextView_textSize, -1)
if (isInEditMode) {
setTextColor(context.getColorCompat(when (colorAttr) {
COLOR_PRIMARY -> R.color.textPrimary
COLOR_SECONDARY -> R.color.textSecondary
COLOR_TERTIARY -> R.color.textTertiary
COLOR_PRIMARY_ON_THEME -> R.color.textPrimaryDark
COLOR_SECONDARY_ON_THEME -> R.color.textSecondaryDark
COLOR_TERTIARY_ON_THEME -> R.color.textTertiaryDark
COLOR_THEME -> R.color.tools_theme
else -> R.color.textPrimary
}))
textSize = when (textSizeAttr) {
SIZE_PRIMARY -> 16f
SIZE_SECONDARY -> 14f
SIZE_TERTIARY -> 12f
SIZE_TOOLBAR -> 20f
else -> textSize
}
} else {
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
COLOR_THEME -> colors.theme
else -> null
}
setTextSize(textSizeAttr)
}
recycle()
textViewStyler.applyAttributes(this, attrs)
}
}
/**
* @see SIZE_PRIMARY
* @see SIZE_SECONDARY
* @see SIZE_TERTIARY
* @see SIZE_TOOLBAR
*/
fun setTextSize(size: Int) {
textSizeAttrSubject.onNext(size)
override fun setTextColor(color: Int) {
super.setTextColor(color)
setLinkTextColor(color)
}
private fun updateSubscription() {
if (isAttachedToWindow) {
textColorDisposable = textColorObservable?.subscribe { color ->
setTextColor(color)
setLinkTextColor(color)
}
}
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (isInEditMode) return
updateSubscription()
fontProvider.typeface
.autoDisposable(scope())
.subscribe { setTypeface(it.value, typeface?.style ?: Typeface.NORMAL) }
Observables
.combineLatest(prefs.textSize.asObservable(), textSizeAttrSubject, { textSizePref, textSizeAttr ->
when (textSizeAttr) {
SIZE_PRIMARY -> textSize = when (textSizePref) {
Preferences.TEXT_SIZE_SMALL -> 14f
Preferences.TEXT_SIZE_NORMAL -> 16f
Preferences.TEXT_SIZE_LARGE -> 18f
Preferences.TEXT_SIZE_LARGER -> 20f
else -> 16f
}
SIZE_SECONDARY -> textSize = when (textSizePref) {
Preferences.TEXT_SIZE_SMALL -> 12f
Preferences.TEXT_SIZE_NORMAL -> 14f
Preferences.TEXT_SIZE_LARGE -> 16f
Preferences.TEXT_SIZE_LARGER -> 18f
else -> 14f
}
SIZE_TERTIARY -> textSize = when (textSizePref) {
Preferences.TEXT_SIZE_SMALL -> 10f
Preferences.TEXT_SIZE_NORMAL -> 12f
Preferences.TEXT_SIZE_LARGE -> 14f
Preferences.TEXT_SIZE_LARGER -> 16f
else -> 12f
}
SIZE_TOOLBAR -> textSize = when (textSizePref) {
Preferences.TEXT_SIZE_SMALL -> 18f
Preferences.TEXT_SIZE_NORMAL -> 20f
Preferences.TEXT_SIZE_LARGE -> 22f
Preferences.TEXT_SIZE_LARGER -> 26f
else -> 20f
}
}
})
.autoDisposable(scope())
.subscribe()
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
textColorDisposable?.dispose()
}
}
@@ -1,53 +0,0 @@
/*
* 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 common.widget
import android.content.Context
import android.util.AttributeSet
import android.view.View
import com.moez.QKSMS.R
import com.uber.autodispose.android.scope
import com.uber.autodispose.kotlin.autoDisposable
import common.util.Colors
import common.util.extensions.getColorCompat
import injection.appComponent
import javax.inject.Inject
class Separator @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : View(context, attrs) {
@Inject lateinit var colors: Colors
init {
if (!isInEditMode) {
appComponent.inject(this)
}
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (isInEditMode) {
setBackgroundColor(context.getColorCompat(R.color.separatorLight))
} else {
colors.separator
.autoDisposable(scope())
.subscribe { color -> setBackgroundColor(color) }
}
}
}