shortening some code related to getting DAOs

This commit is contained in:
tibbi 2020-01-25 10:56:08 +01:00
parent b9369139f1
commit 3ca8e685b8
11 changed files with 69 additions and 94 deletions

View file

@ -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<String>()
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<Directory>) {
ensureBackgroundThread {
storeDirectoryItems(directories, mDirectoryDao)
storeDirectoryItems(directories)
removeInvalidDBDirectories()
}
}

View file

@ -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<ThumbnailItem>()
}
@ -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<String>
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<String>, mMediumDao) {
movePathsInRecycleBin(filtered.map { it.path } as ArrayList<String>) {
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)
}
}
}

View file

@ -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<String>, galleryDB.MediumDao()) {
movePathsInRecycleBin(filtered.map { it.path } as ArrayList<String>) {
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)
}
}
}

View file

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

View file

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

View file

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

View file

@ -198,7 +198,7 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList<Directo
updateDirs(dirs)
ensureBackgroundThread {
try {
activity.galleryDB.DirectoryDao().updateDirectoryAfterRename(firstDir.tmb, firstDir.name, firstDir.path, sourcePath)
activity.directoryDao.updateDirectoryAfterRename(firstDir.tmb, firstDir.name, firstDir.path, sourcePath)
listener?.refreshItems()
} catch (e: Exception) {
activity.showErrorToast(e)

View file

@ -31,7 +31,6 @@ import com.simplemobiletools.gallery.pro.R
import com.simplemobiletools.gallery.pro.activities.SimpleActivity
import com.simplemobiletools.gallery.pro.dialogs.PickDirectoryDialog
import com.simplemobiletools.gallery.pro.helpers.RECYCLE_BIN
import com.simplemobiletools.gallery.pro.interfaces.MediumDao
import com.squareup.picasso.Picasso
import java.io.File
import java.io.FileOutputStream
@ -223,7 +222,7 @@ fun BaseSimpleActivity.tryDeleteFileDirItem(fileDirItem: FileDirItem, allowDelet
deleteFile(fileDirItem, allowDeleteFolder) {
if (deleteFromDatabase) {
ensureBackgroundThread {
deleteDBPath(galleryDB.MediumDao(), fileDirItem.path)
deleteDBPath(fileDirItem.path)
runOnUiThread {
callback?.invoke(it)
}
@ -234,7 +233,7 @@ fun BaseSimpleActivity.tryDeleteFileDirItem(fileDirItem: FileDirItem, allowDelet
}
}
fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList<String>, mediumDao: MediumDao = galleryDB.MediumDao(), callback: ((wasSuccess: Boolean) -> Unit)?) {
fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList<String>, callback: ((wasSuccess: Boolean) -> Unit)?) {
ensureBackgroundThread {
var pathsCnt = paths.size
val OTGPath = config.OTGPath
@ -261,7 +260,7 @@ fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList<String>, 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<String>, 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<String>, mediumDao
}
fun BaseSimpleActivity.restoreRecycleBinPath(path: String, callback: () -> Unit) {
restoreRecycleBinPaths(arrayListOf(path), galleryDB.MediumDao(), callback)
restoreRecycleBinPaths(arrayListOf(path), callback)
}
fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList<String>, mediumDao: MediumDao = galleryDB.MediumDao(), callback: () -> Unit) {
fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList<String>, callback: () -> Unit) {
ensureBackgroundThread {
val newPaths = ArrayList<String>()
for (source in paths) {
@ -328,7 +327,7 @@ fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList<String>, 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<String>, showToasts: Boolean, hasResc
try {
var didUpdateFile = false
val operations = ArrayList<ContentProviderOperation>()
val mediumDao = galleryDB.MediumDao()
ensureBackgroundThread {
for (path in paths) {
@ -442,7 +440,7 @@ fun Activity.fixDateTaken(paths: ArrayList<String>, showToasts: Boolean, hasResc
operations.clear()
}
mediumDao.updateFavoriteDateTaken(path, timestamp)
mediaDB.updateFavoriteDateTaken(path, timestamp)
didUpdateFile = true
if (!hasRescanned && getFileDateTaken(path) == 0L) {

View file

@ -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<Medium>
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<Directory>, directoryDao: DirectoryDao) {
fun Context.storeDirectoryItems(items: ArrayList<Directory>) {
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<Directory>) -> Unit) {
fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, forceShowHidden: Boolean = false, callback: (ArrayList<Directory>) -> Unit) {
ensureBackgroundThread {
val directories = try {
directoryDao.getAll() as ArrayList<Directory>
@ -602,22 +601,21 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly:
val clone = filteredDirectories.clone() as ArrayList<Directory>
callback(clone.distinctBy { it.path.getDistinctPath() } as ArrayList<Directory>)
removeInvalidDBDirectories(filteredDirectories, directoryDao)
removeInvalidDBDirectories(filteredDirectories)
}
}
fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, mediumDao: MediumDao = galleryDB.MediumDao(),
callback: (ArrayList<ThumbnailItem>) -> Unit) {
fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, callback: (ArrayList<ThumbnailItem>) -> Unit) {
ensureBackgroundThread {
val mediaFetcher = MediaFetcher(this)
val foldersToScan = if (path.isEmpty()) mediaFetcher.getFoldersToScan() else arrayListOf(path)
var media = ArrayList<Medium>()
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<Medium>()
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<Directory>? = null, directoryDao: DirectoryDao = galleryDB.DirectoryDao()) {
fun Context.removeInvalidDBDirectories(dirs: ArrayList<Directory>? = 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<Medium> {
fun Context.getUpdatedDeletedMedia(): ArrayList<Medium> {
val media = try {
mediumDao.getDeletedMedia() as ArrayList<Medium>
mediaDB.getDeletedMedia() as ArrayList<Medium>
} catch (ignored: Exception) {
ArrayList<Medium>()
}
@ -745,13 +743,13 @@ fun Context.getUpdatedDeletedMedia(mediumDao: MediumDao): ArrayList<Medium> {
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 {

View file

@ -216,7 +216,7 @@ class MediaFetcher(val context: Context) {
val media = ArrayList<Medium>()
val isRecycleBin = folder == RECYCLE_BIN
val deletedMedia = if (isRecycleBin) {
context.getUpdatedDeletedMedia(context.galleryDB.MediumDao())
context.getUpdatedDeletedMedia()
} else {
ArrayList()
}

View file

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