optimize favorite item fetching, do not query it too often

This commit is contained in:
tibbi 2018-06-20 12:10:41 +02:00
parent 368ffd4b1d
commit dce71ac166
7 changed files with 22 additions and 14 deletions

View file

@ -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
}

View file

@ -263,7 +263,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
private fun initFavorites() {
Thread {
mFavoritePaths = galleryDB.MediumDao().getFavorites() as ArrayList<String>
mFavoritePaths = getFavoritePaths()
}.start()
}

View file

@ -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<Medium> {
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<Medium>()
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)
}
}

View file

@ -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<String>

View file

@ -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

View file

@ -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<Medium> {
fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean, getProperDateTaken: Boolean, favoritePaths: ArrayList<String>): ArrayList<Medium> {
val filterMedia = context.config.filterMedia
if (filterMedia == 0) {
return ArrayList()
@ -21,10 +24,10 @@ class MediaFetcher(val context: Context) {
val curMedia = ArrayList<Medium>()
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<Medium> {
private fun getMediaInFolder(folder: String, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int, getProperDateTaken: Boolean,
favoritePaths: ArrayList<String>): ArrayList<Medium> {
val media = ArrayList<Medium>()
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<Medium> {
private fun getMediaOnOTG(folder: String, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int, favoritePaths: ArrayList<String>): ArrayList<Medium> {
val media = ArrayList<Medium>()
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) {

View file

@ -12,7 +12,7 @@ interface MediumDao {
fun getMediaFromPath(path: String): List<Medium>
@Query("SELECT full_path FROM media WHERE is_favorite = 1")
fun getFavorites(): List<String>
fun getFavoritePaths(): List<String>
@Insert(onConflict = REPLACE)
fun insert(medium: Medium)