From a0798bcd295ca63334dbbb673c4c4b2ab74c056c Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 13 Dec 2018 16:20:00 +0100 Subject: [PATCH] unify the directories funnel through which they are sorted, filtered etc --- .../gallery/pro/activities/MainActivity.kt | 54 +++++++------------ 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt index 699fcf19f..b7bdd5d4d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt @@ -313,7 +313,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { override fun onQueryTextChange(newText: String): Boolean { if (mIsSearchOpen) { - searchQueryChanged(newText) + setupAdapter(mDirs, newText) } return true } @@ -332,25 +332,13 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { if (mIsSearchOpen) { mIsSearchOpen = false directories_refresh_layout.isEnabled = config.enablePullToRefresh - searchQueryChanged("") + setupAdapter(mDirs, "") } return true } }) } - private fun searchQueryChanged(text: String) { - Thread { - val filtered = getUniqueSortedDirs(mDirs).filter { it.name.contains(text, true) } as ArrayList - filtered.sortBy { !it.name.startsWith(text, true) } - - runOnUiThread { - getRecyclerAdapter()?.updateDirs(filtered) - measureRecyclerViewContent(filtered) - } - }.start() - } - private fun removeTempFolder() { if (config.tempFolderPath.isNotEmpty()) { val newFolder = File(config.tempFolderPath) @@ -828,7 +816,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { types = newDir.types } - showSortedDirs(dirs) + setupAdapter(dirs) // update directories and media files in the local db, delete invalid items updateDBDirectory(directory, mDirectoryDao) @@ -855,7 +843,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { mDirectoryDao.deleteDirPath(it.path) } dirs.removeAll(dirsToRemove) - showSortedDirs(dirs) + setupAdapter(dirs) } val foldersToScan = mediaFetcher.getFoldersToScan() @@ -892,7 +880,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { val newDir = createDirectoryFromMedia(folder, newMedia, albumCovers, hiddenString, includedFolders, isSortingAscending) dirs.add(newDir) - showSortedDirs(dirs) + setupAdapter(dirs) mDirectoryDao.insert(newDir) if (folder != RECYCLE_BIN) { mMediumDao.insertAll(newMedia) @@ -964,14 +952,6 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { directories_grid.beVisibleIf(directories_empty_text_label.isGone()) } - private fun showSortedDirs(dirs: ArrayList, checkSubfolders: Boolean = true) { - val updatedDirs = getUniqueSortedDirs(dirs).toMutableList() as ArrayList - val dirsToShow = if (checkSubfolders) getDirsToShow(updatedDirs) else updatedDirs - runOnUiThread { - (directories_grid.adapter as? DirectoryAdapter)?.updateDirs(dirsToShow) - } - } - private fun getDirsToShow(dirs: ArrayList): ArrayList { return if (config.groupDirectSubfolders) { val dirFolders = dirs.map { it.path }.sorted().toMutableSet() as HashSet @@ -1015,29 +995,33 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { return Directory(null, path, thumbnail, dirName, curMedia.size, lastModified, dateTaken, size, getPathLocation(path), mediaTypes) } - private fun setupAdapter(dirs: ArrayList) { + private fun setupAdapter(dirs: ArrayList, textToSearch: String = "") { val currAdapter = directories_grid.adapter - val dirsToShow = getDirsToShow(dirs) + val updatedDirs = getUniqueSortedDirs(dirs).toMutableList() as ArrayList + var dirsToShow = getDirsToShow(updatedDirs).clone() as ArrayList if (currAdapter == null) { initZoomListener() val fastscroller = if (config.scrollHorizontally) directories_horizontal_fastscroller else directories_vertical_fastscroller - DirectoryAdapter(this, dirsToShow.clone() as ArrayList, this, directories_grid, isPickIntent(intent) || isGetAnyContentIntent(intent), fastscroller) { + DirectoryAdapter(this, dirsToShow, this, directories_grid, isPickIntent(intent) || isGetAnyContentIntent(intent), fastscroller) { val path = (it as Directory).path if (path != config.tempFolderPath) { itemClicked(path) } + measureRecyclerViewContent(dirsToShow) }.apply { setupZoomListener(mZoomListener) directories_grid.adapter = this } + setupScrollDirection() } else { - showSortedDirs(dirsToShow, false) + if (textToSearch.isNotEmpty()) { + dirsToShow = dirsToShow.filter { it.name.contains(textToSearch, true) }.sortedBy { !it.name.startsWith(textToSearch, true) }.toMutableList() as ArrayList + } + runOnUiThread { + (directories_grid.adapter as? DirectoryAdapter)?.updateDirs(dirsToShow) + measureRecyclerViewContent(dirsToShow) + } } - - getRecyclerAdapter()?.dirs?.apply { - measureRecyclerViewContent(this) - } - setupScrollDirection() } private fun setupScrollDirection() { @@ -1091,7 +1075,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { if (invalidDirs.isNotEmpty()) { dirs.removeAll(invalidDirs) - showSortedDirs(dirs) + setupAdapter(dirs) invalidDirs.forEach { mDirectoryDao.deleteDirPath(it.path) }