Skip to content

Commit

Permalink
Clean StoresUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Feb 5, 2024
1 parent d65aeaa commit 8f88274
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 149 deletions.
35 changes: 0 additions & 35 deletions Stores/src/main/java/StoresUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
114 changes: 0 additions & 114 deletions Stores/src/standard/java/com.infomaniak.lib.stores/StoreUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<IntentSenderRequest>
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<IntentSenderRequest>,
) = 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 {
Expand Down

0 comments on commit 8f88274

Please sign in to comment.