From 5871d327426652447bc41d2a1215da2b12b13320 Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Tue, 13 Mar 2018 19:34:15 -0700 Subject: [PATCH] Migrate preferences from 2.7.3 --- .../java/interactor/MigratePreferences.kt | 70 +++++++++++++++++++ .../feature/main/MainViewModel.kt | 9 ++- 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 qksms/src/main/java/interactor/MigratePreferences.kt diff --git a/qksms/src/main/java/interactor/MigratePreferences.kt b/qksms/src/main/java/interactor/MigratePreferences.kt new file mode 100644 index 00000000..ab32806f --- /dev/null +++ b/qksms/src/main/java/interactor/MigratePreferences.kt @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2017 Moez Bhatti + * + * 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 . + */ +package interactor + +import com.f2prateek.rx.preferences2.RxSharedPreferences +import common.util.NightModeManager +import common.util.Preferences +import io.reactivex.Flowable +import javax.inject.Inject + +/** + * When upgrading from 2.7.3 to 3.0, migrate the preferences + */ +class MigratePreferences @Inject constructor( + private val nightModeManager: NightModeManager, + private val prefs: Preferences, + private val rxPrefs: RxSharedPreferences +) : Interactor() { + + override fun buildObservable(params: Unit): Flowable<*> { + return Flowable.just(rxPrefs.getBoolean("pref_key_welcome_seen", false)) + .filter { seen -> seen.get() } // Only proceed if this value is true. It will be set false at the end + .doOnNext { + // Theme + val defaultTheme = prefs.theme().get().toString() + val oldTheme = rxPrefs.getString("pref_key_theme", defaultTheme).get() + prefs.theme().set(Integer.parseInt(oldTheme)) + } + .doOnNext { + // Night mode + val background = rxPrefs.getString("pref_key_background", "light").get() + val autoNight = rxPrefs.getBoolean("pref_key_night_auto", false).get() + when { + autoNight -> nightModeManager.updateNightMode(Preferences.NIGHT_MODE_AUTO) + background == "light" -> nightModeManager.updateNightMode(Preferences.NIGHT_MODE_OFF) + background == "grey" -> nightModeManager.updateNightMode(Preferences.NIGHT_MODE_ON) + background == "black" -> { + nightModeManager.updateNightMode(Preferences.NIGHT_MODE_ON) + prefs.black.set(true) + } + } + } + .doOnNext { + // Delivery + prefs.delivery.set(rxPrefs.getBoolean("pref_key_delivery", prefs.delivery.get()).get()) + } + .doOnNext { + // Unicode + prefs.unicode.set(rxPrefs.getBoolean("pref_key_strip_unicode", prefs.unicode.get()).get()) + } + .doOnNext { seen -> seen.delete() } // Clear this value so that we don't need to migrate again + } + +} \ No newline at end of file diff --git a/qksms/src/main/java/presentation/feature/main/MainViewModel.kt b/qksms/src/main/java/presentation/feature/main/MainViewModel.kt index 1e62d8b2..484ab770 100644 --- a/qksms/src/main/java/presentation/feature/main/MainViewModel.kt +++ b/qksms/src/main/java/presentation/feature/main/MainViewModel.kt @@ -27,6 +27,7 @@ import com.moez.QKSMS.R import com.uber.autodispose.android.lifecycle.scope import com.uber.autodispose.kotlin.autoDisposable import common.di.appComponent +import common.util.Preferences import common.util.extensions.toFlowable import common.util.filter.ConversationFilter import data.model.MenuItem @@ -52,7 +53,9 @@ class MainViewModel : QkViewModel(MainState()) { @Inject lateinit var deleteConversation: DeleteConversation @Inject lateinit var markArchived: MarkArchived @Inject lateinit var markUnarchived: MarkUnarchived + @Inject lateinit var migratePreferences: MigratePreferences @Inject lateinit var partialSync: PartialSync + @Inject lateinit var prefs: Preferences private val conversations by lazy { messageRepo.getConversations() @@ -81,10 +84,11 @@ class MainViewModel : QkViewModel(MainState()) { init { appComponent.inject(this) + disposables += deleteConversation disposables += markAllSeen disposables += markArchived disposables += markUnarchived - disposables += deleteConversation + disposables += migratePreferences disposables += partialSync // If it's the first sync, reflect that in the ViewState @@ -100,6 +104,9 @@ class MainViewModel : QkViewModel(MainState()) { newState { it.copy(page = Inbox(data = conversations)) } + // Migrate the preferences from 2.7.3 if necessary + migratePreferences.execute(Unit) + val isNotDefaultSms = Telephony.Sms.getDefaultSmsPackage(context) != context.packageName val hasSmsPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED val hasContactPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED