Skip to content

Commit

Permalink
Fix issue #92 in app update (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
behzodhalil authored Oct 16, 2023
2 parents 2181f62 + 4a85f32 commit 94af68d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
3 changes: 3 additions & 0 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ android {
implementation(libs.androidx.compose.foundation)
implementation(libs.firebase.auth)
implementation(libs.sentry)

implementation(libs.app.update)
implementation(libs.app.update.ktx)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.spherelabs.anypass.android

import android.app.Activity
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.FragmentActivity
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.install.model.UpdateAvailability

class InAppUpdate(
activity: FragmentActivity,
private val onUpdateFailed: () -> Unit
) {

private val appUpdateManager by lazy {
AppUpdateManagerFactory.create(activity)
}
private val updateType = AppUpdateType.IMMEDIATE

private val inAppUpdateResultLauncher = activity.registerForActivityResult(
ActivityResultContracts.StartIntentSenderForResult()
) { result ->
if (result.resultCode != Activity.RESULT_OK)
onUpdateFailed()
}

fun checkForUpdate() {
appUpdateManager.appUpdateInfo.addOnSuccessListener { info ->
val isUpdateAvailable = info.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
val isUpdateAllowed = info.isUpdateTypeAllowed(updateType)

if (isUpdateAvailable && isUpdateAllowed) {
try {
appUpdateManager.startUpdateFlowForResult(
info,
inAppUpdateResultLauncher,
AppUpdateOptions.newBuilder(updateType).build()
)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}

fun onResume() {
appUpdateManager.appUpdateInfo.addOnSuccessListener { info ->
if (info.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
appUpdateManager.startUpdateFlowForResult(
info,
inAppUpdateResultLauncher,
AppUpdateOptions.newBuilder(updateType).build()
)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import androidx.fragment.app.FragmentActivity
import io.spherelabs.anypass.MainView

class MainActivity : FragmentActivity() {

private val inAppUpdate = InAppUpdate(activity = this, onUpdateFailed = { finish() })

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

inAppUpdate.checkForUpdate()
setContent {
MaterialTheme() {
Surface(
Expand All @@ -23,4 +28,10 @@ class MainActivity : FragmentActivity() {

}
}

override fun onResume() {
super.onResume()
inAppUpdate.onResume()
}

}
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ encoding-base32 = "2.0.0"
buffer = "1.3.6"
konsist = "0.13.0"
spotify-ruler = "1.4.0"
app-update = "2.1.0"

[libraries]
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
Expand Down Expand Up @@ -106,6 +107,8 @@ crypto-macs-hmac-sha2 = { module = "org.kotlincrypto.macs:hmac-sha2", version.re
encoding-base32 = { module = "io.matthewnelson.encoding:base32", version.ref = "encoding-base32" }
buffer = { module = "com.ditchoom:buffer", version.ref = "buffer" }
plugin-spotify-ruler = { module = "com.spotify.ruler:ruler-gradle-plugin", version.ref = "spotify-ruler" }
app-update = { group = "com.google.android.play", name = "app-update", version.ref = "app-update"}
app-update-ktx = { group = "com.google.android.play", name = "app-update-ktx", version.ref = "app-update"}

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down

0 comments on commit 94af68d

Please # to comment.