From c6415d8fdf552cebf1f59000195b170dded39397 Mon Sep 17 00:00:00 2001 From: "Jo G." <1451036+jo-elimu@users.noreply.github.com> Date: Fri, 10 Jan 2025 18:41:19 +0700 Subject: [PATCH] feat: add version upgrade functionality resolves #90 --- .../content_provider/BaseApplication.java | 5 +- .../elimu/content_provider/MainActivity.java | 2 +- .../language/LanguageListDialogFragment.java | 6 +-- .../language/SharedPreferencesHelper.java | 20 ------- .../util/SharedPreferencesHelper.java | 51 ++++++++++++++++++ .../content_provider/util/VersionHelper.java | 52 +++++++++++++++++++ 6 files changed, 110 insertions(+), 26 deletions(-) delete mode 100644 app/src/main/java/ai/elimu/content_provider/language/SharedPreferencesHelper.java create mode 100644 app/src/main/java/ai/elimu/content_provider/util/SharedPreferencesHelper.java create mode 100644 app/src/main/java/ai/elimu/content_provider/util/VersionHelper.java diff --git a/app/src/main/java/ai/elimu/content_provider/BaseApplication.java b/app/src/main/java/ai/elimu/content_provider/BaseApplication.java index 07e5666..1f4dfcf 100644 --- a/app/src/main/java/ai/elimu/content_provider/BaseApplication.java +++ b/app/src/main/java/ai/elimu/content_provider/BaseApplication.java @@ -3,7 +3,8 @@ import android.app.Application; import android.util.Log; -import ai.elimu.content_provider.language.SharedPreferencesHelper; +import ai.elimu.content_provider.util.SharedPreferencesHelper; +import ai.elimu.content_provider.util.VersionHelper; import ai.elimu.model.v2.enums.Language; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; @@ -14,6 +15,8 @@ public class BaseApplication extends Application { public void onCreate() { Log.i(getClass().getName(), "onCreate"); super.onCreate(); + + VersionHelper.updateAppVersion(getApplicationContext()); } public Retrofit getRetrofit() { diff --git a/app/src/main/java/ai/elimu/content_provider/MainActivity.java b/app/src/main/java/ai/elimu/content_provider/MainActivity.java index dc94065..f5c6cd0 100644 --- a/app/src/main/java/ai/elimu/content_provider/MainActivity.java +++ b/app/src/main/java/ai/elimu/content_provider/MainActivity.java @@ -18,7 +18,7 @@ import com.google.android.material.navigation.NavigationView; import ai.elimu.content_provider.language.SelectLanguageActivity; -import ai.elimu.content_provider.language.SharedPreferencesHelper; +import ai.elimu.content_provider.util.SharedPreferencesHelper; import ai.elimu.model.v2.enums.Language; public class MainActivity extends AppCompatActivity { diff --git a/app/src/main/java/ai/elimu/content_provider/language/LanguageListDialogFragment.java b/app/src/main/java/ai/elimu/content_provider/language/LanguageListDialogFragment.java index 112f88c..89e4776 100644 --- a/app/src/main/java/ai/elimu/content_provider/language/LanguageListDialogFragment.java +++ b/app/src/main/java/ai/elimu/content_provider/language/LanguageListDialogFragment.java @@ -1,8 +1,6 @@ package ai.elimu.content_provider.language; -import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; @@ -18,6 +16,7 @@ import ai.elimu.content_provider.MainActivity; import ai.elimu.content_provider.R; +import ai.elimu.content_provider.util.SharedPreferencesHelper; import ai.elimu.model.v2.enums.Language; /** @@ -84,8 +83,7 @@ public void onBindViewHolder(ViewHolder holder, int position) { public void onClick(View v) { Log.i(getClass().getName(), "onClick"); Log.i(getClass().getName(), "language: " + language); - SharedPreferences sharedPreferences = getActivity().getSharedPreferences("shared_preferences", Context.MODE_PRIVATE); - sharedPreferences.edit().putString("language", language.toString()).apply(); + SharedPreferencesHelper.storeLanguage(getContext(), language); Intent mainActivityIntent = new Intent(getContext(), MainActivity.class); startActivity(mainActivityIntent); getActivity().finish(); diff --git a/app/src/main/java/ai/elimu/content_provider/language/SharedPreferencesHelper.java b/app/src/main/java/ai/elimu/content_provider/language/SharedPreferencesHelper.java deleted file mode 100644 index 53a8313..0000000 --- a/app/src/main/java/ai/elimu/content_provider/language/SharedPreferencesHelper.java +++ /dev/null @@ -1,20 +0,0 @@ -package ai.elimu.content_provider.language; - -import android.content.Context; -import android.content.SharedPreferences; -import android.text.TextUtils; - -import ai.elimu.model.v2.enums.Language; - -public class SharedPreferencesHelper { - - public static Language getLanguage(Context context) { - SharedPreferences sharedPreferences = context.getSharedPreferences("shared_preferences", Context.MODE_PRIVATE); - String languageAsString = sharedPreferences.getString("language", null); - if (TextUtils.isEmpty(languageAsString)) { - return null; - } else { - return Language.valueOf(languageAsString); - } - } -} diff --git a/app/src/main/java/ai/elimu/content_provider/util/SharedPreferencesHelper.java b/app/src/main/java/ai/elimu/content_provider/util/SharedPreferencesHelper.java new file mode 100644 index 0000000..d4411e0 --- /dev/null +++ b/app/src/main/java/ai/elimu/content_provider/util/SharedPreferencesHelper.java @@ -0,0 +1,51 @@ +package ai.elimu.content_provider.util; + +import android.content.Context; +import android.content.SharedPreferences; +import android.text.TextUtils; + +import ai.elimu.model.v2.enums.Language; + +public class SharedPreferencesHelper { + + public static final String SHARED_PREFS = "shared_prefs"; + + public static final String PREF_APP_VERSION_CODE = "pref_app_version_code"; + public static final String PREF_LANGUAGE = "pref_language"; + + public static void clearAllPreferences(Context context) { + SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE); + sharedPreferences.edit().clear().apply(); + } + + + public static void storeAppVersionCode(Context context, int appVersionCode) { + SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE); + sharedPreferences.edit().putInt(PREF_APP_VERSION_CODE, appVersionCode).apply(); + } + + public static int getAppVersionCode(Context context) { + SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE); + return sharedPreferences.getInt(PREF_APP_VERSION_CODE, 0); + } + + + public static void storeLanguage(Context context, Language language) { + SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE); + sharedPreferences.edit().putString(PREF_LANGUAGE, language.toString()).apply(); + } + + public static Language getLanguage(Context context) { + SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE); + String languageAsString = sharedPreferences.getString(PREF_LANGUAGE, null); + if (TextUtils.isEmpty(languageAsString)) { + return null; + } else { + try { + return Language.valueOf(languageAsString); + } catch (IllegalArgumentException e) { + return null; + } + } + } +} diff --git a/app/src/main/java/ai/elimu/content_provider/util/VersionHelper.java b/app/src/main/java/ai/elimu/content_provider/util/VersionHelper.java new file mode 100644 index 0000000..16bc216 --- /dev/null +++ b/app/src/main/java/ai/elimu/content_provider/util/VersionHelper.java @@ -0,0 +1,52 @@ +package ai.elimu.content_provider.util; + +import android.content.Context; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.util.Log; + +/** + * Helps detect upgrades from previously installed versions of the app. + */ +public class VersionHelper { + + public static int getAppVersionCode(Context context) { + Log.i(VersionHelper.class.getName(), "getAppVersionCode"); + + try { + PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); + return packageInfo.versionCode; + } catch (PackageManager.NameNotFoundException e) { + throw new RuntimeException("Could not get package name: " + e); + } + } + + /** + * Stores the version code of the application currently installed. And detects upgrades from previously installed + * versions. + */ + public static void updateAppVersion(Context context) { + Log.i(VersionHelper.class.getName(), "updateAppVersion"); + + // Check if the application's versionCode was upgraded + int oldVersionCode = SharedPreferencesHelper.getAppVersionCode(context); + int newVersionCode = VersionHelper.getAppVersionCode(context); + if (oldVersionCode == 0) { + SharedPreferencesHelper.storeAppVersionCode(context, newVersionCode); + oldVersionCode = newVersionCode; + } + Log.i(VersionHelper.class.getName(),"oldVersionCode: " + oldVersionCode); + Log.i(VersionHelper.class.getName(),"newVersionCode: " + newVersionCode); + + // Handle upgrade from previous version + if (oldVersionCode < newVersionCode) { + Log.i(VersionHelper.class.getName(), "Upgrading application from version " + oldVersionCode + " to " + newVersionCode + "..."); + +// if (oldVersionCode < ???) { +// ... +// } + + SharedPreferencesHelper.storeAppVersionCode(context, newVersionCode); + } + } +}