update directories if one is renamed

This commit is contained in:
tibbi 2018-04-21 22:49:29 +02:00
parent f08fd48d1f
commit e237afe916
3 changed files with 17 additions and 11 deletions

View file

@ -681,8 +681,12 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
gotDirectories(directories, true) gotDirectories(directories, true)
} else { } else {
mDirs = directories mDirs = directories
storeDirectories()
} }
Thread {
storeDirectoryItems(directories)
removeInvalidDirectories()
}.start()
} }
private fun checkWhatsNewDialog() { private fun checkWhatsNewDialog() {

View file

@ -155,7 +155,8 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList<Directo
} }
private fun renameDir() { private fun renameDir() {
val sourcePath = dirs[selectedPositions.first()].path val firstDir = dirs[selectedPositions.first()]
val sourcePath = firstDir.path
val dir = File(sourcePath) val dir = File(sourcePath)
if (activity.isAStorageRootFolder(dir.absolutePath)) { if (activity.isAStorageRootFolder(dir.absolutePath)) {
activity.toast(R.string.rename_folder_root) activity.toast(R.string.rename_folder_root)
@ -164,11 +165,7 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList<Directo
RenameItemDialog(activity, dir.absolutePath) { RenameItemDialog(activity, dir.absolutePath) {
activity.runOnUiThread { activity.runOnUiThread {
if (selectedPositions.isEmpty()) { firstDir.apply {
return@runOnUiThread
}
dirs[selectedPositions.first()].apply {
path = it path = it
name = it.getFilenameFromPath() name = it.getFilenameFromPath()
tmb = File(it, tmb.getFilenameFromPath()).absolutePath tmb = File(it, tmb.getFilenameFromPath()).absolutePath

View file

@ -23,6 +23,7 @@ import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask
import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask
import com.simplemobiletools.gallery.databases.GalleryDataBase import com.simplemobiletools.gallery.databases.GalleryDataBase
import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.helpers.*
import com.simplemobiletools.gallery.interfaces.DirectoryDao
import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Directory
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import com.simplemobiletools.gallery.views.MySquareImageView import com.simplemobiletools.gallery.views.MySquareImageView
@ -249,10 +250,7 @@ fun Context.getCachedDirectories(callback: (ArrayList<Directory>) -> Unit) {
val directoryDao = galleryDB.DirectoryDao() val directoryDao = galleryDB.DirectoryDao()
val directories = directoryDao.getAll() as ArrayList<Directory> val directories = directoryDao.getAll() as ArrayList<Directory>
callback(directories) callback(directories)
removeInvalidDirectories(directories, directoryDao)
directories.filter { !File(it.path).exists() }.forEach {
directoryDao.deleteDirPath(it.path)
}
}.start() }.start()
} }
@ -268,6 +266,13 @@ fun Context.getCachedMedia(path: String, callback: (ArrayList<Medium>) -> Unit)
}.start() }.start()
} }
fun Context.removeInvalidDirectories(dirs: ArrayList<Directory>? = null, directoryDao: DirectoryDao = galleryDB.DirectoryDao()) {
val dirsToCheck = dirs ?: directoryDao.getAll()
dirsToCheck.filter { !File(it.path).exists() }.forEach {
directoryDao.deleteDirPath(it.path)
}
}
fun Context.updateMediaPath(oldPath: String, newPath: String) { fun Context.updateMediaPath(oldPath: String, newPath: String) {
val newFilename = newPath.getFilenameFromPath() val newFilename = newPath.getFilenameFromPath()
val newParentPath = newPath.getParentPath() val newParentPath = newPath.getParentPath()