feat: handle InfoCard versions state properly (#39)
This commit is contained in:
parent
da2f9490e0
commit
45d75ca0f9
4 changed files with 110 additions and 44 deletions
|
|
@ -1,8 +1,6 @@
|
|||
package com.aliucord.manager.ui.component.home
|
||||
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.text.appendInlineContent
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.*
|
||||
import androidx.compose.material3.*
|
||||
|
|
@ -15,13 +13,15 @@ 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
|
||||
|
||||
@Composable
|
||||
fun InfoCard(
|
||||
packageName: String,
|
||||
supportedVersion: String,
|
||||
@StringRes supportedVersionType: Int,
|
||||
supportedVersionType: VersionType,
|
||||
currentVersion: String,
|
||||
currentVersionType: VersionType,
|
||||
onDownloadClick: () -> Unit,
|
||||
onUninstallClick: () -> Unit,
|
||||
onLaunchClick: () -> Unit
|
||||
|
|
@ -44,16 +44,22 @@ fun InfoCard(
|
|||
append(stringResource(R.string.version_supported))
|
||||
withStyle(SpanStyle(fontWeight = FontWeight.Bold)) {
|
||||
append(" ")
|
||||
append(supportedVersion)
|
||||
append(" - ")
|
||||
append(stringResource(supportedVersionType))
|
||||
if (supportedVersion.isNotEmpty()) {
|
||||
append(supportedVersion)
|
||||
append(" - ")
|
||||
}
|
||||
append(supportedVersionType.toDisplayName())
|
||||
}
|
||||
|
||||
append('\n')
|
||||
append(stringResource(R.string.version_installed))
|
||||
withStyle(SpanStyle(fontWeight = FontWeight.Bold)) {
|
||||
append(" ")
|
||||
append(currentVersion)
|
||||
if (currentVersion.isNotEmpty()) {
|
||||
append(currentVersion)
|
||||
append(" - ")
|
||||
}
|
||||
append(currentVersionType.toDisplayName())
|
||||
}
|
||||
},
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
|
|
@ -65,9 +71,8 @@ fun InfoCard(
|
|||
.padding(top = 10.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(16.dp)
|
||||
) {
|
||||
|
||||
val (icon, description) = when (currentVersion) {
|
||||
"-" -> Icons.Default.Download to R.string.action_install
|
||||
"" -> 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
|
||||
}
|
||||
|
|
@ -77,7 +82,8 @@ fun InfoCard(
|
|||
.weight(1f)
|
||||
.heightIn(min = 50.dp),
|
||||
onClick = onDownloadClick,
|
||||
shape = ShapeDefaults.Large
|
||||
shape = ShapeDefaults.Large,
|
||||
enabled = supportedVersionType != VersionType.ERROR
|
||||
) {
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.spacedBy(5.dp),
|
||||
|
|
@ -87,14 +93,16 @@ fun InfoCard(
|
|||
imageVector = icon,
|
||||
contentDescription = stringResource(description)
|
||||
)
|
||||
if (currentVersion == "-") Text(
|
||||
stringResource(description),
|
||||
style = MaterialTheme.typography.labelLarge
|
||||
)
|
||||
if (!currentVersionType.isVersion()) {
|
||||
Text(
|
||||
stringResource(description),
|
||||
style = MaterialTheme.typography.labelLarge
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (currentVersion != "-") {
|
||||
if (currentVersionType.isVersion()) {
|
||||
FilledTonalIconButton(
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ fun HomeScreen(
|
|||
supportedVersion = viewModel.supportedVersion,
|
||||
supportedVersionType = viewModel.supportedVersionType,
|
||||
currentVersion = viewModel.installedVersion,
|
||||
currentVersionType = viewModel.installedVersionType,
|
||||
onDownloadClick = { showInstallerDialog = true },
|
||||
onLaunchClick = viewModel::launchAliucord,
|
||||
onUninstallClick = viewModel::uninstallAliucord
|
||||
|
|
|
|||
|
|
@ -3,36 +3,39 @@ package com.aliucord.manager.ui.viewmodel
|
|||
import android.app.Application
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.widget.Toast
|
||||
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.*
|
||||
import com.aliucord.manager.BuildConfig
|
||||
import com.aliucord.manager.R
|
||||
import com.aliucord.manager.domain.manager.PreferencesManager
|
||||
import com.aliucord.manager.domain.repository.GithubRepository
|
||||
import com.aliucord.manager.network.dto.Commit
|
||||
import com.aliucord.manager.network.utils.fold
|
||||
import com.aliucord.manager.util.getPackageVersionCode
|
||||
import com.aliucord.manager.util.showToast
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
class HomeViewModel(
|
||||
private val application: Application,
|
||||
private val github: GithubRepository,
|
||||
val preferences: PreferencesManager
|
||||
) : ViewModel() {
|
||||
private val packageManager = application.packageManager
|
||||
|
||||
var supportedVersion by mutableStateOf("")
|
||||
private set
|
||||
|
||||
var supportedVersionType by mutableStateOf(R.string.version_unknown)
|
||||
var supportedVersionType by mutableStateOf(VersionType.NONE)
|
||||
private set
|
||||
|
||||
var installedVersion by mutableStateOf("")
|
||||
private set
|
||||
|
||||
var installedVersionType by mutableStateOf(VersionType.NONE)
|
||||
private set
|
||||
|
||||
val commits = Pager(PagingConfig(pageSize = 30)) {
|
||||
object : PagingSource<Int, Commit>() {
|
||||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Commit> {
|
||||
|
|
@ -61,33 +64,42 @@ class HomeViewModel(
|
|||
|
||||
init {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
_fetchInstalledVersion()
|
||||
_fetchSupportedVersion()
|
||||
}
|
||||
}
|
||||
|
||||
installedVersion = try {
|
||||
@Suppress("DEPRECATION")
|
||||
packageManager.getPackageInfo(preferences.packageName, 0).versionName
|
||||
} catch (th: Throwable) {
|
||||
// TODO: translated error message
|
||||
"-"
|
||||
private suspend fun _fetchInstalledVersion() {
|
||||
try {
|
||||
val (versionName, versionCode) = application.getPackageVersionCode(preferences.packageName)
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
installedVersion = versionName.split("-")[0].trim()
|
||||
installedVersionType = VersionType.parseVersionCode(versionCode)
|
||||
}
|
||||
} catch (t: Throwable) {
|
||||
Log.e(BuildConfig.TAG, Log.getStackTraceString(t))
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
installedVersionType = VersionType.ERROR
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun _fetchSupportedVersion() {
|
||||
github.getDataJson().fold(
|
||||
success = {
|
||||
supportedVersion = it.versionName
|
||||
supportedVersionType = when (it.versionCode[3]) {
|
||||
'0' -> R.string.version_stable
|
||||
'1' -> R.string.version_beta
|
||||
'2' -> R.string.version_alpha
|
||||
else -> R.string.version_unknown
|
||||
val version = github.getDataJson()
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
version.fold(
|
||||
success = {
|
||||
supportedVersion = it.versionName
|
||||
supportedVersionType = VersionType.parseVersionCode(it.versionCode.toIntOrNull())
|
||||
},
|
||||
fail = {
|
||||
supportedVersionType = VersionType.ERROR
|
||||
}
|
||||
},
|
||||
fail = {
|
||||
supportedVersionType = R.string.version_load_fail
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun fetchSupportedVersion() {
|
||||
|
|
@ -97,7 +109,8 @@ class HomeViewModel(
|
|||
}
|
||||
|
||||
fun launchAliucord() {
|
||||
val launchIntent = packageManager.getLaunchIntentForPackage(preferences.packageName)
|
||||
val launchIntent = application.packageManager
|
||||
.getLaunchIntentForPackage(preferences.packageName)
|
||||
|
||||
if (launchIntent != null) {
|
||||
application.startActivity(launchIntent)
|
||||
|
|
@ -114,4 +127,41 @@ class HomeViewModel(
|
|||
|
||||
application.startActivity(uninstallIntent)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
package com.aliucord.manager.util
|
||||
|
||||
import android.content.ClipData
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import android.content.*
|
||||
import android.os.Environment
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
|
|
@ -37,3 +35,12 @@ fun Context.saveFile(name: String, text: String): File? {
|
|||
fun Context.showToast(@StringRes text: Int, vararg args: Any, length: Int = Toast.LENGTH_LONG) {
|
||||
Toast.makeText(this, this.getString(text, *args), length).show()
|
||||
}
|
||||
|
||||
/**
|
||||
* @return (versionName, versionCode)
|
||||
*/
|
||||
fun Context.getPackageVersionCode(pkg: String): Pair<String, Int> {
|
||||
@Suppress("DEPRECATION")
|
||||
return packageManager.getPackageInfo(pkg, 0)
|
||||
.let { it.versionName to it.versionCode }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue