Fix language option crash on android

This commit is contained in:
Syer10
2022-02-28 17:58:38 -05:00
parent ce7074c1b9
commit 6cfad4cd6d
3 changed files with 34 additions and 11 deletions

View File

@@ -0,0 +1,9 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package ca.gosyer.ui.settings
actual fun Any.getResourceLanguages(): Map<String, String> = mapOf()

View File

@@ -0,0 +1,22 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package ca.gosyer.ui.settings
import okio.Path.Companion.toPath
import okio.asResourceFileSystem
import java.util.Locale
actual fun Any.getResourceLanguages(): Map<String, String> = this::class.java.classLoader.asResourceFileSystem().list("/localization/".toPath())
.asSequence()
.drop(1)
.map { it.name.substringBeforeLast('.') }
.map { it.substringAfter("mokoBundle_") }
.map(String::trim)
.map { it.replace("-r", "-") }
.filterNot(String::isBlank)
.associateWith { Locale.forLanguageTag(it).getDisplayName(currentLocale) }
)

View File

@@ -35,8 +35,6 @@ import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.core.screen.ScreenKey
import cafe.adriel.voyager.core.screen.uniqueScreenKey
import me.tatarka.inject.annotations.Inject
import okio.Path.Companion.toPath
import okio.asResourceFileSystem
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter
@@ -61,6 +59,8 @@ class SettingsGeneralScreen : Screen {
}
}
expect fun Any.getResourceLanguages(): Map<String, String>
class SettingsGeneralViewModel @Inject constructor(
uiPreferences: UiPreferences,
contextWrapper: ContextWrapper
@@ -86,15 +86,7 @@ class SettingsGeneralViewModel @Inject constructor(
fun getLanguageChoices(): Map<String, String> = (
mapOf(
"" to stringResource(MR.strings.language_system_default, currentLocale.getDisplayName(currentLocale))
) + this::class.java.classLoader.asResourceFileSystem().list("/localization/".toPath())
.asSequence()
.drop(1)
.map { it.name.substringBeforeLast('.') }
.map { it.substringAfter("mokoBundle_") }
.map(String::trim)
.map { it.replace("-r", "-") }
.filterNot(String::isBlank)
.associateWith { Locale.forLanguageTag(it).getDisplayName(currentLocale) }
) + getResourceLanguages()
)
.toSortedMap(compareBy { it.lowercase() })