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
@@ -90,59 +93,81 @@ fun SearchBarTextField(viewModel: MainModelView) {
var columnViewType = remember { mutableStateOf(false) } var columnViewType = remember { mutableStateOf(false) }
var tDirection = remember { mutableStateOf(1) } var tDirection = remember { mutableStateOf(1) }
Column { Column {
Row( Row(
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
// horizontalArrangement = Arrangement.End, // horizontalArrangement = Arrangement.End,
modifier = Modifier.background(Color.LightGray) modifier = Modifier.background(Color.LightGray)
) { ) {
TextField( TextField(
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
placeholder = { Text("Prelož...") }, viewModel.getTerms(query.value,trans)
singleLine = true, },
leadingIcon = { Icon(Icons.Filled.Search, contentDescription = "Search Icon") }, placeholder = { Text("Prelož...") },
trailingIcon = { singleLine = true,
if (query.value.isNotEmpty()) { leadingIcon = { Icon(Icons.Filled.Search, contentDescription = "Search Icon") },
IconButton(onClick = { trailingIcon = {
query.value = "" if (query.value.isNotEmpty()) {
viewModel.clearSearch() IconButton(onClick = {
}) { query.value = ""
Icon(Icons.Filled.Clear, contentDescription = "Clear Text") viewModel.clearSearch()
} }) {
} Icon(Icons.Filled.Clear, contentDescription = "Clear Text")
}, }
modifier = Modifier.weight(3f) }
) },
Row (Modifier.width(155.dp).border(width = 2.dp, Color.Black)) { modifier = Modifier.weight(3f)
Spacer(modifier = Modifier.padding(2.dp)) )
IconButton(onClick = { Row(Modifier.width(155.dp).border(width = 2.dp, Color.Black)) {
columnViewType.value = !columnViewType.value Spacer(modifier = Modifier.padding(2.dp))
}) { IconButton(onClick = {
if (columnViewType.value) columnViewType.value = !columnViewType.value
Icon(TablerIcons.Table, contentDescription = "Table View", modifier = Modifier.size(width = 50.dp, height = 50.dp)) }) {
else if (columnViewType.value)
Icon(TablerIcons.FileText, contentDescription = "Table View", modifier = Modifier.size(width = 50.dp, height = 50.dp)) Icon(
} TablerIcons.Table,
Spacer(modifier = Modifier.padding(2.dp)) contentDescription = "Table View",
IconButton(onClick = { modifier = Modifier.size(width = 50.dp, height = 50.dp)
tDirection.value = if (tDirection.value == 1) 2 else 1 )
}) { else
if (tDirection.value == 1) Icon(
Icon(TablerIcons.ArrowBigRight, contentDescription = "Table View", modifier = Modifier.size(width = 50.dp, height = 50.dp)) TablerIcons.FileText,
else contentDescription = "List View",
Icon(TablerIcons.ArrowBigLeft, contentDescription = "Table View", modifier = Modifier.size(width = 50.dp, height = 50.dp)) 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.padding(2.dp))
//Spacer(modifier = Modifier.fillMaxWidth()) 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() val terms by viewModel.terms.collectAsState()
if (terms != null) { if (terms != null) {
@@ -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,56 +200,61 @@ 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
itemsIndexed(items = terms2, itemContent = { i, t2 -> itemsIndexed(items = terms2, itemContent = { i, t2 ->
var t : TermFull? = null var t: TermFull? = null
if (t2.size > 0) t = t2[0] if (t2.size > 0) t = t2[0]
else return@itemsIndexed else return@itemsIndexed
when ( i % 2 ) { when (i % 2) {
0 -> rowBackground = Color.LightGray 0 -> rowBackground = Color.LightGray
1 -> rowBackground = Color.Gray 1 -> rowBackground = Color.Gray
} }
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) {
var color = Color.Blue var color = Color.Blue
when (p.typeId) { when (p.typeId) {
1 -> color = Color.Green 1 -> color = Color.Green
2 -> color = Color.Red 2 -> color = Color.Red
3 -> color = Color.Blue 3 -> color = Color.Blue
}
Text("[${p.ipa}]", color = color)
} }
if (p.filename != null) { Text("[${p.ipa}]", color = color)
IconButton( }
modifier = Modifier.size(20.dp), if (p.filename != null) {
onClick = { IconButton(
modifier = Modifier.size(20.dp),
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))
}
}
val str2 = t2.joinToString(separator = ", ") { it.string2 }
Text(text = str2, modifier = Modifier.padding(start = 20.dp)) }
Spacer(modifier = Modifier Spacer(modifier = Modifier.padding(2.dp))
.border(1.dp, Color.Black) }
.padding(1.dp).fillMaxWidth())
} }
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 @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() }
} }
} }