Skip to content

Commit

Permalink
refactor(InAppUpdate): Use new update manager
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Feb 6, 2024
1 parent e0b8eb8 commit 0cc9b1a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 67 deletions.
75 changes: 20 additions & 55 deletions app/src/main/java/com/infomaniak/drive/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,8 @@ import com.infomaniak.lib.core.utils.SnackbarUtils.showSnackbar
import com.infomaniak.lib.core.utils.UtilsUi.generateInitialsAvatarDrawable
import com.infomaniak.lib.core.utils.UtilsUi.getBackgroundColorBasedOnId
import com.infomaniak.lib.core.utils.whenResultIsOk
import com.infomaniak.lib.stores.StoreUtils.checkUpdateIsAvailable
import com.infomaniak.lib.stores.StoreUtils.initAppUpdateManager
import com.infomaniak.lib.stores.StoreUtils.installDownloadedUpdate
import com.infomaniak.lib.stores.InAppUpdateManager
import com.infomaniak.lib.stores.StoreUtils.launchInAppReview
import com.infomaniak.lib.stores.StoreUtils.unregisterAppUpdateListener
import com.infomaniak.lib.stores.StoresLocalSettings
import io.sentry.Breadcrumb
import io.sentry.Sentry
import io.sentry.SentryLevel
Expand All @@ -106,7 +102,6 @@ class MainActivity : BaseActivity() {
private val mainViewModel: MainViewModel by viewModels()
private val navigationArgs: MainActivityArgs? by lazy { intent?.extras?.let { MainActivityArgs.fromBundle(it) } }
private val uiSettings by lazy { UiSettings(this) }
private val storesLocalSettings = StoresLocalSettings.getInstance(this)
private val navController by lazy { setupNavController() }

private lateinit var downloadReceiver: DownloadReceiver
Expand Down Expand Up @@ -148,12 +143,7 @@ class MainActivity : BaseActivity() {
}
}

// private val inAppUpdateResultLauncher = registerForActivityResult(StartIntentSenderForResult()) { result ->
// val isUserWantingUpdates = result.resultCode == RESULT_OK
// uiSettings.isUserWantingUpdates = isUserWantingUpdates
// trackInAppUpdate(if (isUserWantingUpdates) "discoverNow" else "discoverLater")
// }

private val inAppUpdateManager by lazy { initAppUpdateManager() }
private var inAppUpdateSnackbar: Snackbar? = null

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -176,13 +166,7 @@ class MainActivity : BaseActivity() {

LocalBroadcastManager.getInstance(this).registerReceiver(downloadReceiver, IntentFilter(DownloadReceiver.TAG))

initAppUpdateManager()
observeAppUpdateDownload()
}

override fun onStart() {
super.onStart()
handleUpdates()
lifecycle.addObserver(inAppUpdateManager)
}

