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.TermFull
import kotlinx.coroutines.*
import service.DatabaseFactory.getTranslationForLanguages
@Composable
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()
var lastTerm: TermFull? = null
var termsEq: MutableList<TermFull> = mutableListOf()
@@ -69,7 +70,9 @@ fun groupByString1(terms: List<TermFull>): MutableList<MutableList<TermFull>> {
if (lastTerm == null) {
termsEq += t
} 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
} else {
fullList += termsEq
@@ -90,59 +93,81 @@ fun SearchBarTextField(viewModel: MainModelView) {
var columnViewType = remember { mutableStateOf(false) }
var tDirection = remember { mutableStateOf(1) }
Column {
Row(
verticalAlignment = Alignment.CenterVertically,
Row(
verticalAlignment = Alignment.CenterVertically,
// horizontalArrangement = Arrangement.End,
modifier = Modifier.background(Color.LightGray)
) {
modifier = Modifier.background(Color.LightGray)
) {
TextField(
value = query.value,
onValueChange = {
query.value = it
viewModel.getTerms(query.value)
},
placeholder = { Text("Prelož...") },
singleLine = true,
leadingIcon = { Icon(Icons.Filled.Search, contentDescription = "Search Icon") },
trailingIcon = {
if (query.value.isNotEmpty()) {
IconButton(onClick = {
query.value = ""
viewModel.clearSearch()
}) {
Icon(Icons.Filled.Clear, contentDescription = "Clear Text")
}
}
},
modifier = Modifier.weight(3f)
)
Row (Modifier.width(155.dp).border(width = 2.dp, Color.Black)) {
Spacer(modifier = Modifier.padding(2.dp))
IconButton(onClick = {
columnViewType.value = !columnViewType.value
}) {
if (columnViewType.value)
Icon(TablerIcons.Table, contentDescription = "Table View", modifier = Modifier.size(width = 50.dp, height = 50.dp))
else
Icon(TablerIcons.FileText, contentDescription = "Table View", modifier = Modifier.size(width = 50.dp, height = 50.dp))
}
Spacer(modifier = Modifier.padding(2.dp))
IconButton(onClick = {
tDirection.value = if (tDirection.value == 1) 2 else 1
}) {
if (tDirection.value == 1)
Icon(TablerIcons.ArrowBigRight, contentDescription = "Table View", modifier = Modifier.size(width = 50.dp, height = 50.dp))
else
Icon(TablerIcons.ArrowBigLeft, contentDescription = "Table View", modifier = Modifier.size(width = 50.dp, height = 50.dp))
}
Spacer(modifier = Modifier.padding(2.dp))
Icon(Icons.Filled.Settings, contentDescription = "Table View", modifier = Modifier.size(width = 50.dp, height = 50.dp))
//Spacer(modifier = Modifier.fillMaxWidth())
}
TextField(
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)
},
placeholder = { Text("Prelož...") },
singleLine = true,
leadingIcon = { Icon(Icons.Filled.Search, contentDescription = "Search Icon") },
trailingIcon = {
if (query.value.isNotEmpty()) {
IconButton(onClick = {
query.value = ""
viewModel.clearSearch()
}) {
Icon(Icons.Filled.Clear, contentDescription = "Clear Text")
}
}
},
modifier = Modifier.weight(3f)
)
Row(Modifier.width(155.dp).border(width = 2.dp, Color.Black)) {
Spacer(modifier = Modifier.padding(2.dp))
IconButton(onClick = {
columnViewType.value = !columnViewType.value
}) {
if (columnViewType.value)
Icon(
TablerIcons.Table,
contentDescription = "Table View",
modifier = Modifier.size(width = 50.dp, height = 50.dp)
)
else
Icon(
TablerIcons.FileText,
contentDescription = "List View",
modifier = Modifier.size(width = 50.dp, height = 50.dp)
)
}
Spacer(modifier = Modifier.padding(2.dp))
IconButton(onClick = {
tDirection.value = if (tDirection.value == 1) 2 else 1
}) {
if (tDirection.value == 1)
Icon(
TablerIcons.ArrowBigRight,
contentDescription = "Forward",
modifier = Modifier.size(width = 50.dp, height = 50.dp)
)
else
Icon(
TablerIcons.ArrowBigLeft,
contentDescription = "Resverse",
modifier = Modifier.size(width = 50.dp, height = 50.dp)
)
}
Spacer(modifier = Modifier.padding(2.dp))
Icon(
Icons.Filled.Settings,
contentDescription = "Table View",
modifier = Modifier.size(width = 50.dp, height = 50.dp)
)
//Spacer(modifier = Modifier.fillMaxWidth())
}
}
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally ) {
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
val terms by viewModel.terms.collectAsState()
if (terms != null) {
@@ -150,19 +175,24 @@ fun SearchBarTextField(viewModel: MainModelView) {
Column {
if (columnViewType.value) {
Row(Modifier.background(Color.Gray)) {
TableCell(text = "Anglicky", weight = .5f)
TableCell(text = "Slovensky", weight = .5f)
TableCell(text = if (tDirection.value == 1) "Anglicky" else "Slovensky", weight = .5f)
TableCell(text = if (tDirection.value == 1) "Slovensky" else "Anglicky", weight = .5f)
}
val column1Weight = .5f // 50%
val column2Weight = .5f // 50%
// The LazyColumn will be our table. Notice the use of the weights below
LazyColumn(Modifier.fillMaxSize().padding(0.dp)) {
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)) {
TableCell(text = t.string1, weight = column1Weight)
TableCell(text = t.string2, 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)
}
@@ -170,56 +200,61 @@ fun SearchBarTextField(viewModel: MainModelView) {
}
} else {
val terms2: MutableList<MutableList<TermFull>> = groupByString1(terms!!)
val terms2: MutableList<MutableList<TermFull>> = groupByString(terms!!, tDirection.value)
LazyColumn(Modifier.fillMaxSize().padding(0.dp)) {
var rowBackground = Color.LightGray
itemsIndexed(items = terms2, itemContent = { i, t2 ->
var t : TermFull? = null
if (t2.size > 0) t = t2[0]
else return@itemsIndexed
when ( i % 2 ) {
0 -> rowBackground = Color.LightGray
1 -> rowBackground = Color.Gray
}
Row(Modifier.fillMaxWidth().background(color = rowBackground)) {
Column {
Row( verticalAlignment = Alignment.CenterVertically) {
Text(t.string1, fontWeight = FontWeight.Bold)
for (p in t.pronunciations!!) {
Spacer(modifier = Modifier.padding(2.dp))
if (p.ipa != null) {
var color = Color.Blue
when (p.typeId) {
1 -> color = Color.Green
2 -> color = Color.Red
3 -> color = Color.Blue
}
Text("[${p.ipa}]", color = color)
var t: TermFull? = null
if (t2.size > 0) t = t2[0]
else return@itemsIndexed
when (i % 2) {
0 -> rowBackground = Color.LightGray
1 -> rowBackground = Color.Gray
}
Row(Modifier.fillMaxWidth().background(color = rowBackground)) {
Column {
Row(verticalAlignment = Alignment.CenterVertically) {
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) {
var color = Color.Blue
when (p.typeId) {
1 -> color = Color.Green
2 -> color = Color.Red
3 -> color = Color.Blue
}
if (p.filename != null) {
IconButton(
modifier = Modifier.size(20.dp),
onClick = {
Text("[${p.ipa}]", color = color)
}
if (p.filename != null) {
IconButton(
modifier = Modifier.size(20.dp),
onClick = {
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))
}
}
val str2 = t2.joinToString(separator = ", ") { it.string2 }
Text(text = str2, modifier = Modifier.padding(start = 20.dp))
Spacer(modifier = Modifier
.border(1.dp, Color.Black)
.padding(1.dp).fillMaxWidth())
}
Spacer(modifier = Modifier.padding(2.dp))
}
}
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
.border(1.dp, Color.Black)
.padding(1.dp).fillMaxWidth()
)
}
}
})
}
}
@@ -233,9 +268,8 @@ fun SearchBarTextField(viewModel: MainModelView) {
}
@Composable
fun MainView(viewModel: MainModelView = koinInject<MainModelView>(),) {
fun MainView(viewModel: MainModelView = koinInject<MainModelView>()) {
SearchBarTextField(viewModel)
}

View File

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