Skip to content

Commit

Permalink
support app update
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 11, 2024
1 parent 6fc42fa commit 3aaf365
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ kotlin {

implementation(libs.translate)
implementation(libs.instantapps)
implementation(libs.appupdate)
implementation(libs.appupdate.kotlin)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import com.arkivanov.essenty.lifecycle.Lifecycle
import com.arkivanov.essenty.lifecycle.LifecycleOwner
import com.arkivanov.essenty.lifecycle.essentyLifecycle
import com.arkivanov.essenty.statekeeper.stateKeeper
import com.google.android.play.core.appupdate.AppUpdateManagerFactory
import com.google.android.play.core.appupdate.AppUpdateOptions
import com.google.android.play.core.install.model.AppUpdateType
import com.google.android.play.core.ktx.requestAppUpdateInfo
import dev.datlag.aniflow.other.DomainVerifier
import dev.datlag.aniflow.other.UpdateManager
import dev.datlag.aniflow.other.UserHelper
import dev.datlag.aniflow.ui.navigation.RootComponent
import dev.datlag.tooling.compose.launchIO
Expand Down Expand Up @@ -54,6 +59,9 @@ class MainActivity : AppCompatActivity() {
)

DomainVerifier.verify(this)
UpdateManager.checkForUpdates(this) { manager, info, type ->
manager.startUpdateFlow(info, this, AppUpdateOptions.defaultOptions(type))
}

setContent {
CompositionLocalProvider(
Expand Down Expand Up @@ -107,6 +115,9 @@ class MainActivity : AppCompatActivity() {
super.onResume()

DomainVerifier.verify(this)
UpdateManager.checkResume(this) { manager, info ->
manager.startUpdateFlow(info, this, AppUpdateOptions.defaultOptions(AppUpdateType.IMMEDIATE))
}
}

override fun onPause() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dev.datlag.aniflow.other

import android.content.Context
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.install.model.AppUpdateType
import com.google.android.play.core.install.model.UpdateAvailability

data object UpdateManager {

fun checkForUpdates(context: Context, onUpdateAvailable: (AppUpdateManager, AppUpdateInfo, updateType: Int) -> Unit) {
val manager = AppUpdateManagerFactory.create(context)

manager.appUpdateInfo.addOnSuccessListener { info ->
val updateAvailable = info.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE

if (updateAvailable) {
val immediate = info.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)

if (immediate) {
onUpdateAvailable(manager, info, AppUpdateType.IMMEDIATE)
}
}
}
}

fun checkResume(context: Context, onUpdateResume: (AppUpdateManager, AppUpdateInfo) -> Unit) {
val manager = AppUpdateManagerFactory.create(context)

manager.appUpdateInfo.addOnSuccessListener { info ->
if (info.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
onUpdateResume(manager, info)
}
}
}
}
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ android-core = "1.13.1"
android-credentials = "1.3.0-alpha02"
apollo = "4.0.0-beta.6"
appcompat = "1.6.1"
appupdate = "2.1.0"
atomicfu = "0.24.0"
coil = "3.0.0-alpha06"
compose = "1.6.2"
Expand Down Expand Up @@ -60,6 +61,8 @@ apollo = { group = "com.apollographql.apollo3", name = "apollo-runtime", version
apollo-cache = { group = "com.apollographql.apollo3", name = "apollo-normalized-cache", version.ref = "apollo" }
apollo-cache-sql = { group = "com.apollographql.apollo3", name = "apollo-normalized-cache-sqlite", version.ref = "apollo" }
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
appupdate = { group = "com.google.android.play", name = "app-update", version.ref = "appupdate" }
appupdate-kotlin = { group = "com.google.android.play", name = "app-update-ktx", version.ref = "appupdate" }
atomicfu = { group = "org.jetbrains.kotlinx", name = "atomicfu-gradle-plugin", version.ref = "atomicfu" }
coil = { group = "io.coil-kt.coil3", name = "coil", version.ref = "coil" }
coil-network = { group = "io.coil-kt.coil3", name = "coil-network-ktor", version.ref = "coil" }
Expand Down

0 comments on commit 3aaf365

Please sign in to comment.