Skip to content

Instantly share code, notes, and snippets.

@StephenVinouze
Created September 8, 2020 15:35
Show Gist options
  • Save StephenVinouze/c2a1f33e315e653c7cd7987648eee350 to your computer and use it in GitHub Desktop.
Save StephenVinouze/c2a1f33e315e653c7cd7987648eee350 to your computer and use it in GitHub Desktop.
@Singleton
class UpdateDetector @Inject constructor(private val updatePreferences: UpdatePreferences) {
/**
* Store in RAM version so that we can directly save new version in preferences while using previous version in same session (Singleton)
*/
private var version = updatePreferences.version
/**
* Save current version into preferences. Must be called at app launch
*/
fun updateVersion(currentVersion: String) {
updatePreferences.version = currentVersion
}
/**
* Check if the version in RAM exists and is lower than target version
*/
fun doOnUpdate(targetVersion: String? = null, call: ((previousVersion: String?, newVersion: String) -> Unit)? = null) {
val previousVersion = version
val newVersion = updatePreferences.version
if (previousVersion != null && newVersion != null &&
Version(previousVersion).isLowerThan(targetVersion) &&
Version(newVersion).isAtLeast(targetVersion)) {
call?.invoke(previousVersion, newVersion)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment