add file deletion

This commit is contained in:
tibbi 2016-11-19 20:27:29 +01:00
parent 3049e8f1e8
commit c93cfcc729
3 changed files with 46 additions and 36 deletions

View file

@ -5,6 +5,7 @@ import android.app.WallpaperManager
import android.content.Intent import android.content.Intent
import android.database.Cursor import android.database.Cursor
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.Color
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.MediaStore import android.provider.MediaStore
@ -19,10 +20,7 @@ import android.widget.AdapterView
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.bumptech.glide.request.animation.GlideAnimation import com.bumptech.glide.request.animation.GlideAnimation
import com.bumptech.glide.request.target.SimpleTarget import com.bumptech.glide.request.target.SimpleTarget
import com.simplemobiletools.filepicker.extensions.hasStoragePermission import com.simplemobiletools.filepicker.extensions.*
import com.simplemobiletools.filepicker.extensions.scanFiles
import com.simplemobiletools.filepicker.extensions.scanPath
import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.gallery.Constants import com.simplemobiletools.gallery.Constants
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.adapters.MediaAdapter import com.simplemobiletools.gallery.adapters.MediaAdapter
@ -73,7 +71,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
//deleteFiles() deleteFiles()
} }
private fun tryloadGallery() { private fun tryloadGallery() {
@ -199,7 +197,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On
do { do {
val curPath = cursor.getString(pathIndex) ?: continue val curPath = cursor.getString(pathIndex) ?: continue
if (curPath.matches(pattern.toRegex()) && !mToBeDeleted.contains(curPath)) { if (curPath.matches(pattern.toRegex()) && !mToBeDeleted.contains(curPath.toLowerCase())) {
val file = File(curPath) val file = File(curPath)
if (file.exists()) { if (file.exists()) {
val dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED) val dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)
@ -231,22 +229,13 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On
false false
} }
/*private fun prepareForDeleting() { override fun prepareForDeleting(paths: ArrayList<String>) {
if (isShowingPermDialog(File(mPath)))
return
toast(R.string.deleting) toast(R.string.deleting)
val items = media_grid.checkedItemPositions mToBeDeleted = paths
val cnt = items.size() val deletedCnt = mToBeDeleted.size
var deletedCnt = 0
for (i in 0..cnt - 1) { if (isShowingPermDialog(File(mToBeDeleted[0])))
if (items.valueAt(i)) { return
val id = items.keyAt(i)
val path = mMedia[id].path
mToBeDeleted.add(path)
deletedCnt++
}
}
notifyDeletion(deletedCnt) notifyDeletion(deletedCnt)
} }
@ -266,7 +255,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On
show() show()
} }
mIsSnackbarShown = true mIsSnackbarShown = true
updateGridView() updateMediaView()
} }
} }
@ -313,19 +302,19 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On
mIsSnackbarShown = false mIsSnackbarShown = false
mToBeDeleted.clear() mToBeDeleted.clear()
mMedia = getMedia() mMedia = getMedia()
updateGridView() updateMediaView()
}*/ }
private fun updateGridView() { private fun updateMediaView() {
if (!isDirEmpty()) { if (!isDirEmpty()) {
//(media_grid.adapter as MediaAdapter).updateItems(mMedia) (media_grid.adapter as MediaAdapter).updateMedia(mMedia)
initializeGallery()
} }
} }
private fun isSetWallpaperIntent() = intent.getBooleanExtra(Constants.SET_WALLPAPER_INTENT, false) private fun isSetWallpaperIntent() = intent.getBooleanExtra(Constants.SET_WALLPAPER_INTENT, false)
private fun displayCopyDialog() { private fun displayCopyDialog() {
/*val items = media_grid.checkedItemPositions /*val items = media_grid.checkedItemPositions
val cnt = items.size() val cnt = items.size()
val files = (0..cnt - 1) val files = (0..cnt - 1)
@ -390,11 +379,6 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On
/*override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { /*override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.cab_delete -> {
prepareForDeleting()
mode.finish()
return true
}
R.id.cab_copy_move -> { R.id.cab_copy_move -> {
displayCopyDialog() displayCopyDialog()
return true return true
@ -405,7 +389,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On
override fun onTouch(v: View, event: MotionEvent): Boolean { override fun onTouch(v: View, event: MotionEvent): Boolean {
if (mIsSnackbarShown) { if (mIsSnackbarShown) {
//deleteFiles() deleteFiles()
} }
return false return false

View file

@ -186,7 +186,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
private fun prepareForDeleting() { private fun prepareForDeleting() {
val selections = multiSelector.selectedPositions val selections = multiSelector.selectedPositions
val paths = ArrayList<String>() val paths = ArrayList<String>(selections.size)
selections.forEach { paths.add(dirs[it].path.toLowerCase()) } selections.forEach { paths.add(dirs[it].path.toLowerCase()) }
listener?.prepareForDeleting(paths) listener?.prepareForDeleting(paths)
} }

View file

@ -10,6 +10,7 @@ import com.bignerdranch.android.multiselector.SwappingHolder
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.signature.StringSignature import com.bumptech.glide.signature.StringSignature
import com.simplemobiletools.filepicker.dialogs.ConfirmationDialog
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog
import com.simplemobiletools.gallery.Config import com.simplemobiletools.gallery.Config
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
@ -22,7 +23,7 @@ import kotlinx.android.synthetic.main.photo_video_item.view.*
import kotlinx.android.synthetic.main.photo_video_tmb.view.* import kotlinx.android.synthetic.main.photo_video_tmb.view.*
import java.util.* import java.util.*
class MediaAdapter(val activity: SimpleActivity, val media: MutableList<Medium>, val listener: MediaOperationsListener?, val itemClick: (Medium) -> Unit) : class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>, val listener: MediaOperationsListener?, val itemClick: (Medium) -> Unit) :
RecyclerView.Adapter<MediaAdapter.ViewHolder>() { RecyclerView.Adapter<MediaAdapter.ViewHolder>() {
val multiSelector = MultiSelector() val multiSelector = MultiSelector()
val views = ArrayList<View>() val views = ArrayList<View>()
@ -51,6 +52,10 @@ class MediaAdapter(val activity: SimpleActivity, val media: MutableList<Medium>,
shareMedia() shareMedia()
return true return true
} }
R.id.cab_delete -> {
askConfirmDelete()
true
}
else -> false else -> false
} }
} }
@ -90,6 +95,22 @@ class MediaAdapter(val activity: SimpleActivity, val media: MutableList<Medium>,
} }
} }
private fun askConfirmDelete() {
ConfirmationDialog(activity, listener = object : ConfirmationDialog.OnConfirmedListener {
override fun onConfirmed() {
actMode?.finish()
prepareForDeleting()
}
})
}
private fun prepareForDeleting() {
val selections = multiSelector.selectedPositions
val paths = ArrayList<String>(selections.size)
selections.forEach { paths.add(media[it].path.toLowerCase()) }
listener?.prepareForDeleting(paths)
}
private fun getSelectedMedia(): List<Medium> { private fun getSelectedMedia(): List<Medium> {
val selections = multiSelector.selectedPositions val selections = multiSelector.selectedPositions
val cnt = selections.size val cnt = selections.size
@ -113,6 +134,11 @@ class MediaAdapter(val activity: SimpleActivity, val media: MutableList<Medium>,
notifyDataSetChanged() notifyDataSetChanged()
} }
fun updateMedia(media: MutableList<Medium>) {
this.media = media
notifyDataSetChanged()
}
override fun getItemCount() = media.size override fun getItemCount() = media.size
class ViewHolder(view: View, val itemClick: (Medium) -> (Unit)) : SwappingHolder(view, MultiSelector()) { class ViewHolder(view: View, val itemClick: (Medium) -> (Unit)) : SwappingHolder(view, MultiSelector()) {
@ -165,6 +191,6 @@ class MediaAdapter(val activity: SimpleActivity, val media: MutableList<Medium>,
} }
interface MediaOperationsListener { interface MediaOperationsListener {
fun prepareForDeleting(paths: ArrayList<String>)
} }
} }