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 e24681ea3..a1c8d58a7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -16,10 +16,7 @@ import com.simplemobiletools.commons.dialogs.CreateNewFolderDialog import com.simplemobiletools.commons.dialogs.FilePickerDialog import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.extensions.* -import com.simplemobiletools.commons.helpers.PERMISSION_READ_STORAGE -import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE -import com.simplemobiletools.commons.helpers.SORT_BY_DATE_MODIFIED -import com.simplemobiletools.commons.helpers.SORT_BY_DATE_TAKEN +import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.models.Release @@ -34,6 +31,7 @@ import com.simplemobiletools.gallery.dialogs.FilterMediaDialog import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.models.Directory +import com.simplemobiletools.gallery.models.Medium import kotlinx.android.synthetic.main.activity_main.* import java.io.* @@ -587,7 +585,69 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { setupAdapter() } - if (!isFromCache) { + if (isFromCache) { + Thread { + val mediaFetcher = MediaFetcher(applicationContext) + val getImagesOnly = mIsPickImageIntent || mIsGetImageContentIntent + val getVideosOnly = mIsPickVideoIntent || mIsGetVideoContentIntent + val hidden = getString(R.string.hidden) + val albumCovers = config.parseAlbumCovers() + val hasOTG = hasOTGConnected() && config.OTGBasePath.isNotEmpty() + val includedFolders = config.includedFolders + + for (directory in dirs) { + val curMedia = mediaFetcher.getFilesFrom(directory.path, getImagesOnly, getVideosOnly) + Medium.sorting = config.getFileSorting(directory.path) + curMedia.sort() + + val firstItem = curMedia.first() + val lastItem = curMedia.last() + val parentDir = if (hasOTG && firstItem.path.startsWith(OTG_PATH)) { + firstItem.parentPath + } else { + File(firstItem.path).parent + } ?: continue + + var thumbnail = curMedia.firstOrNull { getDoesFilePathExist(it.path) }?.path ?: "" + if (thumbnail.startsWith(OTG_PATH)) { + thumbnail = thumbnail.getOTGPublicPath(applicationContext) + } + + albumCovers.forEach { + if (it.path == parentDir && getDoesFilePathExist(it.tmb)) { + thumbnail = it.tmb + } + } + + val mediaTypes = curMedia.getDirMediaTypes() + val dirName = checkAppendingHidden(parentDir, hidden, includedFolders) + val lastModified = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.modified, lastItem.modified) else Math.min(firstItem.modified, lastItem.modified) + val dateTaken = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.taken, lastItem.taken) else Math.min(firstItem.taken, lastItem.taken) + val size = curMedia.sumByLong { it.size } + val newDir = Directory(null, parentDir, thumbnail, dirName, curMedia.size, lastModified, dateTaken, size, isPathOnSD(parentDir), mediaTypes) + directory.mediaCnt = curMedia.size + if (directory == newDir) { + continue + } + + directory.apply { + tmb = thumbnail + mediaCnt = curMedia.size + modified = lastModified + taken = dateTaken + this@apply.size = size + types = mediaTypes + } + + updateDirectory(directory) + rescanFolderMediaSync(directory.path) + val sortedDirs = getSortedDirectories(dirs).clone() as ArrayList + runOnUiThread { + (directories_grid.adapter as DirectoryAdapter).updateDirs(sortedDirs) + } + } + }.start() + } else { storeDirectories() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt index f3c06916a..64b8d9756 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -10,10 +10,8 @@ import com.simplemobiletools.commons.helpers.sumByLong import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.extensions.checkAppendingHidden import com.simplemobiletools.gallery.extensions.config +import com.simplemobiletools.gallery.extensions.getDirMediaTypes import com.simplemobiletools.gallery.helpers.MediaFetcher -import com.simplemobiletools.gallery.helpers.TYPE_GIFS -import com.simplemobiletools.gallery.helpers.TYPE_IMAGES -import com.simplemobiletools.gallery.helpers.TYPE_VIDEOS import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Medium import java.io.File @@ -58,8 +56,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va } } - val mediaTypes = getDirMediaTypes(curMedia) - + val mediaTypes = curMedia.getDirMediaTypes() val dirName = context.checkAppendingHidden(parentDir, hidden, includedFolders) val lastModified = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.modified, lastItem.modified) else Math.min(firstItem.modified, lastItem.modified) val dateTaken = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.taken, lastItem.taken) else Math.min(firstItem.taken, lastItem.taken) @@ -71,23 +68,6 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va return directories } - private fun getDirMediaTypes(media: ArrayList): Int { - var types = 0 - if (media.any { it.isImage() }) { - types += TYPE_IMAGES - } - - if (media.any { it.isVideo() }) { - types += TYPE_VIDEOS - } - - if (media.any { it.isGif() }) { - types += TYPE_GIFS - } - - return types - } - override fun onPostExecute(dirs: ArrayList) { super.onPostExecute(dirs) callback(dirs) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/ArrayList.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/ArrayList.kt new file mode 100644 index 000000000..afd1c36e5 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/ArrayList.kt @@ -0,0 +1,23 @@ +package com.simplemobiletools.gallery.extensions + +import com.simplemobiletools.gallery.helpers.TYPE_GIFS +import com.simplemobiletools.gallery.helpers.TYPE_IMAGES +import com.simplemobiletools.gallery.helpers.TYPE_VIDEOS +import com.simplemobiletools.gallery.models.Medium + +fun ArrayList.getDirMediaTypes(): Int { + var types = 0 + if (any { it.isImage() }) { + types += TYPE_IMAGES + } + + if (any { it.isVideo() }) { + types += TYPE_VIDEOS + } + + if (any { it.isGif() }) { + types += TYPE_GIFS + } + + return types +} 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 7b261c90d..6c13c28eb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -135,25 +135,29 @@ fun Context.getNoMediaFolders(callback: (folders: ArrayList) -> Unit) { fun Context.rescanFolderMedia(path: String) { Thread { - getCachedMedia(path) { - val cached = it - GetMediaAsynctask(applicationContext, path, false, false, false) { - Thread { - val newMedia = it - val mediumDao = galleryDB.MediumDao() - mediumDao.insertAll(newMedia) - - cached.forEach { - if (!newMedia.contains(it)) { - mediumDao.deleteMediumPath(it.path) - } - } - }.start() - }.execute() - } + rescanFolderMediaSync(path) }.start() } +fun Context.rescanFolderMediaSync(path: String) { + getCachedMedia(path) { + val cached = it + GetMediaAsynctask(applicationContext, path, false, false, false) { + Thread { + val newMedia = it + val mediumDao = galleryDB.MediumDao() + mediumDao.insertAll(newMedia) + + cached.forEach { + if (!newMedia.contains(it)) { + mediumDao.deleteMediumPath(it.path) + } + } + }.start() + }.execute() + } +} + fun Context.updateStoredDirectories() { GetDirectoriesAsynctask(this, false, false) { if (!config.temporarilyShowHidden) { @@ -317,5 +321,9 @@ fun Context.removeInvalidDirectories(dirs: ArrayList? = null, directo fun Context.updateMediaPath(oldPath: String, newPath: String) { val newFilename = newPath.getFilenameFromPath() val newParentPath = newPath.getParentPath() - galleryDB.MediumDao().updateMedia(oldPath, newParentPath, newFilename, newPath) + galleryDB.MediumDao().updateMedium(oldPath, newParentPath, newFilename, newPath) +} + +fun Context.updateDirectory(directory: Directory) { + galleryDB.DirectoryDao().updateDirectory(directory.path, directory.tmb, directory.mediaCnt, directory.modified, directory.taken, directory.size, directory.types) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt index a8172a8b5..0d996ad3d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt @@ -23,4 +23,7 @@ interface DirectoryDao { @Query("DELETE FROM directories WHERE path = :path") fun deleteDirPath(path: String) + + @Query("UPDATE OR REPLACE directories SET thumbnail = :thumbnail, media_count = :mediaCnt, last_modified = :lastModified, date_taken = :dateTaken, size = :size, media_types = :mediaTypes WHERE path = :path") + fun updateDirectory(path: String, thumbnail: String, mediaCnt: Int, lastModified: Long, dateTaken: Long, size: Long, mediaTypes: Int) } 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 da7612e52..eee0430c4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt @@ -24,5 +24,5 @@ interface MediumDao { fun deleteMediumPath(path: String) @Query("UPDATE OR REPLACE media SET filename = :newFilename, full_path = :newFullPath, parent_path = :newParentPath WHERE full_path = :oldPath") - fun updateMedia(oldPath: String, newParentPath: String, newFilename: String, newFullPath: String) + fun updateMedium(oldPath: String, newParentPath: String, newFilename: String, newFullPath: String) } 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 27c15d62e..ef02bdfc8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt @@ -16,11 +16,11 @@ data class Directory( @ColumnInfo(name = "thumbnail") var tmb: String, @ColumnInfo(name = "filename") var name: String, @ColumnInfo(name = "media_count") var mediaCnt: Int, - @ColumnInfo(name = "last_modified") val modified: Long, - @ColumnInfo(name = "date_taken") val taken: Long, - @ColumnInfo(name = "size") val size: Long, + @ColumnInfo(name = "last_modified") var modified: Long, + @ColumnInfo(name = "date_taken") var taken: Long, + @ColumnInfo(name = "size") var size: Long, @ColumnInfo(name = "is_on_sd_card") val isOnSDCard: Boolean, - @ColumnInfo(name = "media_types") val types: Int) : Serializable, Comparable { + @ColumnInfo(name = "media_types") var types: Int) : Serializable, Comparable { companion object { private const val serialVersionUID = -6553345863555455L