Skip to content

Commit

Permalink
Fix incorrect subcategory sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartuzen committed Dec 11, 2023
1 parent 6c51ecd commit 2a1d13e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dev.bartuzen.qbitcontroller.model.MainData
import dev.bartuzen.qbitcontroller.model.ServerState
import dev.bartuzen.qbitcontroller.model.Torrent
import dev.bartuzen.qbitcontroller.utils.formatUri
import kotlin.math.min

class MainDataDeserializer : JsonDeserializer<MainData>() {
override fun deserialize(parser: JsonParser, context: DeserializationContext): MainData {
Expand All @@ -29,7 +30,22 @@ class MainDataDeserializer : JsonDeserializer<MainData>() {
codec.readValue(codec.treeAsTokens(categories), object : TypeReference<Map<String, Category>>() {})
.values
.toList()
.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER, Category::name))
.sortedWith(
Comparator { category1, category2 ->
val category1Name = category1.name
val category2Name = category2.name

for (i in 0..<min(category1Name.length, category2Name.length)) {
when {
category1Name[i] == '/' && category2Name[i] != '/' -> return@Comparator -1
category1Name[i] != '/' && category2Name[i] == '/' -> return@Comparator 1
category1Name[i] != category2Name[i] ->
return@Comparator category1Name[i].compareTo(category2Name[i])
}
}
category1Name.length - category2Name.length
}
)
} ?: emptyList()

val tags = node["tags"]?.let { tags ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class CategoryTagAdapter(

@SuppressLint("NotifyDataSetChanged")
fun submitList(items: Map<String, Int>, allCount: Int, uncategorizedCount: Int) {
this.items = items.toSortedMap().toList()
this.items = items.toList()
this.allCount = allCount
this.uncategorizedCount = uncategorizedCount

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ class TorrentListFragment() : Fragment(R.layout.fragment_torrent_list) {
)

val stateCountMap = mutableMapOf<TorrentFilter, Int>()
val categoryMap = mainData.categories.associateBy({ it.name }, { 0 }).toSortedMap()
val categoryMap = mainData.categories.associateBy({ it.name }, { 0 }).toMutableMap()
val tagMap = mainData.tags.associateBy({ it }, { 0 }).toMutableMap()

var uncategorizedCount = 0
Expand Down

0 comments on commit 2a1d13e

Please sign in to comment.