From 66140894866ac04519221e07269568c72801ac5f Mon Sep 17 00:00:00 2001 From: moezbhatti Date: Tue, 10 Sep 2019 00:02:02 -0400 Subject: [PATCH] Fix changelog version logic --- .../util/extensions/ContextExtensions.kt | 3 +++ .../QKSMS/manager/ChangelogManagerImpl.kt | 22 ++++++------------- .../com/moez/QKSMS/migration/QkMigration.kt | 16 ++++++++------ .../com/moez/QKSMS/util/GlideAppModule.kt | 5 +++-- .../java/com/moez/QKSMS/util/Preferences.kt | 9 ++++---- 5 files changed, 27 insertions(+), 28 deletions(-) diff --git a/common/src/main/java/com/moez/QKSMS/common/util/extensions/ContextExtensions.kt b/common/src/main/java/com/moez/QKSMS/common/util/extensions/ContextExtensions.kt index d1df35680..2ece34d43 100644 --- a/common/src/main/java/com/moez/QKSMS/common/util/extensions/ContextExtensions.kt +++ b/common/src/main/java/com/moez/QKSMS/common/util/extensions/ContextExtensions.kt @@ -66,3 +66,6 @@ fun Context.makeToast(text: String, duration: Int = Toast.LENGTH_SHORT) { fun Context.isInstalled(packageName: String): Boolean { return tryOrNull(false) { packageManager.getApplicationInfo(packageName, 0).enabled } ?: false } + +val Context.versionCode: Int + get() = packageManager.getPackageInfo(packageName, 0).versionCode diff --git a/data/src/main/java/com/moez/QKSMS/manager/ChangelogManagerImpl.kt b/data/src/main/java/com/moez/QKSMS/manager/ChangelogManagerImpl.kt index e5cc336dc..bc01d68b8 100644 --- a/data/src/main/java/com/moez/QKSMS/manager/ChangelogManagerImpl.kt +++ b/data/src/main/java/com/moez/QKSMS/manager/ChangelogManagerImpl.kt @@ -19,6 +19,7 @@ package com.moez.QKSMS.manager import android.content.Context +import com.moez.QKSMS.common.util.extensions.versionCode import com.moez.QKSMS.util.Preferences import com.squareup.moshi.Json import com.squareup.moshi.Moshi @@ -40,19 +41,7 @@ class ChangelogManagerImpl @Inject constructor( private val prefs: Preferences ) : ChangelogManager { - private val oldVersion: Int get() = prefs.changelogVersion.get() - private val versionCode: Int get() = context.packageManager.getPackageInfo(context.packageName, 0).versionCode - - override fun didUpdate(): Boolean { - if (oldVersion == 0) { - prefs.changelogVersion.set(versionCode) - } - - return when { - oldVersion != versionCode -> true - else -> false - } - } + override fun didUpdate(): Boolean = prefs.changelogVersion.get() != context.versionCode override fun getChangelog(): Single { val url = "https://firestore.googleapis.com/v1/projects/qksms-app/databases/(default)/documents/changelog" @@ -74,7 +63,10 @@ class ChangelogManagerImpl @Inject constructor( .map { response -> response.documents .sortedBy { document -> document.fields.versionCode.value } - .filter { document -> document.fields.versionCode.value.toInt() in (oldVersion + 1)..versionCode } + .filter { document -> + val range = (prefs.changelogVersion.get() + 1)..context.versionCode + document.fields.versionCode.value.toInt() in range + } } .map { documents -> val added = documents.fold(listOf()) { acc, document -> @@ -93,7 +85,7 @@ class ChangelogManagerImpl @Inject constructor( } override fun markChangelogSeen() { - prefs.changelogVersion.set(versionCode) + prefs.changelogVersion.set(context.versionCode) } private data class ChangelogResponse( diff --git a/data/src/main/java/com/moez/QKSMS/migration/QkMigration.kt b/data/src/main/java/com/moez/QKSMS/migration/QkMigration.kt index 0204dd489..32e192d38 100644 --- a/data/src/main/java/com/moez/QKSMS/migration/QkMigration.kt +++ b/data/src/main/java/com/moez/QKSMS/migration/QkMigration.kt @@ -20,6 +20,7 @@ package com.moez.QKSMS.migration import android.content.Context import com.moez.QKSMS.blocking.QksmsBlockingClient +import com.moez.QKSMS.common.util.extensions.versionCode import com.moez.QKSMS.repository.ConversationRepository import com.moez.QKSMS.util.Preferences import kotlinx.coroutines.GlobalScope @@ -34,17 +35,18 @@ class QkMigration @Inject constructor( ) { init { - val oldVersion = prefs.version.get() - val newVersion = context.packageManager.getPackageInfo(context.packageName, 0).versionCode + GlobalScope.launch { + val oldVersion = prefs.version.get() - if (oldVersion < 2199) { - upgradeTo370() - } + if (oldVersion < 2199) { + upgradeTo370() + } - prefs.changelogVersion.set(newVersion) + prefs.version.set(context.versionCode) + } } - private fun upgradeTo370() = GlobalScope.launch { + private fun upgradeTo370() { // Migrate changelog version prefs.changelogVersion.set(prefs.version.get()) diff --git a/data/src/main/java/com/moez/QKSMS/util/GlideAppModule.kt b/data/src/main/java/com/moez/QKSMS/util/GlideAppModule.kt index 59bd98737..f3b435076 100644 --- a/data/src/main/java/com/moez/QKSMS/util/GlideAppModule.kt +++ b/data/src/main/java/com/moez/QKSMS/util/GlideAppModule.kt @@ -38,8 +38,9 @@ class GlideAppModule : AppGlideModule() { override fun registerComponents(context: Context, glide: Glide, registry: Registry) { // TODO use DI to create the ContactImageLoader.Factory - registry.append(String::class.java, InputStream::class.java, ContactImageLoader.Factory(context, - Preferences(RxSharedPreferences.create(PreferenceManager.getDefaultSharedPreferences(context))))) + val rxPrefs = RxSharedPreferences.create(PreferenceManager.getDefaultSharedPreferences(context)) + val prefs = Preferences(context, rxPrefs) + registry.append(String::class.java, InputStream::class.java, ContactImageLoader.Factory(context, prefs)) } } \ No newline at end of file diff --git a/domain/src/main/java/com/moez/QKSMS/util/Preferences.kt b/domain/src/main/java/com/moez/QKSMS/util/Preferences.kt index bbce74ef6..5e73c2896 100644 --- a/domain/src/main/java/com/moez/QKSMS/util/Preferences.kt +++ b/domain/src/main/java/com/moez/QKSMS/util/Preferences.kt @@ -18,15 +18,17 @@ */ package com.moez.QKSMS.util +import android.content.Context import android.os.Build import android.provider.Settings import com.f2prateek.rx.preferences2.Preference import com.f2prateek.rx.preferences2.RxSharedPreferences +import com.moez.QKSMS.common.util.extensions.versionCode import javax.inject.Inject import javax.inject.Singleton @Singleton -class Preferences @Inject constructor(private val rxPrefs: RxSharedPreferences) { +class Preferences @Inject constructor(context: Context, private val rxPrefs: RxSharedPreferences) { companion object { const val NIGHT_MODE_SYSTEM = 0 @@ -69,12 +71,11 @@ class Preferences @Inject constructor(private val rxPrefs: RxSharedPreferences) // Internal val night = rxPrefs.getBoolean("night", false) val canUseSubId = rxPrefs.getBoolean("canUseSubId", true) - val version = rxPrefs.getInteger("version", 0) - val changelogVersion = rxPrefs.getInteger("changelogVersion", 0) + val version = rxPrefs.getInteger("version", context.versionCode) + val changelogVersion = rxPrefs.getInteger("changelogVersion", context.versionCode) @Deprecated("This should only be accessed when migrating to @blockingManager") val sia = rxPrefs.getBoolean("sia", false) - // User configurable val nightMode = rxPrefs.getInteger("nightMode", when (Build.VERSION.SDK_INT >= 29) { true -> NIGHT_MODE_SYSTEM