Ver Fonte

fix: 切换语言改为原地重建 Activity,无需杀进程重启

语言与字号在各 Activity onCreate 的 applyLocaleAndFont 中统一应用,
与 fontScale 一致改为发 RECREATE 事件原地重建即可生效;
同时消除了杀进程导致 DataStore 异步写入丢失的竞态。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
5151561 há 3 meses atrás
pai
commit
7e96a6cce3

+ 5 - 8
app/src/main/java/io/legado/app/ui/config/otherConfig/OtherConfigScreen.kt

@@ -22,8 +22,7 @@ import androidx.compose.ui.res.stringResource
 import androidx.compose.ui.unit.dp
 import androidx.lifecycle.compose.collectAsStateWithLifecycle
 import io.legado.app.R
-import io.legado.app.constant.PreferKey
-import io.legado.app.help.config.DsSync
+import io.legado.app.constant.EventBus
 import io.legado.app.service.WebService
 import io.legado.app.ui.config.readMangaConfig.ReadMangaConfig
 import io.legado.app.ui.theme.LegadoTheme
@@ -40,9 +39,8 @@ import io.legado.app.ui.widget.components.settingItem.SwitchSettingItem
 import io.legado.app.ui.widget.components.topbar.GlassMediumFlexibleTopAppBar
 import io.legado.app.ui.widget.components.topbar.GlassTopAppBarDefaults
 import io.legado.app.ui.widget.components.topbar.TopBarNavigationButton
-import io.legado.app.utils.restart
+import io.legado.app.utils.postEvent
 import io.legado.app.utils.takePersistablePermissionSafely
-import kotlinx.coroutines.runBlocking
 import org.koin.androidx.compose.koinViewModel
 
 @OptIn(ExperimentalMaterial3Api::class)
@@ -107,10 +105,9 @@ fun OtherConfigScreen(
                     entryValues = stringArrayResource(R.array.language_value),
                     onValueChange = { newValue ->
                         OtherConfig.language = newValue
-                        // prefDelegate 的 DataStore 写入是异步的,restart() 会立刻杀进程,
-                        // 必须先同步落盘,否则下次启动从 DataStore 读回旧值,语言切换失效
-                        runBlocking { DsSync.putString(PreferKey.language, newValue) }
-                        context.restart()
+                        // 与 fontScale 相同:locale 在各 Activity onCreate 的
+                        // applyLocaleAndFont 里应用,原地重建即可生效,无需杀进程
+                        postEvent(EventBus.RECREATE, "")
                     }
                 )