Skip to content

Commit

Permalink
Fix: Sorting of media in Tree view mode (#1056)
Browse files Browse the repository at this point in the history
* fix sorting of media in tree view mode

* run ktlintFormat
  • Loading branch information
anilbeesetti authored Aug 20, 2024
1 parent df0a8ea commit c2ac9fc
Showing 1 changed file with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ class GetSortedFolderTreeUseCase @Inject constructor(
private val preferencesRepository: PreferencesRepository,
@Dispatcher(NextDispatchers.Default) private val defaultDispatcher: CoroutineDispatcher,
) {
operator fun invoke(folderPath: String? = null): Flow<Folder?> {
return combine(
mediaRepository.getFoldersFlow(),
preferencesRepository.applicationPreferences,
) { folders, preferences ->
val folder = folderPath?.let {
folders.firstOrNull { it.path == folderPath } ?: return@combine null
} ?: Folder.rootFolder
operator fun invoke(folderPath: String? = null): Flow<Folder?> = combine(
mediaRepository.getFoldersFlow(),
preferencesRepository.applicationPreferences,
) { folders, preferences ->
val currentFolder = folderPath?.let {
folders.find { it.path == folderPath } ?: return@combine null
} ?: Folder.rootFolder

val nestedFolders = folders.getFoldersFor(path = folder.path, preferences = preferences)
val sort = Sort(by = preferences.sortBy, order = preferences.sortOrder)
val sort = Sort(by = preferences.sortBy, order = preferences.sortOrder)

return@combine Folder(
name = folder.name,
path = folder.path,
dateModified = folder.dateModified,
currentFolder.copy(
folderList = folders.getFoldersFor(path = currentFolder.path, preferences = preferences),
).let { folder ->
if (folderPath == null) folder.getInitialFolderWithContent() else folder
}.let { folder ->
folder.copy(
mediaList = folder.mediaList.sortedWith(sort.videoComparator()),
folderList = nestedFolders.sortedWith(sort.folderComparator()),
).run { if (folderPath == null) getInitialFolderWithContent() else this }
}.flowOn(defaultDispatcher)
}
folderList = folder.folderList.sortedWith(sort.folderComparator()),
)
}
}.flowOn(defaultDispatcher)

private fun Folder.getInitialFolderWithContent(): Folder {
return when {
Expand Down

0 comments on commit c2ac9fc

Please sign in to comment.