From dce71ac1660029812bc8d8112e6c993ac9d03540 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 20 Jun 2018 12:10:41 +0200 Subject: [PATCH] optimize favorite item fetching, do not query it too often --- .../gallery/activities/MainActivity.kt | 5 +++-- .../gallery/activities/ViewPagerActivity.kt | 2 +- .../gallery/asynctasks/GetMediaAsynctask.kt | 6 ++++-- .../gallery/extensions/Context.kt | 2 ++ .../gallery/helpers/Constants.kt | 1 + .../gallery/helpers/MediaFetcher.kt | 18 ++++++++++-------- .../gallery/interfaces/MediumDao.kt | 2 +- 7 files changed, 22 insertions(+), 14 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 4f4d4b0da..cfb34cdad 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -609,10 +609,11 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { val mediumDao = galleryDB.MediumDao() val directoryDao = galleryDB.DirectoryDao() val getProperDateTaken = config.directorySorting and SORT_BY_DATE_TAKEN != 0 + val favoritePaths = getFavoritePaths() try { for (directory in dirs) { - val curMedia = mediaFetcher.getFilesFrom(directory.path, getImagesOnly, getVideosOnly, getProperDateTaken) + val curMedia = mediaFetcher.getFilesFrom(directory.path, getImagesOnly, getVideosOnly, getProperDateTaken, favoritePaths) val newDir = if (curMedia.isEmpty()) { directory } else { @@ -657,7 +658,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { // check the remaining folders which were not cached at all yet for (folder in foldersToScan) { - val newMedia = mediaFetcher.getFilesFrom(folder, getImagesOnly, getVideosOnly, getProperDateTaken) + val newMedia = mediaFetcher.getFilesFrom(folder, getImagesOnly, getVideosOnly, getProperDateTaken, favoritePaths) if (newMedia.isEmpty()) { continue } 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 1ed1579c2..dfd2b21d5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -263,7 +263,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private fun initFavorites() { Thread { - mFavoritePaths = galleryDB.MediumDao().getFavorites() as ArrayList + mFavoritePaths = getFavoritePaths() }.start() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt index 7f245c98a..07b9bb630 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt @@ -4,6 +4,7 @@ import android.content.Context import android.os.AsyncTask import com.simplemobiletools.commons.helpers.SORT_BY_DATE_TAKEN import com.simplemobiletools.gallery.extensions.config +import com.simplemobiletools.gallery.extensions.getFavoritePaths import com.simplemobiletools.gallery.helpers.MediaFetcher import com.simplemobiletools.gallery.models.Medium import java.util.* @@ -15,18 +16,19 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage override fun doInBackground(vararg params: Void): ArrayList { val getProperDateTaken = context.config.getFileSorting(mPath) and SORT_BY_DATE_TAKEN != 0 + val favoritePaths = context.getFavoritePaths() return if (showAll) { val foldersToScan = mediaFetcher.getFoldersToScan() val media = ArrayList() foldersToScan.forEach { - val newMedia = mediaFetcher.getFilesFrom(it, isPickImage, isPickVideo, getProperDateTaken) + val newMedia = mediaFetcher.getFilesFrom(it, isPickImage, isPickVideo, getProperDateTaken, favoritePaths) media.addAll(newMedia) } MediaFetcher(context).sortMedia(media, context.config.getFileSorting("")) media } else { - mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken) + mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, favoritePaths) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt index 9506dd064..1fb50627e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -374,3 +374,5 @@ fun Context.updateDBDirectory(directory: Directory) { fun Context.getOTGFolderChildren(path: String) = getDocumentFile(path)?.listFiles() fun Context.getOTGFolderChildrenNames(path: String) = getOTGFolderChildren(path)?.map { it.name }?.toList() + +fun Context.getFavoritePaths() = galleryDB.MediumDao().getFavoritePaths() as ArrayList 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 9c9d0b018..39fdd8a4b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt @@ -63,6 +63,7 @@ const val SLIDESHOW_DEFAULT_INTERVAL = 5 const val SLIDESHOW_SCROLL_DURATION = 500L const val NOMEDIA = ".nomedia" +const val FAVORITES = "favorites" 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 14ca604c1..9afb95ccf 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt @@ -6,14 +6,17 @@ import android.net.Uri import android.provider.MediaStore import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.* -import com.simplemobiletools.gallery.extensions.* +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.models.Medium import java.io.File class MediaFetcher(val context: Context) { var shouldStop = false - fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean, getProperDateTaken: Boolean): ArrayList { + fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean, getProperDateTaken: Boolean, favoritePaths: ArrayList): ArrayList { val filterMedia = context.config.filterMedia if (filterMedia == 0) { return ArrayList() @@ -21,10 +24,10 @@ class MediaFetcher(val context: Context) { val curMedia = ArrayList() if (curPath.startsWith(OTG_PATH)) { - val newMedia = getMediaOnOTG(curPath, isPickImage, isPickVideo, filterMedia) + val newMedia = getMediaOnOTG(curPath, isPickImage, isPickVideo, filterMedia, favoritePaths) curMedia.addAll(newMedia) } else { - val newMedia = getMediaInFolder(curPath, isPickImage, isPickVideo, filterMedia, getProperDateTaken) + val newMedia = getMediaInFolder(curPath, isPickImage, isPickVideo, filterMedia, getProperDateTaken, favoritePaths) curMedia.addAll(newMedia) } @@ -153,13 +156,13 @@ class MediaFetcher(val context: Context) { } } - private fun getMediaInFolder(folder: String, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int, getProperDateTaken: Boolean): ArrayList { + private fun getMediaInFolder(folder: String, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int, getProperDateTaken: Boolean, + favoritePaths: ArrayList): ArrayList { val media = ArrayList() val files = File(folder).listFiles() ?: return media val doExtraCheck = context.config.doExtraCheck val showHidden = context.config.shouldShowHidden val dateTakens = if (getProperDateTaken) getFolderDateTakens(folder) else HashMap() - val favoritePaths = context.galleryDB.MediumDao().getFavorites() for (file in files) { if (shouldStop) { @@ -216,12 +219,11 @@ class MediaFetcher(val context: Context) { return media } - private fun getMediaOnOTG(folder: String, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int): ArrayList { + private fun getMediaOnOTG(folder: String, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int, favoritePaths: ArrayList): ArrayList { val media = ArrayList() val files = context.getDocumentFile(folder)?.listFiles() ?: return media val doExtraCheck = context.config.doExtraCheck val showHidden = context.config.shouldShowHidden - val favoritePaths = context.galleryDB.MediumDao().getFavorites() for (file in files) { if (shouldStop) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt index f3c88755b..451f92e27 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt @@ -12,7 +12,7 @@ interface MediumDao { fun getMediaFromPath(path: String): List @Query("SELECT full_path FROM media WHERE is_favorite = 1") - fun getFavorites(): List + fun getFavoritePaths(): List @Insert(onConflict = REPLACE) fun insert(medium: Medium)