private fun getNavHostFragment() = supportFragmentManager.findFragmentById(R.id.hostFragment) as NavHostFragment
Expand Down Expand Up @@ -256,44 +240,27 @@ class MainActivity : BaseActivity() {


//region In-App Updates
private fun initAppUpdateManager() {
initAppUpdateManager(
onUpdateDownloaded = { mainViewModel.toggleAppUpdateStatus(isUpdateDownloaded = true) },
onUpdateInstalled = { mainViewModel.toggleAppUpdateStatus(isUpdateDownloaded = false) },
)
}

private fun handleUpdates() {
if (storesLocalSettings.isUserWantingUpdates || AppSettings.appLaunches % 10 == 0) {
checkUpdateIsAvailable(
appId = BuildConfig.APPLICATION_ID,
versionCode = BuildConfig.VERSION_CODE,
onFDroidResult = { updateIsAvailable ->
if (updateIsAvailable) navController.navigate(R.id.updateAvailableBottomSheetDialog)
},
)
}
}

private fun launchUpdateInstall() {
trackInAppUpdate("installUpdate")
mainViewModel.canInstallUpdate.value = false
installDownloadedUpdate(
onFailure = {
Sentry.captureException(it)
showSnackbar(title = R.string.errorUpdateInstall, anchor = getMainFab())
},
)
}

private fun observeAppUpdateDownload() {
mainViewModel.canInstallUpdate.observe(this) { isUploadDownloaded ->
private fun initAppUpdateManager() = InAppUpdateManager(
activity = this,
appId = BuildConfig.APPLICATION_ID,
versionCode = BuildConfig.VERSION_CODE,
onUserChoice = { isWantingUpdate -> trackInAppUpdate(if (isWantingUpdate) "discoverNow" else "discoverLater") },
onFDroidResult = { updateIsAvailable ->
if (updateIsAvailable) navController.navigate(R.id.updateAvailableBottomSheetDialog)
},
onInstallStart = { trackInAppUpdate("installUpdate") },
onInstallFailure = {
Sentry.captureException(it)
showSnackbar(title = R.string.errorUpdateInstall, anchor = getMainFab())
},
).also {
it.onInAppUpdateUiChange = { isUploadDownloaded ->
if (isUploadDownloaded && canDisplayInAppSnackbar()) {
inAppUpdateSnackbar = showIndefiniteSnackbar(
title = R.string.updateReadyTitle,
actionButtonTitle = R.string.updateInstallButton,
anchor = getMainFab(),
onActionClicked = ::launchUpdateInstall,
onActionClicked = it::installDownloadedUpdate,
)
} else if (!isUploadDownloaded) {
inAppUpdateSnackbar?.dismiss()
Expand Down Expand Up @@ -327,8 +294,6 @@ class MainActivity : BaseActivity() {
startContentObserverService()

handleDeletionOfUploadedPhotos()

mainViewModel.checkAppUpdateStatus()
}

override fun onPause() {
Expand Down Expand Up @@ -474,7 +439,6 @@ class MainActivity : BaseActivity() {
}

override fun onStop() {
unregisterAppUpdateListener()
super.onStop()
saveLastNavigationItemSelected()
}
Expand All @@ -485,6 +449,7 @@ class MainActivity : BaseActivity() {

override fun onDestroy() {
super.onDestroy()
lifecycle.removeObserver(inAppUpdateManager)
fileObserver.stopWatching()
LocalBroadcastManager.getInstance(this).unregisterReceiver(downloadReceiver)
}
Expand Down
10 changes: 0 additions & 10 deletions app/src/main/java/com/infomaniak/drive/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class MainViewModel(appContext: Application) : AndroidViewModel(appContext) {

val navigateFileListTo = SingleLiveEvent<File>()

val canInstallUpdate = MutableLiveData(false)
val deleteFileFromHome = SingleLiveEvent<Boolean>()
val refreshActivities = SingleLiveEvent<Boolean>()
val updateOfflineFile = SingleLiveEvent<FileId>()
Expand Down Expand Up @@ -427,15 +426,6 @@ class MainViewModel(appContext: Application) : AndroidViewModel(appContext) {
UploadFile.deleteAll(fileDeleted)
}

fun checkAppUpdateStatus() {
canInstallUpdate.value = storesLocalSettings.hasAppUpdateDownloaded
StoreUtils.checkStalledUpdate()
}

fun toggleAppUpdateStatus(isUpdateDownloaded: Boolean) {
canInstallUpdate.value = isUpdateDownloaded
}

override fun onCleared() {
realm.close()
super.onCleared()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.infomaniak.lib.stores.StoresLocalSettings

class UpdateAvailableBottomSheetDialog : InformationBottomSheetDialog() {

private val storesSettings = StoresLocalSettings.getInstance(requireContext())
private val storesSettings by lazy { StoresLocalSettings.getInstance(requireContext()) }

override fun onViewCreated(view: View, savedInstanceState: Bundle?): Unit = with(binding) {
super.onViewCreated(view, savedInstanceState)
Expand Down

0 comments on commit 0cc9b1a

Please sign in to comment.