From 0039591a535edb77d3f2c6e60ce8c4d48b483400 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 21 Apr 2018 22:19:27 +0200 Subject: [PATCH] properly update files after renaming in the local database --- app/build.gradle | 2 +- .../gallery/activities/MediaActivity.kt | 16 +++++++--------- .../gallery/activities/ViewPagerActivity.kt | 12 ++++++++++-- .../gallery/adapters/MediaAdapter.kt | 7 ++++++- .../gallery/extensions/Context.kt | 6 ++++++ .../gallery/interfaces/MediumDao.kt | 3 +++ app/src/main/res/values-da/strings.xml | 2 +- 7 files changed, 34 insertions(+), 14 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 4e75f2a77..09ddc0234 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -47,7 +47,7 @@ ext { } dependencies { - implementation 'com.simplemobiletools:commons:3.19.5' + implementation 'com.simplemobiletools:commons:3.19.7' implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.0' implementation 'com.android.support:multidex:1.0.3' implementation 'it.sephiroth.android.exif:library:1.0.1' 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 7ee09f1c0..d4e1b039d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -657,15 +657,6 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { override fun deleteFiles(fileDirItems: ArrayList) { val filtered = fileDirItems.filter { it.path.isImageVideoGif() } as ArrayList deleteFiles(filtered) { - Thread { - val mediumDao = galleryDB.MediumDao() - filtered.forEach { - if (!File(it.path).exists()) { - mediumDao.deleteMediumPath(it.path) - } - } - }.start() - if (!it) { toast(R.string.unknown_error_occurred) } else if (mMedia.isEmpty()) { @@ -673,6 +664,13 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { finish() } else { updateStoredDirectories() + + Thread { + val mediumDao = galleryDB.MediumDao() + filtered.forEach { + mediumDao.deleteMediumPath(it.path) + } + }.start() } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt index ec9ea974d..81ea68367 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -757,8 +757,16 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } private fun renameFile() { - RenameItemDialog(this, getCurrentPath()) { - getCurrentMedia()[mPos].path = it + val oldPath = getCurrentPath() + RenameItemDialog(this, oldPath) { + getCurrentMedia()[mPos].apply { + path = it + name = it.getFilenameFromPath() + } + + Thread { + updateMediaPath(oldPath, it) + }.start() updateActionbarTitle() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt index af8cc80e9..72fa360af 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt @@ -148,7 +148,12 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, } private fun renameFile() { - RenameItemDialog(activity, getCurrentPath()) { + val oldPath = getCurrentPath() + RenameItemDialog(activity, oldPath) { + Thread { + activity.updateMediaPath(oldPath, it) + }.start() + activity.runOnUiThread { listener?.refreshItems() finishActMode() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt index 1794b40f0..5f7b9fa93 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -267,3 +267,9 @@ fun Context.getCachedMedia(path: String, callback: (ArrayList) -> Unit) } }.start() } + +fun Context.updateMediaPath(oldPath: String, newPath: String) { + val newFilename = newPath.getFilenameFromPath() + val newParentPath = newPath.getParentPath() + galleryDB.MediumDao().updateMedia(oldPath, newParentPath, newFilename, newPath) +} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt index 8f04294a2..cd2cb1564 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt @@ -22,4 +22,7 @@ interface MediumDao { @Query("DELETE FROM media WHERE full_path = :path") fun deleteMediumPath(path: String) + + @Query("UPDATE media SET filename = :newFilename, full_path = :newFullPath, parent_path = :newParentPath WHERE full_path = :oldPath") + fun updateMedia(oldPath: String, newParentPath: String, newFilename: String, newFullPath: String) } diff --git a/app/src/main/res/values-da/strings.xml b/app/src/main/res/values-da/strings.xml index dfd763b29..3e07fbbae 100644 --- a/app/src/main/res/values-da/strings.xml +++ b/app/src/main/res/values-da/strings.xml @@ -97,7 +97,7 @@ Slideshow Frekvens (sekunder): Inkluder billeder - Inkluder videoer/string> + Inkluder videoer Inkluder GIF\'er Tilfældig rækkefølge Use fade animations