feat: better version handling
This commit is contained in:
parent
aa76862d44
commit
784fd19d8f
5 changed files with 110 additions and 76 deletions
|
|
@ -13,15 +13,13 @@ import androidx.compose.ui.text.font.FontWeight
|
|||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.aliucord.manager.R
|
||||
import com.aliucord.manager.ui.viewmodel.HomeViewModel.VersionType
|
||||
import com.aliucord.manager.ui.util.DiscordVersion
|
||||
|
||||
@Composable
|
||||
fun InfoCard(
|
||||
packageName: String,
|
||||
supportedVersion: String,
|
||||
supportedVersionType: VersionType,
|
||||
currentVersion: String,
|
||||
currentVersionType: VersionType,
|
||||
supportedVersion: DiscordVersion,
|
||||
currentVersion: DiscordVersion,
|
||||
onDownloadClick: () -> Unit,
|
||||
onUninstallClick: () -> Unit,
|
||||
onLaunchClick: () -> Unit
|
||||
|
|
@ -44,22 +42,22 @@ fun InfoCard(
|
|||
append(stringResource(R.string.version_supported))
|
||||
withStyle(SpanStyle(fontWeight = FontWeight.Bold)) {
|
||||
append(" ")
|
||||
if (supportedVersion.isNotEmpty()) {
|
||||
append(supportedVersion)
|
||||
if (supportedVersion is DiscordVersion.Existing) {
|
||||
append(supportedVersion.name)
|
||||
append(" - ")
|
||||
}
|
||||
append(supportedVersionType.toDisplayName())
|
||||
append(supportedVersion.toDisplayName())
|
||||
}
|
||||
|
||||
append('\n')
|
||||
append(stringResource(R.string.version_installed))
|
||||
withStyle(SpanStyle(fontWeight = FontWeight.Bold)) {
|
||||
append(" ")
|
||||
if (currentVersion.isNotEmpty()) {
|
||||
append(currentVersion)
|
||||
if (currentVersion is DiscordVersion.Existing) {
|
||||
append(currentVersion.name)
|
||||
append(" - ")
|
||||
}
|
||||
append(currentVersionType.toDisplayName())
|
||||
append(currentVersion.toDisplayName())
|
||||
}
|
||||
},
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
|
|
@ -71,10 +69,13 @@ fun InfoCard(
|
|||
.padding(top = 10.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(16.dp)
|
||||
) {
|
||||
val (icon, description) = when (currentVersion) {
|
||||
"" -> Icons.Default.Download to R.string.action_install
|
||||
supportedVersion -> Icons.Default.Refresh to R.string.action_reinstall
|
||||
else -> Icons.Default.Update to R.string.action_update
|
||||
val (icon, description) = when {
|
||||
currentVersion !is DiscordVersion.Existing ->
|
||||
Icons.Default.Download to R.string.action_install
|
||||
currentVersion < supportedVersion ->
|
||||
Icons.Default.Refresh to R.string.action_reinstall
|
||||
else ->
|
||||
Icons.Default.Update to R.string.action_update
|
||||
}
|
||||
|
||||
FilledTonalIconButton(
|
||||
|
|
@ -83,7 +84,7 @@ fun InfoCard(
|
|||
.heightIn(min = 50.dp),
|
||||
onClick = onDownloadClick,
|
||||
shape = ShapeDefaults.Large,
|
||||
enabled = supportedVersionType != VersionType.ERROR
|
||||
enabled = supportedVersion !is DiscordVersion.Error
|
||||
) {
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.spacedBy(5.dp),
|
||||
|
|
@ -93,7 +94,7 @@ fun InfoCard(
|
|||
imageVector = icon,
|
||||
contentDescription = stringResource(description)
|
||||
)
|
||||
if (!currentVersionType.isVersion()) {
|
||||
if (currentVersion is DiscordVersion.None) {
|
||||
Text(
|
||||
stringResource(description),
|
||||
style = MaterialTheme.typography.labelLarge
|
||||
|
|
@ -102,7 +103,7 @@ fun InfoCard(
|
|||
}
|
||||
}
|
||||
|
||||
if (currentVersionType.isVersion()) {
|
||||
if (currentVersion is DiscordVersion.Existing) {
|
||||
FilledTonalIconButton(
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,9 @@
|
|||
|
||||
package com.aliucord.manager.ui.screen
|
||||
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
|
|
@ -46,9 +48,7 @@ fun HomeScreen(
|
|||
InfoCard(
|
||||
packageName = viewModel.preferences.packageName,
|
||||
supportedVersion = viewModel.supportedVersion,
|
||||
supportedVersionType = viewModel.supportedVersionType,
|
||||
currentVersion = viewModel.installedVersion,
|
||||
currentVersionType = viewModel.installedVersionType,
|
||||
onDownloadClick = { showInstallerDialog = true },
|
||||
onLaunchClick = viewModel::launchAliucord,
|
||||
onUninstallClick = viewModel::uninstallAliucord
|
||||
|
|
|
|||
|
|
@ -0,0 +1,63 @@
|
|||
package com.aliucord.manager.ui.util
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import com.aliucord.manager.R
|
||||
|
||||
sealed interface DiscordVersion : Comparable<DiscordVersion> {
|
||||
object Error : DiscordVersion
|
||||
object None : DiscordVersion
|
||||
|
||||
data class Existing(
|
||||
val type: Type,
|
||||
val name: String,
|
||||
val code: Int,
|
||||
) : DiscordVersion {
|
||||
val rawCode: Int
|
||||
get() = (code / 1000 * 100) + code % 100
|
||||
}
|
||||
|
||||
override fun compareTo(other: DiscordVersion): Int {
|
||||
return when (this) {
|
||||
is Error -> 0
|
||||
is None -> 0
|
||||
is Existing -> {
|
||||
if (other is Existing) {
|
||||
other.rawCode.compareTo(rawCode)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun toDisplayName() = when (this) {
|
||||
is Error -> stringResource(R.string.version_load_fail)
|
||||
is None -> stringResource(R.string.version_none)
|
||||
is Existing -> when (type) {
|
||||
Type.STABLE -> stringResource(R.string.version_stable)
|
||||
Type.BETA -> stringResource(R.string.version_beta)
|
||||
Type.ALPHA -> stringResource(R.string.version_alpha)
|
||||
Type.UNKNOWN -> stringResource(R.string.version_unknown)
|
||||
}
|
||||
}
|
||||
|
||||
enum class Type {
|
||||
STABLE,
|
||||
BETA,
|
||||
ALPHA,
|
||||
UNKNOWN,
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun parseVersionType(versionCode: Int?): Type {
|
||||
return when (versionCode?.div(100)?.mod(10)) {
|
||||
0 -> Type.STABLE
|
||||
1 -> Type.BETA
|
||||
2 -> Type.ALPHA
|
||||
else -> Type.UNKNOWN
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
package com.aliucord.manager.ui.viewmodel
|
||||
|
||||
import android.app.Application
|
||||
import android.content.pm.PackageManager
|
||||
import android.util.Log
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.paging.*
|
||||
|
|
@ -14,6 +14,7 @@ import com.aliucord.manager.domain.repository.GithubRepository
|
|||
import com.aliucord.manager.installer.util.uninstallApk
|
||||
import com.aliucord.manager.network.dto.Commit
|
||||
import com.aliucord.manager.network.utils.fold
|
||||
import com.aliucord.manager.ui.util.DiscordVersion
|
||||
import com.aliucord.manager.util.getPackageVersion
|
||||
import com.aliucord.manager.util.showToast
|
||||
import kotlinx.coroutines.*
|
||||
|
|
@ -23,16 +24,10 @@ class HomeViewModel(
|
|||
private val github: GithubRepository,
|
||||
val preferences: PreferencesManager
|
||||
) : ViewModel() {
|
||||
var supportedVersion by mutableStateOf("")
|
||||
var supportedVersion by mutableStateOf<DiscordVersion>(DiscordVersion.None)
|
||||
private set
|
||||
|
||||
var supportedVersionType by mutableStateOf(VersionType.NONE)
|
||||
private set
|
||||
|
||||
var installedVersion by mutableStateOf("")
|
||||
private set
|
||||
|
||||
var installedVersionType by mutableStateOf(VersionType.NONE)
|
||||
var installedVersion by mutableStateOf<DiscordVersion>(DiscordVersion.None)
|
||||
private set
|
||||
|
||||
val commits = Pager(PagingConfig(pageSize = 30)) {
|
||||
|
|
@ -73,15 +68,21 @@ class HomeViewModel(
|
|||
val (versionName, versionCode) = application.getPackageVersion(preferences.packageName)
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
installedVersion = versionName.split("-")[0].trim()
|
||||
installedVersionType = VersionType.parseVersionCode(versionCode)
|
||||
installedVersion = DiscordVersion.Existing(
|
||||
type = DiscordVersion.parseVersionType(versionCode),
|
||||
name = versionName.split("-")[0].trim(),
|
||||
code = versionCode,
|
||||
)
|
||||
}
|
||||
} catch (t: PackageManager.NameNotFoundException) {
|
||||
withContext(Dispatchers.Main) {
|
||||
installedVersion = DiscordVersion.None
|
||||
}
|
||||
} catch (t: Throwable) {
|
||||
Log.e(BuildConfig.TAG, Log.getStackTraceString(t))
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
installedVersion = ""
|
||||
installedVersionType = VersionType.ERROR
|
||||
installedVersion = DiscordVersion.Error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -92,12 +93,17 @@ class HomeViewModel(
|
|||
withContext(Dispatchers.Main) {
|
||||
version.fold(
|
||||
success = {
|
||||
supportedVersion = it.versionName
|
||||
supportedVersionType = VersionType.parseVersionCode(it.versionCode.toIntOrNull())
|
||||
val versionCode = it.versionCode.toIntOrNull() ?: return@fold
|
||||
|
||||
supportedVersion = DiscordVersion.Existing(
|
||||
type = DiscordVersion.parseVersionType(versionCode),
|
||||
name = it.versionName.split("-")[0].trim(),
|
||||
code = versionCode,
|
||||
)
|
||||
},
|
||||
fail = {
|
||||
supportedVersion = ""
|
||||
supportedVersionType = VersionType.ERROR
|
||||
Log.e(BuildConfig.TAG, Log.getStackTraceString(it))
|
||||
supportedVersion = DiscordVersion.Error
|
||||
}
|
||||
)
|
||||
}
|
||||
|
|
@ -129,41 +135,4 @@ class HomeViewModel(
|
|||
fun uninstallAliucord() {
|
||||
application.uninstallApk(preferences.packageName)
|
||||
}
|
||||
|
||||
enum class VersionType {
|
||||
STABLE,
|
||||
BETA,
|
||||
ALPHA,
|
||||
UNKNOWN,
|
||||
NONE,
|
||||
ERROR;
|
||||
|
||||
fun isVersion(): Boolean = when (this) {
|
||||
STABLE -> true
|
||||
BETA -> true
|
||||
ALPHA -> true
|
||||
else -> false
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun toDisplayName() = when (this) {
|
||||
UNKNOWN -> stringResource(R.string.version_unknown)
|
||||
STABLE -> stringResource(R.string.version_stable)
|
||||
BETA -> stringResource(R.string.version_beta)
|
||||
ALPHA -> stringResource(R.string.version_alpha)
|
||||
ERROR -> stringResource(R.string.version_load_fail)
|
||||
NONE -> stringResource(R.string.version_unknown)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun parseVersionCode(versionCode: Int?): VersionType {
|
||||
return when (versionCode?.div(100)?.mod(10)) {
|
||||
0 -> STABLE
|
||||
1 -> BETA
|
||||
2 -> ALPHA
|
||||
else -> UNKNOWN
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -86,10 +86,11 @@
|
|||
|
||||
<string name="version_supported">Supported version:</string>
|
||||
<string name="version_installed">Installed version:</string>
|
||||
<string name="version_none">None</string>
|
||||
<string name="version_stable">Stable</string>
|
||||
<string name="version_beta">Beta</string>
|
||||
<string name="version_alpha">Alpha</string>
|
||||
<string name="version_unknown" translatable="false" />
|
||||
<string name="version_unknown">Unknown</string>
|
||||
<string name="version_load_fail">Failed to retrieve version</string>
|
||||
|
||||
<string name="installer_save_file">Save to file</string>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue