From 8f88274472cadef7269de3cf1b5a73bafceb96a8 Mon Sep 17 00:00:00 2001 From: Fabian Devel Date: Thu, 1 Feb 2024 13:32:56 +0100 Subject: [PATCH] Clean StoresUtils --- Stores/src/main/java/StoresUtils.kt | 35 ------ .../com.infomaniak.lib.stores/StoreUtils.kt | 114 ------------------ 2 files changed, 149 deletions(-) diff --git a/Stores/src/main/java/StoresUtils.kt b/Stores/src/main/java/StoresUtils.kt index 0ddeac11..dcaffdcc 100644 --- a/Stores/src/main/java/StoresUtils.kt +++ b/Stores/src/main/java/StoresUtils.kt @@ -18,44 +18,9 @@ package com.infomaniak.lib.stores import androidx.fragment.app.FragmentActivity -import androidx.lifecycle.lifecycleScope -import com.infomaniak.lib.core.fdroidTools.FdroidApiTools -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext interface StoresUtils { - //region In-App Updates - fun FragmentActivity.initAppUpdateManager( - onUserChoice: (Boolean) -> Unit, - onUpdateDownloaded: () -> Unit, - onUpdateInstalled: () -> Unit, - ) = Unit - - fun FragmentActivity.checkUpdateIsAvailable( - appId: String, - versionCode: Int, - onFDroidResult: (updateIsAvailable: Boolean) -> Unit, - ) { - lifecycleScope.launch { - val lastVersionCode = FdroidApiTools().getLastRelease(appId) - - withContext(Dispatchers.Main) { onFDroidResult(versionCode < lastVersionCode) } - } - } - - fun checkStalledUpdate() = Unit - - fun installDownloadedUpdate( - onInstallStart: (() -> Unit)? = null, - onSuccess: (() -> Unit)? = null, - onFailure: ((Exception) -> Unit)? = null, - ) = Unit - - fun unregisterAppUpdateListener() = Unit - //endregion - //region In-App Review fun FragmentActivity.launchInAppReview() = Unit //endRegion diff --git a/Stores/src/standard/java/com.infomaniak.lib.stores/StoreUtils.kt b/Stores/src/standard/java/com.infomaniak.lib.stores/StoreUtils.kt index bd9ed784..576caa46 100644 --- a/Stores/src/standard/java/com.infomaniak.lib.stores/StoreUtils.kt +++ b/Stores/src/standard/java/com.infomaniak.lib.stores/StoreUtils.kt @@ -17,129 +17,15 @@ */ package com.infomaniak.lib.stores -import androidx.activity.result.ActivityResultLauncher -import androidx.activity.result.IntentSenderRequest -import androidx.activity.result.contract.ActivityResultContracts -import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.FragmentActivity -import com.google.android.play.core.appupdate.AppUpdateInfo -import com.google.android.play.core.appupdate.AppUpdateManager -import com.google.android.play.core.appupdate.AppUpdateManagerFactory -import com.google.android.play.core.appupdate.AppUpdateOptions -import com.google.android.play.core.install.InstallStateUpdatedListener import com.google.android.play.core.install.model.AppUpdateType -import com.google.android.play.core.install.model.InstallStatus -import com.google.android.play.core.install.model.UpdateAvailability import com.google.android.play.core.review.ReviewManagerFactory -import com.infomaniak.lib.core.utils.SentryLog object StoreUtils : StoresUtils { const val APP_UPDATE_TAG = "inAppUpdate" - const val UPDATE_TYPE = AppUpdateType.FLEXIBLE - private lateinit var appUpdateManager: AppUpdateManager - // Result of in app update's bottomSheet user choice - private lateinit var inAppUpdateResultLauncher: ActivityResultLauncher - private lateinit var localSettings: StoresLocalSettings - 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 -> { - SentryLog.d(APP_UPDATE_TAG, "OnUpdateDownloaded triggered by InstallStateUpdated listener") - onUpdateDownloaded() - } - InstallStatus.INSTALLED -> { - SentryLog.d(APP_UPDATE_TAG, "OnUpdateInstalled triggered by InstallStateUpdated listener") - onUpdateInstalled() - unregisterAppUpdateListener() - } - else -> Unit - } - } - } - - //region In-App Update - override fun FragmentActivity.initAppUpdateManager( - onUserChoice: (Boolean) -> Unit, - onUpdateDownloaded: () -> Unit, - onUpdateInstalled: () -> Unit, - ) { - appUpdateManager = AppUpdateManagerFactory.create(this) - localSettings = StoresLocalSettings.getInstance(context = this) - this@StoreUtils.onUpdateDownloaded = onUpdateDownloaded - this@StoreUtils.onUpdateInstalled = onUpdateInstalled - - inAppUpdateResultLauncher = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result -> - val isUserWantingUpdate = result.resultCode == AppCompatActivity.RESULT_OK - localSettings.isUserWantingUpdates = isUserWantingUpdate - onUserChoice(isUserWantingUpdate) - } - } - - override fun FragmentActivity.checkUpdateIsAvailable( - appId: String, - versionCode: Int, - onFDroidResult: (updateIsAvailable: Boolean) -> Unit, - ) { - SentryLog.d(APP_UPDATE_TAG, "Checking for update on GPlay") - appUpdateManager.appUpdateInfo.addOnSuccessListener { appUpdateInfo -> - if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE - && appUpdateInfo.isUpdateTypeAllowed(UPDATE_TYPE) - ) { - SentryLog.d(APP_UPDATE_TAG, "Update available on GPlay") - startUpdateFlow(appUpdateInfo, inAppUpdateResultLauncher) - } - } - } - - override fun checkStalledUpdate(): Unit = with(appUpdateManager) { - registerListener(installStateUpdatedListener) - appUpdateInfo.addOnSuccessListener { appUpdateInfo -> - if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) { - SentryLog.d(APP_UPDATE_TAG, "CheckStalledUpdate downloaded") - // If the update is downloaded but not installed, notify the user to complete the update. - onUpdateDownloaded.invoke() - } - } - } - - override fun installDownloadedUpdate( - onInstallStart: (() -> Unit)?, - onSuccess: (() -> Unit)?, - onFailure: ((Exception) -> Unit)?, - ) { - localSettings.hasAppUpdateDownloaded = false - appUpdateManager.completeUpdate() - .addOnSuccessListener { onSuccess?.invoke() } - .addOnFailureListener { - localSettings.resetUpdateSettings() - onFailure?.invoke(it) - } - } - - override fun unregisterAppUpdateListener() { - appUpdateManager.unregisterListener(installStateUpdatedListener) - } - - private fun startUpdateFlow( - appUpdateInfo: AppUpdateInfo, - downloadUpdateResultLauncher: ActivityResultLauncher, - ) = with(appUpdateManager) { - registerListener(installStateUpdatedListener) - startUpdateFlowForResult( - appUpdateInfo, - downloadUpdateResultLauncher, - AppUpdateOptions.newBuilder(UPDATE_TYPE).build(), - ) - } - //endregion - //region In-App Review override fun FragmentActivity.launchInAppReview() { ReviewManagerFactory.create(this).apply {