Skip to content

Commit

Permalink
Fix changelog version logic
Browse files Browse the repository at this point in the history
  • Loading branch information
moezbhatti committed Sep 10, 2019
1 parent 12dc500 commit 6614089
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 7 additions & 15 deletions data/src/main/java/com/moez/QKSMS/manager/ChangelogManagerImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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 ->
Expand All @@ -93,7 +85,7 @@ class ChangelogManagerImpl @Inject constructor(
}

override fun markChangelogSeen() {
prefs.changelogVersion.set(versionCode)
prefs.changelogVersion.set(context.versionCode)
}

private data class ChangelogResponse(
Expand Down
16 changes: 9 additions & 7 deletions data/src/main/java/com/moez/QKSMS/migration/QkMigration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())

Expand Down
5 changes: 3 additions & 2 deletions data/src/main/java/com/moez/QKSMS/util/GlideAppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

}
9 changes: 5 additions & 4 deletions domain/src/main/java/com/moez/QKSMS/util/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6614089

Please sign in to comment.