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.list()?.isEmpty() == true) {
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 = ""

View file

@ -490,7 +490,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
private fun deleteDirectoryIfEmpty() {
val fileDirItem = FileDirItem(mPath, mPath.getFilenameFromPath())
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
}
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
bundle.putSerializable(MEDIUM, mMedium)

View file

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

View file

@ -133,7 +133,7 @@ fun BaseSimpleActivity.removeNoMedia(path: String, callback: (() -> Unit)? = nul
return
}
tryDeleteFileDirItem(file.toFileDirItem(applicationContext)) {
tryDeleteFileDirItem(file.toFileDirItem(applicationContext), false, false) {
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) {
callback?.invoke(it)
if (deleteFromDatabase) {
Thread {
galleryDB.MediumDao().deleteMediumPath(fileDirItem.path)
}.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 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)
}
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 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)
}

View file

@ -34,4 +34,7 @@ interface MediumDao {
@Query("UPDATE media SET is_favorite = :isFavorite WHERE full_path = :path COLLATE NOCASE")
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 = "type") val type: Int,
@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 {
private const val serialVersionUID = -6553149366975655L

View file

@ -19,7 +19,7 @@ class RefreshMediaReceiver : BroadcastReceiver() {
Thread {
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)
}.start()
}