From c93cfcc7293292dbad97e9f0f395c91a9096584d Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 19 Nov 2016 20:27:29 +0100 Subject: [PATCH] add file deletion --- .../gallery/activities/MediaActivity.kt | 50 +++++++------------ .../gallery/adapters/DirectoryAdapter.kt | 2 +- .../gallery/adapters/MediaAdapter.kt | 30 ++++++++++- 3 files changed, 46 insertions(+), 36 deletions(-) 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 3b7ecbc16..f76378e71 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -5,6 +5,7 @@ import android.app.WallpaperManager import android.content.Intent import android.database.Cursor import android.graphics.Bitmap +import android.graphics.Color import android.net.Uri import android.os.Bundle import android.provider.MediaStore @@ -19,10 +20,7 @@ import android.widget.AdapterView import com.bumptech.glide.Glide import com.bumptech.glide.request.animation.GlideAnimation import com.bumptech.glide.request.target.SimpleTarget -import com.simplemobiletools.filepicker.extensions.hasStoragePermission -import com.simplemobiletools.filepicker.extensions.scanFiles -import com.simplemobiletools.filepicker.extensions.scanPath -import com.simplemobiletools.filepicker.extensions.toast +import com.simplemobiletools.filepicker.extensions.* import com.simplemobiletools.gallery.Constants import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.adapters.MediaAdapter @@ -73,7 +71,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On override fun onPause() { super.onPause() - //deleteFiles() + deleteFiles() } private fun tryloadGallery() { @@ -199,7 +197,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On do { 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) if (file.exists()) { val dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED) @@ -231,22 +229,13 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On false } - /*private fun prepareForDeleting() { - if (isShowingPermDialog(File(mPath))) - return - + override fun prepareForDeleting(paths: ArrayList) { toast(R.string.deleting) - val items = media_grid.checkedItemPositions - val cnt = items.size() - var deletedCnt = 0 - for (i in 0..cnt - 1) { - if (items.valueAt(i)) { - val id = items.keyAt(i) - val path = mMedia[id].path - mToBeDeleted.add(path) - deletedCnt++ - } - } + mToBeDeleted = paths + val deletedCnt = mToBeDeleted.size + + if (isShowingPermDialog(File(mToBeDeleted[0]))) + return notifyDeletion(deletedCnt) } @@ -266,7 +255,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On show() } mIsSnackbarShown = true - updateGridView() + updateMediaView() } } @@ -313,19 +302,19 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On mIsSnackbarShown = false mToBeDeleted.clear() mMedia = getMedia() - updateGridView() - }*/ + updateMediaView() + } - private fun updateGridView() { + private fun updateMediaView() { 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 displayCopyDialog() { - /*val items = media_grid.checkedItemPositions val cnt = items.size() val files = (0..cnt - 1) @@ -390,11 +379,6 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On /*override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { when (item.itemId) { - R.id.cab_delete -> { - prepareForDeleting() - mode.finish() - return true - } R.id.cab_copy_move -> { displayCopyDialog() return true @@ -405,7 +389,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On override fun onTouch(v: View, event: MotionEvent): Boolean { if (mIsSnackbarShown) { - //deleteFiles() + deleteFiles() } return false diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt index 41149c769..302528a2e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -186,7 +186,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList() + val paths = ArrayList(selections.size) selections.forEach { paths.add(dirs[it].path.toLowerCase()) } listener?.prepareForDeleting(paths) } 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 a08476fd5..a3cd701d8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt @@ -10,6 +10,7 @@ import com.bignerdranch.android.multiselector.SwappingHolder import com.bumptech.glide.Glide import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.signature.StringSignature +import com.simplemobiletools.filepicker.dialogs.ConfirmationDialog import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog import com.simplemobiletools.gallery.Config 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 java.util.* -class MediaAdapter(val activity: SimpleActivity, val media: MutableList, val listener: MediaOperationsListener?, val itemClick: (Medium) -> Unit) : +class MediaAdapter(val activity: SimpleActivity, var media: MutableList, val listener: MediaOperationsListener?, val itemClick: (Medium) -> Unit) : RecyclerView.Adapter() { val multiSelector = MultiSelector() val views = ArrayList() @@ -51,6 +52,10 @@ class MediaAdapter(val activity: SimpleActivity, val media: MutableList, shareMedia() return true } + R.id.cab_delete -> { + askConfirmDelete() + true + } else -> false } } @@ -90,6 +95,22 @@ class MediaAdapter(val activity: SimpleActivity, val media: MutableList, } } + 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(selections.size) + selections.forEach { paths.add(media[it].path.toLowerCase()) } + listener?.prepareForDeleting(paths) + } + private fun getSelectedMedia(): List { val selections = multiSelector.selectedPositions val cnt = selections.size @@ -113,6 +134,11 @@ class MediaAdapter(val activity: SimpleActivity, val media: MutableList, notifyDataSetChanged() } + fun updateMedia(media: MutableList) { + this.media = media + notifyDataSetChanged() + } + override fun getItemCount() = media.size class ViewHolder(view: View, val itemClick: (Medium) -> (Unit)) : SwappingHolder(view, MultiSelector()) { @@ -165,6 +191,6 @@ class MediaAdapter(val activity: SimpleActivity, val media: MutableList, } interface MediaOperationsListener { - + fun prepareForDeleting(paths: ArrayList) } }