mirror of
https://github.com/prajwalch/TorrentSearch.git
synced 2026-05-18 09:30:35 +00:00
refactor(TorrentsRepository): Remove redundant implementation for filtering results
Filtering search results is now handled by SearchViewModel. Signed-off-by: prajwalch <prajwal.chapagain58@gmail.com>
This commit is contained in:
+7
-27
@@ -33,39 +33,19 @@ class TorrentsRepository @Inject constructor(
|
||||
query = query,
|
||||
category = category,
|
||||
searchProviders = searchProviders,
|
||||
).scan(SearchResults()) { searchResults, batchResult ->
|
||||
processBatchResult(
|
||||
currentSearchResults = searchResults,
|
||||
batchResult = batchResult,
|
||||
category = category,
|
||||
)
|
||||
}.flowOn(Dispatchers.IO)
|
||||
).scan(
|
||||
initial = SearchResults(),
|
||||
operation = ::appendBatchResult,
|
||||
).flowOn(Dispatchers.IO)
|
||||
|
||||
private fun processBatchResult(
|
||||
private fun appendBatchResult(
|
||||
currentSearchResults: SearchResults,
|
||||
batchResult: Result<List<Torrent>>,
|
||||
category: Category,
|
||||
): SearchResults = batchResult.fold(
|
||||
onSuccess = {
|
||||
val newSuccesses = filterTorrentsByCategory(torrents = it, category = category)
|
||||
currentSearchResults.appendSuccesses(newSuccesses)
|
||||
},
|
||||
onFailure = {
|
||||
currentSearchResults.appendFailure(it as SearchException)
|
||||
},
|
||||
onSuccess = { currentSearchResults.appendSuccesses(it) },
|
||||
onFailure = { currentSearchResults.appendFailure(it as SearchException) },
|
||||
)
|
||||
|
||||
private fun filterTorrentsByCategory(
|
||||
torrents: List<Torrent>,
|
||||
category: Category,
|
||||
): List<Torrent> {
|
||||
return if (category == Category.All) {
|
||||
torrents
|
||||
} else {
|
||||
torrents.filter { it.category == category }
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun downloadTorrentFile(url: String): TorrentFileId {
|
||||
val id = UUID.nameUUIDFromBytes(url.toByteArray())
|
||||
if (torrentFileContentCache.containsKey(id)) return id
|
||||
|
||||
Reference in New Issue
Block a user