migrate fragments to viewbinding

This commit is contained in:
fatih ergin 2023-08-09 18:23:19 +03:00
parent 1ddf7ffc9b
commit 4caf77bc9b
4 changed files with 292 additions and 223 deletions

View File

@ -17,6 +17,7 @@ import com.simplemobiletools.filemanager.pro.R
import com.simplemobiletools.filemanager.pro.activities.MainActivity import com.simplemobiletools.filemanager.pro.activities.MainActivity
import com.simplemobiletools.filemanager.pro.activities.SimpleActivity import com.simplemobiletools.filemanager.pro.activities.SimpleActivity
import com.simplemobiletools.filemanager.pro.adapters.ItemsAdapter import com.simplemobiletools.filemanager.pro.adapters.ItemsAdapter
import com.simplemobiletools.filemanager.pro.databinding.ItemsFragmentBinding
import com.simplemobiletools.filemanager.pro.dialogs.CreateNewItemDialog import com.simplemobiletools.filemanager.pro.dialogs.CreateNewItemDialog
import com.simplemobiletools.filemanager.pro.extensions.config import com.simplemobiletools.filemanager.pro.extensions.config
import com.simplemobiletools.filemanager.pro.extensions.isPathOnRoot import com.simplemobiletools.filemanager.pro.extensions.isPathOnRoot
@ -24,10 +25,10 @@ import com.simplemobiletools.filemanager.pro.helpers.MAX_COLUMN_COUNT
import com.simplemobiletools.filemanager.pro.helpers.RootHelpers import com.simplemobiletools.filemanager.pro.helpers.RootHelpers
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 kotlinx.android.synthetic.main.items_fragment.view.*
import java.io.File import java.io.File
class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), ItemOperationsListener, class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment<MyViewPagerFragment.ItemsInnerBinding>(context, attributeSet),
ItemOperationsListener,
Breadcrumbs.BreadcrumbsListener { Breadcrumbs.BreadcrumbsListener {
private var showHidden = false private var showHidden = false
private var lastSearchedText = "" private var lastSearchedText = ""
@ -36,17 +37,26 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
private var storedItems = ArrayList<ListItem>() private var storedItems = ArrayList<ListItem>()
private var itemsIgnoringSearch = ArrayList<ListItem>() private var itemsIgnoringSearch = ArrayList<ListItem>()
private lateinit var binding: ItemsFragmentBinding
override fun onFinishInflate() {
super.onFinishInflate()
binding = ItemsFragmentBinding.bind(this)
innerBinding = ItemsInnerBinding(binding)
}
override fun setupFragment(activity: SimpleActivity) { override fun setupFragment(activity: SimpleActivity) {
if (this.activity == null) { if (this.activity == null) {
this.activity = activity this.activity = activity
breadcrumbs.listener = this@ItemsFragment binding.apply {
items_swipe_refresh.setOnRefreshListener { refreshFragment() } breadcrumbs.listener = this@ItemsFragment
items_fab.setOnClickListener { itemsSwipeRefresh.setOnRefreshListener { refreshFragment() }
if (isCreateDocumentIntent) { itemsFab.setOnClickListener {
(activity as MainActivity).createDocumentConfirmed(currentPath) if (isCreateDocumentIntent) {
} else { (activity as MainActivity).createDocumentConfirmed(currentPath)
createNewItem() } else {
createNewItem()
}
} }
} }
} }
@ -60,22 +70,24 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
initDrawables() initDrawables()
} }
val properPrimaryColor = context!!.getProperPrimaryColor() binding.apply {
items_fastscroller.updateColors(properPrimaryColor) val properPrimaryColor = context!!.getProperPrimaryColor()
progress_bar.setIndicatorColor(properPrimaryColor) itemsFastscroller.updateColors(properPrimaryColor)
progress_bar.trackColor = properPrimaryColor.adjustAlpha(LOWER_ALPHA) progressBar.setIndicatorColor(properPrimaryColor)
progressBar.trackColor = properPrimaryColor.adjustAlpha(LOWER_ALPHA)
if (currentPath != "") { if (currentPath != "") {
breadcrumbs.updateColor(textColor) breadcrumbs.updateColor(textColor)
}
itemsSwipeRefresh.isEnabled = lastSearchedText.isEmpty() && activity?.config?.enablePullToRefresh != false
} }
items_swipe_refresh.isEnabled = lastSearchedText.isEmpty() && activity?.config?.enablePullToRefresh != false
} }
override fun setupFontSize() { override fun setupFontSize() {
getRecyclerAdapter()?.updateFontSizes() getRecyclerAdapter()?.updateFontSizes()
if (currentPath != "") { if (currentPath != "") {
breadcrumbs.updateFontSize(context!!.getTextSize(), false) binding.breadcrumbs.updateFontSize(context!!.getTextSize(), false)
} }
} }
@ -133,18 +145,18 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
private fun addItems(items: ArrayList<ListItem>, forceRefresh: Boolean = false) { private fun addItems(items: ArrayList<ListItem>, forceRefresh: Boolean = false) {
activity?.runOnUiThread { activity?.runOnUiThread {
items_swipe_refresh?.isRefreshing = false binding.itemsSwipeRefresh?.isRefreshing = false
breadcrumbs.setBreadcrumb(currentPath) binding.breadcrumbs.setBreadcrumb(currentPath)
if (!forceRefresh && items.hashCode() == storedItems.hashCode()) { if (!forceRefresh && items.hashCode() == storedItems.hashCode()) {
return@runOnUiThread return@runOnUiThread
} }
storedItems = items storedItems = items
if (items_list.adapter == null) { if (binding.itemsList.adapter == null) {
breadcrumbs.updateFontSize(context!!.getTextSize(), true) binding.breadcrumbs.updateFontSize(context!!.getTextSize(), true)
} }
ItemsAdapter(activity as SimpleActivity, storedItems, this, items_list, isPickMultipleIntent, items_swipe_refresh) { ItemsAdapter(activity as SimpleActivity, storedItems, this, binding.itemsList, isPickMultipleIntent, binding.itemsSwipeRefresh) {
if ((it as? ListItem)?.isSectionTitle == true) { if ((it as? ListItem)?.isSectionTitle == true) {
openDirectory(it.mPath) openDirectory(it.mPath)
searchClosed() searchClosed()
@ -153,11 +165,11 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
} }
}.apply { }.apply {
setupZoomListener(zoomListener) setupZoomListener(zoomListener)
items_list.adapter = this binding.itemsList.adapter = this
} }
if (context.areSystemAnimationsEnabled) { if (context.areSystemAnimationsEnabled) {
items_list.scheduleLayoutAnimation() binding.itemsList.scheduleLayoutAnimation()
} }
getRecyclerLayoutManager().onRestoreInstanceState(scrollStates[currentPath]) getRecyclerLayoutManager().onRestoreInstanceState(scrollStates[currentPath])
@ -166,7 +178,7 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
private fun getScrollState() = getRecyclerLayoutManager().onSaveInstanceState() private fun getScrollState() = getRecyclerLayoutManager().onSaveInstanceState()
private fun getRecyclerLayoutManager() = (items_list.layoutManager as MyGridLayoutManager) private fun getRecyclerLayoutManager() = (binding.itemsList.layoutManager as MyGridLayoutManager)
@SuppressLint("NewApi") @SuppressLint("NewApi")
private fun getItems(path: String, callback: (originalPath: String, items: ArrayList<ListItem>) -> Unit) { private fun getItems(path: String, callback: (originalPath: String, items: ArrayList<ListItem>) -> Unit) {
@ -295,60 +307,64 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
return return
} }
items_swipe_refresh.isEnabled = text.isEmpty() && activity?.config?.enablePullToRefresh != false binding.apply {
when { itemsSwipeRefresh.isEnabled = text.isEmpty() && activity?.config?.enablePullToRefresh != false
text.isEmpty() -> { when {
items_fastscroller.beVisible() text.isEmpty() -> {
getRecyclerAdapter()?.updateItems(itemsIgnoringSearch) itemsFastscroller.beVisible()
items_placeholder.beGone() getRecyclerAdapter()?.updateItems(itemsIgnoringSearch)
items_placeholder_2.beGone() itemsPlaceholder.beGone()
hideProgressBar() itemsPlaceholder2.beGone()
} hideProgressBar()
text.length == 1 -> { }
items_fastscroller.beGone()
items_placeholder.beVisible()
items_placeholder_2.beVisible()
hideProgressBar()
}
else -> {
showProgressBar()
ensureBackgroundThread {
val files = searchFiles(text, currentPath)
files.sortBy { it.getParentPath() }
if (lastSearchedText != text) { text.length == 1 -> {
return@ensureBackgroundThread itemsFastscroller.beGone()
} itemsPlaceholder.beVisible()
itemsPlaceholder2.beVisible()
hideProgressBar()
}
val listItems = ArrayList<ListItem>() else -> {
showProgressBar()
ensureBackgroundThread {
val files = searchFiles(text, currentPath)
files.sortBy { it.getParentPath() }
var previousParent = "" if (lastSearchedText != text) {
files.forEach { return@ensureBackgroundThread
val parent = it.mPath.getParentPath()
if (!it.isDirectory && parent != previousParent && context != null) {
val sectionTitle = ListItem(parent, context!!.humanizePath(parent), false, 0, 0, 0, true, false)
listItems.add(sectionTitle)
previousParent = parent
} }
if (it.isDirectory) { val listItems = ArrayList<ListItem>()
val sectionTitle = ListItem(it.path, context!!.humanizePath(it.path), true, 0, 0, 0, true, false)
listItems.add(sectionTitle) var previousParent = ""
previousParent = parent files.forEach {
val parent = it.mPath.getParentPath()
if (!it.isDirectory && parent != previousParent && context != null) {
val sectionTitle = ListItem(parent, context!!.humanizePath(parent), false, 0, 0, 0, true, false)
listItems.add(sectionTitle)
previousParent = parent
}
if (it.isDirectory) {
val sectionTitle = ListItem(it.path, context!!.humanizePath(it.path), true, 0, 0, 0, true, false)
listItems.add(sectionTitle)
previousParent = parent
}
if (!it.isDirectory) {
listItems.add(it)
}
} }
if (!it.isDirectory) { activity?.runOnUiThread {
listItems.add(it) getRecyclerAdapter()?.updateItems(listItems, text)
itemsFastscroller.beVisibleIf(listItems.isNotEmpty())
itemsPlaceholder.beVisibleIf(listItems.isEmpty())
itemsPlaceholder2.beGone()
hideProgressBar()
} }
} }
activity?.runOnUiThread {
getRecyclerAdapter()?.updateItems(listItems, text)
items_fastscroller.beVisibleIf(listItems.isNotEmpty())
items_placeholder.beVisibleIf(listItems.isEmpty())
items_placeholder_2.beGone()
hideProgressBar()
}
} }
} }
} }
@ -390,12 +406,14 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
} }
private fun searchClosed() { private fun searchClosed() {
lastSearchedText = "" binding.apply {
items_swipe_refresh.isEnabled = activity?.config?.enablePullToRefresh != false lastSearchedText = ""
items_fastscroller.beVisible() itemsSwipeRefresh.isEnabled = activity?.config?.enablePullToRefresh != false
items_placeholder.beGone() itemsFastscroller.beVisible()
items_placeholder_2.beGone() itemsPlaceholder.beGone()
hideProgressBar() itemsPlaceholder2.beGone()
hideProgressBar()
}
} }
private fun createNewItem() { private fun createNewItem() {
@ -408,7 +426,7 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
} }
} }
private fun getRecyclerAdapter() = items_list.adapter as? ItemsAdapter private fun getRecyclerAdapter() = binding.itemsList.adapter as? ItemsAdapter
private fun setupLayoutManager() { private fun setupLayoutManager() {
if (context!!.config.getFolderViewType(currentPath) == VIEW_TYPE_GRID) { if (context!!.config.getFolderViewType(currentPath) == VIEW_TYPE_GRID) {
@ -419,13 +437,13 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
setupListLayoutManager() setupListLayoutManager()
} }
items_list.adapter = null binding.itemsList.adapter = null
initZoomListener() initZoomListener()
addItems(storedItems, true) addItems(storedItems, true)
} }
private fun setupGridLayoutManager() { private fun setupGridLayoutManager() {
val layoutManager = items_list.layoutManager as MyGridLayoutManager val layoutManager = binding.itemsList.layoutManager as MyGridLayoutManager
layoutManager.spanCount = context?.config?.fileColumnCnt ?: 3 layoutManager.spanCount = context?.config?.fileColumnCnt ?: 3
layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() { layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
@ -440,14 +458,14 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
} }
private fun setupListLayoutManager() { private fun setupListLayoutManager() {
val layoutManager = items_list.layoutManager as MyGridLayoutManager val layoutManager = binding.itemsList.layoutManager as MyGridLayoutManager
layoutManager.spanCount = 1 layoutManager.spanCount = 1
zoomListener = null zoomListener = null
} }
private fun initZoomListener() { private fun initZoomListener() {
if (context?.config?.getFolderViewType(currentPath) == VIEW_TYPE_GRID) { if (context?.config?.getFolderViewType(currentPath) == VIEW_TYPE_GRID) {
val layoutManager = items_list.layoutManager as MyGridLayoutManager val layoutManager = binding.itemsList.layoutManager as MyGridLayoutManager
zoomListener = object : MyRecyclerView.MyZoomListener { zoomListener = object : MyRecyclerView.MyZoomListener {
override fun zoomIn() { override fun zoomIn() {
if (layoutManager.spanCount > 1) { if (layoutManager.spanCount > 1) {
@ -483,21 +501,23 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
} }
override fun columnCountChanged() { override fun columnCountChanged() {
(items_list.layoutManager as MyGridLayoutManager).spanCount = context!!.config.fileColumnCnt (binding.itemsList.layoutManager as MyGridLayoutManager).spanCount = context!!.config.fileColumnCnt
(activity as? MainActivity)?.refreshMenuItems() (activity as? MainActivity)?.refreshMenuItems()
getRecyclerAdapter()?.apply { getRecyclerAdapter()?.apply {
notifyItemRangeChanged(0, listItems.size) notifyItemRangeChanged(0, listItems.size)
} }
} }
private fun showProgressBar() { fun showProgressBar() {
progress_bar.show() binding.progressBar.show()
} }
private fun hideProgressBar() { private fun hideProgressBar() {
progress_bar.hide() binding.progressBar.hide()
} }
fun getBreadcrumbs() = binding.breadcrumbs
override fun toggleFilenameVisibility() { override fun toggleFilenameVisibility() {
getRecyclerAdapter()?.updateDisplayFilenamesInGrid() getRecyclerAdapter()?.updateDisplayFilenamesInGrid()
} }
@ -509,7 +529,7 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
openPath(it) openPath(it)
} }
} else { } else {
val item = breadcrumbs.getItem(id) val item = binding.breadcrumbs.getItem(id)
openPath(item.path) openPath(item.path)
} }
} }

View File

@ -6,15 +6,19 @@ import android.widget.RelativeLayout
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.VIEW_TYPE_LIST import com.simplemobiletools.commons.helpers.VIEW_TYPE_LIST
import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.views.MyFloatingActionButton
import com.simplemobiletools.filemanager.pro.R import com.simplemobiletools.filemanager.pro.R
import com.simplemobiletools.filemanager.pro.activities.MainActivity import com.simplemobiletools.filemanager.pro.activities.MainActivity
import com.simplemobiletools.filemanager.pro.activities.SimpleActivity import com.simplemobiletools.filemanager.pro.activities.SimpleActivity
import com.simplemobiletools.filemanager.pro.databinding.ItemsFragmentBinding
import com.simplemobiletools.filemanager.pro.databinding.RecentsFragmentBinding
import com.simplemobiletools.filemanager.pro.databinding.StorageFragmentBinding
import com.simplemobiletools.filemanager.pro.extensions.isPathOnRoot import com.simplemobiletools.filemanager.pro.extensions.isPathOnRoot
import com.simplemobiletools.filemanager.pro.extensions.tryOpenPathIntent import com.simplemobiletools.filemanager.pro.extensions.tryOpenPathIntent
import com.simplemobiletools.filemanager.pro.helpers.RootHelpers import com.simplemobiletools.filemanager.pro.helpers.RootHelpers
import kotlinx.android.synthetic.main.items_fragment.view.*
abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) : RelativeLayout(context, attributeSet) { abstract class MyViewPagerFragment<BINDING : MyViewPagerFragment.InnerBinding>(context: Context, attributeSet: AttributeSet) :
RelativeLayout(context, attributeSet) {
protected var activity: SimpleActivity? = null protected var activity: SimpleActivity? = null
protected var currentViewType = VIEW_TYPE_LIST protected var currentViewType = VIEW_TYPE_LIST
@ -24,6 +28,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
var isPickMultipleIntent = false var isPickMultipleIntent = false
var wantedMimeType = "" var wantedMimeType = ""
protected var isCreateDocumentIntent = false protected var isCreateDocumentIntent = false
protected lateinit var innerBinding: BINDING
protected fun clickedPath(path: String) { protected fun clickedPath(path: String) {
if (isGetContentIntent || isCreateDocumentIntent) { if (isGetContentIntent || isCreateDocumentIntent) {
@ -48,7 +53,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
this.isCreateDocumentIntent = isCreateDocumentIntent this.isCreateDocumentIntent = isCreateDocumentIntent
val fabIcon = context.resources.getColoredDrawableWithColor(iconId, context.getProperPrimaryColor().getContrastColor()) val fabIcon = context.resources.getColoredDrawableWithColor(iconId, context.getProperPrimaryColor().getContrastColor())
items_fab?.setImageDrawable(fabIcon) innerBinding.itemsFab?.setImageDrawable(fabIcon)
} }
fun handleFileDeleting(files: ArrayList<FileDirItem>, hasFolder: Boolean) { fun handleFileDeleting(files: ArrayList<FileDirItem>, hasFolder: Boolean) {
@ -90,4 +95,20 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
abstract fun refreshFragment() abstract fun refreshFragment()
abstract fun searchQueryChanged(text: String) abstract fun searchQueryChanged(text: String)
interface InnerBinding {
val itemsFab: MyFloatingActionButton?
}
class ItemsInnerBinding(val binding: ItemsFragmentBinding) : InnerBinding {
override val itemsFab: MyFloatingActionButton = binding.itemsFab
}
class RecentsInnerBinding(val binding: RecentsFragmentBinding) : InnerBinding {
override val itemsFab: MyFloatingActionButton? = null
}
class StorageInnerBinding(val binding: StorageFragmentBinding) : InnerBinding {
override val itemsFab: MyFloatingActionButton? = null
}
} }

View File

@ -17,25 +17,31 @@ import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.filemanager.pro.activities.MainActivity import com.simplemobiletools.filemanager.pro.activities.MainActivity
import com.simplemobiletools.filemanager.pro.activities.SimpleActivity import com.simplemobiletools.filemanager.pro.activities.SimpleActivity
import com.simplemobiletools.filemanager.pro.adapters.ItemsAdapter import com.simplemobiletools.filemanager.pro.adapters.ItemsAdapter
import com.simplemobiletools.filemanager.pro.databinding.RecentsFragmentBinding
import com.simplemobiletools.filemanager.pro.extensions.config import com.simplemobiletools.filemanager.pro.extensions.config
import com.simplemobiletools.filemanager.pro.helpers.MAX_COLUMN_COUNT import com.simplemobiletools.filemanager.pro.helpers.MAX_COLUMN_COUNT
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 kotlinx.android.synthetic.main.recents_fragment.view.recents_list
import kotlinx.android.synthetic.main.recents_fragment.view.recents_placeholder
import kotlinx.android.synthetic.main.recents_fragment.view.recents_swipe_refresh
import java.io.File import java.io.File
class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), ItemOperationsListener { class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment<MyViewPagerFragment.RecentsInnerBinding>(context, attributeSet),
ItemOperationsListener {
private val RECENTS_LIMIT = 50 private val RECENTS_LIMIT = 50
private var filesIgnoringSearch = ArrayList<ListItem>() private var filesIgnoringSearch = ArrayList<ListItem>()
private var lastSearchedText = "" private var lastSearchedText = ""
private var zoomListener: MyRecyclerView.MyZoomListener? = null private var zoomListener: MyRecyclerView.MyZoomListener? = null
private lateinit var binding: RecentsFragmentBinding
override fun onFinishInflate() {
super.onFinishInflate()
binding = RecentsFragmentBinding.bind(this)
innerBinding = RecentsInnerBinding(binding)
}
override fun setupFragment(activity: SimpleActivity) { override fun setupFragment(activity: SimpleActivity) {
if (this.activity == null) { if (this.activity == null) {
this.activity = activity this.activity = activity
recents_swipe_refresh.setOnRefreshListener { refreshFragment() } binding.recentsSwipeRefresh.setOnRefreshListener { refreshFragment() }
} }
refreshFragment() refreshFragment()
@ -44,9 +50,11 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
override fun refreshFragment() { override fun refreshFragment() {
ensureBackgroundThread { ensureBackgroundThread {
getRecents { recents -> getRecents { recents ->
recents_swipe_refresh?.isRefreshing = false binding.apply {
recents_list.beVisibleIf(recents.isNotEmpty()) recentsSwipeRefresh?.isRefreshing = false
recents_placeholder.beVisibleIf(recents.isEmpty()) recentsList.beVisibleIf(recents.isNotEmpty())
recentsPlaceholder.beVisibleIf(recents.isEmpty())
}
filesIgnoringSearch = recents filesIgnoringSearch = recents
addItems(recents, false) addItems(recents, false)
@ -58,24 +66,24 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
} }
private fun addItems(recents: ArrayList<ListItem>, forceRefresh: Boolean) { private fun addItems(recents: ArrayList<ListItem>, forceRefresh: Boolean) {
if (!forceRefresh && recents.hashCode() == (recents_list.adapter as? ItemsAdapter)?.listItems.hashCode()) { if (!forceRefresh && recents.hashCode() == (binding.recentsList.adapter as? ItemsAdapter)?.listItems.hashCode()) {
return return
} }
ItemsAdapter(activity as SimpleActivity, recents, this, recents_list, isPickMultipleIntent, recents_swipe_refresh, false) { ItemsAdapter(activity as SimpleActivity, recents, this, binding.recentsList, isPickMultipleIntent, binding.recentsSwipeRefresh, false) {
clickedPath((it as FileDirItem).path) clickedPath((it as FileDirItem).path)
}.apply { }.apply {
setupZoomListener(zoomListener) setupZoomListener(zoomListener)
recents_list.adapter = this binding.recentsList.adapter = this
} }
if (context.areSystemAnimationsEnabled) { if (context.areSystemAnimationsEnabled) {
recents_list.scheduleLayoutAnimation() binding.recentsList.scheduleLayoutAnimation()
} }
} }
override fun onResume(textColor: Int) { override fun onResume(textColor: Int) {
recents_placeholder.setTextColor(textColor) binding.recentsPlaceholder.setTextColor(textColor)
getRecyclerAdapter()?.apply { getRecyclerAdapter()?.apply {
updatePrimaryColor() updatePrimaryColor()
@ -83,7 +91,7 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
initDrawables() initDrawables()
} }
recents_swipe_refresh.isEnabled = lastSearchedText.isEmpty() && activity?.config?.enablePullToRefresh != false binding.recentsSwipeRefresh.isEnabled = lastSearchedText.isEmpty() && activity?.config?.enablePullToRefresh != false
} }
private fun setupLayoutManager() { private fun setupLayoutManager() {
@ -95,26 +103,26 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
setupListLayoutManager() setupListLayoutManager()
} }
val oldItems = (recents_list.adapter as? ItemsAdapter)?.listItems?.toMutableList() as ArrayList<ListItem> val oldItems = (binding.recentsList.adapter as? ItemsAdapter)?.listItems?.toMutableList() as ArrayList<ListItem>
recents_list.adapter = null binding.recentsList.adapter = null
initZoomListener() initZoomListener()
addItems(oldItems, true) addItems(oldItems, true)
} }
private fun setupGridLayoutManager() { private fun setupGridLayoutManager() {
val layoutManager = recents_list.layoutManager as MyGridLayoutManager val layoutManager = binding.recentsList.layoutManager as MyGridLayoutManager
layoutManager.spanCount = context?.config?.fileColumnCnt ?: 3 layoutManager.spanCount = context?.config?.fileColumnCnt ?: 3
} }
private fun setupListLayoutManager() { private fun setupListLayoutManager() {
val layoutManager = recents_list.layoutManager as MyGridLayoutManager val layoutManager = binding.recentsList.layoutManager as MyGridLayoutManager
layoutManager.spanCount = 1 layoutManager.spanCount = 1
zoomListener = null zoomListener = null
} }
private fun initZoomListener() { private fun initZoomListener() {
if (context?.config?.getFolderViewType("") == VIEW_TYPE_GRID) { if (context?.config?.getFolderViewType("") == VIEW_TYPE_GRID) {
val layoutManager = recents_list.layoutManager as MyGridLayoutManager val layoutManager = binding.recentsList.layoutManager as MyGridLayoutManager
zoomListener = object : MyRecyclerView.MyZoomListener { zoomListener = object : MyRecyclerView.MyZoomListener {
override fun zoomIn() { override fun zoomIn() {
if (layoutManager.spanCount > 1) { if (layoutManager.spanCount > 1) {
@ -187,7 +195,7 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
} }
} }
private fun getRecyclerAdapter() = recents_list.adapter as? ItemsAdapter private fun getRecyclerAdapter() = binding.recentsList.adapter as? ItemsAdapter
override fun toggleFilenameVisibility() { override fun toggleFilenameVisibility() {
getRecyclerAdapter()?.updateDisplayFilenamesInGrid() getRecyclerAdapter()?.updateDisplayFilenamesInGrid()
@ -208,7 +216,7 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
} }
override fun columnCountChanged() { override fun columnCountChanged() {
(recents_list.layoutManager as MyGridLayoutManager).spanCount = context!!.config.fileColumnCnt (binding.recentsList.layoutManager as MyGridLayoutManager).spanCount = context!!.config.fileColumnCnt
(activity as? MainActivity)?.refreshMenuItems() (activity as? MainActivity)?.refreshMenuItems()
getRecyclerAdapter()?.apply { getRecyclerAdapter()?.apply {
notifyItemRangeChanged(0, listItems.size) notifyItemRangeChanged(0, listItems.size)
@ -234,9 +242,11 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
override fun searchQueryChanged(text: String) { override fun searchQueryChanged(text: String) {
lastSearchedText = text lastSearchedText = text
val filtered = filesIgnoringSearch.filter { it.mName.contains(text, true) }.toMutableList() as ArrayList<ListItem> val filtered = filesIgnoringSearch.filter { it.mName.contains(text, true) }.toMutableList() as ArrayList<ListItem>
(recents_list.adapter as? ItemsAdapter)?.updateItems(filtered, text) binding.apply {
recents_placeholder.beVisibleIf(filtered.isEmpty()) (recentsList.adapter as? ItemsAdapter)?.updateItems(filtered, text)
recents_swipe_refresh.isEnabled = lastSearchedText.isEmpty() && activity?.config?.enablePullToRefresh != false recentsPlaceholder.beVisibleIf(filtered.isEmpty())
recentsSwipeRefresh.isEnabled = lastSearchedText.isEmpty() && activity?.config?.enablePullToRefresh != false
}
} }
override fun finishActMode() { override fun finishActMode() {

View File

@ -21,28 +21,36 @@ import com.simplemobiletools.filemanager.pro.R
import com.simplemobiletools.filemanager.pro.activities.MimeTypesActivity import com.simplemobiletools.filemanager.pro.activities.MimeTypesActivity
import com.simplemobiletools.filemanager.pro.activities.SimpleActivity import com.simplemobiletools.filemanager.pro.activities.SimpleActivity
import com.simplemobiletools.filemanager.pro.adapters.ItemsAdapter import com.simplemobiletools.filemanager.pro.adapters.ItemsAdapter
import com.simplemobiletools.filemanager.pro.databinding.StorageFragmentBinding
import com.simplemobiletools.filemanager.pro.extensions.config import com.simplemobiletools.filemanager.pro.extensions.config
import com.simplemobiletools.filemanager.pro.extensions.formatSizeThousand import com.simplemobiletools.filemanager.pro.extensions.formatSizeThousand
import com.simplemobiletools.filemanager.pro.helpers.* 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 kotlinx.android.synthetic.main.storage_fragment.view.*
import java.util.* import java.util.*
class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), ItemOperationsListener { class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment<MyViewPagerFragment.StorageInnerBinding>(context, attributeSet),
ItemOperationsListener {
private val SIZE_DIVIDER = 100000 private val SIZE_DIVIDER = 100000
private var allDeviceListItems = ArrayList<ListItem>() private var allDeviceListItems = ArrayList<ListItem>()
private var lastSearchedText = "" private var lastSearchedText = ""
private lateinit var binding: StorageFragmentBinding
override fun onFinishInflate() {
super.onFinishInflate()
binding = StorageFragmentBinding.bind(this)
innerBinding = StorageInnerBinding(binding)
}
override fun setupFragment(activity: SimpleActivity) { override fun setupFragment(activity: SimpleActivity) {
if (this.activity == null) { if (this.activity == null) {
this.activity = activity this.activity = activity
} }
total_space.text = String.format(context.getString(R.string.total_storage), "") binding.totalSpace.text = String.format(context.getString(R.string.total_storage), "")
getSizes() getSizes()
free_space_holder.setOnClickListener { binding.freeSpaceHolder.setOnClickListener {
try { try {
val storageSettingsIntent = Intent(Settings.ACTION_INTERNAL_STORAGE_SETTINGS) val storageSettingsIntent = Intent(Settings.ACTION_INTERNAL_STORAGE_SETTINGS)
activity.startActivity(storageSettingsIntent) activity.startActivity(storageSettingsIntent)
@ -51,12 +59,14 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
} }
} }
images_holder.setOnClickListener { launchMimetypeActivity(IMAGES) } binding.apply {
videos_holder.setOnClickListener { launchMimetypeActivity(VIDEOS) } imagesHolder.setOnClickListener { launchMimetypeActivity(IMAGES) }
audio_holder.setOnClickListener { launchMimetypeActivity(AUDIO) } videosHolder.setOnClickListener { launchMimetypeActivity(VIDEOS) }
documents_holder.setOnClickListener { launchMimetypeActivity(DOCUMENTS) } audioHolder.setOnClickListener { launchMimetypeActivity(AUDIO) }
archives_holder.setOnClickListener { launchMimetypeActivity(ARCHIVES) } documentsHolder.setOnClickListener { launchMimetypeActivity(DOCUMENTS) }
others_holder.setOnClickListener { launchMimetypeActivity(OTHERS) } archivesHolder.setOnClickListener { launchMimetypeActivity(ARCHIVES) }
othersHolder.setOnClickListener { launchMimetypeActivity(OTHERS) }
}
Handler().postDelayed({ Handler().postDelayed({
refreshFragment() refreshFragment()
@ -65,39 +75,42 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
override fun onResume(textColor: Int) { override fun onResume(textColor: Int) {
getSizes() getSizes()
context.updateTextColors(storage_fragment) context.updateTextColors(binding.root)
val properPrimaryColor = context.getProperPrimaryColor() binding.apply {
main_storage_usage_progressbar.setIndicatorColor(properPrimaryColor) val properPrimaryColor = context.getProperPrimaryColor()
main_storage_usage_progressbar.trackColor = properPrimaryColor.adjustAlpha(LOWER_ALPHA) mainStorageUsageProgressbar.setIndicatorColor(properPrimaryColor)
mainStorageUsageProgressbar.trackColor = properPrimaryColor.adjustAlpha(LOWER_ALPHA)
val redColor = context.resources.getColor(R.color.md_red_700) val redColor = context.resources.getColor(R.color.md_red_700)
images_progressbar.setIndicatorColor(redColor) imagesProgressbar.setIndicatorColor(redColor)
images_progressbar.trackColor = redColor.adjustAlpha(LOWER_ALPHA) imagesProgressbar.trackColor = redColor.adjustAlpha(LOWER_ALPHA)
val greenColor = context.resources.getColor(R.color.md_green_700) val greenColor = context.resources.getColor(R.color.md_green_700)
videos_progressbar.setIndicatorColor(greenColor) videosProgressbar.setIndicatorColor(greenColor)
videos_progressbar.trackColor = greenColor.adjustAlpha(LOWER_ALPHA) videosProgressbar.trackColor = greenColor.adjustAlpha(LOWER_ALPHA)
val lightBlueColor = context.resources.getColor(R.color.md_light_blue_700) val lightBlueColor = context.resources.getColor(R.color.md_light_blue_700)
audio_progressbar.setIndicatorColor(lightBlueColor) audioProgressbar.setIndicatorColor(lightBlueColor)
audio_progressbar.trackColor = lightBlueColor.adjustAlpha(LOWER_ALPHA) audioProgressbar.trackColor = lightBlueColor.adjustAlpha(LOWER_ALPHA)
val yellowColor = context.resources.getColor(R.color.md_yellow_700) val yellowColor = context.resources.getColor(R.color.md_yellow_700)
documents_progressbar.setIndicatorColor(yellowColor) documentsProgressbar.setIndicatorColor(yellowColor)
documents_progressbar.trackColor = yellowColor.adjustAlpha(LOWER_ALPHA) documentsProgressbar.trackColor = yellowColor.adjustAlpha(LOWER_ALPHA)
val tealColor = context.resources.getColor(R.color.md_teal_700) val tealColor = context.resources.getColor(R.color.md_teal_700)
archives_progressbar.setIndicatorColor(tealColor) archivesProgressbar.setIndicatorColor(tealColor)
archives_progressbar.trackColor = tealColor.adjustAlpha(LOWER_ALPHA) archivesProgressbar.trackColor = tealColor.adjustAlpha(LOWER_ALPHA)
val pinkColor = context.resources.getColor(R.color.md_pink_700) val pinkColor = context.resources.getColor(R.color.md_pink_700)
others_progressbar.setIndicatorColor(pinkColor) othersProgressbar.setIndicatorColor(pinkColor)
others_progressbar.trackColor = pinkColor.adjustAlpha(LOWER_ALPHA) othersProgressbar.trackColor = pinkColor.adjustAlpha(LOWER_ALPHA)
searchHolder.setBackgroundColor(context.getProperBackgroundColor())
progressBar.setIndicatorColor(properPrimaryColor)
progressBar.trackColor = properPrimaryColor.adjustAlpha(LOWER_ALPHA)
}
search_holder.setBackgroundColor(context.getProperBackgroundColor())
progress_bar.setIndicatorColor(properPrimaryColor)
progress_bar.trackColor = properPrimaryColor.adjustAlpha(LOWER_ALPHA)
} }
private fun launchMimetypeActivity(mimetype: String) { private fun launchMimetypeActivity(mimetype: String) {
@ -116,31 +129,33 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
getMainStorageStats(context) getMainStorageStats(context)
val filesSize = getSizesByMimeType() val filesSize = getSizesByMimeType()
val imagesSize = filesSize[IMAGES]!! val fileSizeImages = filesSize[IMAGES]!!
val videosSize = filesSize[VIDEOS]!! val fileSizeVideos = filesSize[VIDEOS]!!
val audioSize = filesSize[AUDIO]!! val fileSizeAudios = filesSize[AUDIO]!!
val documentsSize = filesSize[DOCUMENTS]!! val fileSizeDocuments = filesSize[DOCUMENTS]!!
val archivesSize = filesSize[ARCHIVES]!! val fileSizeArchives = filesSize[ARCHIVES]!!
val othersSize = filesSize[OTHERS]!! val fileSizeOthers = filesSize[OTHERS]!!
post { post {
images_size.text = imagesSize.formatSize() binding.apply {
images_progressbar.progress = (imagesSize / SIZE_DIVIDER).toInt() imagesSize.text = fileSizeImages.formatSize()
imagesProgressbar.progress = (fileSizeImages / SIZE_DIVIDER).toInt()
videos_size.text = videosSize.formatSize() videosSize.text = fileSizeVideos.formatSize()
videos_progressbar.progress = (videosSize / SIZE_DIVIDER).toInt() videosProgressbar.progress = (fileSizeVideos / SIZE_DIVIDER).toInt()
audio_size.text = audioSize.formatSize() audioSize.text = fileSizeAudios.formatSize()
audio_progressbar.progress = (audioSize / SIZE_DIVIDER).toInt() audioProgressbar.progress = (fileSizeAudios / SIZE_DIVIDER).toInt()
documents_size.text = documentsSize.formatSize() documentsSize.text = fileSizeDocuments.formatSize()
documents_progressbar.progress = (documentsSize / SIZE_DIVIDER).toInt() documentsProgressbar.progress = (fileSizeDocuments / SIZE_DIVIDER).toInt()
archives_size.text = archivesSize.formatSize() archivesSize.text = fileSizeArchives.formatSize()
archives_progressbar.progress = (archivesSize / SIZE_DIVIDER).toInt() archivesProgressbar.progress = (fileSizeArchives / SIZE_DIVIDER).toInt()
others_size.text = othersSize.formatSize() othersSize.text = fileSizeOthers.formatSize()
others_progressbar.progress = (othersSize / SIZE_DIVIDER).toInt() othersProgressbar.progress = (fileSizeOthers / SIZE_DIVIDER).toInt()
}
} }
} }
} }
@ -217,23 +232,25 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
// internal storage // internal storage
val storageStatsManager = context.getSystemService(AppCompatActivity.STORAGE_STATS_SERVICE) as StorageStatsManager val storageStatsManager = context.getSystemService(AppCompatActivity.STORAGE_STATS_SERVICE) as StorageStatsManager
val uuid = StorageManager.UUID_DEFAULT val uuid = StorageManager.UUID_DEFAULT
val totalSpace = storageStatsManager.getTotalBytes(uuid) val totalStorageSpace = storageStatsManager.getTotalBytes(uuid)
val freeSpace = storageStatsManager.getFreeBytes(uuid) val freeStorageSpace = storageStatsManager.getFreeBytes(uuid)
post { post {
arrayOf( binding.apply {
main_storage_usage_progressbar, images_progressbar, videos_progressbar, audio_progressbar, documents_progressbar, arrayOf(
archives_progressbar, others_progressbar mainStorageUsageProgressbar, imagesProgressbar, videosProgressbar, audioProgressbar, documentsProgressbar,
).forEach { archivesProgressbar, othersProgressbar
it.max = (totalSpace / SIZE_DIVIDER).toInt() ).forEach {
it.max = (totalStorageSpace / SIZE_DIVIDER).toInt()
}
mainStorageUsageProgressbar.progress = ((totalStorageSpace - freeStorageSpace) / SIZE_DIVIDER).toInt()
mainStorageUsageProgressbar.beVisible()
freeSpaceValue.text = freeStorageSpace.formatSizeThousand()
totalSpace.text = String.format(context.getString(R.string.total_storage), totalStorageSpace.formatSizeThousand())
freeSpaceLabel.beVisible()
} }
main_storage_usage_progressbar.progress = ((totalSpace - freeSpace) / SIZE_DIVIDER).toInt()
main_storage_usage_progressbar.beVisible()
free_space_value.text = freeSpace.formatSizeThousand()
total_space.text = String.format(context.getString(R.string.total_storage), totalSpace.formatSizeThousand())
free_space_label.beVisible()
} }
} else { } else {
// sd card // sd card
@ -245,41 +262,42 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
override fun searchQueryChanged(text: String) { override fun searchQueryChanged(text: String) {
lastSearchedText = text lastSearchedText = text
binding.apply {
if (text.isNotEmpty()) { if (text.isNotEmpty()) {
if (search_holder.alpha < 1f) { if (searchHolder.alpha < 1f) {
search_holder.fadeIn() searchHolder.fadeIn()
}
} else {
search_holder.animate().alpha(0f).setDuration(SHORT_ANIMATION_DURATION).withEndAction {
search_holder.beGone()
(search_results_list.adapter as? ItemsAdapter)?.updateItems(allDeviceListItems, text)
}.start()
}
if (text.length == 1) {
search_results_list.beGone()
search_placeholder.beVisible()
search_placeholder_2.beVisible()
hideProgressBar()
} else if (text.isEmpty()) {
search_results_list.beGone()
hideProgressBar()
} else {
showProgressBar()
ensureBackgroundThread {
val start = System.currentTimeMillis()
val filtered = allDeviceListItems.filter { it.mName.contains(text, true) }.toMutableList() as ArrayList<ListItem>
if (lastSearchedText != text) {
return@ensureBackgroundThread
} }
} else {
searchHolder.animate().alpha(0f).setDuration(SHORT_ANIMATION_DURATION).withEndAction {
searchHolder.beGone()
(searchResultsList.adapter as? ItemsAdapter)?.updateItems(allDeviceListItems, text)
}.start()
}
(context as? Activity)?.runOnUiThread { if (text.length == 1) {
(search_results_list.adapter as? ItemsAdapter)?.updateItems(filtered, text) searchResultsList.beGone()
search_results_list.beVisible() searchPlaceholder.beVisible()
search_placeholder.beVisibleIf(filtered.isEmpty()) searchPlaceholder2.beVisible()
search_placeholder_2.beGone() hideProgressBar()
hideProgressBar() } else if (text.isEmpty()) {
searchResultsList.beGone()
hideProgressBar()
} else {
showProgressBar()
ensureBackgroundThread {
val start = System.currentTimeMillis()
val filtered = allDeviceListItems.filter { it.mName.contains(text, true) }.toMutableList() as ArrayList<ListItem>
if (lastSearchedText != text) {
return@ensureBackgroundThread
}
(context as? Activity)?.runOnUiThread {
(searchResultsList.adapter as? ItemsAdapter)?.updateItems(filtered, text)
searchResultsList.beVisible()
searchPlaceholder.beVisibleIf(filtered.isEmpty())
searchPlaceholder2.beGone()
hideProgressBar()
}
} }
} }
} }
@ -294,25 +312,25 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
setupListLayoutManager() setupListLayoutManager()
} }
search_results_list.adapter = null binding.searchResultsList.adapter = null
addItems() addItems()
} }
private fun setupGridLayoutManager() { private fun setupGridLayoutManager() {
val layoutManager = search_results_list.layoutManager as MyGridLayoutManager val layoutManager = binding.searchResultsList.layoutManager as MyGridLayoutManager
layoutManager.spanCount = context?.config?.fileColumnCnt ?: 3 layoutManager.spanCount = context?.config?.fileColumnCnt ?: 3
} }
private fun setupListLayoutManager() { private fun setupListLayoutManager() {
val layoutManager = search_results_list.layoutManager as MyGridLayoutManager val layoutManager = binding.searchResultsList.layoutManager as MyGridLayoutManager
layoutManager.spanCount = 1 layoutManager.spanCount = 1
} }
private fun addItems() { private fun addItems() {
ItemsAdapter(context as SimpleActivity, ArrayList(), this, search_results_list, false, null, false) { ItemsAdapter(context as SimpleActivity, ArrayList(), this, binding.searchResultsList, false, null, false) {
clickedPath((it as FileDirItem).path) clickedPath((it as FileDirItem).path)
}.apply { }.apply {
search_results_list.adapter = this binding.searchResultsList.adapter = this
} }
} }
@ -367,14 +385,14 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
} }
private fun showProgressBar() { private fun showProgressBar() {
progress_bar.show() binding.progressBar.show()
} }
private fun hideProgressBar() { private fun hideProgressBar() {
progress_bar.hide() binding.progressBar.hide()
} }
private fun getRecyclerAdapter() = search_results_list.adapter as? ItemsAdapter private fun getRecyclerAdapter() = binding.searchResultsList.adapter as? ItemsAdapter
override fun refreshFragment() { override fun refreshFragment() {
ensureBackgroundThread { ensureBackgroundThread {
@ -403,7 +421,7 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
} }
override fun columnCountChanged() { override fun columnCountChanged() {
(search_results_list.layoutManager as MyGridLayoutManager).spanCount = context!!.config.fileColumnCnt (binding.searchResultsList.layoutManager as MyGridLayoutManager).spanCount = context!!.config.fileColumnCnt
getRecyclerAdapter()?.apply { getRecyclerAdapter()?.apply {
notifyItemRangeChanged(0, listItems.size) notifyItemRangeChanged(0, listItems.size)
} }