lets just move the deleted file into the recycle bin when appropriate

This commit is contained in:
tibbi 2018-06-27 10:40:22 +02:00
parent f7e7482fc8
commit 87fde0f813
9 changed files with 54 additions and 18 deletions

View File

@ -275,7 +275,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
if (newFolder.exists() && newFolder.isDirectory) { if (newFolder.exists() && newFolder.isDirectory) {
if (newFolder.list()?.isEmpty() == true) { if (newFolder.list()?.isEmpty() == true) {
toast(String.format(getString(R.string.deleting_folder), config.tempFolderPath), Toast.LENGTH_LONG) toast(String.format(getString(R.string.deleting_folder), config.tempFolderPath), Toast.LENGTH_LONG)
tryDeleteFileDirItem(newFolder.toFileDirItem(applicationContext), true) tryDeleteFileDirItem(newFolder.toFileDirItem(applicationContext), true, true)
} }
} }
config.tempFolderPath = "" config.tempFolderPath = ""

View File

@ -490,7 +490,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
private fun deleteDirectoryIfEmpty() { private fun deleteDirectoryIfEmpty() {
val fileDirItem = FileDirItem(mPath, mPath.getFilenameFromPath()) val fileDirItem = FileDirItem(mPath, mPath.getFilenameFromPath())
if (config.deleteEmptyFolders && !fileDirItem.isDownloadsFolder() && fileDirItem.isDirectory && fileDirItem.getProperFileCount(applicationContext, true) == 0) { if (config.deleteEmptyFolders && !fileDirItem.isDownloadsFolder() && fileDirItem.isDirectory && fileDirItem.getProperFileCount(applicationContext, true) == 0) {
tryDeleteFileDirItem(fileDirItem, true) tryDeleteFileDirItem(fileDirItem, true, true)
} }
} }

View File

@ -95,7 +95,7 @@ open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentList
else -> TYPE_RAWS else -> TYPE_RAWS
} }
mMedium = Medium(null, getFilenameFromUri(mUri!!), mUri.toString(), mUri!!.path.getParentPath(), 0, 0, file.length(), type, false, 0) mMedium = Medium(null, getFilenameFromUri(mUri!!), mUri.toString(), mUri!!.path.getParentPath(), 0, 0, file.length(), type, false, 0L)
supportActionBar?.title = mMedium!!.name supportActionBar?.title = mMedium!!.name
bundle.putSerializable(MEDIUM, mMedium) bundle.putSerializable(MEDIUM, mMedium)

View File

@ -589,7 +589,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
} }
if (getDoesFilePathExist(newPath)) { if (getDoesFilePathExist(newPath)) {
tryDeleteFileDirItem(FileDirItem(newPath, newPath.getFilenameFromPath())) tryDeleteFileDirItem(FileDirItem(newPath, newPath.getFilenameFromPath()), false, true)
} }
copyFile(tmpPath, newPath) copyFile(tmpPath, newPath)
@ -618,7 +618,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
} catch (e: Exception) { } catch (e: Exception) {
showErrorToast(e) showErrorToast(e)
} finally { } finally {
tryDeleteFileDirItem(tmpFileDirItem) tryDeleteFileDirItem(tmpFileDirItem, false, true)
} }
} }
@ -837,9 +837,23 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
} }
private fun deleteConfirmed() { private fun deleteConfirmed() {
val path = getCurrentMedia()[mPos].path val path = getCurrentMedia().getOrNull(mPos)?.path ?: return
tryDeleteFileDirItem(FileDirItem(path, path.getFilenameFromPath())) { if (config.useRecycleBin) {
Thread {
movePathInRecycleBin(path) {
if (it) {
galleryDB.MediumDao().updateDeleted(path, System.currentTimeMillis())
refreshViewPager() refreshViewPager()
} else {
toast(R.string.unknown_error_occurred)
}
}
}.start()
} else {
val fileDirItem = FileDirItem(path, path.getFilenameFromPath())
tryDeleteFileDirItem(fileDirItem, false, true) {
refreshViewPager()
}
} }
} }
@ -926,7 +940,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
private fun deleteDirectoryIfEmpty() { private fun deleteDirectoryIfEmpty() {
val fileDirItem = FileDirItem(mDirectory, mDirectory.getFilenameFromPath(), getIsPathDirectory(mDirectory)) val fileDirItem = FileDirItem(mDirectory, mDirectory.getFilenameFromPath(), getIsPathDirectory(mDirectory))
if (config.deleteEmptyFolders && !fileDirItem.isDownloadsFolder() && fileDirItem.isDirectory && fileDirItem.getProperFileCount(applicationContext, true) == 0) { if (config.deleteEmptyFolders && !fileDirItem.isDownloadsFolder() && fileDirItem.isDirectory && fileDirItem.getProperFileCount(applicationContext, true) == 0) {
tryDeleteFileDirItem(fileDirItem, true) tryDeleteFileDirItem(fileDirItem, true, true)
} }
scanPathRecursively(mDirectory) scanPathRecursively(mDirectory)

View File

@ -133,7 +133,7 @@ fun BaseSimpleActivity.removeNoMedia(path: String, callback: (() -> Unit)? = nul
return return
} }
tryDeleteFileDirItem(file.toFileDirItem(applicationContext)) { tryDeleteFileDirItem(file.toFileDirItem(applicationContext), false, false) {
callback?.invoke() callback?.invoke()
} }
} }
@ -168,12 +168,31 @@ fun BaseSimpleActivity.tryCopyMoveFilesTo(fileDirItems: ArrayList<FileDirItem>,
} }
} }
fun BaseSimpleActivity.tryDeleteFileDirItem(fileDirItem: FileDirItem, allowDeleteFolder: Boolean = false, callback: ((wasSuccess: Boolean) -> Unit)? = null) { fun BaseSimpleActivity.tryDeleteFileDirItem(fileDirItem: FileDirItem, allowDeleteFolder: Boolean = false, deleteFromDatabase: Boolean,
callback: ((wasSuccess: Boolean) -> Unit)? = null) {
deleteFile(fileDirItem, allowDeleteFolder) { deleteFile(fileDirItem, allowDeleteFolder) {
callback?.invoke(it) callback?.invoke(it)
if (deleteFromDatabase) {
Thread { Thread {
galleryDB.MediumDao().deleteMediumPath(fileDirItem.path) galleryDB.MediumDao().deleteMediumPath(fileDirItem.path)
}.start() }.start()
} }
}
}
fun BaseSimpleActivity.movePathInRecycleBin(path: String, callback: (success: Boolean) -> Unit) {
val file = File(path)
val internalFile = File(filesDir, path)
return try {
file.copyRecursively(internalFile, true)
val fileDirItem = FileDirItem(path, path.getFilenameFromPath())
tryDeleteFileDirItem(fileDirItem, getIsPathDirectory(path), false) {
Thread {
callback(it)
}.start()
}
} catch (ignored: Exception) {
callback(false)
}
} }

