Skip to content

Commit

Permalink
Fix torrents filters
Browse files Browse the repository at this point in the history
  • Loading branch information
equeim committed Oct 25, 2023
1 parent aed7848 commit 1112c90
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ class DirectoriesViewAdapter(
return directories[position].path
}

fun update(torrents: List<Torrent>, allTorrentsCount: Int, currentDirectoryPath: String) {
fun update(torrents: List<Torrent>, currentDirectoryPath: String) {
directories = torrents.groupingBy { it.downloadDirectory }
.eachCount()
.mapTo(
mutableListOf(DirectoryItem(null, null, allTorrentsCount))
mutableListOf(DirectoryItem(null, null, torrents.size))
) { (path, torrents) -> DirectoryItem(path.value, path.toNativeSeparators(), torrents) }
.apply { sortWith(comparator) }
currentDirectoryIndex = if (currentDirectoryPath.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ class StatusFilterViewAdapter(
return getItem(statusFilterMode.ordinal)
}

fun update(torrents: List<Torrent>, allTorrentsCount: Int, statusFilterMode: StatusFilterMode) {
fun update(torrents: List<Torrent>, statusFilterMode: StatusFilterMode) {
this.statusFilterMode = statusFilterMode

allTorrents = allTorrentsCount
allTorrents = torrents.size
activeTorrents = 0
downloadingTorrents = 0
seedingTorrents = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,21 @@ class TorrentsFiltersDialogFragment : NavigationBottomSheetDialogFragment(R.layo
updateSortView(sortOrder, sortMode)
}

val torrents = model.torrentsListState.map { (it as? RpcRequestState.Loaded)?.response.orEmpty() }
val allTorrents = model.allTorrents.map { (it as? RpcRequestState.Loaded)?.response.orEmpty() }

combine(torrents, model.allTorrentsCount, model.statusFilterMode, ::Triple)
.launchAndCollectWhenStarted(viewLifecycleOwner) { (torrents, allTorrentsCount, statusFilterMode) ->
statusFilterViewAdapter.update(torrents, allTorrentsCount, statusFilterMode)
combine(allTorrents, model.statusFilterMode, ::Pair)
.launchAndCollectWhenStarted(viewLifecycleOwner) { (torrents, statusFilterMode) ->
statusFilterViewAdapter.update(torrents, statusFilterMode)
}

combine(torrents, model.allTorrentsCount, model.trackerFilter, ::Triple)
.launchAndCollectWhenStarted(viewLifecycleOwner) { (torrents, allTorrentsCount, trackerFilter) ->
trackersViewAdapter.update(torrents, allTorrentsCount, trackerFilter)
combine(allTorrents, model.trackerFilter, ::Pair)
.launchAndCollectWhenStarted(viewLifecycleOwner) { (torrents, trackerFilter) ->
trackersViewAdapter.update(torrents, trackerFilter)
}

combine(torrents, model.allTorrentsCount, model.directoryFilter, ::Triple)
.launchAndCollectWhenStarted(viewLifecycleOwner) { (torrents, allTorrentsCount, directoryFilter) ->
directoriesViewAdapter.update(torrents, allTorrentsCount, directoryFilter)
combine(allTorrents, model.directoryFilter, ::Pair)
.launchAndCollectWhenStarted(viewLifecycleOwner) { (torrents, directoryFilter) ->
directoriesViewAdapter.update(torrents, directoryFilter)
}

TooltipCompat.setTooltipText(resetButton, resetButton.contentDescription)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class TorrentsListFragmentViewModel(application: Application, savedStateHandle:
private val _torrentsLoadedEvents = MutableSharedFlow<Unit>()
val torrentsLoadedEvents: Flow<Unit> by ::_torrentsLoadedEvents

private val allTorrents: StateFlow<RpcRequestState<List<Torrent>>> =
val allTorrents: StateFlow<RpcRequestState<List<Torrent>>> =
GlobalRpcClient.performPeriodicRequest(refreshRequests) { getTorrentsList() }
.onEach {
when (it) {
Expand All @@ -184,10 +184,6 @@ class TorrentsListFragmentViewModel(application: Application, savedStateHandle:
val torrentsListState: StateFlow<RpcRequestState<List<Torrent>>> =
allTorrents.filterAndSortTorrents().stateIn(GlobalRpcClient, viewModelScope)

val allTorrentsCount: StateFlow<Int> = allTorrents
.map { (it as? RpcRequestState.Loaded)?.response?.size ?: 0 }
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(1000), 0)

val sortSettingsChanged: Flow<Unit> = combine(sortMode, sortOrder, ::Pair)
.drop(1)
.transform { emit(Unit) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ class TrackersViewAdapter(
return trackers[position].tracker
}

fun update(torrents: List<Torrent>, allTorrentsCount: Int, trackerFilter: String) {
fun update(torrents: List<Torrent>, trackerFilter: String) {
trackers = torrents
.asSequence()
.flatMap { torrent -> torrent.trackerSites.asSequence().map { torrent to it } }
.groupingBy { (_, tracker) -> tracker }
.eachCount()
.mapTo(mutableListOf(TrackerItem(null, allTorrentsCount))) { (tracker, torrents) ->
.mapTo(mutableListOf(TrackerItem(null, torrents.size))) { (tracker, torrents) ->
TrackerItem(
tracker,
torrents
Expand Down

0 comments on commit 1112c90

Please sign in to comment.