allow showing sections in the file lists too

This commit is contained in:
tibbi
2019-03-27 11:39:24 +01:00
parent 0da2d0ee33
commit 052821d27f
3 changed files with 87 additions and 52 deletions

View File

@ -29,7 +29,8 @@ import com.simplemobiletools.filemanager.pro.helpers.*
import com.simplemobiletools.filemanager.pro.interfaces.ItemOperationsListener import com.simplemobiletools.filemanager.pro.interfaces.ItemOperationsListener
import com.simplemobiletools.filemanager.pro.models.ListItem import com.simplemobiletools.filemanager.pro.models.ListItem
import com.stericson.RootTools.RootTools import com.stericson.RootTools.RootTools
import kotlinx.android.synthetic.main.list_item.view.* import kotlinx.android.synthetic.main.item_list_file_dir.view.*
import kotlinx.android.synthetic.main.item_list_section.view.*
import java.io.Closeable import java.io.Closeable
import java.io.File import java.io.File
import java.io.FileInputStream import java.io.FileInputStream
@ -38,13 +39,15 @@ import java.util.zip.ZipEntry
import java.util.zip.ZipFile import java.util.zip.ZipFile
import java.util.zip.ZipOutputStream import java.util.zip.ZipOutputStream
class ItemsAdapter(activity: SimpleActivity, var fileDirItems: MutableList<ListItem>, val listener: ItemOperationsListener?, recyclerView: MyRecyclerView, class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem>, val listener: ItemOperationsListener?, recyclerView: MyRecyclerView,
val isPickMultipleIntent: Boolean, fastScroller: FastScroller, itemClick: (Any) -> Unit) : val isPickMultipleIntent: Boolean, fastScroller: FastScroller, itemClick: (Any) -> Unit) :
MyRecyclerViewAdapter(activity, recyclerView, fastScroller, itemClick) { MyRecyclerViewAdapter(activity, recyclerView, fastScroller, itemClick) {
private val TYPE_FILE_DIR = 1
private val TYPE_SECTION = 2
private lateinit var folderDrawable: Drawable private lateinit var folderDrawable: Drawable
private lateinit var fileDrawable: Drawable private lateinit var fileDrawable: Drawable
private var currentItemsHash = fileDirItems.hashCode() private var currentItemsHash = listItems.hashCode()
private var textToHighlight = "" private var textToHighlight = ""
var adjustedPrimaryColor = activity.getAdjustedPrimaryColor() var adjustedPrimaryColor = activity.getAdjustedPrimaryColor()
@ -93,27 +96,38 @@ class ItemsAdapter(activity: SimpleActivity, var fileDirItems: MutableList<ListI
} }
} }
override fun getSelectableItemCount() = fileDirItems.size override fun getSelectableItemCount() = listItems.size
override fun getIsItemSelectable(position: Int) = !fileDirItems[position].isSectionTitle override fun getIsItemSelectable(position: Int) = !listItems[position].isSectionTitle
override fun getItemSelectionKey(position: Int) = fileDirItems.getOrNull(position)?.path?.hashCode() override fun getItemSelectionKey(position: Int) = listItems.getOrNull(position)?.path?.hashCode()
override fun getItemKeyPosition(key: Int) = fileDirItems.indexOfFirst { it.path.hashCode() == key } override fun getItemKeyPosition(key: Int) = listItems.indexOfFirst { it.path.hashCode() == key }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.list_item, parent) override fun getItemViewType(position: Int): Int {
return if (listItems[position].isSectionTitle) {
TYPE_SECTION
} else {
TYPE_FILE_DIR
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layout = if (viewType == TYPE_SECTION) R.layout.item_list_section else R.layout.item_list_file_dir
return createViewHolder(layout, parent)
}
override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) { override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) {
val fileDirItem = fileDirItems[position] val fileDirItem = listItems[position]
holder.bindView(fileDirItem, true, true) { itemView, layoutPosition -> holder.bindView(fileDirItem, true, true) { itemView, layoutPosition ->
setupView(itemView, fileDirItem) setupView(itemView, fileDirItem)
} }
bindViewHolder(holder) bindViewHolder(holder)
} }
override fun getItemCount() = fileDirItems.size override fun getItemCount() = listItems.size
private fun getItemWithKey(key: Int): FileDirItem? = fileDirItems.firstOrNull { it.path.hashCode() == key } private fun getItemWithKey(key: Int): FileDirItem? = listItems.firstOrNull { it.path.hashCode() == key }
fun initDrawables() { fun initDrawables() {
folderDrawable = activity.resources.getColoredDrawableWithColor(R.drawable.ic_folder, textColor) folderDrawable = activity.resources.getColoredDrawableWithColor(R.drawable.ic_folder, textColor)
@ -512,10 +526,10 @@ class ItemsAdapter(activity: SimpleActivity, var fileDirItems: MutableList<ListI
selectedKeys.forEach { selectedKeys.forEach {
activity.config.removeFavorite(getItemWithKey(it)?.path ?: "") activity.config.removeFavorite(getItemWithKey(it)?.path ?: "")
val key = it val key = it
val position = fileDirItems.indexOfFirst { it.path.hashCode() == key } val position = listItems.indexOfFirst { it.path.hashCode() == key }
if (position != -1) { if (position != -1) {
positions.add(position) positions.add(position)
files.add(fileDirItems[position]) files.add(listItems[position])
} }
} }
@ -523,20 +537,20 @@ class ItemsAdapter(activity: SimpleActivity, var fileDirItems: MutableList<ListI
removeSelectedItems(positions) removeSelectedItems(positions)
listener?.deleteFiles(files) listener?.deleteFiles(files)
positions.forEach { positions.forEach {
fileDirItems.removeAt(it) listItems.removeAt(it)
} }
} }
} }
private fun getFirstSelectedItemPath() = getSelectedFileDirItems().first().path private fun getFirstSelectedItemPath() = getSelectedFileDirItems().first().path
private fun getSelectedFileDirItems() = fileDirItems.filter { selectedKeys.contains(it.path.hashCode()) } as ArrayList<FileDirItem> private fun getSelectedFileDirItems() = listItems.filter { selectedKeys.contains(it.path.hashCode()) } as ArrayList<FileDirItem>
fun updateItems(newItems: ArrayList<ListItem>, highlightText: String = "") { fun updateItems(newItems: ArrayList<ListItem>, highlightText: String = "") {
if (newItems.hashCode() != currentItemsHash) { if (newItems.hashCode() != currentItemsHash) {
currentItemsHash = newItems.hashCode() currentItemsHash = newItems.hashCode()
textToHighlight = highlightText textToHighlight = highlightText
fileDirItems = newItems.clone() as ArrayList<ListItem> listItems = newItems.clone() as ArrayList<ListItem>
notifyDataSetChanged() notifyDataSetChanged()
finishActMode() finishActMode()
} else if (textToHighlight != highlightText) { } else if (textToHighlight != highlightText) {
@ -548,32 +562,39 @@ class ItemsAdapter(activity: SimpleActivity, var fileDirItems: MutableList<ListI
override fun onViewRecycled(holder: ViewHolder) { override fun onViewRecycled(holder: ViewHolder) {
super.onViewRecycled(holder) super.onViewRecycled(holder)
if (!activity.isDestroyed) { if (!activity.isDestroyed && !activity.isFinishing) {
Glide.with(activity).clear(holder.itemView.item_icon!!) val icon = holder.itemView.item_icon
if (icon != null) {
Glide.with(activity).clear(icon)
}
} }
} }
private fun setupView(view: View, fileDirItem: ListItem) { private fun setupView(view: View, listItem: ListItem) {
val isSelected = selectedKeys.contains(fileDirItem.path.hashCode()) val isSelected = selectedKeys.contains(listItem.path.hashCode())
view.apply { view.apply {
if (listItem.isSectionTitle) {
item_section.text = listItem.mName
item_section.setTextColor(adjustedPrimaryColor)
} else {
item_frame.isSelected = isSelected item_frame.isSelected = isSelected
val fileName = fileDirItem.name val fileName = listItem.name
item_name.text = if (textToHighlight.isEmpty()) fileName else fileName.highlightTextPart(textToHighlight, adjustedPrimaryColor) item_name.text = if (textToHighlight.isEmpty()) fileName else fileName.highlightTextPart(textToHighlight, adjustedPrimaryColor)
item_name.setTextColor(textColor) item_name.setTextColor(textColor)
item_details.setTextColor(textColor) item_details.setTextColor(textColor)
if (fileDirItem.isDirectory) { if (listItem.isDirectory) {
item_icon.setImageDrawable(folderDrawable) item_icon.setImageDrawable(folderDrawable)
item_details.text = getChildrenCnt(fileDirItem) item_details.text = getChildrenCnt(listItem)
} else { } else {
item_details.text = fileDirItem.size.formatSize() item_details.text = listItem.size.formatSize()
val path = fileDirItem.path val path = listItem.path
val options = RequestOptions() val options = RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.RESOURCE) .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.error(fileDrawable) .error(fileDrawable)
.centerCrop() .centerCrop()
val itemToLoad = if (fileDirItem.name.endsWith(".apk", true)) { val itemToLoad = if (listItem.name.endsWith(".apk", true)) {
val packageInfo = context.packageManager.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES) val packageInfo = context.packageManager.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES)
if (packageInfo != null) { if (packageInfo != null) {
val appInfo = packageInfo.applicationInfo val appInfo = packageInfo.applicationInfo
@ -593,6 +614,7 @@ class ItemsAdapter(activity: SimpleActivity, var fileDirItems: MutableList<ListI
} }
} }
} }
}
private fun getChildrenCnt(item: FileDirItem): String { private fun getChildrenCnt(item: FileDirItem): String {
val children = item.children val children = item.children

View File

@ -14,7 +14,7 @@
android:id="@+id/item_holder" android:id="@+id/item_holder"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingRight="@dimen/activity_margin"> android:paddingEnd="@dimen/activity_margin">
<ImageView <ImageView
android:id="@+id/item_icon" android:id="@+id/item_icon"
@ -28,10 +28,10 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignTop="@+id/item_icon" android:layout_alignTop="@+id/item_icon"
android:layout_toRightOf="@+id/item_icon" android:layout_toEndOf="@+id/item_icon"
android:ellipsize="end" android:ellipsize="end"
android:maxLines="1" android:maxLines="1"
android:paddingLeft="@dimen/small_margin" android:paddingStart="@dimen/small_margin"
android:paddingTop="@dimen/small_margin" android:paddingTop="@dimen/small_margin"
tools:text="Directory"/> tools:text="Directory"/>
@ -40,8 +40,8 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@+id/item_name" android:layout_below="@+id/item_name"
android:layout_toRightOf="@+id/item_icon" android:layout_toEndOf="@+id/item_icon"
android:paddingLeft="@dimen/small_margin" android:paddingStart="@dimen/small_margin"
android:textSize="@dimen/smaller_text_size" android:textSize="@dimen/smaller_text_size"
tools:text="1 KB"/> tools:text="1 KB"/>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_section"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/medium_margin"
android:paddingTop="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:paddingBottom="@dimen/activity_margin"
android:textSize="@dimen/bigger_text_size"
tools:text="@string/in_this_folder"/>