View File

@ -224,7 +224,7 @@ class MediaFetcher(val context: Context) {
val path = file.absolutePath val path = file.absolutePath
val isFavorite = favoritePaths.contains(path) val isFavorite = favoritePaths.contains(path)
val medium = Medium(null, filename, path, file.parent, lastModified, dateTaken, size, type, isFavorite, 0) val medium = Medium(null, filename, path, file.parent, lastModified, dateTaken, size, type, isFavorite, 0L)
media.add(medium) media.add(medium)
} }
return media return media
@ -281,7 +281,7 @@ class MediaFetcher(val context: Context) {
val path = Uri.decode(file.uri.toString().replaceFirst("${context.config.OTGTreeUri}/document/${context.config.OTGPartition}%3A", OTG_PATH)) val path = Uri.decode(file.uri.toString().replaceFirst("${context.config.OTGTreeUri}/document/${context.config.OTGPartition}%3A", OTG_PATH))
val isFavorite = favoritePaths.contains(path) val isFavorite = favoritePaths.contains(path)
val medium = Medium(null, filename, path, folder, dateModified, dateTaken, size, type, isFavorite, 0) val medium = Medium(null, filename, path, folder, dateModified, dateTaken, size, type, isFavorite, 0L)
media.add(medium) media.add(medium)
} }

View File

@ -34,4 +34,7 @@ interface MediumDao {
@Query("UPDATE media SET is_favorite = :isFavorite WHERE full_path = :path COLLATE NOCASE") @Query("UPDATE media SET is_favorite = :isFavorite WHERE full_path = :path COLLATE NOCASE")
fun updateFavorite(path: String, isFavorite: Boolean) fun updateFavorite(path: String, isFavorite: Boolean)
@Query("UPDATE media SET deleted_ts = :deletedTS WHERE full_path = :path COLLATE NOCASE")
fun updateDeleted(path: String, deletedTS: Long)
} }

View File

@ -26,7 +26,7 @@ data class Medium(
@ColumnInfo(name = "size") val size: Long, @ColumnInfo(name = "size") val size: Long,
@ColumnInfo(name = "type") val type: Int, @ColumnInfo(name = "type") val type: Int,
@ColumnInfo(name = "is_favorite") var isFavorite: Boolean, @ColumnInfo(name = "is_favorite") var isFavorite: Boolean,
@ColumnInfo(name = "deleted_ts") var deletedTS: Int) : Serializable, ThumbnailItem() { @ColumnInfo(name = "deleted_ts") var deletedTS: Long) : Serializable, ThumbnailItem() {
companion object { companion object {
private const val serialVersionUID = -6553149366975655L private const val serialVersionUID = -6553149366975655L

View File

@ -19,7 +19,7 @@ class RefreshMediaReceiver : BroadcastReceiver() {
Thread { Thread {
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(), getFileType(path), false, 0) File(path).length(), getFileType(path), false, 0L)
context.galleryDB.MediumDao().insert(medium) context.galleryDB.MediumDao().insert(medium)
}.start() }.start()
} }