Skip to content

Commit

Permalink
Change the way isFirstRunEver() works
Browse files Browse the repository at this point in the history
  • Loading branch information
cketti committed Jul 10, 2021
1 parent 204d4ff commit 1a6e27a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 15 additions & 4 deletions ckchangelog-core/src/main/java/de/cketti/changelog/ChangeLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@
*/
public final class ChangeLog {
private static final String LOG_TAG = "ckChangeLog";
private static final String VERSION_KEY = "ckChangeLog_last_version_code";
private static final String FIRST_VERSION_KEY = "ckChangeLog_first_version_code";
private static final String LAST_VERSION_KEY = "ckChangeLog_last_version_code";
private static final int NO_VERSION = -1;


private final Context context;
private final SharedPreferences preferences;
private final ChangeLogProvider changeLogProvider;
private int firstVersionCode;
private int lastVersionCode;
private int currentVersionCode;
private String currentVersionName;
Expand Down Expand Up @@ -118,7 +120,8 @@ private ChangeLog(Context context, SharedPreferences preferences, ChangeLogProvi
}

private void init() {
lastVersionCode = preferences.getInt(VERSION_KEY, NO_VERSION);
firstVersionCode = preferences.getInt(FIRST_VERSION_KEY, NO_VERSION);
lastVersionCode = preferences.getInt(LAST_VERSION_KEY, NO_VERSION);

try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
Expand All @@ -129,6 +132,14 @@ private void init() {
currentVersionCode = NO_VERSION;
Log.e(LOG_TAG, "Could not get version information from manifest!", e);
}

if (firstVersionCode == NO_VERSION) {
firstVersionCode = lastVersionCode != NO_VERSION ? lastVersionCode : currentVersionCode;
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(FIRST_VERSION_KEY, firstVersionCode);
editor.putInt(LAST_VERSION_KEY, firstVersionCode);
editor.apply();
}
}

/**
Expand Down Expand Up @@ -183,7 +194,7 @@ public boolean isFirstRun() {
* Also {@code true} if your app was uninstalled and installed again.
*/
public boolean isFirstRunEver() {
return lastVersionCode == NO_VERSION;
return firstVersionCode == currentVersionCode;
}

/**
Expand All @@ -198,7 +209,7 @@ public void writeCurrentVersion() {
lastVersionCode = currentVersionCode;

SharedPreferences.Editor editor = preferences.edit();
editor.putInt(VERSION_KEY, currentVersionCode);
editor.putInt(LAST_VERSION_KEY, currentVersionCode);
editor.apply();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.content.pm.PackageManager;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
Expand Down Expand Up @@ -124,6 +125,7 @@ public void isFirstRunEver_withLastVersionCodeUnset_shouldReturnTrue() {
}

@Test
@Ignore("Tested implementation details that have been changed")
public void isFirstRunEver_afterCallingWriteCurrentVersion_shouldReturnFalse() {
setLastVersionCode(-1);
ChangeLog changeLog = ChangeLog.newInstance(context, preferences, changeLogProvider);
Expand Down

0 comments on commit 1a6e27a

Please sign in to comment.