From c2ac9fc6ff9b544d0de2b447fec1b78c906ddfec Mon Sep 17 00:00:00 2001 From: Anil Kumar Beesetti <66936649+anilbeesetti@users.noreply.github.com> Date: Tue, 20 Aug 2024 09:54:16 +0530 Subject: [PATCH] Fix: Sorting of media in Tree view mode (#1056) * fix sorting of media in tree view mode * run ktlintFormat --- .../core/domain/GetSortedFolderTreeUseCase.kt | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/core/domain/src/main/java/dev/anilbeesetti/nextplayer/core/domain/GetSortedFolderTreeUseCase.kt b/core/domain/src/main/java/dev/anilbeesetti/nextplayer/core/domain/GetSortedFolderTreeUseCase.kt index 79e2b9c78..7dd067d92 100644 --- a/core/domain/src/main/java/dev/anilbeesetti/nextplayer/core/domain/GetSortedFolderTreeUseCase.kt +++ b/core/domain/src/main/java/dev/anilbeesetti/nextplayer/core/domain/GetSortedFolderTreeUseCase.kt @@ -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 { - 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 = 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 {