From 06c3645bf902a2440339169bf40d98cdb941b28d Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 17 Nov 2016 11:07:00 +0100 Subject: [PATCH] allow renaming dirs --- .../gallery/activities/MainActivity.kt | 44 +++---------------- .../gallery/adapters/DirectoryAdapter.kt | 33 +++++++++++++- .../gallery/dialogs/PickAlbumDialog.kt | 2 +- 3 files changed, 38 insertions(+), 41 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 d98597a80..1b5abb642 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -22,13 +22,12 @@ import com.simplemobiletools.gallery.Utils import com.simplemobiletools.gallery.adapters.DirectoryAdapter import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog -import com.simplemobiletools.gallery.dialogs.RenameDirectoryDialog import com.simplemobiletools.gallery.models.Directory import kotlinx.android.synthetic.main.activity_main.* import java.io.File import java.util.* -class MainActivity : SimpleActivity(), SwipeRefreshLayout.OnRefreshListener, GetDirectoriesAsynctask.GetDirectoriesListener { +class MainActivity : SimpleActivity(), SwipeRefreshLayout.OnRefreshListener, GetDirectoriesAsynctask.GetDirectoriesListener, DirectoryAdapter.DirOperationsListener { companion object { private val STORAGE_PERMISSION = 1 private val PICK_MEDIA = 2 @@ -208,7 +207,7 @@ class MainActivity : SimpleActivity(), SwipeRefreshLayout.OnRefreshListener, Get } } updatedFiles.add(dir) - if (dir.listFiles().size == 0) + if (dir.listFiles().isEmpty()) deleteItem(dir) } } @@ -252,32 +251,8 @@ class MainActivity : SimpleActivity(), SwipeRefreshLayout.OnRefreshListener, Get }*/ } - private fun editDirectory() { - /*val items = directories_grid.checkedItemPositions - val cnt = items.size() - for (i in 0..cnt - 1) { - if (items.valueAt(i)) { - val id = items.keyAt(i) - val path = mDirs[id].path - renameDir(path) - break - } - }*/ - } - - private fun renameDir(path: String) { - val dir = File(path) - if (isAStorageRootFolder(path)) { - toast(R.string.rename_folder_root) - return - } - - RenameDirectoryDialog(this, dir, object : RenameDirectoryDialog.OnRenameDirListener { - override fun onRenameDirSuccess(changedPaths: ArrayList) { - mActionMode!!.finish() - applicationContext.scanPaths(changedPaths) { scanCompleted(path) } - } - }) + override fun refreshItems() { + getDirectories() } private fun displayCopyDialog() { @@ -490,15 +465,6 @@ class MainActivity : SimpleActivity(), SwipeRefreshLayout.OnRefreshListener, Get return selectedPaths*/ } - private fun scanCompleted(path: String) { - val dir = File(path) - if (dir.isDirectory) { - getDirectories() - - runOnUiThread { toast(R.string.rename_folder_ok) } - } - } - override fun onRefresh() { getDirectories() directories_holder.isRefreshing = false @@ -511,7 +477,7 @@ class MainActivity : SimpleActivity(), SwipeRefreshLayout.OnRefreshListener, Get } mDirs = dirs - val adapter = DirectoryAdapter(this, mDirs) { + val adapter = DirectoryAdapter(this, mDirs, this) { itemClicked(it.path) } directories_grid.adapter = adapter 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 11aba4cdf..955fc0fd4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -10,14 +10,19 @@ 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.extensions.isAStorageRootFolder +import com.simplemobiletools.filepicker.extensions.scanPaths +import com.simplemobiletools.filepicker.extensions.toast import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.activities.SimpleActivity +import com.simplemobiletools.gallery.dialogs.RenameDirectoryDialog import com.simplemobiletools.gallery.models.Directory import kotlinx.android.synthetic.main.directory_item.view.* import kotlinx.android.synthetic.main.directory_tmb.view.* +import java.io.File import java.util.* -class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList, val itemClick: (Directory) -> Unit) : +class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList, val listener: DirOperationsListener?, val itemClick: (Directory) -> Unit) : RecyclerView.Adapter() { val multiSelector = MultiSelector() @@ -38,6 +43,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList { + editDirectory() true } else -> false @@ -63,6 +69,27 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList) { + activity.scanPaths(changedPaths) { + activity.runOnUiThread { + actMode?.finish() + listener?.refreshItems() + activity.toast(R.string.rename_folder_ok) + } + } + } + }) + } + override fun onBindViewHolder(holder: ViewHolder, position: Int) { views.add(holder.bindView(activity, multiSelectorMode, multiSelector, dirs[position])) } @@ -120,4 +147,8 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList) { - DirectoryAdapter(activity, dirs) { + DirectoryAdapter(activity, dirs, null) { listener.onSuccess(it.path) dialog.dismiss() }