-
Notifications
You must be signed in to change notification settings - Fork 283
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
Add version check service #653
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const APPSTORE_URL = 'itms-apps://itunes.apple.com/us/app/id1508266966'; | ||
export const PLAYSTORE_URL = 'market://details?id=org.pathcheck.covidsafepaths'; | ||
export const VERSION_URL = | ||
'https://rawcdn.githack.com/tripleblindmarket/covid-safe-paths/0008e269c355e14bb216bb6e69cf4b89e25012b2/versions.yaml'; | ||
export const YAML_MANDATORY_VERSION_KEY = 'mandatory update for less than'; | ||
export const YAML_LATEST_VERSION_KEY = 'latest version'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import Yaml from 'js-yaml'; | ||
import { Alert, Linking } from 'react-native'; | ||
import BackgroundTimer from 'react-native-background-timer'; | ||
import PushNotification from 'react-native-push-notification'; | ||
|
||
import { version as currentVersion } from '../../package.json'; | ||
import { | ||
APPSTORE_URL, | ||
PLAYSTORE_URL, | ||
VERSION_URL, | ||
YAML_LATEST_VERSION_KEY, | ||
YAML_MANDATORY_VERSION_KEY, | ||
} from '../constants/versionCheck'; | ||
import languages from '../locales/languages'; | ||
import { isPlatformiOS, isVersionGreater } from '../Util'; | ||
|
||
const POLL_INTERVAL = 24 * 60 * 60 * 1000; //one day | ||
|
||
const updateLink = isPlatformiOS() ? APPSTORE_URL : PLAYSTORE_URL; | ||
let isPollStarted = false; | ||
//make sure we show only one notification | ||
let notificationShown = false; | ||
|
||
export default class VersionCheckService { | ||
static start() { | ||
if (!isPollStarted) { | ||
this.checkUpdate(); | ||
// currenly BackgroundTimer is not used anywhere else. | ||
// But this thing should be called only once, otherwise it behaves not as expected. | ||
// It worths making a separate service. | ||
BackgroundTimer.runBackgroundTimer( | ||
() => this.checkUpdate(), | ||
POLL_INTERVAL, | ||
); | ||
isPollStarted = true; | ||
} | ||
} | ||
|
||
static stop() { | ||
BackgroundTimer.stopBackgroundTimer(); | ||
} | ||
|
||
static async checkUpdate() { | ||
if (notificationShown) return; | ||
let mandatoryVersion, latestVersion; | ||
try { | ||
const response = await fetch(VERSION_URL); | ||
const responseText = await response.text(); | ||
const parsedFile = Yaml.safeLoad(responseText); | ||
mandatoryVersion = parsedFile[YAML_MANDATORY_VERSION_KEY]; | ||
latestVersion = parsedFile[YAML_LATEST_VERSION_KEY]; | ||
} catch (err) { | ||
console.log(err); | ||
return; | ||
} | ||
|
||
if (!isVersionGreater(latestVersion, currentVersion)) return; | ||
|
||
const isMandatoryUpdate = isVersionGreater( | ||
mandatoryVersion, | ||
currentVersion, | ||
); | ||
this.showNotifications(isMandatoryUpdate); | ||
} | ||
|
||
static showNotifications(isMandatoryUpdate) { | ||
const updateOption = { | ||
text: languages.t('version_update.update'), | ||
onPress: () => { | ||
notificationShown = false; | ||
Linking.canOpenURL(updateLink).then( | ||
supported => supported && Linking.openURL(updateLink), | ||
err => console.log(err), | ||
); | ||
}, | ||
}; | ||
const laterOption = { | ||
text: languages.t('version_update.later'), | ||
onPress: () => { | ||
notificationShown = false; | ||
console.log('User does not want to update the app'); | ||
}, | ||
style: 'cancel', | ||
}; | ||
|
||
const alertOptions = [updateOption]; | ||
|
||
if (isMandatoryUpdate) { | ||
PushNotification.localNotification({ | ||
title: languages.t('version_update.push_notification_title'), | ||
message: languages.t('version_update.push_notification_message'), | ||
}); | ||
} else { | ||
alertOptions.push(laterOption); | ||
} | ||
|
||
notificationShown = true; | ||
Alert.alert( | ||
languages.t('version_update.alert_label'), | ||
languages.t('version_update.alert_sublabel'), | ||
alertOptions, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change to
https://rawcdn.githack.com/tripleblindmarket/covid-safe-paths/master/versions.yaml
. This will still use the CDN with longer caching, but won't include a tag or commit hash