Skip to content

Commit

Permalink
feat: ability to dismiss updates
Browse files Browse the repository at this point in the history
  • Loading branch information
aliernfrog committed Sep 9, 2024
1 parent 84d328d commit b660bb3
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.aliernfrog.lactool.data

data class ReleaseInfo(
val versionName: String,
val versionCode: Long,
val preRelease: Boolean,
val body: String,
val htmlUrl: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ fun MainScreen(
updateAvailable = mainViewModel.updateAvailable,
onCheckUpdatesRequest = { scope.launch {
mainViewModel.checkUpdates(manuallyTriggered = true)
} }
} },
onIgnoreRequest = {
mainViewModel.prefs.ignoredUpdateVersionCode.value = mainViewModel.latestVersionInfo.versionCode
}
)

LaunchedEffect(navController) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private fun SettingsRootPage(
.navigationBarsPadding()
) {
UpdateNotification(
isShown = mainViewModel.updateAvailable,
isShown = mainViewModel.showUpdateNotifications,
versionInfo = mainViewModel.latestVersionInfo,
onClick = { scope.launch {
mainViewModel.updateSheetState.show()
Expand Down
119 changes: 76 additions & 43 deletions app/src/main/java/com/aliernfrog/lactool/ui/sheet/UpdateSheet.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.aliernfrog.lactool.ui.sheet

import androidx.compose.animation.AnimatedContent
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Update
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
Expand All @@ -20,24 +22,26 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.SheetState
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aliernfrog.lactool.R
import com.aliernfrog.lactool.data.ReleaseInfo
import com.aliernfrog.lactool.ui.component.BaseModalBottomSheet
import com.aliernfrog.lactool.ui.component.ButtonIcon
import com.aliernfrog.lactool.ui.component.form.DividerRow
import com.aliernfrog.lactool.util.extension.horizontalFadingEdge
import dev.jeziellago.compose.markdowntext.MarkdownText

@OptIn(ExperimentalMaterial3Api::class)
Expand All @@ -46,7 +50,8 @@ fun UpdateSheet(
sheetState: SheetState,
latestVersionInfo: ReleaseInfo,
updateAvailable: Boolean,
onCheckUpdatesRequest: () -> Unit
onCheckUpdatesRequest: () -> Unit,
onIgnoreRequest: () -> Unit
) {
val uriHandler = LocalUriHandler.current
BaseModalBottomSheet(
Expand All @@ -58,7 +63,8 @@ fun UpdateSheet(
updateAvailable = updateAvailable,
onGithubClick = { uriHandler.openUri(latestVersionInfo.htmlUrl) },
onUpdateClick = { uriHandler.openUri(latestVersionInfo.downloadLink) },
onCheckUpdatesRequest = onCheckUpdatesRequest
onCheckUpdatesRequest = onCheckUpdatesRequest,
onIgnoreRequest = onIgnoreRequest
)
DividerRow(
alpha = 0.3f
Expand Down Expand Up @@ -88,26 +94,18 @@ private fun Actions(
updateAvailable: Boolean,
onGithubClick: () -> Unit,
onUpdateClick: () -> Unit,
onCheckUpdatesRequest: () -> Unit
onCheckUpdatesRequest: () -> Unit,
onIgnoreRequest: () -> Unit
) {
val versionNameScrollState = rememberScrollState()
Row(
var ignoreDialogShown by remember { mutableStateOf(false) }

Column(
modifier = Modifier
.fillMaxWidth()
.padding(start = 16.dp, end = 16.dp, bottom = 4.dp),
horizontalArrangement = Arrangement.spacedBy(2.dp),
verticalAlignment = Alignment.CenterVertically
.padding(start = 16.dp, end = 16.dp, bottom = 4.dp)
) {
Row(
modifier = Modifier
.fillMaxWidth()
.weight(1f)
.horizontalFadingEdge(
scrollState = versionNameScrollState,
edgeColor = MaterialTheme.colorScheme.surfaceContainerLow,
isRTL = LocalLayoutDirection.current == LayoutDirection.Rtl
)
.horizontalScroll(versionNameScrollState),
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Text(
Expand All @@ -123,33 +121,68 @@ private fun Actions(
fontSize = 15.sp,
fontWeight = FontWeight.Light,
color = LocalContentColor.current.copy(alpha = 0.7f),
modifier = Modifier.padding(horizontal = 6.dp)
modifier = Modifier
.weight(1f)
.padding(horizontal = 6.dp)
.fillMaxWidth()
)
}
IconButton(onClick = onGithubClick) {
Icon(
painter = painterResource(R.drawable.github),
contentDescription = stringResource(R.string.updates_openInGithub),
modifier = Modifier.padding(6.dp)
)
}
AnimatedContent(updateAvailable) { showUpdate ->
if (showUpdate) Button(
onClick = onUpdateClick
) {
ButtonIcon(
painter = rememberVectorPainter(Icons.Default.Update)
IconButton(onClick = onGithubClick) {
Icon(
painter = painterResource(R.drawable.github),
contentDescription = stringResource(R.string.updates_openInGithub),
modifier = Modifier.size(28.dp)
)
Text(stringResource(R.string.updates_update))
}
else OutlinedButton(
onClick = onCheckUpdatesRequest
}
AnimatedContent(updateAvailable) { showUpdate ->
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp, Alignment.End),
modifier = Modifier.fillMaxWidth()
) {
ButtonIcon(
painter = rememberVectorPainter(Icons.Default.Update)
)
Text(stringResource(R.string.updates_checkUpdates))
if (showUpdate) {
OutlinedButton(
onClick = { ignoreDialogShown = true },
modifier = Modifier.weight(1f).fillMaxWidth()
) {
Text(stringResource(R.string.updates_ignore))
}
Button(
onClick = onUpdateClick,
modifier = Modifier.weight(1f).fillMaxWidth()
) {
ButtonIcon(
painter = rememberVectorPainter(Icons.Default.Update)
)
Text(stringResource(R.string.updates_update))
}
} else OutlinedButton(
onClick = onCheckUpdatesRequest,
modifier = Modifier.fillMaxWidth()
) {
ButtonIcon(
painter = rememberVectorPainter(Icons.Default.Update)
)
Text(stringResource(R.string.updates_checkUpdates))
}
}
}
}

if (ignoreDialogShown) AlertDialog(
onDismissRequest = { ignoreDialogShown = false },
confirmButton = {
Button(onClick = onIgnoreRequest) {
Text(stringResource(R.string.updates_ignore_ignore))
}
},
dismissButton = {
TextButton(onClick = { ignoreDialogShown = false }) {
Text(stringResource(R.string.action_cancel))
}
},
text = {
Text(stringResource(R.string.updates_ignore_question))
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class MainViewModel(

var latestVersionInfo by mutableStateOf(ReleaseInfo(
versionName = applicationVersionName,
versionCode = applicationVersionCode,
preRelease = applicationIsPreRelease,
body = context.getString(R.string.updates_noChangelog),
htmlUrl = githubRepoURL,
Expand All @@ -107,6 +108,9 @@ class MainViewModel(
var updateAvailable by mutableStateOf(false)
private set

val showUpdateNotifications: Boolean
get() = updateAvailable && prefs.ignoredUpdateVersionCode.value != latestVersionInfo.versionCode

val debugInfo: String
get() = arrayOf(
"LAC Tool $applicationVersionLabel",
Expand Down Expand Up @@ -137,9 +141,10 @@ class MainViewModel(
val json = responseJson.getJSONObject(
if (applicationIsPreRelease && responseJson.has("preRelease")) "preRelease" else "stable"
)
val latestVersionCode = json.getInt("versionCode")
val latestVersionCode = json.getLong("versionCode")
latestVersionInfo = ReleaseInfo(
versionName = json.getString("versionName"),
versionCode = latestVersionCode,
preRelease = json.getBoolean("preRelease"),
body = json.getString("body"),
htmlUrl = json.getString("htmlUrl"),
Expand All @@ -149,7 +154,7 @@ class MainViewModel(
if (updateAvailable) {
if (manuallyTriggered) coroutineScope {
updateSheetState.show()
} else {
} else if (showUpdateNotifications) {
showUpdateToast()
Destination.SETTINGS.hasNotification.value = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class PreferenceManager(context: Context) : BasePreferenceManager(
// Experimental (developer) options
val experimentalOptionsEnabled = booleanPreference("experimentalOptionsEnabled", false)
val shizukuNeverLoad = booleanPreference("shizukuNeverLoad", false, experimental = true, includeInDebugInfo = false)
val ignoredUpdateVersionCode = longPreference("ignoredUpdateVersionCode", 0, experimental = true, includeInDebugInfo = false)
val lastKnownInstalledVersion = longPreference("lastKnownInstalledVersion", GeneralUtil.getAppVersionCode(context), experimental = true, includeInDebugInfo = false)
val updatesURL = stringPreference("updatesUrl", "https://aliernfrog.github.io/lactool/latest.json", experimental = true, includeInDebugInfo = false)
}
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@
<string name="updates_preRelease">Pre-release</string>
<string name="updates_openInGithub">View in GitHub</string>
<string name="updates_update">Update</string>
<string name="updates_ignore">Ignore</string>
<string name="updates_ignore_question">Are you sure you want to ignore this update?\nYou can manually update later in Settings > About</string>
<string name="updates_ignore_ignore">Ignore update</string>
<string name="updates_checkUpdates">Check updates</string>
<string name="updates_noChangelog">Check updates to view changelog</string>
</resources>

0 comments on commit b660bb3

Please sign in to comment.