Skip to content

Commit

Permalink
Add support for in app update
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Sep 21, 2023
1 parent 31f8709 commit 6a9caa5
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 15 deletions.
13 changes: 2 additions & 11 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies {
implementation(libs.google.play)
implementation(libs.google.play.ktx)
implementation(compose.material3)
implementation(compose.materialIconsExtended)
implementation(libs.kotlinx.coroutines.play.services)
}

Expand Down
4 changes: 3 additions & 1 deletion app/android/src/main/java/AppActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class AppActivity : AppCompatActivity() {
context.resetApi()
ProvideContext(context) {
ProvideImageLoader {
TonbrettApp()
UpdateAwareAppScope(activity = this) {
TonbrettApp()
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion app/android/src/main/java/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class MainActivity : AppCompatActivity() {
if (context.getTokenOrNull() == null) {
setContent {
TonbrettAuthorizationScreenTheme {
AuthorizationScreen()
UpdateAwareAppScope(activity = this) {
AuthorizationScreen()
}
}
}
} else {
Expand Down
136 changes: 136 additions & 0 deletions app/android/src/main/java/UpdateAwareAppScope.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
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.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.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import com.google.android.play.core.appupdate.AppUpdateInfo
import com.google.android.play.core.appupdate.AppUpdateManagerFactory
import com.google.android.play.core.appupdate.AppUpdateOptions
import com.google.android.play.core.install.InstallStateUpdatedListener
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

@Composable
fun UpdateAwareAppScope(activity: Activity, content: @Composable () -> Unit) {
val context = LocalContext.current.applicationContext
val scope = rememberCoroutineScope()
val appUpdateManager =
remember(context) { AppUpdateManagerFactory.create(context.applicationContext) }
var updateInfo by remember(appUpdateManager) { mutableStateOf<AppUpdateInfo?>(null) }
var progress by remember(updateInfo) { mutableStateOf<Double?>(null) }

val progressListener = remember {
InstallStateUpdatedListener {
when (it.installStatus()) {
InstallStatus.DOWNLOADING -> {
progress =
it.bytesDownloaded().toDouble() / it.totalBytesToDownload().toDouble()
}

InstallStatus.DOWNLOADED -> {
progress = 2.0
}

else -> {}
}
}
}

SideEffect { appUpdateManager.registerListener(progressListener) }

if (updateInfo == null) {
LaunchedEffect(appUpdateManager) {
try {
updateInfo = appUpdateManager.appUpdateInfo.await()
} catch (e: Throwable) {
Log.w("Tonbrett", "Could not load Update info", e)
}
}
}


Box(Modifier.fillMaxSize()) {
content()

val currentUpdateInfo = updateInfo
if(currentUpdateInfo != null) {
Column(
verticalArrangement = Arrangement.Bottom,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.fillMaxSize()
.padding(vertical = 10.dp)
.zIndex(10f)
) {
Row(
modifier = Modifier
.padding(vertical = 7.dp)
.background(MaterialTheme.colorScheme.onSecondary, RoundedCornerShape(50.dp))
) {
val currentProgress = progress ?: -1.0

@Composable
fun Info(text: String) {
Text(
text,
style = MaterialTheme.typography.headlineSmall,
modifier = Modifier.padding(horizontal = 15.dp, vertical = 10.dp)
)

}

if (currentProgress < 0) {
Button(onClick = { scope.launch {
appUpdateManager.startUpdateFlow(
currentUpdateInfo,
activity,
AppUpdateOptions.defaultOptions(AppUpdateType.FLEXIBLE)
)
} }) {
Info("Update Available")
}
} else if (currentProgress in 0.0..1.0) {
Info("Updating")
} else {
Button(onClick = {
scope.launch {
appUpdateManager.completeUpdate().await()
}
}) {
Icon(Icons.Default.OpenInNew, null)
Info("Update Available")
}
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {

allprojects {
group = "dev.schlaubi.tonbrett"
version = "1.14.1"
version = "1.14.6"

repositories {
mavenCentral()
Expand Down

0 comments on commit 6a9caa5

Please sign in to comment.