Add multiple languages

This commit is contained in:
2025-02-09 18:31:51 +01:00
parent 47e1b47028
commit 66617923b7
11 changed files with 141 additions and 20 deletions

View File

@@ -69,6 +69,7 @@ kotlin {
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.material)
implementation(compose.material3)
implementation(compose.ui)
implementation(compose.components.resources)
implementation(compose.components.uiToolingPreview)
@@ -86,8 +87,7 @@ kotlin {
implementation(libs.composeIcons.tablerIcons)
implementation(libs.composeIcons.fontAwesome)
implementation(libs.korau)
implementation(libs.flagkit)
}
desktopMain.dependencies {
implementation(compose.desktop.currentOs)

View File

@@ -21,6 +21,8 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.runtime.Composable
import kotlinx.coroutines.flow.observeOn
import org.koin.compose.koinInject
@@ -45,6 +47,13 @@ import models.Term
import models.TermFull
import kotlinx.coroutines.*
import service.DatabaseFactory.getTranslationForLanguages
import service.DatabaseFactory.getLanguageForCode
import service.DatabaseFactory.getDictionaies
import service.DatabaseFactory.getLanguageForId
import dev.carlsen.flagkit.FlagKit
import dev.carlsen.flagkit.FlagIcons
import models.Dictionary
@Composable
fun RowScope.TableCell(
@@ -61,6 +70,42 @@ fun RowScope.TableCell(
)
}
@Composable
fun LangugeDropdown(onChangeClick: (dict: Dictionary) -> Unit,dictionary: Dictionary?) {
var expanded by remember { mutableStateOf(false) }
IconButton(onClick = {
expanded = !expanded
}) {
getLanguageForId(dictionary!!.lang1Id)?.let{ l ->
FlagKit.getFlag(countryCode = l.alphaCode!!)?.let {
Image(
imageVector = it,
contentDescription = "English",
)
}
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
for ((id, dict) in getDictionaies()) {
getLanguageForId(dict.lang1Id)?.let { l ->
DropdownMenuItem(
leadingIcon = {
Image(
imageVector = FlagKit.getFlag(countryCode = l.alphaCode!!)!!,
contentDescription = l.name
)
},
text = { Text(l.name) },
onClick = { onChangeClick(dict); expanded = false }
)
}
}
}
}
}
fun groupByString(terms: List<TermFull>, direction: Int): MutableList<MutableList<TermFull>> {
val fullList: MutableList<MutableList<TermFull>> = mutableListOf()
@@ -92,7 +137,9 @@ fun SearchBarTextField(viewModel: MainModelView) {
val query = remember { mutableStateOf("") }
var columnViewType = remember { mutableStateOf(false) }
var tDirection = remember { mutableStateOf(1) }
Column {
val dict by viewModel.dictionary.collectAsState()
Row(
verticalAlignment = Alignment.CenterVertically,
// horizontalArrangement = Arrangement.End,
@@ -103,9 +150,14 @@ fun SearchBarTextField(viewModel: MainModelView) {
value = query.value,
onValueChange = {
query.value = it
val trans = if (tDirection.value == 1) getTranslationForLanguages("an","sl") else getTranslationForLanguages("sl","an")
if (trans == null) return@TextField
viewModel.getTerms(query.value,trans)
if (dict == null) return@TextField
val l1 = getLanguageForId(dict!!.lang1Id)
val l2 = getLanguageForId(dict!!.lang2Id)
if (l1 != null && l2 != null) {
val trans = if (tDirection.value == 1) getTranslationForLanguages(l1.shortName,l2.shortName) else getTranslationForLanguages(l2.shortName,l1.shortName)
if (trans == null) return@TextField
viewModel.getTerms(query.value, trans)
}
},
placeholder = { Text("Prelož...") },
singleLine = true,
@@ -122,7 +174,7 @@ fun SearchBarTextField(viewModel: MainModelView) {
},
modifier = Modifier.weight(3f)
)
Row(Modifier.width(155.dp).border(width = 2.dp, Color.Black)) {
Row(Modifier.width(200.dp).border(width = 2.dp, Color.Black)) {
Spacer(modifier = Modifier.padding(2.dp))
IconButton(onClick = {
columnViewType.value = !columnViewType.value
@@ -157,6 +209,10 @@ fun SearchBarTextField(viewModel: MainModelView) {
modifier = Modifier.size(width = 50.dp, height = 50.dp)
)
}
LangugeDropdown(
onChangeClick = viewModel::changeDictionary,
dictionary = dict
)
Spacer(modifier = Modifier.padding(2.dp))
Icon(
Icons.Filled.Settings,
@@ -191,8 +247,14 @@ fun SearchBarTextField(viewModel: MainModelView) {
) else Color.hsl(hue = 217f, saturation = .77f, lightness = .68f, alpha = 1f)
Row(Modifier.fillMaxWidth().background(color)) {
TableCell(text = if (tDirection.value == 1) t.string1 else t.string2, weight = column1Weight)
TableCell(text = if (tDirection.value == 1) t.string2 else t.string1, weight = column2Weight)
TableCell(
text = if (tDirection.value == 1) t.string1 else t.string2,
weight = column1Weight
)
TableCell(
text = if (tDirection.value == 1) t.string2 else t.string1,
weight = column2Weight
)
}
@@ -215,7 +277,10 @@ fun SearchBarTextField(viewModel: MainModelView) {
Row(Modifier.fillMaxWidth().background(color = rowBackground)) {
Column {
Row(verticalAlignment = Alignment.CenterVertically) {
Text( if (tDirection.value == 1) t.string1 else t.string2, fontWeight = FontWeight.Bold)
Text(
if (tDirection.value == 1) t.string1 else t.string2,
fontWeight = FontWeight.Bold
)
for (p in t.pronunciations!!) {
Spacer(modifier = Modifier.padding(2.dp))
if (p.ipa != null) {
@@ -246,7 +311,10 @@ fun SearchBarTextField(viewModel: MainModelView) {
}
}
val str2 = if (tDirection.value == 1) t2.joinToString(separator = ", ") { it.string2 } else t2.joinToString(separator = ", ") { it.string1 }
val str2 =
if (tDirection.value == 1) t2.joinToString(separator = ", ") { it.string2 } else t2.joinToString(
separator = ", "
) { it.string1 }
Text(text = str2, modifier = Modifier.padding(start = 20.dp))
Spacer(
modifier = Modifier

View File

@@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import korlibs.audio.sound.*
import korlibs.io.file.std.resourcesVfs
import korlibs.io.file.std.ZipVfs
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -13,12 +14,20 @@ import service.DatabaseFactory.getTranslationForLanguages
import service.SearchType
import service.TermServiceImpl
import korlibs.audio.format.*
import korlibs.io.compression.zip.ZipEntry
import korlibs.io.file.VfsFile
import korlibs.io.file.std.openAsZip
import models.Dictionary
import models.Translation
import service.DatabaseFactory.getDictionaies
class MainModelView(private val repository: TermServiceImpl): ViewModel() {
val terms : StateFlow<List<TermFull>?> get() = _terms
private val _terms = MutableStateFlow<List<TermFull>?>(null)
val dictionary: StateFlow<Dictionary?> get() = _dictionary
private val _dictionary = MutableStateFlow<Dictionary?>(getDictionaies()[1])
fun getTerms(term: String, trans: Translation) {
viewModelScope.launch(Dispatchers.IO) {
var sType = SearchType.START
@@ -28,6 +37,11 @@ class MainModelView(private val repository: TermServiceImpl): ViewModel() {
}
}
fun changeDictionary(dict: Dictionary) {
_dictionary.value = dict
}
fun clearSearch() {
_terms.value = listOf()