Skip to content

Commit

Permalink
Add onUpdateInstalled
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Dec 5, 2023
1 parent 309bb3b commit 34de6dd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 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
19 changes: 12 additions & 7 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,16 +66,17 @@ 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(
appId: String,
versionCode: Int,
inAppResultLauncher: ActivityResultLauncher<IntentSenderRequest>?,
onFdroidResult: (updateIsAvailable: Boolean) -> Unit,
onFDroidResult: (updateIsAvailable: Boolean) -> Unit,
) {
appUpdateManager.appUpdateInfo.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
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 34de6dd

Please sign in to comment.