manager: fix clipboard toast
This commit is contained in:
@@ -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,7 +87,9 @@ fun AppProfileTemplateScreen(
|
||||
val clipboardManager = LocalClipboardManager.current
|
||||
val context = LocalContext.current
|
||||
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() },
|
||||
onSync = {
|
||||
@@ -94,13 +97,18 @@ fun AppProfileTemplateScreen(
|
||||
},
|
||||
onImport = {
|
||||
clipboardManager.getText()?.text?.let {
|
||||
if (it.isEmpty()) {
|
||||
showToast(context.getString(R.string.app_profile_template_import_empty))
|
||||
return@let
|
||||
}
|
||||
scope.launch {
|
||||
viewModel.importTemplates(it, {
|
||||
showToast(context.getString(R.string.app_profile_template_import_success))
|
||||
scope.launch {
|
||||
viewModel.importTemplates(
|
||||
it, {
|
||||
showToast(context.getString(R.string.app_profile_template_import_success))
|
||||
viewModel.fetchTemplates(false)
|
||||
}
|
||||
}, showToast)
|
||||
},
|
||||
showToast
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user