#1013 - Add option to use system font

This commit is contained in:
Moez Bhatti
2018-04-18 18:18:28 -04:00
parent b0ee5a03bb
commit 597de70748
11 changed files with 79 additions and 25 deletions
+1
View File
@@ -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)
@@ -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<Optional<Typeface>> = 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<Optional<Typeface>> = 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<Optional<Typeface>>
subject.onNext(Optional(typeface))
}
}, null)
}
fun getLato(callback: (Typeface) -> Unit) {
lato?.run(callback)
if (lato == null) {
pendingCallbacks += callback
}
}
}
@@ -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) }
@@ -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) {
@@ -57,11 +57,13 @@ class PlusActivity : QkThemedActivity<PlusViewModel>(), 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())
@@ -131,6 +131,7 @@ class SettingsActivity : QkThemedActivity<SettingsViewModel>(), SettingsView {
qkreplyTapDismiss.checkbox.isChecked = state.qkReplyTapDismiss
textSize.summary = state.textSizeSummary
systemFont.checkbox.isChecked = state.systemFontEnabled
unicode.checkbox.isChecked = state.stripUnicodeEnabled
@@ -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",
@@ -94,6 +94,9 @@ class SettingsViewModel : QkViewModel<SettingsView, SettingsState>(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<SettingsView, SettingsState>(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()
@@ -0,0 +1,27 @@
<!--
~ 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/>.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M5,4v3h5.5v12h3V7H19V4z"/>
</vector>
@@ -119,6 +119,14 @@
app:icon="@drawable/ic_format_size_black_24dp"
app:title="@string/settings_text_size_title" />
<common.widget.PreferenceView
android:id="@+id/systemFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:icon="@drawable/ic_title_black_24dp"
app:title="@string/settings_system_font_title"
app:widget="@layout/settings_switch_widget" />
<common.widget.Separator
android:layout_width="match_parent"
android:layout_height="1dp"
@@ -207,10 +215,10 @@
android:id="@+id/version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginBottom="8dp"
app:textColor="tertiary"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:layout_marginTop="24dp"
app:textColor="tertiary"
app:textSize="secondary"
tools:text="Version 3.0.0" />
@@ -114,6 +114,7 @@
<string name="settings_qkreply_tap_dismiss_title">Tap to dismiss</string>
<string name="settings_qkreply_tap_dismiss_summary">Tap outside of the popup to close it</string>
<string name="settings_text_size_title">Font size</string>
<string name="settings_system_font_title">Use system font</string>
<string name="settings_unicode_title">Strip accents</string>
<string name="settings_unicode_summary">Remove accents from characters in outgoing SMS messages</string>
<string name="settings_mms_size_title">Max MMS attachment size</string>