Skip to content

Commit

Permalink
#1337 refactor, fix style, and remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
sds100 committed Dec 29, 2024
1 parent 0a28629 commit 5ddba58
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import kotlinx.serialization.json.Json
class SortKeyMapsUseCase(
private val preferenceRepository: PreferenceRepository,
) {
private val defaultOrder
get() = listOf(
SortFieldOrder(SortField.TRIGGER),
SortFieldOrder(SortField.ACTIONS),
SortFieldOrder(SortField.CONSTRAINTS),
SortFieldOrder(SortField.OPTIONS),
)
private val defaultOrder = listOf(
SortFieldOrder(SortField.TRIGGER),
SortFieldOrder(SortField.ACTIONS),
SortFieldOrder(SortField.CONSTRAINTS),
SortFieldOrder(SortField.OPTIONS),
)

/**
* Observes the order in which key map fields should be sorted, prioritizing specific fields.
Expand Down Expand Up @@ -55,11 +54,12 @@ class SortKeyMapsUseCase(
}

fun observeKeyMapsSorter(): Flow<Comparator<KeyMap>> {
return observeSortFieldOrder().map { list ->
list
.filter { it.order != SortOrder.NONE }
.map(SortFieldOrder::getComparator)
}.map { Sorter(it) }
return observeSortFieldOrder()
.map { list ->
list.filter { it.order != SortOrder.NONE }
.map(SortFieldOrder::getComparator)
}
.map { Sorter(it) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,32 @@ class SortViewModel(
started = SharingStarted.WhileSubscribed(),
initialValue = runBlocking {
preferenceRepository.get(Keys.sortShowHelp).first()
} ?: true
} ?: true,
)

val state: MutableStateFlow<List<SortFieldOrder>> = MutableStateFlow(
runBlocking { sortKeyMapsUseCase.observeSortFieldOrder().first() }
)
val sortFieldOrder: MutableStateFlow<List<SortFieldOrder>> = MutableStateFlow(emptyList())

init {
saveState()
// Set the initial value of the sort field order to whatever is saved.
// The modified value will be saved when they click Apply.
viewModelScope.launch {
sortFieldOrder.value = sortKeyMapsUseCase.observeSortFieldOrder().first()
}
}

fun swapSortPriority(fromIndex: Int, toIndex: Int) {
state.update {
sortFieldOrder.update {
val newList = it.toMutableList()
newList.add(toIndex, newList.removeAt(fromIndex))
newList
}
}

fun toggleSortOrder(field: SortField) {
state.update {
val index = it.indexOfFirst { it.field == field }
sortFieldOrder.update { sortFieldOrder ->
val index = sortFieldOrder.indexOfFirst { it.field == field }

it.mapIndexed { i, sortFieldOrder ->
sortFieldOrder.mapIndexed { i, sortFieldOrder ->
if (i != index) {
return@mapIndexed sortFieldOrder
}
Expand All @@ -60,35 +62,28 @@ class SortViewModel(
}

fun resetSortPriority() {
state.update {
sortFieldOrder.update {
it.map { sortFieldOrder ->
sortFieldOrder.copy(order = SortOrder.NONE)
}
}
}

fun applySortPriority() {
viewModelScope.launch {
sortKeyMapsUseCase.setSortFieldOrder(state.value)
saveState()
}
sortKeyMapsUseCase.setSortFieldOrder(sortFieldOrder.value)
}

fun setShowHelp(show: Boolean) {
viewModelScope.launch {
preferenceRepository.set(Keys.sortShowHelp, show)
}
preferenceRepository.set(Keys.sortShowHelp, show)
}

fun showExample() {
viewModelScope.launch {
state.value = listOf(
SortFieldOrder(SortField.ACTIONS, SortOrder.ASCENDING),
SortFieldOrder(SortField.TRIGGER, SortOrder.DESCENDING),
SortFieldOrder(SortField.CONSTRAINTS),
SortFieldOrder(SortField.OPTIONS),
)
}
sortFieldOrder.value = listOf(
SortFieldOrder(SortField.ACTIONS, SortOrder.ASCENDING),
SortFieldOrder(SortField.TRIGGER, SortOrder.DESCENDING),
SortFieldOrder(SortField.CONSTRAINTS),
SortFieldOrder(SortField.OPTIONS),
)
}

class Factory(
Expand All @@ -102,16 +97,4 @@ class SortViewModel(
preferenceRepository,
) as T
}

private var savedState: List<SortFieldOrder>? = null

private fun saveState(toSave: List<SortFieldOrder> = state.value) {
savedState = toSave.toList()
}

fun restoreState() {
savedState?.let {
state.value = it
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class KeyMapTriggerComparator(

// Compare keys one by one
for (i in 0 until maxLength) {
val key = trigger.keys.elementAt(i)
val otherKey = otherTrigger.keys.elementAt(i)
val key = trigger.keys[i]
val otherKey = otherTrigger.keys[i]

val result = key.compareTo(otherKey)

Expand Down
Loading

0 comments on commit 5ddba58

Please sign in to comment.