Skip to content

Commit

Permalink
feat: automatically scroll to selected language (closes #330)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Sep 7, 2023
1 parent 110b90f commit f5c180b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
12 changes: 11 additions & 1 deletion app/src/main/java/com/bnyro/translate/db/obj/Language.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,14 @@ data class Language(
@PrimaryKey(autoGenerate = false)
val code: String = "",
@ColumnInfo val name: String = ""
)
) {
override fun equals(other: Any?): Boolean {
(other as? Language)?.let { otherLang ->
return otherLang.name.lowercase() == this.name.lowercase() ||
otherLang.code.lowercase() == this.code.lowercase()
}
return super.equals(other)
}

override fun hashCode() = 31 * code.hashCode() + name.hashCode()
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ fun LanguageItem(
onPinnedChange: () -> Unit,
onClick: () -> Unit
) {
val isSelected = language.name.lowercase() == selectedLanguage.name.lowercase() ||
language.code.lowercase() == selectedLanguage.code.lowercase()
val isSelected = language == selectedLanguage

Card(
shape = RoundedCornerShape(30.dp),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.foundation.basicMarquee
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
Expand All @@ -32,6 +33,7 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -61,6 +63,14 @@ fun LanguageSelector(
var showDialog by remember {
mutableStateOf(false)
}
val lazyListState = rememberLazyListState()

LaunchedEffect(Unit, availableLanguages) {
// automatically scroll to the selected language
availableLanguages.indexOfFirst { it == selectedLanguage }.let {
if (it != -1) lazyListState.animateScrollToItem(it)
}
}

ElevatedButton(
modifier = Modifier
Expand Down Expand Up @@ -111,7 +121,9 @@ fun LanguageSelector(
)
},
content = {
LazyColumn {
LazyColumn(
state = lazyListState
) {
if (autoLanguageEnabled) {
item {
val autoLanguage = Language("", stringResource(R.string.auto))
Expand Down

0 comments on commit f5c180b

Please sign in to comment.