Skip to content

Commit

Permalink
fix: do not use ChangelogDialog sharedPrefs before fragment attach
Browse files Browse the repository at this point in the history
Fixes OPENFOODFACTS-ANDROID-3K3
  • Loading branch information
VaiTon committed Jun 19, 2021
1 parent eb78bf4 commit 7f637bc
Showing 1 changed file with 21 additions and 14 deletions.
@@ -1,5 +1,6 @@
package openfoodfacts.github.scrachx.openfood.features.changelog

import android.app.Activity
import android.content.SharedPreferences
import android.content.pm.PackageManager.NameNotFoundException
import android.net.Uri
Expand All @@ -13,6 +14,8 @@ import androidx.core.content.edit
import androidx.core.content.pm.PackageInfoCompat
import androidx.core.view.isVisible
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import dagger.hilt.android.AndroidEntryPoint
Expand Down Expand Up @@ -44,6 +47,9 @@ class ChangelogDialog : DialogFragment(R.layout.fragment_changelog) {
}
}
}

private fun getSavedVersionCode(activity: Activity) =
PreferenceManager.getDefaultSharedPreferences(activity).getLong(LAST_VERSION_CODE, 0)
}

@Inject
Expand Down Expand Up @@ -83,20 +89,22 @@ class ChangelogDialog : DialogFragment(R.layout.fragment_changelog) {

@Suppress("DEPRECATION")
fun presentAutomatically(activity: AppCompatActivity) {
arguments?.let {
if (it.getBoolean(FORCE_SHOW_KEY, false)) {
show(activity.supportFragmentManager, TAG)
} else {
try {
val lastVersionCode = getSavedVersionCode()
val packageInfo = activity.packageManager.getPackageInfo(activity.packageName, 0)
val currentVersionCode = PackageInfoCompat.getLongVersionCode(packageInfo)
if (currentVersionCode >= 0 && currentVersionCode > lastVersionCode) {
show(activity.supportFragmentManager, TAG)
saveVersionCode(currentVersionCode)
lifecycleScope.launchWhenResumed {
arguments?.let {
if (it.getBoolean(FORCE_SHOW_KEY, false)) {
show(activity.supportFragmentManager, TAG)
} else {
try {
val lastVersionCode = getSavedVersionCode(activity)
val packageInfo = activity.packageManager.getPackageInfo(activity.packageName, 0)
val currentVersionCode = PackageInfoCompat.getLongVersionCode(packageInfo)
if (currentVersionCode >= 0 && currentVersionCode > lastVersionCode) {
show(activity.supportFragmentManager, TAG)
saveVersionCode(currentVersionCode)
}
} catch (ex: NameNotFoundException) {
sentryAnalytics.record(ex)
}
} catch (ex: NameNotFoundException) {
sentryAnalytics.record(ex)
}
}
}
Expand Down Expand Up @@ -168,5 +176,4 @@ class ChangelogDialog : DialogFragment(R.layout.fragment_changelog) {

}

private fun getSavedVersionCode() = sharedPreferences.getLong(LAST_VERSION_CODE, 0)
}

0 comments on commit 7f637bc

Please sign in to comment.