Skip to content

Commit

Permalink
Add onUpdateInstalled
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Dec 6, 2023
1 parent 0b0fade commit af6dc75
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ object StoreUtils {
//endRegion

//region In-App Update
fun initAppUpdateManager(context: Context, onInstall: () -> Unit) = Unit
fun initAppUpdateManager(context: Context, onUpdateDownloaded: () -> Unit, onUpdateInstalled: () -> Unit) = Unit

fun FragmentActivity.checkUpdateIsAvailable(
appId: String,
Expand Down
17 changes: 11 additions & 6 deletions Stores/src/standard/java/com.infomaniak.lib.stores/StoreUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ object StoreUtils {
private const val UPDATE_TYPE = AppUpdateType.FLEXIBLE

private lateinit var appUpdateManager: AppUpdateManager
private lateinit var onInstallDownloaded: () -> Unit
private lateinit var onUpdateDownloaded: () -> Unit
private lateinit var onUpdateInstalled: () -> Unit

// Create a listener to track request state updates.
private val installStateUpdatedListener by lazy {
InstallStateUpdatedListener { state ->
when (state.installStatus()) {
InstallStatus.DOWNLOADED -> onInstallDownloaded()
InstallStatus.INSTALLED -> unregisterAppUpdateListener()
InstallStatus.DOWNLOADED -> onUpdateDownloaded()
InstallStatus.INSTALLED -> {
onUpdateInstalled()
unregisterAppUpdateListener()
}
else -> Unit
}
}
Expand All @@ -62,9 +66,10 @@ object StoreUtils {
//endRegion

//region In-App Update
fun initAppUpdateManager(context: Context, onInstall: () -> Unit) {
fun initAppUpdateManager(context: Context, onUpdateDownloaded: () -> Unit, onUpdateInstalled: () -> Unit) {
appUpdateManager = AppUpdateManagerFactory.create(context)
onInstallDownloaded = onInstall
this.onUpdateDownloaded = onUpdateDownloaded
this.onUpdateInstalled = onUpdateInstalled
}

fun FragmentActivity.checkUpdateIsAvailable(
Expand All @@ -87,7 +92,7 @@ object StoreUtils {
appUpdateInfo.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
// If the update is downloaded but not installed, notify the user to complete the update.
onInstallDownloaded.invoke()
onUpdateDownloaded.invoke()
}
}
}
Expand Down

0 comments on commit af6dc75

Please sign in to comment.