use a separate DecompressItemsAdapter at decompression

This commit is contained in:
tibbi 2020-05-26 21:39:58 +02:00
parent 72a10cc917
commit 9b85e50f7f
4 changed files with 175 additions and 5 deletions

View File

@ -9,7 +9,7 @@ import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.filemanager.pro.R
import com.simplemobiletools.filemanager.pro.adapters.ItemsAdapter
import com.simplemobiletools.filemanager.pro.adapters.DecompressItemsAdapter
import com.simplemobiletools.filemanager.pro.models.ListItem
import kotlinx.android.synthetic.main.activity_decompress.*
import java.io.BufferedInputStream
@ -27,13 +27,13 @@ class DecompressActivity : SimpleActivity() {
return
}
getRealPathFromURI(uri)?.apply {
title = getFilenameFromPath()
}
val realPath = getRealPathFromURI(uri)
title = realPath?.getFilenameFromPath() ?: uri.toString().getFilenameFromPath()
try {
val listItems = getListItems(uri)
ItemsAdapter(this, listItems, null, decompress_list, false, null) {
DecompressItemsAdapter(this, listItems, decompress_list) {
}.apply {
decompress_list.adapter = this
}

View File

@ -0,0 +1,137 @@
package com.simplemobiletools.filemanager.pro.adapters
import android.content.pm.PackageManager
import android.graphics.drawable.Drawable
import android.util.TypedValue
import android.view.Menu
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
import com.simplemobiletools.commons.extensions.getFileSignature
import com.simplemobiletools.commons.extensions.getTextSize
import com.simplemobiletools.commons.extensions.getTimeFormat
import com.simplemobiletools.commons.helpers.getFilePlaceholderDrawables
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.filemanager.pro.R
import com.simplemobiletools.filemanager.pro.activities.SimpleActivity
import com.simplemobiletools.filemanager.pro.extensions.config
import com.simplemobiletools.filemanager.pro.models.ListItem
import kotlinx.android.synthetic.main.item_list_file_dir.view.*
import java.util.*
class DecompressItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem>, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) :
MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) {
private lateinit var fileDrawable: Drawable
private lateinit var folderDrawable: Drawable
private var fileDrawables = HashMap<String, Drawable>()
private var fontSize = 0f
private var smallerFontSize = 0f
private var dateFormat = ""
private var timeFormat = ""
init {
initDrawables()
fontSize = activity.getTextSize()
smallerFontSize = fontSize * 0.8f
dateFormat = activity.config.dateFormat
timeFormat = activity.getTimeFormat()
}
override fun getActionMenuId() = 0
override fun prepareActionMode(menu: Menu) {}
override fun actionItemPressed(id: Int) {}
override fun getSelectableItemCount() = 0
override fun getIsItemSelectable(position: Int) = false
override fun getItemSelectionKey(position: Int) = 0
override fun getItemKeyPosition(key: Int) = 0
override fun onActionModeCreated() {}
override fun onActionModeDestroyed() {}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.item_decompression_list_file_dir, parent)
override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) {
val fileDirItem = listItems[position]
holder.bindView(fileDirItem, false, false) { itemView, layoutPosition ->
setupView(itemView, fileDirItem)
}
bindViewHolder(holder)
}
override fun getItemCount() = listItems.size
override fun onViewRecycled(holder: ViewHolder) {
super.onViewRecycled(holder)
if (!activity.isDestroyed && !activity.isFinishing) {
val icon = holder.itemView.item_icon
if (icon != null) {
Glide.with(activity).clear(icon)
}
}
}
private fun setupView(view: View, listItem: ListItem) {
view.apply {
val fileName = listItem.name
item_name.text = fileName
item_name.setTextColor(textColor)
item_name.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)
if (listItem.isDirectory) {
item_icon.setImageDrawable(folderDrawable)
} else {
val drawable = fileDrawables.getOrElse(fileName.substringAfterLast(".").toLowerCase(), { fileDrawable })
val options = RequestOptions()
.signature(listItem.mPath.getFileSignature())
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.error(drawable)
.centerCrop()
val itemToLoad = getImagePathToLoad(listItem.path)
if (!activity.isDestroyed) {
Glide.with(activity)
.load(itemToLoad)
.transition(DrawableTransitionOptions.withCrossFade())
.apply(options)
.into(item_icon)
}
}
}
}
private fun getImagePathToLoad(path: String): Any {
return if (path.endsWith(".apk", true)) {
val packageInfo = activity.packageManager.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES)
if (packageInfo != null) {
val appInfo = packageInfo.applicationInfo
appInfo.sourceDir = path
appInfo.publicSourceDir = path
appInfo.loadIcon(activity.packageManager)
} else {
path
}
} else {
path
}
}
private fun initDrawables() {
folderDrawable = resources.getColoredDrawableWithColor(R.drawable.ic_folder_vector, textColor)
folderDrawable.alpha = 180
fileDrawable = resources.getDrawable(R.drawable.ic_file_generic)
fileDrawables = getFilePlaceholderDrawables(activity)
}
}

View File

@ -10,6 +10,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="@dimen/small_margin"
android:scrollbars="none"
app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager" />

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingEnd="@dimen/activity_margin">
<ImageView
android:id="@+id/item_icon"
android:layout_width="@dimen/file_picker_icon_size"
android:layout_height="@dimen/file_picker_icon_size"
android:layout_centerVertical="true"
android:padding="@dimen/medium_margin"
android:src="@drawable/ic_folder_vector" />
<TextView
android:id="@+id/item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/item_icon"
android:layout_alignBottom="@+id/item_icon"
android:layout_toEndOf="@+id/item_icon"
android:ellipsize="end"
android:gravity="center_vertical"
android:maxLines="1"
android:paddingStart="@dimen/tiny_margin"
android:paddingTop="@dimen/small_margin"
android:textSize="@dimen/bigger_text_size"
tools:text="Directory" />
</RelativeLayout>