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.dialogs.FilterMediaDialog
import com.simplemobiletools.gallery.pro.extensions.* import com.simplemobiletools.gallery.pro.extensions.*
import com.simplemobiletools.gallery.pro.helpers.* 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.DirectoryOperationsListener
import com.simplemobiletools.gallery.pro.interfaces.MediumDao
import com.simplemobiletools.gallery.pro.jobs.NewPhotoFetcher import com.simplemobiletools.gallery.pro.jobs.NewPhotoFetcher
import com.simplemobiletools.gallery.pro.models.Directory import com.simplemobiletools.gallery.pro.models.Directory
import com.simplemobiletools.gallery.pro.models.Medium import com.simplemobiletools.gallery.pro.models.Medium
@ -85,17 +83,11 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
private var mStoredTextColor = 0 private var mStoredTextColor = 0
private var mStoredPrimaryColor = 0 private var mStoredPrimaryColor = 0
private lateinit var mMediumDao: MediumDao
private lateinit var mDirectoryDao: DirectoryDao
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
appLaunched(BuildConfig.APPLICATION_ID) appLaunched(BuildConfig.APPLICATION_ID)
mMediumDao = galleryDB.MediumDao()
mDirectoryDao = galleryDB.DirectoryDao()
if (savedInstanceState == null) { if (savedInstanceState == null) {
config.temporarilyShowHidden = false config.temporarilyShowHidden = false
config.tempSkipDeleteConfirmation = false config.tempSkipDeleteConfirmation = false
@ -464,7 +456,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
val getImagesOnly = mIsPickImageIntent || mIsGetImageContentIntent val getImagesOnly = mIsPickImageIntent || mIsGetImageContentIntent
val getVideosOnly = mIsPickVideoIntent || mIsGetVideoContentIntent val getVideosOnly = mIsPickVideoIntent || mIsGetVideoContentIntent
getCachedDirectories(getVideosOnly, getImagesOnly, mDirectoryDao) { getCachedDirectories(getVideosOnly, getImagesOnly) {
gotDirectories(addTempFolderIfNeeded(it)) gotDirectories(addTempFolderIfNeeded(it))
} }
} }
@ -569,7 +561,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
val pathsToDelete = ArrayList<String>() val pathsToDelete = ArrayList<String>()
itemsToDelete.mapTo(pathsToDelete) { it.path } itemsToDelete.mapTo(pathsToDelete) { it.path }
movePathsInRecycleBin(pathsToDelete, mMediumDao) { movePathsInRecycleBin(pathsToDelete) {
if (it) { if (it) {
deleteFilteredFileDirItems(itemsToDelete, folders) deleteFilteredFileDirItems(itemsToDelete, folders)
} else { } else {
@ -590,7 +582,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
ensureBackgroundThread { ensureBackgroundThread {
folders.filter { !getDoesFilePathExist(it.absolutePath, OTGPath) }.forEach { folders.filter { !getDoesFilePathExist(it.absolutePath, OTGPath) }.forEach {
mDirectoryDao.deleteDirPath(it.absolutePath) directoryDao.deleteDirPath(it.absolutePath)
} }
if (config.deleteEmptyFolders) { if (config.deleteEmptyFolders) {
@ -936,16 +928,16 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
setupAdapter(dirs) setupAdapter(dirs)
// update directories and media files in the local db, delete invalid items // update directories and media files in the local db, delete invalid items
updateDBDirectory(directory, mDirectoryDao) updateDBDirectory(directory)
if (!directory.isRecycleBin()) { if (!directory.isRecycleBin()) {
mMediumDao.insertAll(curMedia) mediaDB.insertAll(curMedia)
} }
getCachedMedia(directory.path, getVideosOnly, getImagesOnly, mMediumDao) { getCachedMedia(directory.path, getVideosOnly, getImagesOnly) {
it.forEach { it.forEach {
if (!curMedia.contains(it)) { if (!curMedia.contains(it)) {
val path = (it as? Medium)?.path val path = (it as? Medium)?.path
if (path != null) { if (path != null) {
deleteDBPath(mMediumDao, path) deleteDBPath(path)
} }
} }
} }
@ -955,7 +947,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
if (dirPathsToRemove.isNotEmpty()) { if (dirPathsToRemove.isNotEmpty()) {
val dirsToRemove = dirs.filter { dirPathsToRemove.contains(it.path) } val dirsToRemove = dirs.filter { dirPathsToRemove.contains(it.path) }
dirsToRemove.forEach { dirsToRemove.forEach {
mDirectoryDao.deleteDirPath(it.path) directoryDao.deleteDirPath(it.path)
} }
dirs.removeAll(dirsToRemove) dirs.removeAll(dirsToRemove)
setupAdapter(dirs) setupAdapter(dirs)
@ -999,9 +991,9 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
dirs.add(newDir) dirs.add(newDir)
setupAdapter(dirs) setupAdapter(dirs)
try { try {
mDirectoryDao.insert(newDir) directoryDao.insert(newDir)
if (folder != RECYCLE_BIN) { if (folder != RECYCLE_BIN) {
mMediumDao.insertAll(newMedia) mediaDB.insertAll(newMedia)
} }
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }
@ -1156,7 +1148,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
if (config.useRecycleBin) { if (config.useRecycleBin) {
try { try {
val binFolder = dirs.firstOrNull { it.path == RECYCLE_BIN } val binFolder = dirs.firstOrNull { it.path == RECYCLE_BIN }
if (binFolder != null && mMediumDao.getDeletedMedia().isEmpty()) { if (binFolder != null && mediaDB.getDeletedMedia().isEmpty()) {
invalidDirs.add(binFolder) invalidDirs.add(binFolder)
} }
} catch (ignored: Exception) { } catch (ignored: Exception) {
@ -1168,7 +1160,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
setupAdapter(dirs) setupAdapter(dirs)
invalidDirs.forEach { invalidDirs.forEach {
try { try {
mDirectoryDao.deleteDirPath(it.path) directoryDao.deleteDirPath(it.path)
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }
} }
@ -1218,7 +1210,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
Handler().postDelayed({ Handler().postDelayed({
ensureBackgroundThread { ensureBackgroundThread {
try { try {
mMediumDao.deleteOldRecycleBinItems(System.currentTimeMillis() - MONTH_MILLISECONDS) mediaDB.deleteOldRecycleBinItems(System.currentTimeMillis() - MONTH_MILLISECONDS)
} catch (e: Exception) { } catch (e: Exception) {
} }
} }
@ -1283,7 +1275,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
override fun updateDirectories(directories: ArrayList<Directory>) { override fun updateDirectories(directories: ArrayList<Directory>) {
ensureBackgroundThread { ensureBackgroundThread {
storeDirectoryItems(directories, mDirectoryDao) storeDirectoryItems(directories)
removeInvalidDBDirectories() 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.dialogs.*
import com.simplemobiletools.gallery.pro.extensions.* import com.simplemobiletools.gallery.pro.extensions.*
import com.simplemobiletools.gallery.pro.helpers.* 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.MediaOperationsListener
import com.simplemobiletools.gallery.pro.interfaces.MediumDao
import com.simplemobiletools.gallery.pro.models.Medium import com.simplemobiletools.gallery.pro.models.Medium
import com.simplemobiletools.gallery.pro.models.ThumbnailItem import com.simplemobiletools.gallery.pro.models.ThumbnailItem
import com.simplemobiletools.gallery.pro.models.ThumbnailSection import com.simplemobiletools.gallery.pro.models.ThumbnailSection
@ -78,9 +76,6 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener {
private var mStoredTextColor = 0 private var mStoredTextColor = 0
private var mStoredPrimaryColor = 0 private var mStoredPrimaryColor = 0
private lateinit var mMediumDao: MediumDao
private lateinit var mDirectoryDao: DirectoryDao
companion object { companion object {
var mMedia = ArrayList<ThumbnailItem>() var mMedia = ArrayList<ThumbnailItem>()
} }
@ -89,9 +84,6 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_media) setContentView(R.layout.activity_media)
mMediumDao = galleryDB.MediumDao()
mDirectoryDao = galleryDB.DirectoryDao()
intent.apply { intent.apply {
mIsGetImageIntent = getBooleanExtra(GET_IMAGE_INTENT, false) mIsGetImageIntent = getBooleanExtra(GET_IMAGE_INTENT, false)
mIsGetVideoIntent = getBooleanExtra(GET_VIDEO_INTENT, false) mIsGetVideoIntent = getBooleanExtra(GET_VIDEO_INTENT, false)
@ -495,9 +487,9 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener {
private fun restoreAllFiles() { private fun restoreAllFiles() {
val paths = mMedia.filter { it is Medium }.map { (it as Medium).path } as ArrayList<String> val paths = mMedia.filter { it is Medium }.map { (it as Medium).path } as ArrayList<String>
restoreRecycleBinPaths(paths, mMediumDao) { restoreRecycleBinPaths(paths) {
ensureBackgroundThread { ensureBackgroundThread {
mDirectoryDao.deleteDirPath(RECYCLE_BIN) directoryDao.deleteDirPath(RECYCLE_BIN)
} }
finish() finish()
} }
@ -584,7 +576,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener {
if (mLoadedInitialPhotos) { if (mLoadedInitialPhotos) {
startAsyncTask() startAsyncTask()
} else { } else {
getCachedMedia(mPath, mIsGetVideoIntent, mIsGetImageIntent, mMediumDao) { getCachedMedia(mPath, mIsGetVideoIntent, mIsGetImageIntent) {
if (it.isEmpty()) { if (it.isEmpty()) {
runOnUiThread { runOnUiThread {
media_refresh_layout.isRefreshing = true media_refresh_layout.isRefreshing = true
@ -608,7 +600,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener {
try { try {
gotMedia(newMedia, false) gotMedia(newMedia, false)
oldMedia.filter { !newMedia.contains(it) }.mapNotNull { it as? Medium }.filter { !getDoesFilePathExist(it.path) }.forEach { 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) { } catch (e: Exception) {
} }
@ -627,7 +619,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener {
if (mPath == FAVORITES) { if (mPath == FAVORITES) {
ensureBackgroundThread { ensureBackgroundThread {
mDirectoryDao.deleteDirPath(FAVORITES) directoryDao.deleteDirPath(FAVORITES)
} }
} }
@ -641,7 +633,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener {
private fun deleteDBDirectory() { private fun deleteDBDirectory() {
ensureBackgroundThread { ensureBackgroundThread {
try { try {
mDirectoryDao.deleteDirPath(mPath) directoryDao.deleteDirPath(mPath)
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }
} }
@ -897,7 +889,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener {
if (!isFromCache) { if (!isFromCache) {
val mediaToInsert = (mMedia).filter { it is Medium && it.deletedTS == 0L }.map { it as Medium } val mediaToInsert = (mMedia).filter { it is Medium && it.deletedTS == 0L }.map { it as Medium }
try { try {
mMediumDao.insertAll(mediaToInsert) mediaDB.insertAll(mediaToInsert)
} catch (e: Exception) { } 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) val movingItems = resources.getQuantityString(R.plurals.moving_items_into_bin, filtered.size, filtered.size)
toast(movingItems) toast(movingItems)
movePathsInRecycleBin(filtered.map { it.path } as ArrayList<String>, mMediumDao) { movePathsInRecycleBin(filtered.map { it.path } as ArrayList<String>) {
if (it) { if (it) {
deleteFilteredFiles(filtered) deleteFilteredFiles(filtered)
} else { } else {
@ -940,7 +932,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener {
val useRecycleBin = config.useRecycleBin val useRecycleBin = config.useRecycleBin
filtered.forEach { filtered.forEach {
if (it.path.startsWith(recycleBinPath) || !useRecycleBin) { 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) val movingItems = resources.getQuantityString(R.plurals.moving_items_into_bin, filtered.size, filtered.size)
toast(movingItems) toast(movingItems)
movePathsInRecycleBin(filtered.map { it.path } as ArrayList<String>, galleryDB.MediumDao()) { movePathsInRecycleBin(filtered.map { it.path } as ArrayList<String>) {
if (it) { if (it) {
deleteFilteredFiles(filtered) deleteFilteredFiles(filtered)
} else { } else {
@ -342,7 +342,7 @@ class SearchActivity : SimpleActivity(), MediaOperationsListener {
val useRecycleBin = config.useRecycleBin val useRecycleBin = config.useRecycleBin
filtered.forEach { filtered.forEach {
if (it.path.startsWith(recycleBinPath) || !useRecycleBin) { 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.R
import com.simplemobiletools.gallery.pro.dialogs.ManageBottomActionsDialog import com.simplemobiletools.gallery.pro.dialogs.ManageBottomActionsDialog
import com.simplemobiletools.gallery.pro.dialogs.ManageExtendedDetailsDialog import com.simplemobiletools.gallery.pro.dialogs.ManageExtendedDetailsDialog
import com.simplemobiletools.gallery.pro.extensions.config import com.simplemobiletools.gallery.pro.extensions.*
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.helpers.* import com.simplemobiletools.gallery.pro.helpers.*
import com.simplemobiletools.gallery.pro.models.AlbumCover import com.simplemobiletools.gallery.pro.models.AlbumCover
import kotlinx.android.synthetic.main.activity_settings.* import kotlinx.android.synthetic.main.activity_settings.*
@ -593,7 +590,7 @@ class SettingsActivity : SimpleActivity() {
private fun setupEmptyRecycleBin() { private fun setupEmptyRecycleBin() {
ensureBackgroundThread { ensureBackgroundThread {
try { try {
mRecycleBinContentSize = galleryDB.MediumDao().getDeletedMedia().sumByLong { it.size } mRecycleBinContentSize = mediaDB.getDeletedMedia().sumByLong { it.size }
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }
runOnUiThread { runOnUiThread {

View file

@ -364,8 +364,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
if (intent.action == "com.android.camera.action.REVIEW") { if (intent.action == "com.android.camera.action.REVIEW") {
ensureBackgroundThread { ensureBackgroundThread {
val mediumDao = galleryDB.MediumDao() if (mediaDB.getMediaFromPath(mPath).isEmpty()) {
if (mediumDao.getMediaFromPath(mPath).isEmpty()) {
val type = when { val type = when {
mPath.isVideoFast() -> TYPE_VIDEOS mPath.isVideoFast() -> TYPE_VIDEOS
mPath.isGif() -> TYPE_GIFS 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 duration = if (type == TYPE_VIDEOS) mPath.getVideoDuration() else 0
val ts = System.currentTimeMillis() val ts = System.currentTimeMillis()
val medium = Medium(null, mPath.getFilenameFromPath(), mPath, mPath.getParentPath(), ts, ts, File(mPath).length(), type, duration, isFavorite, 0) 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 { ensureBackgroundThread {
val path = directoryDB.getDirectoryThumbnail(folderPath) val path = directoryDao.getDirectoryThumbnail(folderPath)
if (path != null) { if (path != null) {
runOnUiThread { runOnUiThread {
loadJpg(path, config_image, config.cropThumbnails) loadJpg(path, config_image, config.cropThumbnails)

View file

@ -198,7 +198,7 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList<Directo
updateDirs(dirs) updateDirs(dirs)
ensureBackgroundThread { ensureBackgroundThread {
try { try {
activity.galleryDB.DirectoryDao().updateDirectoryAfterRename(firstDir.tmb, firstDir.name, firstDir.path, sourcePath) activity.directoryDao.updateDirectoryAfterRename(firstDir.tmb, firstDir.name, firstDir.path, sourcePath)
listener?.refreshItems() listener?.refreshItems()
} catch (e: Exception) { } catch (e: Exception) {
activity.showErrorToast(e) 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.activities.SimpleActivity
import com.simplemobiletools.gallery.pro.dialogs.PickDirectoryDialog import com.simplemobiletools.gallery.pro.dialogs.PickDirectoryDialog
import com.simplemobiletools.gallery.pro.helpers.RECYCLE_BIN import com.simplemobiletools.gallery.pro.helpers.RECYCLE_BIN
import com.simplemobiletools.gallery.pro.interfaces.MediumDao
import com.squareup.picasso.Picasso import com.squareup.picasso.Picasso
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
@ -223,7 +222,7 @@ fun BaseSimpleActivity.tryDeleteFileDirItem(fileDirItem: FileDirItem, allowDelet
deleteFile(fileDirItem, allowDeleteFolder) { deleteFile(fileDirItem, allowDeleteFolder) {
if (deleteFromDatabase) { if (deleteFromDatabase) {
ensureBackgroundThread { ensureBackgroundThread {
deleteDBPath(galleryDB.MediumDao(), fileDirItem.path) deleteDBPath(fileDirItem.path)
runOnUiThread { runOnUiThread {
callback?.invoke(it) 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 { ensureBackgroundThread {
var pathsCnt = paths.size var pathsCnt = paths.size
val OTGPath = config.OTGPath val OTGPath = config.OTGPath
@ -261,7 +260,7 @@ fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList<String>, mediumDao
out?.flush() out?.flush()
if (fileDocument?.getItemSize(true) == copiedSize && getDoesFilePathExist(destination)) { if (fileDocument?.getItemSize(true) == copiedSize && getDoesFilePathExist(destination)) {
mediumDao.updateDeleted("$RECYCLE_BIN$source", System.currentTimeMillis(), source) mediaDB.updateDeleted("$RECYCLE_BIN$source", System.currentTimeMillis(), source)
pathsCnt-- pathsCnt--
} }
} catch (e: Exception) { } catch (e: Exception) {
@ -277,7 +276,7 @@ fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList<String>, mediumDao
val lastModified = file.lastModified() val lastModified = file.lastModified()
try { try {
if (file.copyRecursively(internalFile, true)) { if (file.copyRecursively(internalFile, true)) {
mediumDao.updateDeleted("$RECYCLE_BIN$source", System.currentTimeMillis(), source) mediaDB.updateDeleted("$RECYCLE_BIN$source", System.currentTimeMillis(), source)
pathsCnt-- pathsCnt--
if (config.keepLastModified) { if (config.keepLastModified) {
@ -295,10 +294,10 @@ fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList<String>, mediumDao
} }
fun BaseSimpleActivity.restoreRecycleBinPath(path: String, callback: () -> Unit) { 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 { ensureBackgroundThread {
val newPaths = ArrayList<String>() val newPaths = ArrayList<String>()
for (source in paths) { for (source in paths) {
@ -328,7 +327,7 @@ fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList<String>, mediumDa
out?.flush() out?.flush()
if (File(source).length() == copiedSize) { 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) newPaths.add(destination)
@ -357,8 +356,8 @@ fun BaseSimpleActivity.emptyTheRecycleBin(callback: (() -> Unit)? = null) {
ensureBackgroundThread { ensureBackgroundThread {
try { try {
recycleBin.deleteRecursively() recycleBin.deleteRecursively()
galleryDB.MediumDao().clearRecycleBin() mediaDB.clearRecycleBin()
galleryDB.DirectoryDao().deleteRecycleBin() directoryDao.deleteRecycleBin()
toast(R.string.recycle_bin_emptied) toast(R.string.recycle_bin_emptied)
callback?.invoke() callback?.invoke()
} catch (e: Exception) { } catch (e: Exception) {
@ -413,7 +412,6 @@ fun Activity.fixDateTaken(paths: ArrayList<String>, showToasts: Boolean, hasResc
try { try {
var didUpdateFile = false var didUpdateFile = false
val operations = ArrayList<ContentProviderOperation>() val operations = ArrayList<ContentProviderOperation>()
val mediumDao = galleryDB.MediumDao()
ensureBackgroundThread { ensureBackgroundThread {
for (path in paths) { for (path in paths) {
@ -442,7 +440,7 @@ fun Activity.fixDateTaken(paths: ArrayList<String>, showToasts: Boolean, hasResc
operations.clear() operations.clear()
} }
mediumDao.updateFavoriteDateTaken(path, timestamp) mediaDB.updateFavoriteDateTaken(path, timestamp)
didUpdateFile = true didUpdateFile = true
if (!hasRescanned && getFileDateTaken(path) == 0L) { if (!hasRescanned && getFileDateTaken(path) == 0L) {

View file

@ -111,11 +111,11 @@ fun Context.launchSettings() {
val Context.config: Config get() = Config.newInstance(applicationContext) 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.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() val Context.favoritesDB: FavoritesDao get() = GalleryDatabase.getInstance(applicationContext).FavoritesDao()
@ -400,16 +400,15 @@ fun Context.rescanFolderMediaSync(path: String) {
GetMediaAsynctask(applicationContext, path, false, false, false) { GetMediaAsynctask(applicationContext, path, false, false, false) {
ensureBackgroundThread { ensureBackgroundThread {
val newMedia = it val newMedia = it
val mediumDao = galleryDB.MediumDao()
val media = newMedia.filter { it is Medium } as ArrayList<Medium> val media = newMedia.filter { it is Medium } as ArrayList<Medium>
try { try {
mediumDao.insertAll(media) mediaDB.insertAll(media)
cached.forEach { cached.forEach {
if (!newMedia.contains(it)) { if (!newMedia.contains(it)) {
val mediumPath = (it as? Medium)?.path val mediumPath = (it as? Medium)?.path
if (mediumPath != null) { 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 { ensureBackgroundThread {
directoryDao.insertAll(items) directoryDao.insertAll(items)
} }
@ -559,7 +558,7 @@ fun Context.loadSVG(path: String, target: MySquareImageView, cropThumbnails: Boo
.into(target) .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 { ensureBackgroundThread {
val directories = try { val directories = try {
directoryDao.getAll() as ArrayList<Directory> directoryDao.getAll() as ArrayList<Directory>
@ -602,22 +601,21 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly:
val clone = filteredDirectories.clone() as ArrayList<Directory> val clone = filteredDirectories.clone() as ArrayList<Directory>
callback(clone.distinctBy { it.path.getDistinctPath() } 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(), fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, callback: (ArrayList<ThumbnailItem>) -> Unit) {
callback: (ArrayList<ThumbnailItem>) -> Unit) {
ensureBackgroundThread { ensureBackgroundThread {
val mediaFetcher = MediaFetcher(this) val mediaFetcher = MediaFetcher(this)
val foldersToScan = if (path.isEmpty()) mediaFetcher.getFoldersToScan() else arrayListOf(path) val foldersToScan = if (path.isEmpty()) mediaFetcher.getFoldersToScan() else arrayListOf(path)
var media = ArrayList<Medium>() var media = ArrayList<Medium>()
if (path == FAVORITES) { if (path == FAVORITES) {
media.addAll(mediumDao.getFavorites()) media.addAll(mediaDB.getFavorites())
} }
if (path == RECYCLE_BIN) { if (path == RECYCLE_BIN) {
media.addAll(getUpdatedDeletedMedia(mediumDao)) media.addAll(getUpdatedDeletedMedia())
} }
if (config.filterMedia and TYPE_PORTRAITS != 0) { if (config.filterMedia and TYPE_PORTRAITS != 0) {
@ -634,7 +632,7 @@ fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImag
val shouldShowHidden = config.shouldShowHidden val shouldShowHidden = config.shouldShowHidden
foldersToScan.filter { path.isNotEmpty() || !config.isFolderProtected(it) }.forEach { foldersToScan.filter { path.isNotEmpty() || !config.isFolderProtected(it) }.forEach {
try { try {
val currMedia = mediumDao.getMediaFromPath(it) val currMedia = mediaDB.getMediaFromPath(it)
media.addAll(currMedia) media.addAll(currMedia)
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }
@ -667,7 +665,7 @@ fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImag
val mediaToDelete = ArrayList<Medium>() val mediaToDelete = ArrayList<Medium>()
media.filter { !getDoesFilePathExist(it.path, OTGPath) }.forEach { media.filter { !getDoesFilePathExist(it.path, OTGPath) }.forEach {
if (it.path.startsWith(recycleBinPath)) { if (it.path.startsWith(recycleBinPath)) {
deleteDBPath(mediumDao, it.path) deleteDBPath(it.path)
} else { } else {
mediaToDelete.add(it) mediaToDelete.add(it)
} }
@ -675,14 +673,14 @@ fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImag
try { try {
if (mediaToDelete.isNotEmpty()) { if (mediaToDelete.isNotEmpty()) {
mediumDao.deleteMedia(*mediaToDelete.toTypedArray()) mediaDB.deleteMedia(*mediaToDelete.toTypedArray())
} }
} catch (ignored: Exception) { } 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 dirsToCheck = dirs ?: directoryDao.getAll()
val OTGPath = config.OTGPath val OTGPath = config.OTGPath
dirsToCheck.filter { !it.areFavorites() && !it.isRecycleBin() && !getDoesFilePathExist(it.path, OTGPath) && it.path != config.tempFolderPath }.forEach { 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 newFilename = newPath.getFilenameFromPath()
val newParentPath = newPath.getParentPath() val newParentPath = newPath.getParentPath()
try { try {
galleryDB.MediumDao().updateMedium(oldPath, newParentPath, newFilename, newPath) mediaDB.updateMedium(oldPath, newParentPath, newFilename, newPath)
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }
} }
fun Context.updateDBDirectory(directory: Directory, directoryDao: DirectoryDao) { fun Context.updateDBDirectory(directory: Directory) {
try { try {
directoryDao.updateDirectory(directory.path, directory.tmb, directory.mediaCnt, directory.modified, directory.taken, directory.size, directory.types) directoryDao.updateDirectory(directory.path, directory.tmb, directory.mediaCnt, directory.modified, directory.taken, directory.size, directory.types)
} catch (ignored: Exception) { } 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... // 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 { val media = try {
mediumDao.getDeletedMedia() as ArrayList<Medium> mediaDB.getDeletedMedia() as ArrayList<Medium>
} catch (ignored: Exception) { } catch (ignored: Exception) {
ArrayList<Medium>() ArrayList<Medium>()
} }
@ -745,13 +743,13 @@ fun Context.getUpdatedDeletedMedia(mediumDao: MediumDao): ArrayList<Medium> {
return media return media
} }
fun Context.deleteDBPath(mediumDao: MediumDao, path: String) { fun Context.deleteDBPath(path: String) {
deleteMediumWithPath(mediumDao, path.replaceFirst(recycleBinPath, RECYCLE_BIN)) deleteMediumWithPath(path.replaceFirst(recycleBinPath, RECYCLE_BIN))
} }
fun Context.deleteMediumWithPath(mediumDao: MediumDao, path: String) { fun Context.deleteMediumWithPath(path: String) {
try { try {
mediumDao.deleteMediumPath(path) mediaDB.deleteMediumPath(path)
favoritesDB.deleteFavoritePath(path) favoritesDB.deleteFavoritePath(path)
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }
@ -841,13 +839,12 @@ fun Context.addPathToDB(path: String) {
} }
try { try {
val mediumDao = galleryDB.MediumDao()
val isFavorite = favoritesDB.isFavorite(path) val isFavorite = favoritesDB.isFavorite(path)
val videoDuration = if (type == TYPE_VIDEOS) path.getVideoDuration() else 0 val videoDuration = if (type == TYPE_VIDEOS) path.getVideoDuration() else 0
val medium = Medium(null, path.getFilenameFromPath(), path, path.getParentPath(), System.currentTimeMillis(), System.currentTimeMillis(), val medium = Medium(null, path.getFilenameFromPath(), path, path.getParentPath(), System.currentTimeMillis(), System.currentTimeMillis(),
File(path).length(), type, videoDuration, isFavorite, 0L) File(path).length(), type, videoDuration, isFavorite, 0L)
mediumDao.insert(medium) mediaDB.insert(medium)
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }
} }
@ -891,7 +888,7 @@ fun Context.updateDirectoryPath(path: String) {
val favoritePaths = getFavoritePaths() val favoritePaths = getFavoritePaths()
val curMedia = mediaFetcher.getFilesFrom(path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperFileSize, favoritePaths, false) val curMedia = mediaFetcher.getFilesFrom(path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperFileSize, favoritePaths, false)
val directory = createDirectoryFromMedia(path, curMedia, albumCovers, hiddenString, includedFolders, isSortingAscending, getProperFileSize) val directory = createDirectoryFromMedia(path, curMedia, albumCovers, hiddenString, includedFolders, isSortingAscending, getProperFileSize)
updateDBDirectory(directory, galleryDB.DirectoryDao()) updateDBDirectory(directory)
} }
fun Context.getFileDateTaken(path: String): Long { fun Context.getFileDateTaken(path: String): Long {

View file

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

View file

@ -18,7 +18,7 @@ import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.R
import com.simplemobiletools.gallery.pro.activities.MediaActivity import com.simplemobiletools.gallery.pro.activities.MediaActivity
import com.simplemobiletools.gallery.pro.extensions.config 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.getFolderNameFromPath
import com.simplemobiletools.gallery.pro.extensions.widgetsDB import com.simplemobiletools.gallery.pro.extensions.widgetsDB
import com.simplemobiletools.gallery.pro.models.Widget import com.simplemobiletools.gallery.pro.models.Widget
@ -45,7 +45,7 @@ class MyWidgetProvider : AppWidgetProvider() {
setText(R.id.widget_folder_name, context.getFolderNameFromPath(it.folderPath)) 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() val options = RequestOptions()
.signature(path.getFileSignature()) .signature(path.getFileSignature())
.diskCacheStrategy(DiskCacheStrategy.RESOURCE) .diskCacheStrategy(DiskCacheStrategy.RESOURCE)