Skip to content

Commit c1ff632

Browse files
committed
Toast on up to date
* Update confirm dialog button texts
1 parent 8458195 commit c1ff632

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

app/src/main/java/com/paulcoding/hviewer/network/Github.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ sealed class SiteConfigsState {
159159

160160
fun getToastMessage() = when (this) {
161161
is NewConfigsInstall -> R.string.scripts_installed
162-
is UpToDate -> R.string.up_to_Date
162+
is UpToDate -> R.string.up_to_date
163163
is Updated -> R.string.scripts_updated
164164
}
165165
}

app/src/main/java/com/paulcoding/hviewer/ui/component/ConfirmDialog.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ fun ConfirmDialog(
1717
text: String = "",
1818
confirmColor: Color? = null,
1919
dismissColor: Color? = null,
20+
confirmText: String? = null,
21+
dismissText: String? = null,
2022
onDismiss: () -> Unit,
2123
onConfirm: () -> Unit
2224
) {
@@ -28,15 +30,15 @@ fun ConfirmDialog(
2830
confirmButton = {
2931
TextButton(onClick = { onConfirm() }) {
3032
Text(
31-
stringResource(R.string.confirm),
33+
confirmText ?: stringResource(R.string.confirm),
3234
color = confirmColor ?: MaterialTheme.colorScheme.error
3335
)
3436
}
3537
},
3638
dismissButton = {
3739
TextButton(onClick = { onDismiss() }) {
3840
Text(
39-
stringResource(R.string.cancel),
41+
dismissText ?: stringResource(R.string.cancel),
4042
color = dismissColor ?: MaterialTheme.colorScheme.onBackground
4143
)
4244
}

app/src/main/java/com/paulcoding/hviewer/ui/page/AppViewModel.kt

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.paulcoding.hviewer.ui.page
22

33
import android.content.Context
44
import android.content.Intent
5+
import android.net.Uri
56
import androidx.core.content.FileProvider
67
import androidx.lifecycle.ViewModel
78
import androidx.lifecycle.viewModelScope
@@ -190,10 +191,18 @@ class AppViewModel : ViewModel() {
190191
}
191192
}
192193

193-
fun checkForUpdate(currentVersion: String, onUpdateAvailable: (String, String) -> Unit) {
194+
fun checkForUpdate(
195+
currentVersion: String,
196+
onUpToDate: () -> Unit,
197+
onUpdateAvailable: (String, String) -> Unit
198+
) {
194199
viewModelScope.launch {
195200
_stateFlow.update { it.copy(updatingApk = true) }
196-
Github.checkForUpdate(currentVersion, onUpdateAvailable)
201+
val release = Github.checkForUpdate(currentVersion)
202+
if (release != null)
203+
onUpdateAvailable(release.version, release.downloadUrl)
204+
else
205+
onUpToDate()
197206
_stateFlow.update { it.copy(updatingApk = false) }
198207
}
199208
}
@@ -207,13 +216,17 @@ class AppViewModel : ViewModel() {
207216
"${context.packageName}.fileprovider",
208217
file
209218
)
210-
val intent = Intent(Intent.ACTION_VIEW).apply {
211-
setDataAndType(uri, "application/vnd.android.package-archive")
212-
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
213-
}
214-
context.startActivity(intent)
219+
installApk(context, uri)
215220
}
216221
_stateFlow.update { it.copy(updatingApk = false) }
217222
}
218223
}
224+
225+
fun installApk(context: Context, uri: Uri) {
226+
val intent = Intent(Intent.ACTION_VIEW).apply {
227+
setDataAndType(uri, "application/vnd.android.package-archive")
228+
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
229+
}
230+
context.startActivity(intent)
231+
}
219232
}

app/src/main/java/com/paulcoding/hviewer/ui/page/settings/SettingsPage.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,14 @@ fun SettingsPage(
166166
appViewModel.setDevMode(it)
167167
}
168168
HIcon(Icons.Outlined.Update, tint = MaterialTheme.colorScheme.primary) {
169-
appViewModel.checkForUpdate(BuildConfig.VERSION_NAME) { version, url ->
170-
newVersion = version
171-
downloadUrl = url
172-
}
169+
appViewModel.checkForUpdate(BuildConfig.VERSION_NAME,
170+
onUpToDate = {
171+
makeToast(R.string.up_to_date)
172+
},
173+
onUpdateAvailable = { version, url ->
174+
newVersion = version
175+
downloadUrl = url
176+
})
173177
}
174178
}
175179
}
@@ -194,9 +198,10 @@ fun SettingsPage(
194198

195199
ConfirmDialog(
196200
showDialog = newVersion.isNotEmpty(),
197-
title = "Update Available",
201+
title = stringResource(R.string.update_available),
198202
text = newVersion,
199203
confirmColor = MaterialTheme.colorScheme.primary,
204+
confirmText = stringResource(R.string.install_now),
200205
dismissColor = MaterialTheme.colorScheme.onBackground,
201206
onDismiss = {
202207
newVersion = ""

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<string name="version_">Version %1$s</string>
4343
<string name="scripts_installed">Scripts Installed</string>
4444
<string name="from_repo_">From repo %1$s</string>
45-
<string name="up_to_Date">Up to date</string>
45+
<string name="up_to_date">Up to date</string>
4646
<string name="h_viewer_scripts_url">https://github.com/paulcoding810/h-viewer-scripts</string>
4747
<string name="branch">Branch</string>
4848
<string name="clear_history">Clear history</string>
@@ -52,4 +52,5 @@
5252
<string name="install_now">Install now</string>
5353
<string name="error">Error</string>
5454
<string name="install">Install</string>
55+
<string name="update_available">Update Available</string>
5556
</resources>

0 commit comments

Comments
 (0)