-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Context We need a fine control of the distributed app versions. - If a major bug is released, we may need to force users to update to a newer version. - Currently the app does not have a domain to its APIs, we're using a CloudFront provided url. In the future I may change and I need everyone to update to the latest version. ## Code - Implementing Play Store InApp Update ## Additional notes ![image](https://user-images.githubusercontent.com/38049362/109011124-58f84a80-768f-11eb-874a-9e587c4da641.png)
- Loading branch information
Showing
36 changed files
with
598 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
apply from: "$rootDir/project-config/feature-complete-build.gradle" | ||
apply plugin: 'kotlin-parcelize' | ||
|
||
dependencies { | ||
implementation AndroidLibConfig.playCore | ||
implementation AndroidLibConfig.playCoreExtensions | ||
|
||
implementation project(ProjectConfig.configuration) | ||
implementation project(ProjectConfig.monitoring) | ||
implementation project(ProjectConfig.analytics) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="br.com.recipebook.inappupdate"> | ||
</manifest> |
102 changes: 102 additions & 0 deletions
102
.../in-app-update/src/main/java/br/com/recipebook/inappupdate/InAppUpdateHeadlessFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package br.com.recipebook.inappupdate | ||
|
||
import android.content.Intent | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.fragment.app.Fragment | ||
import br.com.recipebook.utilityandroid.view.putSafeArgs | ||
import br.com.recipebook.utilityandroid.view.safeArgs | ||
import com.google.android.play.core.appupdate.AppUpdateManagerFactory | ||
import com.google.android.play.core.install.model.UpdateAvailability | ||
import com.google.android.play.core.ktx.startUpdateFlowForResult | ||
import kotlinx.coroutines.CompletableDeferred | ||
|
||
internal class InAppUpdateHeadlessFragment : Fragment() { | ||
private val safeArgs by safeArgs<InAppUpdateSafeArgs>() | ||
|
||
lateinit var completableDeferred: CompletableDeferred<InAppUpdateResult> | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
checkInAppUpdate() | ||
} | ||
|
||
private fun checkInAppUpdate() { | ||
// Creates instance of the manager. | ||
val appUpdateManager = AppUpdateManagerFactory.create(requireContext()) | ||
|
||
// Returns an intent object that you use to check for an update. | ||
val appUpdateInfoTask = appUpdateManager.appUpdateInfo | ||
|
||
// Checks that the platform will allow the specified type of update. | ||
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo -> | ||
if (isUpdateAvailable(appUpdateInfo.updateAvailability()) && | ||
appUpdateInfo.isUpdateTypeAllowed(safeArgs.appUpdateType) | ||
) { | ||
// Request the update. | ||
appUpdateManager.startUpdateFlowForResult( | ||
appUpdateInfo, | ||
safeArgs.appUpdateType, | ||
this, | ||
safeArgs.requestCode | ||
) | ||
} else { | ||
completeAndRemoveFragment(InAppUpdateResult.UpdateNotAvailable) | ||
} | ||
} | ||
} | ||
|
||
private fun isUpdateAvailable(updateAvailability: Int): Boolean { | ||
return updateAvailability == UpdateAvailability.UPDATE_AVAILABLE || | ||
updateAvailability == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS | ||
} | ||
|
||
override fun onActivityResult( | ||
requestCode: Int, | ||
resultCode: Int, | ||
data: Intent? | ||
) { | ||
super.onActivityResult(requestCode, resultCode, data) | ||
if (requestCode == safeArgs.requestCode) { | ||
if (resultCode != AppCompatActivity.RESULT_OK) { | ||
// If the update is cancelled or fails, | ||
// you can request to start the update again. | ||
completeAndRemoveFragment(InAppUpdateResult.UpdateFailed) | ||
} else { | ||
completeAndRemoveFragment(InAppUpdateResult.UpdateCompleted) | ||
} | ||
} | ||
} | ||
|
||
private fun completeAndRemoveFragment(result: InAppUpdateResult) { | ||
if (::completableDeferred.isInitialized) { | ||
completableDeferred.complete(result) | ||
} | ||
removeThisFragment() | ||
} | ||
|
||
private fun removeThisFragment() { | ||
parentFragmentManager.beginTransaction() | ||
.remove(this) | ||
.commitAllowingStateLoss() | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
if (::completableDeferred.isInitialized && completableDeferred.isActive) { | ||
completableDeferred.cancel() | ||
} | ||
} | ||
|
||
companion object { | ||
fun newInstance( | ||
appUpdateType: Int, | ||
requestCode: Int | ||
) = InAppUpdateHeadlessFragment() | ||
.putSafeArgs( | ||
InAppUpdateSafeArgs( | ||
appUpdateType = appUpdateType, | ||
requestCode = requestCode, | ||
) | ||
) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
features/in-app-update/src/main/java/br/com/recipebook/inappupdate/InAppUpdateManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package br.com.recipebook.inappupdate | ||
|
||
import androidx.fragment.app.FragmentActivity | ||
import kotlinx.coroutines.CompletableDeferred | ||
|
||
private const val TAG = "InAppUpdateManager" | ||
|
||
suspend fun FragmentActivity.requestInAppUpdate( | ||
appUpdateType: Int, | ||
requestCode: Int | ||
): InAppUpdateResult { | ||
val fragment = supportFragmentManager.findFragmentByTag(TAG) as? InAppUpdateHeadlessFragment | ||
return if (fragment == null) { | ||
val inAppUpdate = InAppUpdateHeadlessFragment.newInstance( | ||
appUpdateType = appUpdateType, | ||
requestCode = requestCode | ||
).apply { | ||
completableDeferred = CompletableDeferred() | ||
} | ||
supportFragmentManager.beginTransaction().add(inAppUpdate, TAG).commitAllowingStateLoss() | ||
inAppUpdate.completableDeferred.await() | ||
} else { | ||
fragment.completableDeferred.await() | ||
} | ||
} | ||
|
||
sealed class InAppUpdateResult { | ||
object UpdateCompleted : InAppUpdateResult() | ||
object UpdateNotAvailable : InAppUpdateResult() | ||
object UpdateFailed : InAppUpdateResult() | ||
} |
10 changes: 10 additions & 0 deletions
10
features/in-app-update/src/main/java/br/com/recipebook/inappupdate/InAppUpdateSafeArgs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package br.com.recipebook.inappupdate | ||
|
||
import android.os.Parcelable | ||
import kotlinx.android.parcel.Parcelize | ||
|
||
@Parcelize | ||
data class InAppUpdateSafeArgs( | ||
val appUpdateType: Int, | ||
val requestCode: Int, | ||
) : Parcelable |
17 changes: 17 additions & 0 deletions
17
...s/in-app-update/src/main/java/br/com/recipebook/inappupdate/analytics/InAppUpdateEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package br.com.recipebook.inappupdate.analytics | ||
|
||
import br.com.recipebook.analytics.Event | ||
|
||
data class InAppUpdateEvent( | ||
private val shouldUpdate: Boolean, | ||
private val updateStatus: String?, | ||
private val currentVersionCode: Int, | ||
) : Event { | ||
override val id: String = "in_app_update" | ||
override val properties: Map<String, Any?> | ||
get() = mapOf( | ||
"should_update" to shouldUpdate, | ||
"update_status" to updateStatus, | ||
"current_version_code" to currentVersionCode, | ||
) | ||
} |
31 changes: 31 additions & 0 deletions
31
features/in-app-update/src/main/java/br/com/recipebook/inappupdate/di/InAppUpdateModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package br.com.recipebook.inappupdate.di | ||
|
||
import br.com.recipebook.inappupdate.domain.CheckInAppUpdate | ||
import br.com.recipebook.inappupdate.domain.CheckInAppUpdateUseCase | ||
import br.com.recipebook.inappupdate.domain.InAppUpdater | ||
import br.com.recipebook.inappupdate.view.InAppUpdaterImpl | ||
import org.koin.dsl.module | ||
|
||
val inAppUpdateDomainModule = module { | ||
factory<CheckInAppUpdateUseCase> { | ||
CheckInAppUpdate( | ||
buildConfiguration = get(), | ||
configurationRepository = get(), | ||
inAppUpdater = get(), | ||
analytics = get(), | ||
) | ||
} | ||
} | ||
|
||
val inAppUpdateViewModule = module { | ||
factory<InAppUpdater> { | ||
InAppUpdaterImpl( | ||
activityProvider = get(), | ||
) | ||
} | ||
} | ||
|
||
val inAppUpdateModules = listOf( | ||
inAppUpdateDomainModule, | ||
inAppUpdateViewModule | ||
) |
65 changes: 65 additions & 0 deletions
65
...-app-update/src/main/java/br/com/recipebook/inappupdate/domain/CheckInAppUpdateUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package br.com.recipebook.inappupdate.domain | ||
|
||
import br.com.recipebook.analytics.Analytics | ||
import br.com.recipebook.di.BuildConfiguration | ||
import br.com.recipebook.domain.ConfigurationRepository | ||
import br.com.recipebook.domain.model.AppUpdateInfoModel | ||
import br.com.recipebook.inappupdate.InAppUpdateResult | ||
import br.com.recipebook.inappupdate.analytics.InAppUpdateEvent | ||
import br.com.recipebook.monitoring.crashreport.Breadcrumb | ||
import br.com.recipebook.monitoring.crashreport.Monitoring | ||
import br.com.recipebook.utilitykotlin.ResultWrapper | ||
|
||
interface CheckInAppUpdateUseCase { | ||
suspend operator fun invoke(): Boolean | ||
} | ||
|
||
internal class CheckInAppUpdate( | ||
private val buildConfiguration: BuildConfiguration, | ||
private val configurationRepository: ConfigurationRepository, | ||
private val inAppUpdater: InAppUpdater, | ||
private val analytics: Analytics, | ||
) : CheckInAppUpdateUseCase { | ||
override suspend fun invoke(): Boolean { | ||
val shouldUpdate = when (val result = configurationRepository.getAppUpdateInfo()) { | ||
is ResultWrapper.Success -> shouldAskUpdate(result.data) | ||
is ResultWrapper.Failure -> true | ||
} | ||
|
||
return if (shouldUpdate) { | ||
Monitoring.addBreadcrumb(Breadcrumb.StartingInAppUpdate) | ||
val result = inAppUpdater() | ||
sendEvent(shouldUpdate, result) | ||
result !is InAppUpdateResult.UpdateFailed | ||
} else { | ||
sendEvent(shouldUpdate, null) | ||
true | ||
} | ||
} | ||
|
||
private fun shouldAskUpdate(info: AppUpdateInfoModel): Boolean { | ||
return buildConfiguration.appInfo.versionCode.let { currentVersionCode -> | ||
(currentVersionCode < info.minimumVersionCode ?: 0) || | ||
info.excludedVersionCodes.contains(currentVersionCode) | ||
} | ||
} | ||
|
||
private fun sendEvent( | ||
shouldUpdate: Boolean, | ||
updateStatus: InAppUpdateResult? | ||
) { | ||
val updateResult = when (updateStatus) { | ||
null -> null | ||
is InAppUpdateResult.UpdateNotAvailable -> "Not available" | ||
is InAppUpdateResult.UpdateCompleted -> "Completed" | ||
is InAppUpdateResult.UpdateFailed -> "Failed" | ||
} | ||
analytics.sendEvent( | ||
InAppUpdateEvent( | ||
shouldUpdate = shouldUpdate, | ||
updateStatus = updateResult, | ||
currentVersionCode = buildConfiguration.appInfo.versionCode | ||
) | ||
) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
features/in-app-update/src/main/java/br/com/recipebook/inappupdate/domain/InAppUpdater.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package br.com.recipebook.inappupdate.domain | ||
|
||
import br.com.recipebook.inappupdate.InAppUpdateResult | ||
|
||
interface InAppUpdater { | ||
suspend operator fun invoke(): InAppUpdateResult | ||
} |
21 changes: 21 additions & 0 deletions
21
features/in-app-update/src/main/java/br/com/recipebook/inappupdate/view/InAppUpdaterImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package br.com.recipebook.inappupdate.view | ||
|
||
import androidx.fragment.app.FragmentActivity | ||
import br.com.recipebook.inappupdate.InAppUpdateResult | ||
import br.com.recipebook.inappupdate.domain.InAppUpdater | ||
import br.com.recipebook.inappupdate.requestInAppUpdate | ||
import br.com.recipebook.view.ActivityProvider | ||
import com.google.android.play.core.install.model.AppUpdateType | ||
|
||
private const val REQUEST_CODE = 1292 | ||
|
||
internal class InAppUpdaterImpl( | ||
private val activityProvider: ActivityProvider, | ||
) : InAppUpdater { | ||
override suspend fun invoke(): InAppUpdateResult { | ||
return (activityProvider.activeActivity as? FragmentActivity)?.requestInAppUpdate( | ||
appUpdateType = AppUpdateType.IMMEDIATE, | ||
requestCode = REQUEST_CODE | ||
) ?: InAppUpdateResult.UpdateFailed | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
.../src/main/java/br/com/recipebook/recipecollection/presentation/RecipeCollectionCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package br.com.recipebook.recipecollection.presentation | ||
|
||
sealed class RecipeCollectionCommand { | ||
object FinishApp : RecipeCollectionCommand() | ||
data class OpenRecipeDetail(val recipeId: String, val title: String?) : RecipeCollectionCommand() | ||
} |
Oops, something went wrong.