Move suggestion generation to ViewModel.
This commit is contained in:
@@ -31,15 +31,6 @@ fun TypeaheadTextField(
|
|||||||
var isFocused by remember { mutableStateOf(false) }
|
var isFocused by remember { mutableStateOf(false) }
|
||||||
val focusManager = LocalFocusManager.current
|
val focusManager = LocalFocusManager.current
|
||||||
|
|
||||||
var filtered = items.filter { it.isNotEmpty() && it != value }
|
|
||||||
|
|
||||||
if(value.isNotEmpty())
|
|
||||||
{
|
|
||||||
filtered = filtered.filter { it.contains(value) }
|
|
||||||
}
|
|
||||||
|
|
||||||
filtered = filtered.sorted().sortedBy { it.length }.take(5)
|
|
||||||
|
|
||||||
ExposedDropdownMenuBox(
|
ExposedDropdownMenuBox(
|
||||||
expanded = isFocused,
|
expanded = isFocused,
|
||||||
modifier = modifier,
|
modifier = modifier,
|
||||||
@@ -79,13 +70,13 @@ fun TypeaheadTextField(
|
|||||||
colors = colors
|
colors = colors
|
||||||
)
|
)
|
||||||
ExposedDropdownMenu(
|
ExposedDropdownMenu(
|
||||||
expanded = isFocused && filtered.isNotEmpty(),
|
expanded = isFocused && items.isNotEmpty(),
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.width(with(LocalDensity.current){dropDownWidth.toDp()}),
|
.width(with(LocalDensity.current){dropDownWidth.toDp()}),
|
||||||
onDismissRequest = { }
|
onDismissRequest = { }
|
||||||
) {
|
) {
|
||||||
|
|
||||||
filtered.forEach {
|
items.forEach {
|
||||||
DropdownMenuItem(
|
DropdownMenuItem(
|
||||||
onClick = {
|
onClick = {
|
||||||
onValueChange(it)
|
onValueChange(it)
|
||||||
|
|||||||
@@ -35,7 +35,8 @@ fun Landscape(viewModel: ICounterViewModel) {
|
|||||||
TeamNamesView(
|
TeamNamesView(
|
||||||
viewModel.teamNameA,
|
viewModel.teamNameA,
|
||||||
viewModel.teamNameB,
|
viewModel.teamNameB,
|
||||||
viewModel.distinctTeamNames,
|
viewModel.teamNameSuggestionsA,
|
||||||
|
viewModel.teamNameSuggestionsB,
|
||||||
{ viewModel.updateNameA(it) },
|
{ viewModel.updateNameA(it) },
|
||||||
{ viewModel.updateNameB(it) }
|
{ viewModel.updateNameB(it) }
|
||||||
)
|
)
|
||||||
@@ -65,7 +66,8 @@ fun Portrait(viewModel: ICounterViewModel) {
|
|||||||
TeamNamesView(
|
TeamNamesView(
|
||||||
viewModel.teamNameA,
|
viewModel.teamNameA,
|
||||||
viewModel.teamNameB,
|
viewModel.teamNameB,
|
||||||
viewModel.distinctTeamNames,
|
viewModel.teamNameSuggestionsA,
|
||||||
|
viewModel.teamNameSuggestionsB,
|
||||||
{ viewModel.updateNameA(it) },
|
{ viewModel.updateNameA(it) },
|
||||||
{ viewModel.updateNameB(it) }
|
{ viewModel.updateNameB(it) }
|
||||||
)
|
)
|
||||||
@@ -113,7 +115,8 @@ internal class PreviewViewModel : ICounterViewModel {
|
|||||||
override var activeValue: String = currentScoreA
|
override var activeValue: String = currentScoreA
|
||||||
override var inactiveValue: String = currentScoreB
|
override var inactiveValue: String = currentScoreB
|
||||||
override var keyboardHidden: Boolean = false
|
override var keyboardHidden: Boolean = false
|
||||||
override val distinctTeamNames: List<String> = listOf("TeamA", "asdffd", "TeamB", "really really long Team Name that is way too long")
|
override val teamNameSuggestionsA: List<String> = listOf("TeamA", "asdffd", "TeamB", "really really long Team Name that is way too long")
|
||||||
|
override val teamNameSuggestionsB: List<String> = listOf("TeamA", "asdffd", "TeamB", "really really long Team Name that is way too long")
|
||||||
|
|
||||||
override fun focusLastInput() {
|
override fun focusLastInput() {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ interface IKeyBoardViewModel {
|
|||||||
val activeValue: String
|
val activeValue: String
|
||||||
val inactiveValue: String
|
val inactiveValue: String
|
||||||
val keyboardHidden: Boolean
|
val keyboardHidden: Boolean
|
||||||
val distinctTeamNames: List<String>
|
|
||||||
|
|
||||||
fun focusLastInput()
|
fun focusLastInput()
|
||||||
fun updateOtherScore()
|
fun updateOtherScore()
|
||||||
@@ -59,6 +58,8 @@ interface ICounterViewModel : IKeyBoardViewModel {
|
|||||||
val totalScoreB: Int
|
val totalScoreB: Int
|
||||||
val teamNameA: String
|
val teamNameA: String
|
||||||
val teamNameB: String
|
val teamNameB: String
|
||||||
|
val teamNameSuggestionsA: List<String>
|
||||||
|
val teamNameSuggestionsB: List<String>
|
||||||
|
|
||||||
fun updateNameA(value: String)
|
fun updateNameA(value: String)
|
||||||
fun updateNameB(value: String)
|
fun updateNameB(value: String)
|
||||||
@@ -109,7 +110,10 @@ class CounterViewModel @Inject constructor(
|
|||||||
override var keyboardHidden by mutableStateOf(false)
|
override var keyboardHidden by mutableStateOf(false)
|
||||||
private set
|
private set
|
||||||
|
|
||||||
override var distinctTeamNames by mutableStateOf(listOf<String>())
|
override var teamNameSuggestionsA by mutableStateOf(listOf<String>())
|
||||||
|
private set
|
||||||
|
|
||||||
|
override var teamNameSuggestionsB by mutableStateOf(listOf<String>())
|
||||||
private set
|
private set
|
||||||
|
|
||||||
override var activeValue: String
|
override var activeValue: String
|
||||||
@@ -151,6 +155,8 @@ class CounterViewModel @Inject constructor(
|
|||||||
|
|
||||||
private var deleteJob: Job? = null
|
private var deleteJob: Job? = null
|
||||||
|
|
||||||
|
private var distinctTeamNames = listOf<String>()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
gameRepository.getActiveGameFlow().collect {
|
gameRepository.getActiveGameFlow().collect {
|
||||||
@@ -164,6 +170,9 @@ class CounterViewModel @Inject constructor(
|
|||||||
|
|
||||||
teamNameA = it.game.nameA
|
teamNameA = it.game.nameA
|
||||||
teamNameB = it.game.nameB
|
teamNameB = it.game.nameB
|
||||||
|
|
||||||
|
buildTeamNameSuggestions()
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -171,6 +180,8 @@ class CounterViewModel @Inject constructor(
|
|||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
gameRepository.getDistinctTeamNames().collect() {
|
gameRepository.getDistinctTeamNames().collect() {
|
||||||
distinctTeamNames = it
|
distinctTeamNames = it
|
||||||
|
|
||||||
|
buildTeamNameSuggestions()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -339,4 +350,20 @@ class CounterViewModel @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun buildTeamNameSuggestions(){
|
||||||
|
teamNameSuggestionsA = buildTypeaheadList(distinctTeamNames, teamNameA)
|
||||||
|
teamNameSuggestionsB = buildTypeaheadList(distinctTeamNames, teamNameB)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun buildTypeaheadList(rawList: List<String>, currentInput: String ): List<String> {
|
||||||
|
var filtered = rawList.filter { it.isNotEmpty() && it != currentInput }
|
||||||
|
|
||||||
|
if(currentInput.isNotEmpty())
|
||||||
|
{
|
||||||
|
filtered = filtered.filter { it.contains(currentInput) }
|
||||||
|
}
|
||||||
|
|
||||||
|
return filtered.sorted().sortedBy { it.length }.take(10)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -19,7 +19,8 @@ import me.zobrist.tichucounter.ui.composables.TypeaheadTextField
|
|||||||
fun TeamNamesView(
|
fun TeamNamesView(
|
||||||
nameA: String,
|
nameA: String,
|
||||||
nameB: String,
|
nameB: String,
|
||||||
nameSuggestions: List<String>,
|
nameSuggestionsA: List<String>,
|
||||||
|
nameSuggestionsB: List<String>,
|
||||||
updateA: (String) -> Unit,
|
updateA: (String) -> Unit,
|
||||||
updateB: (String) -> Unit
|
updateB: (String) -> Unit
|
||||||
) {
|
) {
|
||||||
@@ -32,7 +33,7 @@ fun TeamNamesView(
|
|||||||
|
|
||||||
TypeaheadTextField(
|
TypeaheadTextField(
|
||||||
value = nameA,
|
value = nameA,
|
||||||
items = nameSuggestions,
|
items = nameSuggestionsA,
|
||||||
onValueChange = { updateA(it) },
|
onValueChange = { updateA(it) },
|
||||||
modifier = Modifier.weight(1f),
|
modifier = Modifier.weight(1f),
|
||||||
colors = color,
|
colors = color,
|
||||||
@@ -41,7 +42,7 @@ fun TeamNamesView(
|
|||||||
|
|
||||||
TypeaheadTextField(
|
TypeaheadTextField(
|
||||||
value = nameB,
|
value = nameB,
|
||||||
items = nameSuggestions,
|
items = nameSuggestionsB,
|
||||||
onValueChange = { updateB(it) },
|
onValueChange = { updateB(it) },
|
||||||
modifier = Modifier.weight(1f),
|
modifier = Modifier.weight(1f),
|
||||||
colors = color,
|
colors = color,
|
||||||
@@ -57,6 +58,6 @@ fun TeamNamesView(
|
|||||||
@Composable
|
@Composable
|
||||||
private fun TeamNamesViewPreview() {
|
private fun TeamNamesViewPreview() {
|
||||||
AppTheme {
|
AppTheme {
|
||||||
TeamNamesView("TeamA", "TeamB", listOf("Test1", "Test3"), {}, {})
|
TeamNamesView("TeamA", "TeamB", listOf("Test1", "Test3"), listOf("Test3", "Test5"), {}, {})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user