Skip to content

Commit

Permalink
Fix ordering issues in the component list
Browse files Browse the repository at this point in the history
Change-Id: Id1ddeb4eaa8221b46b38a690fed02ddf6e39ea07
  • Loading branch information
lihenggui committed May 1, 2023
1 parent b15bf44 commit 6f2a26a
Showing 1 changed file with 17 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ import com.merxury.blocker.core.model.data.ControllerType.SHIZUKU
import com.merxury.blocker.core.model.preference.ComponentShowPriority.DISABLED_COMPONENTS_FIRST
import com.merxury.blocker.core.model.preference.ComponentShowPriority.ENABLED_COMPONENTS_FIRST
import com.merxury.blocker.core.model.preference.ComponentShowPriority.NONE
import com.merxury.blocker.core.model.preference.ComponentSorting
import com.merxury.blocker.core.model.preference.ComponentSorting.COMPONENT_NAME
import com.merxury.blocker.core.model.preference.ComponentSorting.PACKAGE_NAME
import com.merxury.blocker.core.model.preference.ComponentSortingOrder
import com.merxury.blocker.core.model.preference.ComponentSortingOrder.ASCENDING
import com.merxury.blocker.core.model.preference.ComponentSortingOrder.DESCENDING
import com.merxury.blocker.core.rule.entity.RuleWorkResult
Expand Down Expand Up @@ -366,32 +364,29 @@ class AppDetailViewModel @Inject constructor(
},
)
}
.sortedWith(componentComparator(sorting, order))
.apply {
.let { origList ->
when (sorting) {
COMPONENT_NAME -> when (order) {
ASCENDING -> origList.sortedBy { it.simpleName.lowercase() }
DESCENDING -> origList.sortedByDescending { it.simpleName.lowercase() }
}

PACKAGE_NAME -> when (order) {
ASCENDING -> origList.sortedBy { it.name.lowercase() }
DESCENDING -> origList.sortedByDescending { it.name.lowercase() }
}
}
}
.let { sortedList ->
when (userData.componentShowPriority) {
NONE -> return@apply
DISABLED_COMPONENTS_FIRST -> sortedBy { it.enabled() }
ENABLED_COMPONENTS_FIRST -> sortedByDescending { it.enabled() }
NONE -> sortedList
DISABLED_COMPONENTS_FIRST -> sortedList.sortedBy { it.enabled() }
ENABLED_COMPONENTS_FIRST -> sortedList.sortedByDescending { it.enabled() }
}
}
.sortedByDescending { it.isRunning }
.toMutableStateList()
}

private fun componentComparator(
sorting: ComponentSorting,
order: ComponentSortingOrder,
): Comparator<ComponentItem> {
val nameComparator: Comparator<ComponentItem> = when (sorting) {
COMPONENT_NAME -> compareBy { it.simpleName }
PACKAGE_NAME -> compareBy { it.packageName }
}
return when (order) {
ASCENDING -> nameComparator
DESCENDING -> nameComparator.reversed()
}
}

private fun updateSearchKeyword() {
val keyword = appDetailArgs.searchKeyword
.map { it.trim() }
Expand Down

0 comments on commit 6f2a26a

Please sign in to comment.