implement the hidden folder adapter for adding/removing items

This commit is contained in:
tibbi 2017-12-29 18:11:23 +01:00
parent aef6e32de6
commit 865db7099f
3 changed files with 114 additions and 1 deletions

View file

@ -7,6 +7,8 @@ import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.adapters.ManageHiddenFoldersAdapter
import com.simplemobiletools.gallery.extensions.addNoMedia
import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.getNoMediaFolders
import kotlinx.android.synthetic.main.activity_manage_folders.*
@ -25,6 +27,10 @@ class HiddenFoldersActivity : SimpleActivity(), RefreshRecyclerViewListener {
beVisibleIf(folders.isEmpty())
setTextColor(config.textColor)
}
val adapter = ManageHiddenFoldersAdapter(this, folders, this, manage_folders_list) {}
adapter.setupDragListener(true)
manage_folders_list.adapter = adapter
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
@ -46,7 +52,11 @@ class HiddenFoldersActivity : SimpleActivity(), RefreshRecyclerViewListener {
private fun addFolder() {
FilePickerDialog(this, pickFile = false, showHidden = config.shouldShowHidden) {
Thread {
addNoMedia(it) {
updateFolders()
}
}.start()
}
}
}

View file

@ -0,0 +1,94 @@
package com.simplemobiletools.gallery.adapters
import android.view.Menu
import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.extensions.isPathOnSD
import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.removeNoMedia
import kotlinx.android.synthetic.main.item_manage_folder.view.*
import java.io.File
import java.util.*
class ManageHiddenFoldersAdapter(activity: BaseSimpleActivity, var folders: ArrayList<String>, val listener: RefreshRecyclerViewListener?,
recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) {
private val config = activity.config
override fun getActionMenuId() = R.menu.cab_hidden_folders
override fun prepareActionMode(menu: Menu) {}
override fun prepareItemSelection(view: View) {}
override fun markItemSelection(select: Boolean, view: View?) {
view?.manage_folder_holder?.isSelected = select
}
override fun actionItemPressed(id: Int) {
when (id) {
R.id.cab_unhide -> tryUnhideFolders()
}
}
override fun getSelectableItemCount() = folders.size
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int) = createViewHolder(R.layout.item_manage_folder, parent)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val folder = folders[position]
val view = holder.bindView(folder) { itemView, layoutPosition ->
setupView(itemView, folder)
}
bindViewHolder(holder, position, view)
}
override fun getItemCount() = folders.size
private fun setupView(view: View, folder: String) {
view.apply {
manage_folder_title.apply {
text = folder
setTextColor(config.textColor)
}
}
}
private fun tryUnhideFolders() {
val removeFolders = ArrayList<String>(selectedPositions.size)
val sdCardPaths = ArrayList<String>()
selectedPositions.forEach {
if (activity.isPathOnSD(folders[it])) {
sdCardPaths.add(folders[it])
}
}
if (sdCardPaths.isNotEmpty()) {
activity.handleSAFDialog(File(sdCardPaths.first())) {
unhideFolders(removeFolders)
}
} else {
unhideFolders(removeFolders)
}
}
private fun unhideFolders(removeFolders: ArrayList<String>) {
selectedPositions.sortedDescending().forEach {
val folder = folders[it]
removeFolders.add(folder)
activity.removeNoMedia(folder)
}
folders.removeAll(removeFolders)
removeSelectedItems()
if (folders.isEmpty()) {
listener?.refreshItems()
}
}
}

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/cab_unhide"
android:icon="@drawable/ic_unhide"
android:title="@string/unhide"
app:showAsAction="ifRoom"/>
</menu>