From 3ca8e685b81a21efb3f3c0d11bec1060fc1682e8 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 25 Jan 2020 10:56:08 +0100 Subject: [PATCH] shortening some code related to getting DAOs --- .../gallery/pro/activities/MainActivity.kt | 36 +++++-------- .../gallery/pro/activities/MediaActivity.kt | 26 ++++----- .../gallery/pro/activities/SearchActivity.kt | 4 +- .../pro/activities/SettingsActivity.kt | 7 +-- .../pro/activities/ViewPagerActivity.kt | 5 +- .../pro/activities/WidgetConfigureActivity.kt | 2 +- .../gallery/pro/adapters/DirectoryAdapter.kt | 2 +- .../gallery/pro/extensions/Activity.kt | 22 ++++---- .../gallery/pro/extensions/Context.kt | 53 +++++++++---------- .../gallery/pro/helpers/MediaFetcher.kt | 2 +- .../gallery/pro/helpers/MyWidgetProvider.kt | 4 +- 11 files changed, 69 insertions(+), 94 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt index 1af9c13d0..7cad1ca04 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt @@ -36,9 +36,7 @@ import com.simplemobiletools.gallery.pro.dialogs.ChangeViewTypeDialog import com.simplemobiletools.gallery.pro.dialogs.FilterMediaDialog import com.simplemobiletools.gallery.pro.extensions.* import com.simplemobiletools.gallery.pro.helpers.* -import com.simplemobiletools.gallery.pro.interfaces.DirectoryDao import com.simplemobiletools.gallery.pro.interfaces.DirectoryOperationsListener -import com.simplemobiletools.gallery.pro.interfaces.MediumDao import com.simplemobiletools.gallery.pro.jobs.NewPhotoFetcher import com.simplemobiletools.gallery.pro.models.Directory import com.simplemobiletools.gallery.pro.models.Medium @@ -85,17 +83,11 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { private var mStoredTextColor = 0 private var mStoredPrimaryColor = 0 - private lateinit var mMediumDao: MediumDao - private lateinit var mDirectoryDao: DirectoryDao - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) appLaunched(BuildConfig.APPLICATION_ID) - mMediumDao = galleryDB.MediumDao() - mDirectoryDao = galleryDB.DirectoryDao() - if (savedInstanceState == null) { config.temporarilyShowHidden = false config.tempSkipDeleteConfirmation = false @@ -464,7 +456,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { val getImagesOnly = mIsPickImageIntent || mIsGetImageContentIntent val getVideosOnly = mIsPickVideoIntent || mIsGetVideoContentIntent - getCachedDirectories(getVideosOnly, getImagesOnly, mDirectoryDao) { + getCachedDirectories(getVideosOnly, getImagesOnly) { gotDirectories(addTempFolderIfNeeded(it)) } } @@ -569,7 +561,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { val pathsToDelete = ArrayList() itemsToDelete.mapTo(pathsToDelete) { it.path } - movePathsInRecycleBin(pathsToDelete, mMediumDao) { + movePathsInRecycleBin(pathsToDelete) { if (it) { deleteFilteredFileDirItems(itemsToDelete, folders) } else { @@ -590,7 +582,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { ensureBackgroundThread { folders.filter { !getDoesFilePathExist(it.absolutePath, OTGPath) }.forEach { - mDirectoryDao.deleteDirPath(it.absolutePath) + directoryDao.deleteDirPath(it.absolutePath) } if (config.deleteEmptyFolders) { @@ -936,16 +928,16 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { setupAdapter(dirs) // update directories and media files in the local db, delete invalid items - updateDBDirectory(directory, mDirectoryDao) + updateDBDirectory(directory) if (!directory.isRecycleBin()) { - mMediumDao.insertAll(curMedia) + mediaDB.insertAll(curMedia) } - getCachedMedia(directory.path, getVideosOnly, getImagesOnly, mMediumDao) { + getCachedMedia(directory.path, getVideosOnly, getImagesOnly) { it.forEach { if (!curMedia.contains(it)) { val path = (it as? Medium)?.path if (path != null) { - deleteDBPath(mMediumDao, path) + deleteDBPath(path) } } } @@ -955,7 +947,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { if (dirPathsToRemove.isNotEmpty()) { val dirsToRemove = dirs.filter { dirPathsToRemove.contains(it.path) } dirsToRemove.forEach { - mDirectoryDao.deleteDirPath(it.path) + directoryDao.deleteDirPath(it.path) } dirs.removeAll(dirsToRemove) setupAdapter(dirs) @@ -999,9 +991,9 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { dirs.add(newDir) setupAdapter(dirs) try { - mDirectoryDao.insert(newDir) + directoryDao.insert(newDir) if (folder != RECYCLE_BIN) { - mMediumDao.insertAll(newMedia) + mediaDB.insertAll(newMedia) } } catch (ignored: Exception) { } @@ -1156,7 +1148,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { if (config.useRecycleBin) { try { val binFolder = dirs.firstOrNull { it.path == RECYCLE_BIN } - if (binFolder != null && mMediumDao.getDeletedMedia().isEmpty()) { + if (binFolder != null && mediaDB.getDeletedMedia().isEmpty()) { invalidDirs.add(binFolder) } } catch (ignored: Exception) { @@ -1168,7 +1160,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { setupAdapter(dirs) invalidDirs.forEach { try { - mDirectoryDao.deleteDirPath(it.path) + directoryDao.deleteDirPath(it.path) } catch (ignored: Exception) { } } @@ -1218,7 +1210,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { Handler().postDelayed({ ensureBackgroundThread { try { - mMediumDao.deleteOldRecycleBinItems(System.currentTimeMillis() - MONTH_MILLISECONDS) + mediaDB.deleteOldRecycleBinItems(System.currentTimeMillis() - MONTH_MILLISECONDS) } catch (e: Exception) { } } @@ -1283,7 +1275,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { override fun updateDirectories(directories: ArrayList) { ensureBackgroundThread { - storeDirectoryItems(directories, mDirectoryDao) + storeDirectoryItems(directories) removeInvalidDBDirectories() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt index 6aa7d5fcd..7f05afad3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt @@ -39,9 +39,7 @@ import com.simplemobiletools.gallery.pro.databases.GalleryDatabase import com.simplemobiletools.gallery.pro.dialogs.* import com.simplemobiletools.gallery.pro.extensions.* import com.simplemobiletools.gallery.pro.helpers.* -import com.simplemobiletools.gallery.pro.interfaces.DirectoryDao import com.simplemobiletools.gallery.pro.interfaces.MediaOperationsListener -import com.simplemobiletools.gallery.pro.interfaces.MediumDao import com.simplemobiletools.gallery.pro.models.Medium import com.simplemobiletools.gallery.pro.models.ThumbnailItem import com.simplemobiletools.gallery.pro.models.ThumbnailSection @@ -78,9 +76,6 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { private var mStoredTextColor = 0 private var mStoredPrimaryColor = 0 - private lateinit var mMediumDao: MediumDao - private lateinit var mDirectoryDao: DirectoryDao - companion object { var mMedia = ArrayList() } @@ -89,9 +84,6 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { super.onCreate(savedInstanceState) setContentView(R.layout.activity_media) - mMediumDao = galleryDB.MediumDao() - mDirectoryDao = galleryDB.DirectoryDao() - intent.apply { mIsGetImageIntent = getBooleanExtra(GET_IMAGE_INTENT, false) mIsGetVideoIntent = getBooleanExtra(GET_VIDEO_INTENT, false) @@ -495,9 +487,9 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { private fun restoreAllFiles() { val paths = mMedia.filter { it is Medium }.map { (it as Medium).path } as ArrayList - restoreRecycleBinPaths(paths, mMediumDao) { + restoreRecycleBinPaths(paths) { ensureBackgroundThread { - mDirectoryDao.deleteDirPath(RECYCLE_BIN) + directoryDao.deleteDirPath(RECYCLE_BIN) } finish() } @@ -584,7 +576,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { if (mLoadedInitialPhotos) { startAsyncTask() } else { - getCachedMedia(mPath, mIsGetVideoIntent, mIsGetImageIntent, mMediumDao) { + getCachedMedia(mPath, mIsGetVideoIntent, mIsGetImageIntent) { if (it.isEmpty()) { runOnUiThread { media_refresh_layout.isRefreshing = true @@ -608,7 +600,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { try { gotMedia(newMedia, false) oldMedia.filter { !newMedia.contains(it) }.mapNotNull { it as? Medium }.filter { !getDoesFilePathExist(it.path) }.forEach { - mMediumDao.deleteMediumPath(it.path) + mediaDB.deleteMediumPath(it.path) } } catch (e: Exception) { } @@ -627,7 +619,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { if (mPath == FAVORITES) { ensureBackgroundThread { - mDirectoryDao.deleteDirPath(FAVORITES) + directoryDao.deleteDirPath(FAVORITES) } } @@ -641,7 +633,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { private fun deleteDBDirectory() { ensureBackgroundThread { try { - mDirectoryDao.deleteDirPath(mPath) + directoryDao.deleteDirPath(mPath) } catch (ignored: Exception) { } } @@ -897,7 +889,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { if (!isFromCache) { val mediaToInsert = (mMedia).filter { it is Medium && it.deletedTS == 0L }.map { it as Medium } try { - mMediumDao.insertAll(mediaToInsert) + mediaDB.insertAll(mediaToInsert) } catch (e: Exception) { } } @@ -913,7 +905,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { val movingItems = resources.getQuantityString(R.plurals.moving_items_into_bin, filtered.size, filtered.size) toast(movingItems) - movePathsInRecycleBin(filtered.map { it.path } as ArrayList, mMediumDao) { + movePathsInRecycleBin(filtered.map { it.path } as ArrayList) { if (it) { deleteFilteredFiles(filtered) } else { @@ -940,7 +932,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { val useRecycleBin = config.useRecycleBin filtered.forEach { if (it.path.startsWith(recycleBinPath) || !useRecycleBin) { - deleteDBPath(mMediumDao, it.path) + deleteDBPath(it.path) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SearchActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SearchActivity.kt index 0febbfd0b..2ef59a3bc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SearchActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SearchActivity.kt @@ -315,7 +315,7 @@ class SearchActivity : SimpleActivity(), MediaOperationsListener { val movingItems = resources.getQuantityString(R.plurals.moving_items_into_bin, filtered.size, filtered.size) toast(movingItems) - movePathsInRecycleBin(filtered.map { it.path } as ArrayList, galleryDB.MediumDao()) { + movePathsInRecycleBin(filtered.map { it.path } as ArrayList) { if (it) { deleteFilteredFiles(filtered) } else { @@ -342,7 +342,7 @@ class SearchActivity : SimpleActivity(), MediaOperationsListener { val useRecycleBin = config.useRecycleBin filtered.forEach { if (it.path.startsWith(recycleBinPath) || !useRecycleBin) { - deleteDBPath(galleryDB.MediumDao(), it.path) + deleteDBPath(it.path) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SettingsActivity.kt index 0be79e7fe..50133ccf5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SettingsActivity.kt @@ -13,10 +13,7 @@ import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.dialogs.ManageBottomActionsDialog import com.simplemobiletools.gallery.pro.dialogs.ManageExtendedDetailsDialog -import com.simplemobiletools.gallery.pro.extensions.config -import com.simplemobiletools.gallery.pro.extensions.emptyTheRecycleBin -import com.simplemobiletools.gallery.pro.extensions.galleryDB -import com.simplemobiletools.gallery.pro.extensions.showRecycleBinEmptyingDialog +import com.simplemobiletools.gallery.pro.extensions.* import com.simplemobiletools.gallery.pro.helpers.* import com.simplemobiletools.gallery.pro.models.AlbumCover import kotlinx.android.synthetic.main.activity_settings.* @@ -593,7 +590,7 @@ class SettingsActivity : SimpleActivity() { private fun setupEmptyRecycleBin() { ensureBackgroundThread { try { - mRecycleBinContentSize = galleryDB.MediumDao().getDeletedMedia().sumByLong { it.size } + mRecycleBinContentSize = mediaDB.getDeletedMedia().sumByLong { it.size } } catch (ignored: Exception) { } runOnUiThread { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt index 4ffb2d73d..089d3da5d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt @@ -364,8 +364,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View if (intent.action == "com.android.camera.action.REVIEW") { ensureBackgroundThread { - val mediumDao = galleryDB.MediumDao() - if (mediumDao.getMediaFromPath(mPath).isEmpty()) { + if (mediaDB.getMediaFromPath(mPath).isEmpty()) { val type = when { mPath.isVideoFast() -> TYPE_VIDEOS mPath.isGif() -> TYPE_GIFS @@ -379,7 +378,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View val duration = if (type == TYPE_VIDEOS) mPath.getVideoDuration() else 0 val ts = System.currentTimeMillis() val medium = Medium(null, mPath.getFilenameFromPath(), mPath, mPath.getParentPath(), ts, ts, File(mPath).length(), type, duration, isFavorite, 0) - mediumDao.insert(medium) + mediaDB.insert(medium) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/WidgetConfigureActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/WidgetConfigureActivity.kt index d2e1696bb..3f0c66912 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/WidgetConfigureActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/WidgetConfigureActivity.kt @@ -163,7 +163,7 @@ class WidgetConfigureActivity : SimpleActivity() { } ensureBackgroundThread { - val path = directoryDB.getDirectoryThumbnail(folderPath) + val path = directoryDao.getDirectoryThumbnail(folderPath) if (path != null) { runOnUiThread { loadJpg(path, config_image, config.cropThumbnails) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt index a70362d67..84113cfa9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt @@ -198,7 +198,7 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList, mediumDao: MediumDao = galleryDB.MediumDao(), callback: ((wasSuccess: Boolean) -> Unit)?) { +fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList, callback: ((wasSuccess: Boolean) -> Unit)?) { ensureBackgroundThread { var pathsCnt = paths.size val OTGPath = config.OTGPath @@ -261,7 +260,7 @@ fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList, mediumDao out?.flush() if (fileDocument?.getItemSize(true) == copiedSize && getDoesFilePathExist(destination)) { - mediumDao.updateDeleted("$RECYCLE_BIN$source", System.currentTimeMillis(), source) + mediaDB.updateDeleted("$RECYCLE_BIN$source", System.currentTimeMillis(), source) pathsCnt-- } } catch (e: Exception) { @@ -277,7 +276,7 @@ fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList, mediumDao val lastModified = file.lastModified() try { if (file.copyRecursively(internalFile, true)) { - mediumDao.updateDeleted("$RECYCLE_BIN$source", System.currentTimeMillis(), source) + mediaDB.updateDeleted("$RECYCLE_BIN$source", System.currentTimeMillis(), source) pathsCnt-- if (config.keepLastModified) { @@ -295,10 +294,10 @@ fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList, mediumDao } fun BaseSimpleActivity.restoreRecycleBinPath(path: String, callback: () -> Unit) { - restoreRecycleBinPaths(arrayListOf(path), galleryDB.MediumDao(), callback) + restoreRecycleBinPaths(arrayListOf(path), callback) } -fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList, mediumDao: MediumDao = galleryDB.MediumDao(), callback: () -> Unit) { +fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList, callback: () -> Unit) { ensureBackgroundThread { val newPaths = ArrayList() for (source in paths) { @@ -328,7 +327,7 @@ fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList, mediumDa out?.flush() if (File(source).length() == copiedSize) { - mediumDao.updateDeleted(destination.removePrefix(recycleBinPath), 0, "$RECYCLE_BIN$destination") + mediaDB.updateDeleted(destination.removePrefix(recycleBinPath), 0, "$RECYCLE_BIN$destination") } newPaths.add(destination) @@ -357,8 +356,8 @@ fun BaseSimpleActivity.emptyTheRecycleBin(callback: (() -> Unit)? = null) { ensureBackgroundThread { try { recycleBin.deleteRecursively() - galleryDB.MediumDao().clearRecycleBin() - galleryDB.DirectoryDao().deleteRecycleBin() + mediaDB.clearRecycleBin() + directoryDao.deleteRecycleBin() toast(R.string.recycle_bin_emptied) callback?.invoke() } catch (e: Exception) { @@ -413,7 +412,6 @@ fun Activity.fixDateTaken(paths: ArrayList, showToasts: Boolean, hasResc try { var didUpdateFile = false val operations = ArrayList() - val mediumDao = galleryDB.MediumDao() ensureBackgroundThread { for (path in paths) { @@ -442,7 +440,7 @@ fun Activity.fixDateTaken(paths: ArrayList, showToasts: Boolean, hasResc operations.clear() } - mediumDao.updateFavoriteDateTaken(path, timestamp) + mediaDB.updateFavoriteDateTaken(path, timestamp) didUpdateFile = true if (!hasRescanned && getFileDateTaken(path) == 0L) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt index 8b3574847..31c37b75a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt @@ -111,11 +111,11 @@ fun Context.launchSettings() { val Context.config: Config get() = Config.newInstance(applicationContext) -val Context.galleryDB: GalleryDatabase get() = GalleryDatabase.getInstance(applicationContext) - val Context.widgetsDB: WidgetsDao get() = GalleryDatabase.getInstance(applicationContext).WidgetsDao() -val Context.directoryDB: DirectoryDao get() = GalleryDatabase.getInstance(applicationContext).DirectoryDao() +val Context.mediaDB: MediumDao get() = GalleryDatabase.getInstance(applicationContext).MediumDao() + +val Context.directoryDao: DirectoryDao get() = GalleryDatabase.getInstance(applicationContext).DirectoryDao() val Context.favoritesDB: FavoritesDao get() = GalleryDatabase.getInstance(applicationContext).FavoritesDao() @@ -400,16 +400,15 @@ fun Context.rescanFolderMediaSync(path: String) { GetMediaAsynctask(applicationContext, path, false, false, false) { ensureBackgroundThread { val newMedia = it - val mediumDao = galleryDB.MediumDao() val media = newMedia.filter { it is Medium } as ArrayList try { - mediumDao.insertAll(media) + mediaDB.insertAll(media) cached.forEach { if (!newMedia.contains(it)) { val mediumPath = (it as? Medium)?.path if (mediumPath != null) { - deleteDBPath(mediumDao, mediumPath) + deleteDBPath(mediumPath) } } } @@ -420,7 +419,7 @@ fun Context.rescanFolderMediaSync(path: String) { } } -fun Context.storeDirectoryItems(items: ArrayList, directoryDao: DirectoryDao) { +fun Context.storeDirectoryItems(items: ArrayList) { ensureBackgroundThread { directoryDao.insertAll(items) } @@ -559,7 +558,7 @@ fun Context.loadSVG(path: String, target: MySquareImageView, cropThumbnails: Boo .into(target) } -fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, directoryDao: DirectoryDao = galleryDB.DirectoryDao(), forceShowHidden: Boolean = false, callback: (ArrayList) -> Unit) { +fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, forceShowHidden: Boolean = false, callback: (ArrayList) -> Unit) { ensureBackgroundThread { val directories = try { directoryDao.getAll() as ArrayList @@ -602,22 +601,21 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly: val clone = filteredDirectories.clone() as ArrayList callback(clone.distinctBy { it.path.getDistinctPath() } as ArrayList) - removeInvalidDBDirectories(filteredDirectories, directoryDao) + removeInvalidDBDirectories(filteredDirectories) } } -fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, mediumDao: MediumDao = galleryDB.MediumDao(), - callback: (ArrayList) -> Unit) { +fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, callback: (ArrayList) -> Unit) { ensureBackgroundThread { val mediaFetcher = MediaFetcher(this) val foldersToScan = if (path.isEmpty()) mediaFetcher.getFoldersToScan() else arrayListOf(path) var media = ArrayList() if (path == FAVORITES) { - media.addAll(mediumDao.getFavorites()) + media.addAll(mediaDB.getFavorites()) } if (path == RECYCLE_BIN) { - media.addAll(getUpdatedDeletedMedia(mediumDao)) + media.addAll(getUpdatedDeletedMedia()) } if (config.filterMedia and TYPE_PORTRAITS != 0) { @@ -634,7 +632,7 @@ fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImag val shouldShowHidden = config.shouldShowHidden foldersToScan.filter { path.isNotEmpty() || !config.isFolderProtected(it) }.forEach { try { - val currMedia = mediumDao.getMediaFromPath(it) + val currMedia = mediaDB.getMediaFromPath(it) media.addAll(currMedia) } catch (ignored: Exception) { } @@ -667,7 +665,7 @@ fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImag val mediaToDelete = ArrayList() media.filter { !getDoesFilePathExist(it.path, OTGPath) }.forEach { if (it.path.startsWith(recycleBinPath)) { - deleteDBPath(mediumDao, it.path) + deleteDBPath(it.path) } else { mediaToDelete.add(it) } @@ -675,14 +673,14 @@ fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImag try { if (mediaToDelete.isNotEmpty()) { - mediumDao.deleteMedia(*mediaToDelete.toTypedArray()) + mediaDB.deleteMedia(*mediaToDelete.toTypedArray()) } } catch (ignored: Exception) { } } } -fun Context.removeInvalidDBDirectories(dirs: ArrayList? = null, directoryDao: DirectoryDao = galleryDB.DirectoryDao()) { +fun Context.removeInvalidDBDirectories(dirs: ArrayList? = null) { val dirsToCheck = dirs ?: directoryDao.getAll() val OTGPath = config.OTGPath dirsToCheck.filter { !it.areFavorites() && !it.isRecycleBin() && !getDoesFilePathExist(it.path, OTGPath) && it.path != config.tempFolderPath }.forEach { @@ -697,12 +695,12 @@ fun Context.updateDBMediaPath(oldPath: String, newPath: String) { val newFilename = newPath.getFilenameFromPath() val newParentPath = newPath.getParentPath() try { - galleryDB.MediumDao().updateMedium(oldPath, newParentPath, newFilename, newPath) + mediaDB.updateMedium(oldPath, newParentPath, newFilename, newPath) } catch (ignored: Exception) { } } -fun Context.updateDBDirectory(directory: Directory, directoryDao: DirectoryDao) { +fun Context.updateDBDirectory(directory: Directory) { try { directoryDao.updateDirectory(directory.path, directory.tmb, directory.mediaCnt, directory.modified, directory.taken, directory.size, directory.types) } catch (ignored: Exception) { @@ -732,9 +730,9 @@ fun Context.updateFavorite(path: String, isFavorite: Boolean) { } // remove the "recycle_bin" from the file path prefix, replace it with real bin path /data/user... -fun Context.getUpdatedDeletedMedia(mediumDao: MediumDao): ArrayList { +fun Context.getUpdatedDeletedMedia(): ArrayList { val media = try { - mediumDao.getDeletedMedia() as ArrayList + mediaDB.getDeletedMedia() as ArrayList } catch (ignored: Exception) { ArrayList() } @@ -745,13 +743,13 @@ fun Context.getUpdatedDeletedMedia(mediumDao: MediumDao): ArrayList { return media } -fun Context.deleteDBPath(mediumDao: MediumDao, path: String) { - deleteMediumWithPath(mediumDao, path.replaceFirst(recycleBinPath, RECYCLE_BIN)) +fun Context.deleteDBPath(path: String) { + deleteMediumWithPath(path.replaceFirst(recycleBinPath, RECYCLE_BIN)) } -fun Context.deleteMediumWithPath(mediumDao: MediumDao, path: String) { +fun Context.deleteMediumWithPath(path: String) { try { - mediumDao.deleteMediumPath(path) + mediaDB.deleteMediumPath(path) favoritesDB.deleteFavoritePath(path) } catch (ignored: Exception) { } @@ -841,13 +839,12 @@ fun Context.addPathToDB(path: String) { } try { - val mediumDao = galleryDB.MediumDao() val isFavorite = favoritesDB.isFavorite(path) val videoDuration = if (type == TYPE_VIDEOS) path.getVideoDuration() else 0 val medium = Medium(null, path.getFilenameFromPath(), path, path.getParentPath(), System.currentTimeMillis(), System.currentTimeMillis(), File(path).length(), type, videoDuration, isFavorite, 0L) - mediumDao.insert(medium) + mediaDB.insert(medium) } catch (ignored: Exception) { } } @@ -891,7 +888,7 @@ fun Context.updateDirectoryPath(path: String) { val favoritePaths = getFavoritePaths() val curMedia = mediaFetcher.getFilesFrom(path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperFileSize, favoritePaths, false) val directory = createDirectoryFromMedia(path, curMedia, albumCovers, hiddenString, includedFolders, isSortingAscending, getProperFileSize) - updateDBDirectory(directory, galleryDB.DirectoryDao()) + updateDBDirectory(directory) } fun Context.getFileDateTaken(path: String): Long { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt index 8b625e5e5..6a4db386a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt @@ -216,7 +216,7 @@ class MediaFetcher(val context: Context) { val media = ArrayList() val isRecycleBin = folder == RECYCLE_BIN val deletedMedia = if (isRecycleBin) { - context.getUpdatedDeletedMedia(context.galleryDB.MediumDao()) + context.getUpdatedDeletedMedia() } else { ArrayList() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MyWidgetProvider.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MyWidgetProvider.kt index 199d90952..f2e1b3d02 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MyWidgetProvider.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MyWidgetProvider.kt @@ -18,7 +18,7 @@ import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.activities.MediaActivity import com.simplemobiletools.gallery.pro.extensions.config -import com.simplemobiletools.gallery.pro.extensions.directoryDB +import com.simplemobiletools.gallery.pro.extensions.directoryDao import com.simplemobiletools.gallery.pro.extensions.getFolderNameFromPath import com.simplemobiletools.gallery.pro.extensions.widgetsDB import com.simplemobiletools.gallery.pro.models.Widget @@ -45,7 +45,7 @@ class MyWidgetProvider : AppWidgetProvider() { setText(R.id.widget_folder_name, context.getFolderNameFromPath(it.folderPath)) } - val path = context.directoryDB.getDirectoryThumbnail(it.folderPath) ?: return@forEach + val path = context.directoryDao.getDirectoryThumbnail(it.folderPath) ?: return@forEach val options = RequestOptions() .signature(path.getFileSignature()) .diskCacheStrategy(DiskCacheStrategy.RESOURCE)