Created
September 8, 2020 15:35
-
-
Save StephenVinouze/c2a1f33e315e653c7cd7987648eee350 to your computer and use it in GitHub Desktop.
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
@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