From c75f564a35d626452b1e8ef110282b4d073bf689 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 27 Jun 2018 12:05:36 +0200 Subject: [PATCH] show a Recycle Bin folder on the main screen if not empty --- .../gallery/activities/MainActivity.kt | 17 +++++++++++++---- .../gallery/activities/MediaActivity.kt | 12 +++++++----- .../gallery/activities/ViewPagerActivity.kt | 10 +++++++--- .../gallery/adapters/DirectoryAdapter.kt | 4 ++-- .../gallery/extensions/Context.kt | 15 ++++++++++++++- .../gallery/helpers/Constants.kt | 1 + .../gallery/helpers/MediaFetcher.kt | 13 +++++-------- .../gallery/models/Directory.kt | 3 +++ 8 files changed, 52 insertions(+), 23 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index 580599a23..c4bc687b5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -679,7 +679,9 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { // update directories and media files in the local db, delete invalid items updateDBDirectory(directory) - mediumDao.insertAll(curMedia) + if (!directory.isRecycleBin()) { + mediumDao.insertAll(curMedia) + } getCachedMedia(directory.path, getVideosOnly, getImagesOnly) { it.forEach { if (!curMedia.contains(it)) { @@ -696,6 +698,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { val foldersToScan = mediaFetcher.getFoldersToScan() foldersToScan.add(FAVORITES) + foldersToScan.add(RECYCLE_BIN) dirs.forEach { foldersToScan.remove(it.path) } @@ -720,7 +723,9 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { dirs.add(newDir) showSortedDirs(dirs) directoryDao.insert(newDir) - mediumDao.insertAll(newMedia) + if (folder != RECYCLE_BIN) { + mediumDao.insertAll(newMedia) + } } mIsGettingDirs = false @@ -764,7 +769,11 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } val mediaTypes = curMedia.getDirMediaTypes() - val dirName = if (path == FAVORITES) getString(R.string.favorites) else checkAppendingHidden(path, hiddenString, includedFolders) + val dirName = when (path) { + FAVORITES -> getString(R.string.favorites) + RECYCLE_BIN -> getString(R.string.recycle_bin) + else -> checkAppendingHidden(path, hiddenString, includedFolders) + } val firstItem = curMedia.first() val lastItem = curMedia.last() @@ -822,7 +831,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { private fun checkInvalidDirectories(dirs: ArrayList, directoryDao: DirectoryDao) { val invalidDirs = ArrayList() - dirs.filter { !it.areFavorites() }.forEach { + dirs.filter { !it.areFavorites() && !it.isRecycleBin() }.forEach { if (!getDoesFilePathExist(it.path)) { invalidDirs.add(it) } else if (it.path != config.tempFolderPath) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index 1a8234c7d..934b7263b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -194,9 +194,9 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { menu.apply { findItem(R.id.group).isVisible = !config.scrollHorizontally - findItem(R.id.hide_folder).isVisible = !isFolderHidden && !mShowAll && mPath != FAVORITES - findItem(R.id.unhide_folder).isVisible = isFolderHidden && !mShowAll && mPath != FAVORITES - findItem(R.id.exclude_folder).isVisible = !mShowAll && mPath != FAVORITES + findItem(R.id.hide_folder).isVisible = !isFolderHidden && !mShowAll && mPath != FAVORITES && mPath != RECYCLE_BIN + findItem(R.id.unhide_folder).isVisible = isFolderHidden && !mShowAll && mPath != FAVORITES && mPath != RECYCLE_BIN + findItem(R.id.exclude_folder).isVisible = !mShowAll && mPath != FAVORITES && mPath != RECYCLE_BIN findItem(R.id.folder_view).isVisible = mShowAll findItem(R.id.open_camera).isVisible = mShowAll @@ -304,6 +304,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { if (it) { val dirName = when { mPath == FAVORITES -> getString(R.string.favorites) + mPath == RECYCLE_BIN -> getString(R.string.recycle_bin) mPath == OTG_PATH -> getString(R.string.otg) mPath.startsWith(OTG_PATH) -> mPath.trimEnd('/').substringAfterLast('/') else -> getHumanizedFilename(mPath) @@ -525,7 +526,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { private fun isDirEmpty(): Boolean { return if (mMedia.size <= 0 && config.filterMedia > 0) { - if (mPath != FAVORITES) { + if (mPath != FAVORITES && mPath != RECYCLE_BIN) { deleteDirectoryIfEmpty() deleteDBDirectory() } @@ -736,6 +737,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { putExtra(PATH, path) putExtra(SHOW_ALL, mShowAll) putExtra(SHOW_FAVORITES, mPath == FAVORITES) + putExtra(SHOW_RECYCLE_BIN, mPath == RECYCLE_BIN) startActivity(this) } } @@ -747,7 +749,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { mLatestMediaId = getLatestMediaId() mLatestMediaDateId = getLatestMediaByDateId() if (!isFromCache) { - val mediaToInsert = (mMedia.clone() as ArrayList).filter { it is Medium }.map { it as Medium } + val mediaToInsert = (mMedia.clone() as ArrayList).filter { it is Medium && it.deletedTS == 0L }.map { it as Medium } galleryDB.MediumDao().insertAll(mediaToInsert) } }.start() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt index 6ac4f0744..b00879def 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -60,7 +60,6 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private var mPos = -1 private var mShowAll = false private var mIsSlideshowActive = false - private var mIsShowingFavorites = false private var mRotationDegrees = 0 private var mPrevHashcode = 0 @@ -86,7 +85,6 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View super.onCreate(savedInstanceState) setContentView(R.layout.activity_medium) (MediaActivity.mMedia.clone() as ArrayList).filter { it is Medium }.mapTo(mMediaFiles) { it as Medium } - mIsShowingFavorites = intent.getBooleanExtra(SHOW_FAVORITES, false) handlePermission(PERMISSION_WRITE_STORAGE) { if (it) { @@ -216,7 +214,13 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View showSystemUI() - mDirectory = if (mIsShowingFavorites) FAVORITES else mPath.getParentPath() + val isShowingFavorites = intent.getBooleanExtra(SHOW_FAVORITES, false) + val isShowingRecycleBin = intent.getBooleanExtra(SHOW_RECYCLE_BIN, false) + mDirectory = when { + isShowingFavorites -> FAVORITES + isShowingRecycleBin -> RECYCLE_BIN + else -> mPath.getParentPath() + } if (mDirectory.startsWith(OTG_PATH.trimEnd('/'))) { mDirectory += "/" } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt index 95ba983bd..ba83dcd3b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -181,7 +181,7 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList? = null, directoryDao: DirectoryDao = galleryDB.DirectoryDao()) { val dirsToCheck = dirs ?: directoryDao.getAll() - dirsToCheck.filter { !it.areFavorites() && !getDoesFilePathExist(it.path) && it.path != config.tempFolderPath }.forEach { + dirsToCheck.filter { !it.areFavorites() && !it.isRecycleBin() && !getDoesFilePathExist(it.path) && it.path != config.tempFolderPath }.forEach { directoryDao.deleteDirPath(it.path) } } @@ -387,3 +392,11 @@ fun Context.getOTGFolderChildren(path: String) = getDocumentFile(path)?.listFile fun Context.getOTGFolderChildrenNames(path: String) = getOTGFolderChildren(path)?.map { it.name }?.toList() fun Context.getFavoritePaths() = galleryDB.MediumDao().getFavoritePaths() as ArrayList + +fun Context.getUpdatedDeletedMedia(mediumDao: MediumDao): ArrayList { + val media = mediumDao.getDeletedMedia() as ArrayList + media.forEach { + it.path = File(filesDir, it.path).toString() + } + return media +} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt index 68a5df3de..5f099c5a1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt @@ -71,6 +71,7 @@ const val NOMEDIA = ".nomedia" const val FAVORITES = "favorites" const val RECYCLE_BIN = "recycle_bin" const val SHOW_FAVORITES = "show_favorites" +const val SHOW_RECYCLE_BIN = "show_recycle_bin" const val MAX_COLUMN_COUNT = 20 const val SHOW_TEMP_HIDDEN_DURATION = 300000L const val CLICK_MAX_DURATION = 150 diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt index 3e495cb7c..5a64f8f67 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt @@ -8,10 +8,7 @@ import android.text.format.DateFormat import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.gallery.R -import com.simplemobiletools.gallery.extensions.config -import com.simplemobiletools.gallery.extensions.getDistinctPath -import com.simplemobiletools.gallery.extensions.getOTGFolderChildren -import com.simplemobiletools.gallery.extensions.shouldFolderBeVisible +import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.ThumbnailItem import com.simplemobiletools.gallery.models.ThumbnailSection @@ -165,10 +162,10 @@ class MediaFetcher(val context: Context) { favoritePaths: ArrayList): ArrayList { val media = ArrayList() - val files = if (folder == FAVORITES) { - favoritePaths.map { File(it) }.toTypedArray() - } else { - File(folder).listFiles() ?: return media + val files = when (folder) { + FAVORITES -> favoritePaths.map { File(it) }.toTypedArray() + RECYCLE_BIN -> context.getUpdatedDeletedMedia(context.galleryDB.MediumDao()).map { File(it.path) }.toTypedArray() + else -> File(folder).listFiles() ?: return media } val doExtraCheck = context.config.doExtraCheck diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt index 85835f165..3c748c50e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt @@ -11,6 +11,7 @@ import com.simplemobiletools.commons.helpers.SORT_BY_NAME import com.simplemobiletools.commons.helpers.SORT_BY_PATH import com.simplemobiletools.commons.helpers.SORT_BY_SIZE import com.simplemobiletools.gallery.helpers.FAVORITES +import com.simplemobiletools.gallery.helpers.RECYCLE_BIN import java.io.Serializable @Entity(tableName = "directories", indices = [Index(value = "path", unique = true)]) @@ -39,4 +40,6 @@ data class Directory( } fun areFavorites() = path == FAVORITES + + fun isRecycleBin() = path == RECYCLE_BIN }