Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add version upgrade functionality #132

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,6 +15,8 @@ public class BaseApplication extends Application {
public void onCreate() {
Log.i(getClass().getName(), "onCreate");
super.onCreate();

VersionHelper.updateAppVersion(getApplicationContext());
nya-elimu marked this conversation as resolved.
Show resolved Hide resolved
}

public Retrofit getRetrofit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Loading