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 b4434fc41..a31c36f8e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -33,6 +33,7 @@ import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.interfaces.DirectoryDao import com.simplemobiletools.gallery.interfaces.DirectoryOperationsListener +import com.simplemobiletools.gallery.interfaces.MediumDao import com.simplemobiletools.gallery.models.AlbumCover import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Medium @@ -73,11 +74,17 @@ 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() + config.temporarilyShowHidden = false config.tempSkipDeleteConfirmation = false mIsPickImageIntent = isPickImageIntent(intent) @@ -435,9 +442,8 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { } Thread { - val directoryDao = galleryDB.DirectoryDao() folders.filter { !it.exists() }.forEach { - directoryDao.deleteDirPath(it.absolutePath) + mDirectoryDao.deleteDirPath(it.absolutePath) } }.start() } @@ -693,8 +699,6 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { val albumCovers = config.parseAlbumCovers() val includedFolders = config.includedFolders val isSortingAscending = config.directorySorting and SORT_DESCENDING == 0 - val mediumDao = galleryDB.MediumDao() - val directoryDao = galleryDB.DirectoryDao() val getProperDateTaken = config.directorySorting and SORT_BY_DATE_TAKEN != 0 val favoritePaths = getFavoritePaths() @@ -731,14 +735,14 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { // update directories and media files in the local db, delete invalid items updateDBDirectory(directory) if (!directory.isRecycleBin()) { - mediumDao.insertAll(curMedia) + mMediumDao.insertAll(curMedia) } getCachedMedia(directory.path, getVideosOnly, getImagesOnly) { it.forEach { if (!curMedia.contains(it)) { val path = (it as? Medium)?.path if (path != null) { - mediumDao.deleteMediumPath(path) + mMediumDao.deleteMediumPath(path) } } } @@ -782,9 +786,9 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { val newDir = createDirectoryFromMedia(folder, newMedia, albumCovers, hiddenString, includedFolders, isSortingAscending) dirs.add(newDir) showSortedDirs(dirs) - directoryDao.insert(newDir) + mDirectoryDao.insert(newDir) if (folder != RECYCLE_BIN) { - mediumDao.insertAll(newMedia) + mMediumDao.insertAll(newMedia) } } @@ -795,7 +799,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { directories_refresh_layout.isRefreshing = false checkPlaceholderVisibility(dirs) } - checkInvalidDirectories(dirs, directoryDao) + checkInvalidDirectories(dirs) val everShownFolders = config.everShownFolders as HashSet dirs.mapTo(everShownFolders) { it.path } @@ -895,7 +899,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { } } - private fun checkInvalidDirectories(dirs: ArrayList, directoryDao: DirectoryDao) { + private fun checkInvalidDirectories(dirs: ArrayList) { val invalidDirs = ArrayList() dirs.filter { !it.areFavorites() && !it.isRecycleBin() }.forEach { if (!getDoesFilePathExist(it.path)) { @@ -918,7 +922,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { if (config.useRecycleBin) { val binFolder = dirs.firstOrNull { it.path == RECYCLE_BIN } - if (binFolder != null && galleryDB.MediumDao().getDeletedMedia().isEmpty()) { + if (binFolder != null && mMediumDao.getDeletedMedia().isEmpty()) { invalidDirs.add(binFolder) } } @@ -927,7 +931,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { dirs.removeAll(invalidDirs) showSortedDirs(dirs) invalidDirs.forEach { - directoryDao.deleteDirPath(it.path) + mDirectoryDao.deleteDirPath(it.path) } } } @@ -971,11 +975,10 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { private fun checkRecycleBinItems() { if (config.useRecycleBin) { Thread { - val mediumDao = galleryDB.MediumDao() - val deletedMedia = mediumDao.getDeletedMedia() + val deletedMedia = mMediumDao.getDeletedMedia() deletedMedia.forEach { if (System.currentTimeMillis() > it.deletedTS + MONTH_MILLISECONDS) { - mediumDao.deleteMediumPath(it.path) + mMediumDao.deleteMediumPath(it.path) } } }.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 5dc69d41e..50e09efaf 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -39,7 +39,9 @@ import com.simplemobiletools.gallery.dialogs.ExcludeFolderDialog import com.simplemobiletools.gallery.dialogs.FilterMediaDialog import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.helpers.* +import com.simplemobiletools.gallery.interfaces.DirectoryDao import com.simplemobiletools.gallery.interfaces.MediaOperationsListener +import com.simplemobiletools.gallery.interfaces.MediumDao import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.ThumbnailItem import com.simplemobiletools.gallery.models.ThumbnailSection @@ -75,6 +77,9 @@ 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() } @@ -82,6 +87,10 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { override fun onCreate(savedInstanceState: Bundle?) { 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) @@ -447,7 +456,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { val paths = mMedia.filter { it is Medium }.map { (it as Medium).path } as ArrayList restoreRecycleBinPaths(paths) { Thread { - galleryDB.DirectoryDao().deleteDirPath(RECYCLE_BIN) + mDirectoryDao.deleteDirPath(RECYCLE_BIN) }.start() finish() } @@ -575,7 +584,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { if (mPath == FAVORITES) { Thread { - galleryDB.DirectoryDao().deleteDirPath(FAVORITES) + mDirectoryDao.deleteDirPath(FAVORITES) }.start() } @@ -588,7 +597,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { private fun deleteDBDirectory() { Thread { - galleryDB.DirectoryDao().deleteDirPath(mPath) + mDirectoryDao.deleteDirPath(mPath) }.start() } @@ -815,7 +824,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { mLatestMediaDateId = getLatestMediaByDateId() if (!isFromCache) { val mediaToInsert = (mMedia).filter { it is Medium && it.deletedTS == 0L }.map { it as Medium } - galleryDB.MediumDao().insertAll(mediaToInsert) + mMediumDao.insertAll(mediaToInsert) } } @@ -847,11 +856,10 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { mMedia.removeAll { filtered.map { it.path }.contains((it as? Medium)?.path) } Thread { - val mediumDao = galleryDB.MediumDao() val useRecycleBin = config.useRecycleBin filtered.forEach { if (!useRecycleBin) { - mediumDao.deleteMediumPath(it.path) + mMediumDao.deleteMediumPath(it.path) } } }.start()