Create utility class to make preference usage easier and more reliable

This commit is contained in:
Moez Bhatti
2017-12-04 00:58:00 -05:00
parent 64504a2433
commit 61a9d87afd
4 changed files with 33 additions and 10 deletions
@@ -0,0 +1,24 @@
package com.moez.QKSMS.common.util
import android.provider.Settings
import com.f2prateek.rx.preferences2.RxSharedPreferences
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class Preferences @Inject constructor(rxPrefs: RxSharedPreferences) {
val defaultSms = rxPrefs.getBoolean("defaultSms", false)
val theme = rxPrefs.getInteger("theme", 0xFF008389.toInt())
val dark = rxPrefs.getBoolean("dark", false)
val autoEmoji = rxPrefs.getBoolean("autoEmoji", true)
val notifications = rxPrefs.getBoolean("notifications", true)
val vibration = rxPrefs.getBoolean("vibration", true)
val ringtone = rxPrefs.getString("ringtone", Settings.System.DEFAULT_NOTIFICATION_URI.toString())
val delivery = rxPrefs.getBoolean("delivery", false)
val split = rxPrefs.getBoolean("split", false)
val unicode = rxPrefs.getBoolean("unicode", false)
val mms = rxPrefs.getBoolean("mms", true)
val mmsSize = rxPrefs.getLong("mmsSize", -1)
}
@@ -1,6 +1,5 @@
package com.moez.QKSMS.common.util package com.moez.QKSMS.common.util
import com.f2prateek.rx.preferences2.RxSharedPreferences
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.subjects.BehaviorSubject import io.reactivex.subjects.BehaviorSubject
@@ -10,7 +9,7 @@ import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class ThemeManager @Inject constructor(prefs: RxSharedPreferences) { class ThemeManager @Inject constructor(prefs: Preferences) {
val color: Observable<Int> val color: Observable<Int>
@@ -23,7 +22,7 @@ class ThemeManager @Inject constructor(prefs: RxSharedPreferences) {
init { init {
val colorSubject: Subject<Int> = BehaviorSubject.createDefault(0xFF008389.toInt()) val colorSubject: Subject<Int> = BehaviorSubject.createDefault(0xFF008389.toInt())
prefs.getInteger("theme").asObservable().subscribe { prefs.theme.asObservable().subscribe {
colorSubject.onNext(it) colorSubject.onNext(it)
} }
@@ -3,9 +3,9 @@ package com.moez.QKSMS.presentation.settings
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.provider.Telephony import android.provider.Telephony
import com.f2prateek.rx.preferences2.RxSharedPreferences
import com.moez.QKSMS.R import com.moez.QKSMS.R
import com.moez.QKSMS.common.di.AppComponentManager import com.moez.QKSMS.common.di.AppComponentManager
import com.moez.QKSMS.common.util.Preferences
import com.moez.QKSMS.domain.interactor.FullSync import com.moez.QKSMS.domain.interactor.FullSync
import com.moez.QKSMS.presentation.base.QkViewModel import com.moez.QKSMS.presentation.base.QkViewModel
import io.reactivex.rxkotlin.plusAssign import io.reactivex.rxkotlin.plusAssign
@@ -15,7 +15,7 @@ import javax.inject.Inject
class SettingsViewModel : QkViewModel<SettingsView, SettingsState>(SettingsState()) { class SettingsViewModel : QkViewModel<SettingsView, SettingsState>(SettingsState()) {
@Inject lateinit var context: Context @Inject lateinit var context: Context
@Inject lateinit var prefs: RxSharedPreferences @Inject lateinit var prefs: Preferences
@Inject lateinit var fullSync: FullSync @Inject lateinit var fullSync: FullSync
init { init {
@@ -23,7 +23,7 @@ class SettingsViewModel : QkViewModel<SettingsView, SettingsState>(SettingsState
disposables += fullSync disposables += fullSync
disposables += prefs.getBoolean("defaultSms") disposables += prefs.defaultSms
.asObservable() .asObservable()
.subscribe { isDefaultSmsApp -> .subscribe { isDefaultSmsApp ->
newState { it.copy(isDefaultSmsApp = isDefaultSmsApp) } newState { it.copy(isDefaultSmsApp = isDefaultSmsApp) }
@@ -59,7 +59,7 @@ class SettingsViewModel : QkViewModel<SettingsView, SettingsState>(SettingsState
} }
intents += view.themeSelectedIntent.subscribe { color -> intents += view.themeSelectedIntent.subscribe { color ->
prefs.getInteger("theme").set(color) prefs.theme.set(color)
newState { it.copy(selectingTheme = false) } newState { it.copy(selectingTheme = false) }
} }
} }
@@ -6,22 +6,22 @@ import android.content.Intent
import android.os.Build import android.os.Build
import android.provider.Telephony import android.provider.Telephony
import android.support.annotation.RequiresApi import android.support.annotation.RequiresApi
import com.f2prateek.rx.preferences2.RxSharedPreferences
import com.moez.QKSMS.common.di.AppComponentManager import com.moez.QKSMS.common.di.AppComponentManager
import com.moez.QKSMS.common.util.Preferences
import com.moez.QKSMS.domain.interactor.PartialSync import com.moez.QKSMS.domain.interactor.PartialSync
import javax.inject.Inject import javax.inject.Inject
class DefaultSmsChangedReceiver : BroadcastReceiver() { class DefaultSmsChangedReceiver : BroadcastReceiver() {
@Inject lateinit var partialSync: PartialSync @Inject lateinit var partialSync: PartialSync
@Inject lateinit var prefs: RxSharedPreferences @Inject lateinit var prefs: Preferences
@RequiresApi(Build.VERSION_CODES.N) @RequiresApi(Build.VERSION_CODES.N)
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
AppComponentManager.appComponent.inject(this) AppComponentManager.appComponent.inject(this)
val isDefaultSmsApp = intent.getBooleanExtra(Telephony.Sms.Intents.EXTRA_IS_DEFAULT_SMS_APP, false) val isDefaultSmsApp = intent.getBooleanExtra(Telephony.Sms.Intents.EXTRA_IS_DEFAULT_SMS_APP, false)
prefs.getBoolean("defaultSms").set(isDefaultSmsApp) prefs.defaultSms.set(isDefaultSmsApp)
if (isDefaultSmsApp) { if (isDefaultSmsApp) {
val pendingResult = goAsync() val pendingResult = goAsync()