From f65b5013abfa58ff369685a49ae3b2c4bcd0947c Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 17 Nov 2016 15:02:59 +0100 Subject: [PATCH] add delete handling --- .../gallery/activities/MainActivity.kt | 70 ++++++------------- .../gallery/adapters/DirectoryAdapter.kt | 14 ++++ 2 files changed, 37 insertions(+), 47 deletions(-) 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 46b19c78b..a0a9088e6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -11,9 +11,9 @@ import android.provider.MediaStore import android.support.design.widget.Snackbar import android.support.v4.app.ActivityCompat import android.support.v4.widget.SwipeRefreshLayout -import android.view.ActionMode import android.view.Menu import android.view.MenuItem +import android.view.MotionEvent import android.view.View import com.simplemobiletools.filepicker.extensions.* import com.simplemobiletools.gallery.Constants @@ -27,16 +27,15 @@ import kotlinx.android.synthetic.main.activity_main.* import java.io.File import java.util.* -class MainActivity : SimpleActivity(), SwipeRefreshLayout.OnRefreshListener, GetDirectoriesAsynctask.GetDirectoriesListener, DirectoryAdapter.DirOperationsListener { +class MainActivity : SimpleActivity(), SwipeRefreshLayout.OnRefreshListener, GetDirectoriesAsynctask.GetDirectoriesListener, View.OnTouchListener, DirectoryAdapter.DirOperationsListener { companion object { private val STORAGE_PERMISSION = 1 private val PICK_MEDIA = 2 private val PICK_WALLPAPER = 3 - lateinit var mDirs: MutableList private var mSnackbar: Snackbar? = null + lateinit var mDirs: MutableList lateinit var mToBeDeleted: MutableList - private var mActionMode: ActionMode? = null private var mIsSnackbarShown = false private var mIsPickImageIntent = false @@ -47,7 +46,6 @@ class MainActivity : SimpleActivity(), SwipeRefreshLayout.OnRefreshListener, Get private var mIsSetWallpaperIntent = false private var mIsThirdPartyIntent = false private var mIsGettingDirs = false - private var mSelectedItemsCnt = 0 } override fun onCreate(savedInstanceState: Bundle?) { @@ -125,7 +123,7 @@ class MainActivity : SimpleActivity(), SwipeRefreshLayout.OnRefreshListener, Get super.onRequestPermissionsResult(requestCode, permissions, grantResults) if (requestCode == STORAGE_PERMISSION) { - if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { getDirectories() } else { toast(R.string.no_permissions) @@ -151,27 +149,15 @@ class MainActivity : SimpleActivity(), SwipeRefreshLayout.OnRefreshListener, Get }) } - private fun prepareForDeleting() { + override fun prepareForDeleting(paths: ArrayList) { toast(R.string.deleting) - /*val items = directories_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 = mDirs[id].path - mToBeDeleted.add(path) - deletedCnt++ - } - } + mToBeDeleted = paths + val deletedCnt = mToBeDeleted.size - for (path in mToBeDeleted) { - if (isShowingPermDialog(File(path))) { - return - } - } + if (isShowingPermDialog(File(mToBeDeleted[0]))) + return - notifyDeletion(deletedCnt)*/ + notifyDeletion(deletedCnt) } private fun notifyDeletion(cnt: Int) { @@ -200,10 +186,10 @@ class MainActivity : SimpleActivity(), SwipeRefreshLayout.OnRefreshListener, Get val dir = File(delPath) if (dir.exists()) { val files = dir.listFiles() - for (file in files) { - if (file.isFile && file.isPhotoVideo()) { - updatedFiles.add(file) - deleteItem(file) + files.forEach { + if (it.isFile && it.isPhotoVideo()) { + updatedFiles.add(it) + deleteItem(it) } } updatedFiles.add(dir) @@ -301,25 +287,6 @@ class MainActivity : SimpleActivity(), SwipeRefreshLayout.OnRefreshListener, Get } } - /*override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { - return when (item.itemId) { - R.id.cab_delete -> { - prepareForDeleting() - mode.finish() - true - } - else -> false - } - } - - override fun onTouch(v: View, event: MotionEvent): Boolean { - if (mIsSnackbarShown) { - deleteDirs() - } - - return false - }*/ - override fun onRefresh() { getDirectories() directories_holder.isRefreshing = false @@ -336,9 +303,18 @@ class MainActivity : SimpleActivity(), SwipeRefreshLayout.OnRefreshListener, Get itemClicked(it.path) } directories_grid.adapter = adapter + directories_grid.setOnTouchListener(this) } override fun refreshItems() { getDirectories() } + + override fun onTouch(v: View, event: MotionEvent): Boolean { + if (mIsSnackbarShown && event.action == MotionEvent.ACTION_MOVE) { + deleteDirs() + } + + 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 2d1424c6d..17d8cd8ff 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -69,6 +69,11 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList { + prepareForDeleting() + mode.finish() + true + } else -> false } } @@ -170,6 +175,13 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList() + selections.forEach { paths.add(dirs[it].path) } + listener?.prepareForDeleting(paths) + } + private fun getSelectedPaths(): HashSet { val positions = multiSelector.selectedPositions val paths = HashSet() @@ -238,5 +250,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList) } }