Skip to content
This repository has been archived by the owner on Dec 2, 2023. It is now read-only.

Commit

Permalink
Overwrite old configuration when new version installs
Browse files Browse the repository at this point in the history
  • Loading branch information
autoreleasefool committed Sep 3, 2018
1 parent c31e72a commit b12f089
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/containers/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { connect } from 'react-redux';

// Imports
import AppHeader from '../components/AppHeader';
import DeviceInfo from 'react-native-device-info';
import TabView from './TabView';
import * as Analytics from '../util/Analytics';
import * as Configuration from '../util/Configuration';
Expand Down Expand Up @@ -129,6 +130,13 @@ class Main extends React.PureComponent<Props, State> {
*/
async _checkConfiguration(fallbackToUpdate: boolean): Promise<void> {
try {
const previousVersion = await Preferences.getPreviousAppVersion(AsyncStorage);
const currentVersion = parseInt(DeviceInfo.getBuildNumber());
if (previousVersion < currentVersion) {
await Configuration.setupDefaultConfiguration(Platform.OS);
}

Preferences.setPreviousAppVersion(AsyncStorage, currentVersion);
await Configuration.init();
const university = await Configuration.getConfig('/university.json');
this.props.setUniversity(university);
Expand Down
29 changes: 29 additions & 0 deletions src/util/Preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const PREFER_SHORTEST_ROUTE = 'app_pref_shortest_route';
const PREFERRED_TIME_FORMAT = 'app_time_format';
// Represents the user's preference to view their schedule by week or by course
const PREFER_BY_COURSE = 'app_by_course';
// Represents the version of the app the user had installed the last time they opened it
const PREVIOUS_APP_VERSION = 'app_previous_version';

/**
* Retrieves the value of a key from AsyncStorage.
Expand Down Expand Up @@ -220,3 +222,30 @@ export function setPreferScheduleByCourse(asyncStorage: AsyncStorageStatic, pref
Analytics.setPreference(PREFER_BY_COURSE, prefer.toString());
}
}

/**
* Gets the version of the app that was installed when the user last opened the app.
*
* @param {AsyncStorageStatic} asyncStorage instance of React Native AsyncStorage
* @returns {number} the version code of the app the last time it was opened
*/
export async function getPreviousAppVersion(asyncStorage: AsyncStorageStatic): Promise<number> {
const value = await retrieveFromAsyncStorage(asyncStorage, PREVIOUS_APP_VERSION);

return (value == undefined)
? 0
: parseInt(value);
}

/**
* Updates the version of the app.
*
* @param {AsyncStorageStatic} asyncStorage instance of React Native AsyncStorage
* @param {boolean} versionCode version of the app the user is using
*/
export function setPreviousAppVersion(asyncStorage: AsyncStorageStatic, versionCode: any): void {
if (typeof (versionCode) === 'number' && versionCode >= 0) {
asyncStorage.setItem(PREVIOUS_APP_VERSION, versionCode.toString());
Analytics.setPreference(PREVIOUS_APP_VERSION, versionCode.toString());
}
}

0 comments on commit b12f089

Please sign in to comment.