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

View File

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

View File

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

View File

@@ -102,4 +102,5 @@
<string name="app_profile_template_import_success">Imported successfully</string> <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_sync">Sync online templates</string>
<string name="app_profile_template_save_failed">Failed to save template</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> </resources>