manager: fix clipboard toast

This commit is contained in:
weishu
2023-10-24 15:59:30 +08:00
parent 7371eae382
commit 03068f279d
4 changed files with 21 additions and 11 deletions

View File

@@ -48,6 +48,7 @@ import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.result.ResultRecipient
import com.ramcosta.composedestinations.result.getOr
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.weishu.kernelsu.R
import me.weishu.kernelsu.ui.screen.destinations.TemplateEditorScreenDestination
@@ -86,21 +87,28 @@ fun AppProfileTemplateScreen(
val clipboardManager = LocalClipboardManager.current
val context = LocalContext.current
val showToast = fun(msg: String) {
scope.launch(Dispatchers.Main) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
}
}
TopBar(onBack = { navigator.popBackStack() },
onSync = {
scope.launch { viewModel.fetchTemplates(true) }
},
onImport = {
clipboardManager.getText()?.text?.let {
scope.launch {
viewModel.importTemplates(it, {
showToast(context.getString(R.string.app_profile_template_import_success))
scope.launch {
viewModel.fetchTemplates(false)
if (it.isEmpty()) {
showToast(context.getString(R.string.app_profile_template_import_empty))
return@let
}
}, showToast)
scope.launch {
viewModel.importTemplates(
it, {
showToast(context.getString(R.string.app_profile_template_import_success))
viewModel.fetchTemplates(false)
},
showToast
)
}
}
},

View File

@@ -88,8 +88,8 @@ class TemplateViewModel : ViewModel() {
suspend fun importTemplates(
templates: String,
onSuccess: () -> Unit,
onFailure: (String) -> Unit
onSuccess: suspend () -> Unit,
onFailure: suspend (String) -> Unit
) {
withContext(Dispatchers.IO) {
runCatching {
@@ -208,9 +208,9 @@ private fun getLocaleString(json: JSONObject, key: String): String {
val fallback = json.getString(key)
val locale = Locale.getDefault()
val localeKey = "${locale.language}_${locale.country}"
json.optJSONObject("locales")?.let { locale ->
locale.optJSONObject(localeKey)?.let {
return it.optString(key, fallback)
json.optJSONObject("locales")?.let {
it.optJSONObject(localeKey)?.let { json->
return json.optString(key, fallback)
}
}
return fallback

View File

@@ -100,4 +100,5 @@
<string name="app_profile_template_import_success">导入成功!</string>
<string name="app_profile_template_sync">同步在线规则</string>
<string name="app_profile_template_save_failed">模版保存失败!</string>
<string name="app_profile_template_import_empty">剪切板为空!</string>
</resources>

View File

@@ -102,4 +102,5 @@
<string name="app_profile_template_import_success">Imported successfully</string>
<string name="app_profile_template_sync">Sync online templates</string>
<string name="app_profile_template_save_failed">Failed to save template</string>
<string name="app_profile_template_import_empty">Clipboard is empty!</string>
</resources>