From 597de70748a609df5112ce5245da263641ad4b5a Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Wed, 18 Apr 2018 18:18:28 -0400 Subject: [PATCH] #1013 - Add option to use system font --- data/src/main/java/util/Preferences.kt | 1 + .../src/main/java/common/util/FontProvider.kt | 32 ++++++++++--------- .../src/main/java/common/widget/QkEditText.kt | 5 ++- .../src/main/java/common/widget/QkTextView.kt | 5 ++- .../main/java/feature/plus/PlusActivity.kt | 12 ++++--- .../java/feature/settings/SettingsActivity.kt | 1 + .../java/feature/settings/SettingsState.kt | 1 + .../feature/settings/SettingsViewModel.kt | 5 +++ .../main/res/drawable/ic_title_black_24dp.xml | 27 ++++++++++++++++ .../src/main/res/layout/settings_activity.xml | 14 ++++++-- presentation/src/main/res/values/strings.xml | 1 + 11 files changed, 79 insertions(+), 25 deletions(-) create mode 100644 presentation/src/main/res/drawable/ic_title_black_24dp.xml diff --git a/data/src/main/java/util/Preferences.kt b/data/src/main/java/util/Preferences.kt index 4027c0f6..4bc71ee6 100644 --- a/data/src/main/java/util/Preferences.kt +++ b/data/src/main/java/util/Preferences.kt @@ -48,6 +48,7 @@ class Preferences @Inject constructor(private val rxPrefs: RxSharedPreferences) val sia = rxPrefs.getBoolean("sia", false) val autoEmoji = rxPrefs.getBoolean("autoEmoji", true) val delivery = rxPrefs.getBoolean("delivery", false) + val systemFont = rxPrefs.getBoolean("systemFont", false) val textSize = rxPrefs.getInteger("textSize", TEXT_SIZE_NORMAL) val qkreply = rxPrefs.getBoolean("qkreply", Build.VERSION.SDK_INT < Build.VERSION_CODES.N) val qkreplyTapDismiss = rxPrefs.getBoolean("qkreplyTapDismiss", true) diff --git a/presentation/src/main/java/common/util/FontProvider.kt b/presentation/src/main/java/common/util/FontProvider.kt index a31fc874..51d83ae8 100644 --- a/presentation/src/main/java/common/util/FontProvider.kt +++ b/presentation/src/main/java/common/util/FontProvider.kt @@ -22,16 +22,28 @@ 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) { +class FontProvider @Inject constructor(context: Context, prefs: Preferences) { - private var lato: Typeface? = null + val typeface: Observable> = prefs.systemFont.asObservable() + .distinctUntilChanged() + .switchMap { systemFont -> + when (systemFont) { + true -> Observable.just(Optional(null)) + false -> lato + } + } - private val pendingCallbacks = ArrayList<(Typeface) -> Unit>() + private val lato: Observable> = BehaviorSubject.create() init { ResourcesCompat.getFont(context, R.font.lato, object : ResourcesCompat.FontCallback() { @@ -40,20 +52,10 @@ class FontProvider @Inject constructor(context: Context) { } override fun onFontRetrieved(typeface: Typeface) { - lato = typeface - - pendingCallbacks.forEach { lato?.run(it) } - pendingCallbacks.clear() + val subject = lato as Subject> + subject.onNext(Optional(typeface)) } }, null) } - fun getLato(callback: (Typeface) -> Unit) { - lato?.run(callback) - - if (lato == null) { - pendingCallbacks += callback - } - } - } \ No newline at end of file diff --git a/presentation/src/main/java/common/widget/QkEditText.kt b/presentation/src/main/java/common/widget/QkEditText.kt index c62e450c..6c51aacf 100644 --- a/presentation/src/main/java/common/widget/QkEditText.kt +++ b/presentation/src/main/java/common/widget/QkEditText.kt @@ -73,7 +73,6 @@ class QkEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet init { if (!isInEditMode) { appComponent.inject(this) - fontProvider.getLato { setTypeface(it, typeface?.style ?: Typeface.NORMAL) } context.obtainStyledAttributes(attrs, R.styleable.QkEditText)?.run { val colorAttr = getInt(R.styleable.QkEditText_textColor, -1) @@ -118,6 +117,10 @@ class QkEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet super.onAttachedToWindow() if (isInEditMode) return + fontProvider.typeface + .autoDisposable(scope()) + .subscribe { setTypeface(it.value, typeface?.style ?: Typeface.NORMAL) } + textColorObservable ?.autoDisposable(scope()) ?.subscribe { color -> setTextColor(color) } diff --git a/presentation/src/main/java/common/widget/QkTextView.kt b/presentation/src/main/java/common/widget/QkTextView.kt index 04b43e8e..a884d23f 100644 --- a/presentation/src/main/java/common/widget/QkTextView.kt +++ b/presentation/src/main/java/common/widget/QkTextView.kt @@ -75,7 +75,6 @@ open class QkTextView @JvmOverloads constructor(context: Context, attrs: Attribu init { if (!isInEditMode) { appComponent.inject(this) - fontProvider.getLato { setTypeface(it, typeface?.style ?: Typeface.NORMAL) } } context.obtainStyledAttributes(attrs, R.styleable.QkTextView)?.run { @@ -143,6 +142,10 @@ open class QkTextView @JvmOverloads constructor(context: Context, attrs: Attribu updateSubscription() + fontProvider.typeface + .autoDisposable(scope()) + .subscribe { setTypeface(it.value, typeface?.style ?: Typeface.NORMAL) } + Observables .combineLatest(prefs.textSize.asObservable(), textSizeAttrSubject, { textSizePref, textSizeAttr -> when (textSizeAttr) { diff --git a/presentation/src/main/java/feature/plus/PlusActivity.kt b/presentation/src/main/java/feature/plus/PlusActivity.kt index dd16fa42..0028ec82 100644 --- a/presentation/src/main/java/feature/plus/PlusActivity.kt +++ b/presentation/src/main/java/feature/plus/PlusActivity.kt @@ -57,11 +57,13 @@ class PlusActivity : QkThemedActivity(), PlusView { free.setVisible(false) - fontProvider.getLato { lato -> - val typeface = Typeface.create(lato, Typeface.BOLD) - collapsingToolbar.setCollapsedTitleTypeface(typeface) - collapsingToolbar.setExpandedTitleTypeface(typeface) - } + fontProvider.typeface + .autoDisposable(scope()) + .subscribe { lato -> + val typeface = Typeface.create(lato.value, Typeface.BOLD) + collapsingToolbar.setCollapsedTitleTypeface(typeface) + collapsingToolbar.setExpandedTitleTypeface(typeface) + } colors.textPrimary .autoDisposable(scope()) diff --git a/presentation/src/main/java/feature/settings/SettingsActivity.kt b/presentation/src/main/java/feature/settings/SettingsActivity.kt index 4c19eeb1..7fd663f0 100644 --- a/presentation/src/main/java/feature/settings/SettingsActivity.kt +++ b/presentation/src/main/java/feature/settings/SettingsActivity.kt @@ -131,6 +131,7 @@ class SettingsActivity : QkThemedActivity(), SettingsView { qkreplyTapDismiss.checkbox.isChecked = state.qkReplyTapDismiss textSize.summary = state.textSizeSummary + systemFont.checkbox.isChecked = state.systemFontEnabled unicode.checkbox.isChecked = state.stripUnicodeEnabled diff --git a/presentation/src/main/java/feature/settings/SettingsState.kt b/presentation/src/main/java/feature/settings/SettingsState.kt index 72511c2f..852395c8 100644 --- a/presentation/src/main/java/feature/settings/SettingsState.kt +++ b/presentation/src/main/java/feature/settings/SettingsState.kt @@ -37,6 +37,7 @@ data class SettingsState( val qkReplyEnabled: Boolean = Build.VERSION.SDK_INT < Build.VERSION_CODES.N, val qkReplyTapDismiss: Boolean = true, val textSizeSummary: String = "", + val systemFontEnabled: Boolean = false, val splitSmsEnabled: Boolean = false, val stripUnicodeEnabled: Boolean = false, val maxMmsSizeSummary: String = "100KB", diff --git a/presentation/src/main/java/feature/settings/SettingsViewModel.kt b/presentation/src/main/java/feature/settings/SettingsViewModel.kt index dba09ad9..75394e23 100644 --- a/presentation/src/main/java/feature/settings/SettingsViewModel.kt +++ b/presentation/src/main/java/feature/settings/SettingsViewModel.kt @@ -94,6 +94,9 @@ class SettingsViewModel : QkViewModel(SettingsState disposables += prefs.textSize.asObservable() .subscribe { textSize -> newState { it.copy(textSizeSummary = textSizeLabels[textSize]) } } + disposables += prefs.systemFont.asObservable() + .subscribe { enabled -> newState { it.copy(systemFontEnabled = enabled) } } + disposables += prefs.unicode.asObservable() .subscribe { enabled -> newState { it.copy(stripUnicodeEnabled = enabled) } } @@ -149,6 +152,8 @@ class SettingsViewModel : QkViewModel(SettingsState R.id.textSize -> view.showTextSizePicker() + R.id.systemFont -> prefs.systemFont.set(!prefs.systemFont.get()) + R.id.unicode -> prefs.unicode.set(!prefs.unicode.get()) R.id.mmsSize -> view.showMmsSizePicker() diff --git a/presentation/src/main/res/drawable/ic_title_black_24dp.xml b/presentation/src/main/res/drawable/ic_title_black_24dp.xml new file mode 100644 index 00000000..dc7ec5d3 --- /dev/null +++ b/presentation/src/main/res/drawable/ic_title_black_24dp.xml @@ -0,0 +1,27 @@ + + + + diff --git a/presentation/src/main/res/layout/settings_activity.xml b/presentation/src/main/res/layout/settings_activity.xml index 3105f5c7..083bfc7d 100644 --- a/presentation/src/main/res/layout/settings_activity.xml +++ b/presentation/src/main/res/layout/settings_activity.xml @@ -119,6 +119,14 @@ app:icon="@drawable/ic_format_size_black_24dp" app:title="@string/settings_text_size_title" /> + + diff --git a/presentation/src/main/res/values/strings.xml b/presentation/src/main/res/values/strings.xml index 974f6f4b..8738c4ba 100644 --- a/presentation/src/main/res/values/strings.xml +++ b/presentation/src/main/res/values/strings.xml @@ -114,6 +114,7 @@ Tap to dismiss Tap outside of the popup to close it Font size + Use system font Strip accents Remove accents from characters in outgoing SMS messages Max MMS attachment size