Fix bugs and add direction of translation

This commit is contained in:
2025-02-02 17:00:26 +01:00
parent 52eef8dbd4
commit 47e1b47028
3 changed files with 134 additions and 100 deletions

View File

@@ -44,6 +44,7 @@ import compose.icons.tablericons.ArrowBigLeft
import models.Term import models.Term
import models.TermFull import models.TermFull
import kotlinx.coroutines.* import kotlinx.coroutines.*
import service.DatabaseFactory.getTranslationForLanguages
@Composable @Composable
fun RowScope.TableCell( fun RowScope.TableCell(
@@ -61,7 +62,7 @@ fun RowScope.TableCell(
} }
fun groupByString1(terms: List<TermFull>): MutableList<MutableList<TermFull>> { fun groupByString(terms: List<TermFull>, direction: Int): MutableList<MutableList<TermFull>> {
val fullList: MutableList<MutableList<TermFull>> = mutableListOf() val fullList: MutableList<MutableList<TermFull>> = mutableListOf()
var lastTerm: TermFull? = null var lastTerm: TermFull? = null
var termsEq: MutableList<TermFull> = mutableListOf() var termsEq: MutableList<TermFull> = mutableListOf()
@@ -69,7 +70,9 @@ fun groupByString1(terms: List<TermFull>): MutableList<MutableList<TermFull>> {
if (lastTerm == null) { if (lastTerm == null) {
termsEq += t termsEq += t
} else { } else {
if (lastTerm.string1 == t.string1){ val s1 = if (direction == 1) lastTerm.string1 else lastTerm.string2
val s2 = if (direction == 1) t.string1 else t.string2
if (s1 == s2) {
termsEq += t termsEq += t
} else { } else {
fullList += termsEq fullList += termsEq
@@ -100,7 +103,9 @@ fun SearchBarTextField(viewModel: MainModelView) {
value = query.value, value = query.value,
onValueChange = { onValueChange = {
query.value = it query.value = it
viewModel.getTerms(query.value) val trans = if (tDirection.value == 1) getTranslationForLanguages("an","sl") else getTranslationForLanguages("sl","an")
if (trans == null) return@TextField
viewModel.getTerms(query.value,trans)
}, },
placeholder = { Text("Prelož...") }, placeholder = { Text("Prelož...") },
singleLine = true, singleLine = true,
@@ -123,21 +128,41 @@ fun SearchBarTextField(viewModel: MainModelView) {
columnViewType.value = !columnViewType.value columnViewType.value = !columnViewType.value
}) { }) {
if (columnViewType.value) if (columnViewType.value)
Icon(TablerIcons.Table, contentDescription = "Table View", modifier = Modifier.size(width = 50.dp, height = 50.dp)) Icon(
TablerIcons.Table,
contentDescription = "Table View",
modifier = Modifier.size(width = 50.dp, height = 50.dp)
)
else else
Icon(TablerIcons.FileText, contentDescription = "Table View", modifier = Modifier.size(width = 50.dp, height = 50.dp)) Icon(
TablerIcons.FileText,
contentDescription = "List View",
modifier = Modifier.size(width = 50.dp, height = 50.dp)
)
} }
Spacer(modifier = Modifier.padding(2.dp)) Spacer(modifier = Modifier.padding(2.dp))
IconButton(onClick = { IconButton(onClick = {
tDirection.value = if (tDirection.value == 1) 2 else 1 tDirection.value = if (tDirection.value == 1) 2 else 1
}) { }) {
if (tDirection.value == 1) if (tDirection.value == 1)
Icon(TablerIcons.ArrowBigRight, contentDescription = "Table View", modifier = Modifier.size(width = 50.dp, height = 50.dp)) Icon(
TablerIcons.ArrowBigRight,
contentDescription = "Forward",
modifier = Modifier.size(width = 50.dp, height = 50.dp)
)
else else
Icon(TablerIcons.ArrowBigLeft, contentDescription = "Table View", modifier = Modifier.size(width = 50.dp, height = 50.dp)) Icon(
TablerIcons.ArrowBigLeft,
contentDescription = "Resverse",
modifier = Modifier.size(width = 50.dp, height = 50.dp)
)
} }
Spacer(modifier = Modifier.padding(2.dp)) Spacer(modifier = Modifier.padding(2.dp))
Icon(Icons.Filled.Settings, contentDescription = "Table View", modifier = Modifier.size(width = 50.dp, height = 50.dp)) Icon(
Icons.Filled.Settings,
contentDescription = "Table View",
modifier = Modifier.size(width = 50.dp, height = 50.dp)
)
//Spacer(modifier = Modifier.fillMaxWidth()) //Spacer(modifier = Modifier.fillMaxWidth())
} }
} }
@@ -150,19 +175,24 @@ fun SearchBarTextField(viewModel: MainModelView) {
Column { Column {
if (columnViewType.value) { if (columnViewType.value) {
Row(Modifier.background(Color.Gray)) { Row(Modifier.background(Color.Gray)) {
TableCell(text = "Anglicky", weight = .5f) TableCell(text = if (tDirection.value == 1) "Anglicky" else "Slovensky", weight = .5f)
TableCell(text = "Slovensky", weight = .5f) TableCell(text = if (tDirection.value == 1) "Slovensky" else "Anglicky", weight = .5f)
} }
val column1Weight = .5f // 50% val column1Weight = .5f // 50%
val column2Weight = .5f // 50% val column2Weight = .5f // 50%
// The LazyColumn will be our table. Notice the use of the weights below // The LazyColumn will be our table. Notice the use of the weights below
LazyColumn(Modifier.fillMaxSize().padding(0.dp)) { LazyColumn(Modifier.fillMaxSize().padding(0.dp)) {
itemsIndexed(items = terms!!, itemContent = { i, t -> itemsIndexed(items = terms!!, itemContent = { i, t ->
val color = if (i % 2 == 1) Color.hsl(hue = 168f, saturation = .77f, lightness = .68f, alpha = 1f) else Color.hsl(hue = 217f, saturation = .77f, lightness = .68f, alpha = 1f) val color = if (i % 2 == 1) Color.hsl(
hue = 168f,
saturation = .77f,
lightness = .68f,
alpha = 1f
) else Color.hsl(hue = 217f, saturation = .77f, lightness = .68f, alpha = 1f)
Row(Modifier.fillMaxWidth().background(color)) { Row(Modifier.fillMaxWidth().background(color)) {
TableCell(text = t.string1, weight = column1Weight) TableCell(text = if (tDirection.value == 1) t.string1 else t.string2, weight = column1Weight)
TableCell(text = t.string2, weight = column2Weight) TableCell(text = if (tDirection.value == 1) t.string2 else t.string1, weight = column2Weight)
} }
@@ -170,7 +200,7 @@ fun SearchBarTextField(viewModel: MainModelView) {
} }
} else { } else {
val terms2: MutableList<MutableList<TermFull>> = groupByString1(terms!!) val terms2: MutableList<MutableList<TermFull>> = groupByString(terms!!, tDirection.value)
LazyColumn(Modifier.fillMaxSize().padding(0.dp)) { LazyColumn(Modifier.fillMaxSize().padding(0.dp)) {
var rowBackground = Color.LightGray var rowBackground = Color.LightGray
@@ -185,7 +215,7 @@ fun SearchBarTextField(viewModel: MainModelView) {
Row(Modifier.fillMaxWidth().background(color = rowBackground)) { Row(Modifier.fillMaxWidth().background(color = rowBackground)) {
Column { Column {
Row(verticalAlignment = Alignment.CenterVertically) { Row(verticalAlignment = Alignment.CenterVertically) {
Text(t.string1, fontWeight = FontWeight.Bold) Text( if (tDirection.value == 1) t.string1 else t.string2, fontWeight = FontWeight.Bold)
for (p in t.pronunciations!!) { for (p in t.pronunciations!!) {
Spacer(modifier = Modifier.padding(2.dp)) Spacer(modifier = Modifier.padding(2.dp))
if (p.ipa != null) { if (p.ipa != null) {
@@ -203,21 +233,26 @@ fun SearchBarTextField(viewModel: MainModelView) {
onClick = { onClick = {
viewModel.playSound("media/${p.filename}") viewModel.playSound("media/${p.filename}")
}) { }) {
Icon(FontAwesomeIcons.Solid.PlayCircle,contentDescription = "Play sound",Modifier.size(20.dp).padding(start = 2.dp),) Icon(
FontAwesomeIcons.Solid.PlayCircle,
contentDescription = "Play sound",
Modifier.size(20.dp).padding(start = 2.dp),
)
} }
} }
Spacer(modifier = Modifier.padding(2.dp)) Spacer(modifier = Modifier.padding(2.dp))
} }
} }
val str2 = t2.joinToString(separator = ", ") { it.string2 } 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)) Text(text = str2, modifier = Modifier.padding(start = 20.dp))
Spacer(modifier = Modifier Spacer(
modifier = Modifier
.border(1.dp, Color.Black) .border(1.dp, Color.Black)
.padding(1.dp).fillMaxWidth()) .padding(1.dp).fillMaxWidth()
)
} }
} }
}) })
@@ -233,9 +268,8 @@ fun SearchBarTextField(viewModel: MainModelView) {
} }
@Composable @Composable
fun MainView(viewModel: MainModelView = koinInject<MainModelView>(),) { fun MainView(viewModel: MainModelView = koinInject<MainModelView>()) {
SearchBarTextField(viewModel) SearchBarTextField(viewModel)
} }

View File

@@ -13,16 +13,16 @@ import service.DatabaseFactory.getTranslationForLanguages
import service.SearchType import service.SearchType
import service.TermServiceImpl import service.TermServiceImpl
import korlibs.audio.format.* import korlibs.audio.format.*
import models.Translation
class MainModelView(private val repository: TermServiceImpl): ViewModel() { class MainModelView(private val repository: TermServiceImpl): ViewModel() {
val terms : StateFlow<List<TermFull>?> get() = _terms val terms : StateFlow<List<TermFull>?> get() = _terms
private val _terms = MutableStateFlow<List<TermFull>?>(null) private val _terms = MutableStateFlow<List<TermFull>?>(null)
fun getTerms(term: String) { fun getTerms(term: String, trans: Translation) {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
var sType = SearchType.START var sType = SearchType.START
sType = if (term.length > 2) SearchType.START else SearchType.EXACT sType = if (term.length > 2) SearchType.START else SearchType.EXACT
val trans = getTranslationForLanguages("an","sl") ?: return@launch
val transTerms = repository.getTranslationForTerm(term,trans,sType) val transTerms = repository.getTranslationForTerm(term,trans,sType)
_terms.value = transTerms _terms.value = transTerms

View File

@@ -47,7 +47,7 @@ class TermServiceImpl : TermService {
if (type == SearchType.EXACT) if (type == SearchType.EXACT)
TermDao.find { Terms.string2 eq s }.map { it.toFullModel() } TermDao.find { Terms.string2 eq s }.map { it.toFullModel() }
else else
TermDao.find { Terms.string1 like s }.map { it.toFullModel() } TermDao.find { Terms.string2 like s }.map { it.toFullModel() }
} }
} }