From 5ea99ab22dec109fc8930d74e5e47f447f7ad16b Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 29 Jul 2018 22:06:49 +0200 Subject: [PATCH] pass the MediumDao and DirectoryDao to some activity extension functions --- .../gallery/activities/MainActivity.kt | 10 +++++----- .../gallery/activities/MediaActivity.kt | 6 +++--- .../gallery/extensions/Activity.kt | 9 ++++----- .../gallery/extensions/Context.kt | 15 +++++++-------- 4 files changed, 19 insertions(+), 21 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 a31c36f8e..33cb4bf9c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -336,7 +336,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { val getImagesOnly = mIsPickImageIntent || mIsGetImageContentIntent val getVideosOnly = mIsPickVideoIntent || mIsGetVideoContentIntent - getCachedDirectories(getVideosOnly, getImagesOnly) { + getCachedDirectories(getVideosOnly, getImagesOnly, mDirectoryDao) { gotDirectories(addTempFolderIfNeeded(it)) } } @@ -423,7 +423,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { files?.filter { it.absolutePath.isImageVideoGif() }?.mapTo(pathsToDelete) { it.absolutePath } } - movePathsInRecycleBin(pathsToDelete) { + movePathsInRecycleBin(pathsToDelete, mMediumDao) { if (it) { deleteFilteredFolders(fileDirItems, folders) } else { @@ -733,11 +733,11 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { showSortedDirs(dirs) // update directories and media files in the local db, delete invalid items - updateDBDirectory(directory) + updateDBDirectory(directory, mDirectoryDao) if (!directory.isRecycleBin()) { mMediumDao.insertAll(curMedia) } - getCachedMedia(directory.path, getVideosOnly, getImagesOnly) { + getCachedMedia(directory.path, getVideosOnly, getImagesOnly, mMediumDao) { it.forEach { if (!curMedia.contains(it)) { val path = (it as? Medium)?.path @@ -997,7 +997,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { override fun updateDirectories(directories: ArrayList) { Thread { - storeDirectoryItems(directories) + storeDirectoryItems(directories, mDirectoryDao) removeInvalidDBDirectories() }.start() } 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 50e09efaf..28cea3331 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -454,7 +454,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { private fun restoreAllFiles() { val paths = mMedia.filter { it is Medium }.map { (it as Medium).path } as ArrayList - restoreRecycleBinPaths(paths) { + restoreRecycleBinPaths(paths, mMediumDao) { Thread { mDirectoryDao.deleteDirPath(RECYCLE_BIN) }.start() @@ -546,7 +546,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { mIsGettingMedia = true if (!mLoadedInitialPhotos) { - getCachedMedia(mPath, mIsGetVideoIntent, mIsGetImageIntent) { + getCachedMedia(mPath, mIsGetVideoIntent, mIsGetImageIntent, mMediumDao) { if (it.isEmpty()) { runOnUiThread { media_refresh_layout.isRefreshing = true @@ -834,7 +834,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { toast(deletingItems) if (config.useRecycleBin && !filtered.first().path.startsWith(filesDir.absolutePath)) { - movePathsInRecycleBin(filtered.map { it.path } as ArrayList) { + movePathsInRecycleBin(filtered.map { it.path } as ArrayList, mMediumDao) { if (it) { deleteFilteredFiles(filtered) } else { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt index 7ea8421e2..a8820e8cd 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt @@ -16,6 +16,7 @@ import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.activities.SimpleActivity import com.simplemobiletools.gallery.dialogs.PickDirectoryDialog import com.simplemobiletools.gallery.helpers.NOMEDIA +import com.simplemobiletools.gallery.interfaces.MediumDao import java.io.File import java.io.InputStream import java.io.OutputStream @@ -200,9 +201,8 @@ fun BaseSimpleActivity.tryDeleteFileDirItem(fileDirItem: FileDirItem, allowDelet } } -fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList, callback: ((wasSuccess: Boolean) -> Unit)?) { +fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList, mediumDao: MediumDao = galleryDB.MediumDao(), callback: ((wasSuccess: Boolean) -> Unit)?) { Thread { - val mediumDao = galleryDB.MediumDao() var pathsCnt = paths.size paths.forEach { val file = File(it) @@ -220,12 +220,11 @@ fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList, callback: } fun BaseSimpleActivity.restoreRecycleBinPath(path: String, callback: () -> Unit) { - restoreRecycleBinPaths(arrayListOf(path), callback) + restoreRecycleBinPaths(arrayListOf(path), galleryDB.MediumDao(), callback) } -fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList, callback: () -> Unit) { +fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList, mediumDao: MediumDao = galleryDB.MediumDao(), callback: () -> Unit) { Thread { - val mediumDao = galleryDB.MediumDao() paths.forEach { val source = it val destination = it.removePrefix(filesDir.absolutePath) 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 5fc78e20e..5a768e1be 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -181,9 +181,9 @@ fun Context.rescanFolderMediaSync(path: String) { } } -fun Context.storeDirectoryItems(items: ArrayList) { +fun Context.storeDirectoryItems(items: ArrayList, directoryDao: DirectoryDao) { Thread { - galleryDB.DirectoryDao().insertAll(items) + directoryDao.insertAll(items) }.start() } @@ -280,9 +280,8 @@ fun Context.loadJpg(path: String, target: MySquareImageView, cropThumbnails: Boo builder.apply(options).transition(DrawableTransitionOptions.withCrossFade()).into(target) } -fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, callback: (ArrayList) -> Unit) { +fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, directoryDao: DirectoryDao = galleryDB.DirectoryDao(), callback: (ArrayList) -> Unit) { Thread { - val directoryDao = galleryDB.DirectoryDao() val directories = try { directoryDao.getAll() as ArrayList } catch (e: SQLiteException) { @@ -326,10 +325,10 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly: }.start() } -fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, callback: (ArrayList) -> Unit) { +fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, mediumDao: MediumDao = galleryDB.MediumDao(), + callback: (ArrayList) -> Unit) { Thread { val mediaFetcher = MediaFetcher(this) - val mediumDao = galleryDB.MediumDao() val foldersToScan = if (path.isEmpty()) mediaFetcher.getFoldersToScan() else arrayListOf(path) var media = ArrayList() if (path == FAVORITES) { @@ -394,8 +393,8 @@ fun Context.updateDBMediaPath(oldPath: String, newPath: String) { galleryDB.MediumDao().updateMedium(oldPath, newParentPath, newFilename, newPath) } -fun Context.updateDBDirectory(directory: Directory) { - galleryDB.DirectoryDao().updateDirectory(directory.path, directory.tmb, directory.mediaCnt, directory.modified, directory.taken, directory.size, directory.types) +fun Context.updateDBDirectory(directory: Directory, directoryDao: DirectoryDao) { + directoryDao.updateDirectory(directory.path, directory.tmb, directory.mediaCnt, directory.modified, directory.taken, directory.size, directory.types) } fun Context.getOTGFolderChildren(path: String) = getDocumentFile(path)?.listFiles()