fix: error highlight on invalid pkg name (#30)
This commit is contained in:
parent
e7a796ad58
commit
e8096c888f
8 changed files with 70 additions and 3 deletions
|
|
@ -64,7 +64,7 @@ class MainActivity : ComponentActivity() {
|
|||
is BaseScreenDestination -> BaseScreen(
|
||||
currentNavItem = dest,
|
||||
bottomNavItems = listOf(AppDestination.Home, AppDestination.Plugins, AppDestination.Settings),
|
||||
onNavChanged = { navController.navigate(it) }
|
||||
onNavChanged = { navController.replaceLast(it) }
|
||||
) {
|
||||
when (dest) {
|
||||
is AppDestination.Home -> HomeScreen(
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ fun SettingsTextField(
|
|||
label: String,
|
||||
disabled: Boolean = false,
|
||||
pref: String,
|
||||
error: Boolean = false,
|
||||
onPrefChange: (String) -> Unit,
|
||||
) {
|
||||
Box(modifier = Modifier.padding(horizontal = 18.dp, vertical = 10.dp)) {
|
||||
|
|
@ -21,6 +22,7 @@ fun SettingsTextField(
|
|||
onValueChange = onPrefChange,
|
||||
enabled = !disabled,
|
||||
label = { Text(label) },
|
||||
isError = error,
|
||||
singleLine = true
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,10 @@ fun HomeScreen(
|
|||
) {
|
||||
var showInstallerDialog by remember { mutableStateOf(false) }
|
||||
|
||||
LaunchedEffect(viewModel.preferences.packageName) {
|
||||
viewModel.fetchInstalledVersion()
|
||||
}
|
||||
|
||||
if (showInstallerDialog) {
|
||||
InstallerDialog(
|
||||
onDismiss = { showInstallerDialog = false },
|
||||
|
|
|
|||
|
|
@ -100,7 +100,8 @@ fun SettingsScreen(
|
|||
SettingsTextField(
|
||||
label = stringResource(R.string.setting_package_name),
|
||||
pref = preferences.packageName,
|
||||
onPrefChange = viewModel::setPackageName
|
||||
onPrefChange = viewModel::setPackageName,
|
||||
error = viewModel.packageNameError
|
||||
)
|
||||
|
||||
SettingsTextField(
|
||||
|
|
|
|||
|
|
@ -102,6 +102,12 @@ class HomeViewModel(
|
|||
}
|
||||
}
|
||||
|
||||
fun fetchInstalledVersion() {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
_fetchInstalledVersion()
|
||||
}
|
||||
}
|
||||
|
||||
fun fetchSupportedVersion() {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
_fetchSupportedVersion()
|
||||
|
|
|
|||
|
|
@ -3,16 +3,27 @@ package com.aliucord.manager.ui.viewmodel
|
|||
import android.app.Application
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.aliucord.manager.R
|
||||
import com.aliucord.manager.domain.manager.PreferencesManager
|
||||
import com.aliucord.manager.ui.theme.Theme
|
||||
import com.aliucord.manager.util.showToast
|
||||
import com.aliucord.manager.util.throttle
|
||||
|
||||
class SettingsViewModel(
|
||||
private val application: Application,
|
||||
val preferences: PreferencesManager
|
||||
) : ViewModel() {
|
||||
private val checkPackageNameThrottled = throttle(50L, viewModelScope) {
|
||||
packageNameError = !PACKAGE_REGEX.matches(preferences.packageName)
|
||||
}
|
||||
|
||||
var showThemeDialog by mutableStateOf(false)
|
||||
private set
|
||||
|
||||
var packageNameError by mutableStateOf(false)
|
||||
private set
|
||||
|
||||
fun showThemeDialog() {
|
||||
showThemeDialog = true
|
||||
}
|
||||
|
|
@ -22,7 +33,8 @@ class SettingsViewModel(
|
|||
}
|
||||
|
||||
fun setPackageName(packageName: String) {
|
||||
if (packageName.isNotBlank()) preferences.packageName = packageName
|
||||
preferences.packageName = packageName
|
||||
checkPackageNameThrottled()
|
||||
}
|
||||
|
||||
fun setAppName(appName: String) {
|
||||
|
|
@ -40,4 +52,20 @@ class SettingsViewModel(
|
|||
fun clearCacheDir() {
|
||||
application.cacheDir.deleteRecursively()
|
||||
}
|
||||
|
||||
init {
|
||||
checkPackageNameThrottled()
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
if (!PACKAGE_REGEX.matches(preferences.packageName)) {
|
||||
preferences.packageName = "com.aliucord"
|
||||
application.showToast(R.string.settings_invalid_package_name)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val PACKAGE_REGEX = "^[a-z]\\w*(\\.[a-z]\\w*)+\$"
|
||||
.toRegex(RegexOption.IGNORE_CASE)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
25
app/src/main/kotlin/com/aliucord/manager/util/Throttling.kt
Normal file
25
app/src/main/kotlin/com/aliucord/manager/util/Throttling.kt
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package com.aliucord.manager.util
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* Constructs a function that executes [destinationFunction] only once per [skipMs].
|
||||
*/
|
||||
inline fun throttle(
|
||||
skipMs: Long,
|
||||
coroutineScope: CoroutineScope,
|
||||
crossinline destinationFunction: suspend () -> Unit
|
||||
): () -> Unit {
|
||||
var throttleJob: Job? = null
|
||||
return {
|
||||
if (throttleJob?.isCompleted != false) {
|
||||
throttleJob = coroutineScope.launch {
|
||||
destinationFunction()
|
||||
delay(skipMs)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -51,6 +51,7 @@
|
|||
<string name="setting_package_name">Package Name</string>
|
||||
<string name="setting_target_version">Download Version</string>
|
||||
<string name="setting_dynamic_color">Dynamic color</string>
|
||||
<string name="settings_invalid_package_name">Invalid package name, reset to com.aliucord</string>
|
||||
|
||||
<string name="theme_system">System</string>
|
||||
<string name="theme_dark">Dark</string>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue