Skip to content

Commit

Permalink
Improve in app update
Browse files Browse the repository at this point in the history
- Add translations
- Handle status PENDING, CANCELLED and FAILED
  • Loading branch information
DRSchlaubi committed Sep 22, 2023
1 parent 9d7f342 commit 1c4b557
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
34 changes: 16 additions & 18 deletions app/android/src/main/java/UpdateAwareAppScope.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,19 @@ package dev.schlaubi.tonbrett.app.android
import android.app.Activity
import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.OpenInNew
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import com.google.android.play.core.appupdate.AppUpdateInfo
Expand All @@ -37,6 +26,7 @@ import com.google.android.play.core.install.model.AppUpdateType
import com.google.android.play.core.install.model.InstallStatus
import kotlinx.coroutines.launch
import kotlinx.coroutines.tasks.await
import java.text.NumberFormat

@Composable
fun UpdateAwareAppScope(activity: Activity, content: @Composable () -> Unit) {
Expand All @@ -50,6 +40,7 @@ fun UpdateAwareAppScope(activity: Activity, content: @Composable () -> Unit) {
val progressListener = remember {
InstallStateUpdatedListener {
when (it.installStatus()) {
InstallStatus.PENDING -> progress = -100.0
InstallStatus.DOWNLOADING -> {
progress =
it.bytesDownloaded().toDouble() / it.totalBytesToDownload().toDouble()
Expand All @@ -59,6 +50,11 @@ fun UpdateAwareAppScope(activity: Activity, content: @Composable () -> Unit) {
progress = 2.0
}

InstallStatus.FAILED, InstallStatus.CANCELED -> {
progress = null
updateInfo = null
}

else -> {}
}
}
Expand Down Expand Up @@ -107,26 +103,28 @@ fun UpdateAwareAppScope(activity: Activity, content: @Composable () -> Unit) {

}

if (currentProgress < 0) {
if (currentProgress == -100.0) {
Info(stringResource(R.string.update_pending))
} else if (currentProgress < 0) {
Button(onClick = { scope.launch {
appUpdateManager.startUpdateFlow(
currentUpdateInfo,
activity,
AppUpdateOptions.defaultOptions(AppUpdateType.FLEXIBLE)
)
} }) {
Info("Update Available")
Info(stringResource(R.string.update_available))
}
} else if (currentProgress in 0.0..1.0) {
Info("Updating")
Info(stringResource(R.string.update_downloading, NumberFormat.getPercentInstance().format(currentProgress)))
} else {
Button(onClick = {
scope.launch {
appUpdateManager.completeUpdate().await()
}
}) {
Icon(Icons.Default.OpenInNew, null)
Info("Update Available")
Info(stringResource(R.string.update_download_done))
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions app/android/src/main/res/values-de-rDE/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="retry">Erneut versuchen</string>
<string name="update_available">Update verfügbar</string>
<string name="update_pending">Update begonnen</string>
<string name="update_downloading">Update wird hertuntergeladen: %s</string>
<string name="update_download_done">Update Installieren</string>
</resources>
4 changes: 4 additions & 0 deletions app/android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<resources>
<string name="retry">Retry</string>
<string name="update_available">Update available</string>
<string name="update_pending">Update queued</string>
<string name="update_downloading">Downloading: %s</string>
<string name="update_download_done">Install Update</string>
</resources>

0 comments on commit 1c4b557

Please sign in to comment.