Commit 66140894 authored by moezbhatti's avatar moezbhatti
Browse files

Fix changelog version logic

parent 12dc500e
......@@ -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
......@@ -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<ChangelogManager.Changelog> {
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<String>()) { 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(
......
......@@ -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())
......
......@@ -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
......@@ -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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment