From 73ac82266165819a14c1ad315e7f48c02da93913 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 20 Apr 2018 13:20:44 +0200 Subject: [PATCH] handle deleting folders from local db --- .../simplemobiletools/gallery/activities/MainActivity.kt | 5 +++++ .../com/simplemobiletools/gallery/extensions/Activity.kt | 8 +++++++- .../simplemobiletools/gallery/interfaces/DirectoryDao.kt | 7 +++++++ 3 files changed, 19 insertions(+), 1 deletion(-) 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 1954b593d..c017e10f3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -362,6 +362,11 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { runOnUiThread { refreshItems() } + Thread { + folders.filter { !it.exists() }.forEach { + galleryDB.DirectoryDao().deleteDirPath(it.absolutePath) + } + }.start() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt index 78d66eb98..bdcb171c2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt @@ -246,7 +246,13 @@ fun Activity.loadJpg(path: String, target: MySquareImageView, cropThumbnails: Bo fun Activity.getCachedDirectories(callback: (ArrayList) -> Unit) { Thread { - callback(galleryDB.DirectoryDao().getAll() as ArrayList) + val directoryDao = galleryDB.DirectoryDao() + val directories = directoryDao.getAll() as ArrayList + callback(directories) + + directories.filter { !File(it.path).exists() }.forEach { + directoryDao.deleteDir(it) + } }.start() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt index 3a242dd77..ed84a1319 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt @@ -1,6 +1,7 @@ package com.simplemobiletools.gallery.interfaces import android.arch.persistence.room.Dao +import android.arch.persistence.room.Delete import android.arch.persistence.room.Insert import android.arch.persistence.room.OnConflictStrategy.REPLACE import android.arch.persistence.room.Query @@ -16,4 +17,10 @@ interface DirectoryDao { @Insert(onConflict = REPLACE) fun insertAll(directories: List) + + @Delete + fun deleteDir(directory: Directory) + + @Query("DELETE FROM directories WHERE path = :path") + fun deleteDirPath(path: String) }