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

View File

@ -6,15 +6,19 @@ import android.widget.RelativeLayout
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.VIEW_TYPE_LIST
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.views.MyFloatingActionButton
import com.simplemobiletools.filemanager.pro.R
import com.simplemobiletools.filemanager.pro.activities.MainActivity
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.tryOpenPathIntent
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 currentViewType = VIEW_TYPE_LIST
@ -24,6 +28,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
var isPickMultipleIntent = false
var wantedMimeType = ""
protected var isCreateDocumentIntent = false
protected lateinit var innerBinding: BINDING
protected fun clickedPath(path: String) {
if (isGetContentIntent || isCreateDocumentIntent) {
@ -48,7 +53,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
this.isCreateDocumentIntent = isCreateDocumentIntent
val fabIcon = context.resources.getColoredDrawableWithColor(iconId, context.getProperPrimaryColor().getContrastColor())
items_fab?.setImageDrawable(fabIcon)
innerBinding.itemsFab?.setImageDrawable(fabIcon)
}
fun handleFileDeleting(files: ArrayList<FileDirItem>, hasFolder: Boolean) {
@ -90,4 +95,20 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
abstract fun refreshFragment()
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.SimpleActivity
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.helpers.MAX_COLUMN_COUNT
import com.simplemobiletools.filemanager.pro.interfaces.ItemOperationsListener
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
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 var filesIgnoringSearch = ArrayList<ListItem>()
private var lastSearchedText = ""
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) {
if (this.activity == null) {
this.activity = activity
recents_swipe_refresh.setOnRefreshListener { refreshFragment() }
binding.recentsSwipeRefresh.setOnRefreshListener { refreshFragment() }
}
refreshFragment()
@ -44,9 +50,11 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
override fun refreshFragment() {
ensureBackgroundThread {
getRecents { recents ->
recents_swipe_refresh?.isRefreshing = false
recents_list.beVisibleIf(recents.isNotEmpty())
recents_placeholder.beVisibleIf(recents.isEmpty())
binding.apply {
recentsSwipeRefresh?.isRefreshing = false
recentsList.beVisibleIf(recents.isNotEmpty())
recentsPlaceholder.beVisibleIf(recents.isEmpty())
}
filesIgnoringSearch = recents
addItems(recents, false)
@ -58,24 +66,24 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
}
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
}
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)
}.apply {
setupZoomListener(zoomListener)
recents_list.adapter = this
binding.recentsList.adapter = this
}
if (context.areSystemAnimationsEnabled) {
recents_list.scheduleLayoutAnimation()
binding.recentsList.scheduleLayoutAnimation()
}
}
override fun onResume(textColor: Int) {
recents_placeholder.setTextColor(textColor)
binding.recentsPlaceholder.setTextColor(textColor)
getRecyclerAdapter()?.apply {
updatePrimaryColor()
@ -83,7 +91,7 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
initDrawables()
}
recents_swipe_refresh.isEnabled = lastSearchedText.isEmpty() && activity?.config?.enablePullToRefresh != false
binding.recentsSwipeRefresh.isEnabled = lastSearchedText.isEmpty() && activity?.config?.enablePullToRefresh != false
}
private fun setupLayoutManager() {
@ -95,26 +103,26 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
setupListLayoutManager()
}
val oldItems = (recents_list.adapter as? ItemsAdapter)?.listItems?.toMutableList() as ArrayList<ListItem>
recents_list.adapter = null
val oldItems = (binding.recentsList.adapter as? ItemsAdapter)?.listItems?.toMutableList() as ArrayList<ListItem>
binding.recentsList.adapter = null
initZoomListener()
addItems(oldItems, true)
}
private fun setupGridLayoutManager() {
val layoutManager = recents_list.layoutManager as MyGridLayoutManager
val layoutManager = binding.recentsList.layoutManager as MyGridLayoutManager
layoutManager.spanCount = context?.config?.fileColumnCnt ?: 3
}
private fun setupListLayoutManager() {
val layoutManager = recents_list.layoutManager as MyGridLayoutManager
val layoutManager = binding.recentsList.layoutManager as MyGridLayoutManager
layoutManager.spanCount = 1
zoomListener = null
}
private fun initZoomListener() {
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 {
override fun zoomIn() {
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() {
getRecyclerAdapter()?.updateDisplayFilenamesInGrid()
@ -208,7 +216,7 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
}
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()
getRecyclerAdapter()?.apply {
notifyItemRangeChanged(0, listItems.size)
@ -234,9 +242,11 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
override fun searchQueryChanged(text: String) {
lastSearchedText = text
val filtered = filesIgnoringSearch.filter { it.mName.contains(text, true) }.toMutableList() as ArrayList<ListItem>
(recents_list.adapter as? ItemsAdapter)?.updateItems(filtered, text)
recents_placeholder.beVisibleIf(filtered.isEmpty())
recents_swipe_refresh.isEnabled = lastSearchedText.isEmpty() && activity?.config?.enablePullToRefresh != false
binding.apply {
(recentsList.adapter as? ItemsAdapter)?.updateItems(filtered, text)
recentsPlaceholder.beVisibleIf(filtered.isEmpty())
recentsSwipeRefresh.isEnabled = lastSearchedText.isEmpty() && activity?.config?.enablePullToRefresh != false
}
}
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.SimpleActivity
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.formatSizeThousand
import com.simplemobiletools.filemanager.pro.helpers.*
import com.simplemobiletools.filemanager.pro.interfaces.ItemOperationsListener
import com.simplemobiletools.filemanager.pro.models.ListItem
import kotlinx.android.synthetic.main.storage_fragment.view.*
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 var allDeviceListItems = ArrayList<ListItem>()
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) {
if (this.activity == null) {
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()
free_space_holder.setOnClickListener {
binding.freeSpaceHolder.setOnClickListener {
try {
val storageSettingsIntent = Intent(Settings.ACTION_INTERNAL_STORAGE_SETTINGS)
activity.startActivity(storageSettingsIntent)
@ -51,12 +59,14 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
}
}
images_holder.setOnClickListener { launchMimetypeActivity(IMAGES) }
videos_holder.setOnClickListener { launchMimetypeActivity(VIDEOS) }
audio_holder.setOnClickListener { launchMimetypeActivity(AUDIO) }
documents_holder.setOnClickListener { launchMimetypeActivity(DOCUMENTS) }
archives_holder.setOnClickListener { launchMimetypeActivity(ARCHIVES) }
others_holder.setOnClickListener { launchMimetypeActivity(OTHERS) }
binding.apply {
imagesHolder.setOnClickListener { launchMimetypeActivity(IMAGES) }
videosHolder.setOnClickListener { launchMimetypeActivity(VIDEOS) }
audioHolder.setOnClickListener { launchMimetypeActivity(AUDIO) }
documentsHolder.setOnClickListener { launchMimetypeActivity(DOCUMENTS) }
archivesHolder.setOnClickListener { launchMimetypeActivity(ARCHIVES) }
othersHolder.setOnClickListener { launchMimetypeActivity(OTHERS) }
}
Handler().postDelayed({
refreshFragment()
@ -65,39 +75,42 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
override fun onResume(textColor: Int) {
getSizes()
context.updateTextColors(storage_fragment)
context.updateTextColors(binding.root)
val properPrimaryColor = context.getProperPrimaryColor()
main_storage_usage_progressbar.setIndicatorColor(properPrimaryColor)
main_storage_usage_progressbar.trackColor = properPrimaryColor.adjustAlpha(LOWER_ALPHA)
binding.apply {
val properPrimaryColor = context.getProperPrimaryColor()
mainStorageUsageProgressbar.setIndicatorColor(properPrimaryColor)
mainStorageUsageProgressbar.trackColor = properPrimaryColor.adjustAlpha(LOWER_ALPHA)
val redColor = context.resources.getColor(R.color.md_red_700)
images_progressbar.setIndicatorColor(redColor)
images_progressbar.trackColor = redColor.adjustAlpha(LOWER_ALPHA)
val redColor = context.resources.getColor(R.color.md_red_700)
imagesProgressbar.setIndicatorColor(redColor)
imagesProgressbar.trackColor = redColor.adjustAlpha(LOWER_ALPHA)
val greenColor = context.resources.getColor(R.color.md_green_700)
videos_progressbar.setIndicatorColor(greenColor)
videos_progressbar.trackColor = greenColor.adjustAlpha(LOWER_ALPHA)
val greenColor = context.resources.getColor(R.color.md_green_700)
videosProgressbar.setIndicatorColor(greenColor)
videosProgressbar.trackColor = greenColor.adjustAlpha(LOWER_ALPHA)
val lightBlueColor = context.resources.getColor(R.color.md_light_blue_700)
audio_progressbar.setIndicatorColor(lightBlueColor)
audio_progressbar.trackColor = lightBlueColor.adjustAlpha(LOWER_ALPHA)
val lightBlueColor = context.resources.getColor(R.color.md_light_blue_700)
audioProgressbar.setIndicatorColor(lightBlueColor)
audioProgressbar.trackColor = lightBlueColor.adjustAlpha(LOWER_ALPHA)
val yellowColor = context.resources.getColor(R.color.md_yellow_700)
documents_progressbar.setIndicatorColor(yellowColor)
documents_progressbar.trackColor = yellowColor.adjustAlpha(LOWER_ALPHA)
val yellowColor = context.resources.getColor(R.color.md_yellow_700)
documentsProgressbar.setIndicatorColor(yellowColor)
documentsProgressbar.trackColor = yellowColor.adjustAlpha(LOWER_ALPHA)
val tealColor = context.resources.getColor(R.color.md_teal_700)
archives_progressbar.setIndicatorColor(tealColor)
archives_progressbar.trackColor = tealColor.adjustAlpha(LOWER_ALPHA)
val tealColor = context.resources.getColor(R.color.md_teal_700)
archivesProgressbar.setIndicatorColor(tealColor)
archivesProgressbar.trackColor = tealColor.adjustAlpha(LOWER_ALPHA)
val pinkColor = context.resources.getColor(R.color.md_pink_700)
others_progressbar.setIndicatorColor(pinkColor)
others_progressbar.trackColor = pinkColor.adjustAlpha(LOWER_ALPHA)
val pinkColor = context.resources.getColor(R.color.md_pink_700)
othersProgressbar.setIndicatorColor(pinkColor)
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) {
@ -116,31 +129,33 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
getMainStorageStats(context)
val filesSize = getSizesByMimeType()
val imagesSize = filesSize[IMAGES]!!
val videosSize = filesSize[VIDEOS]!!
val audioSize = filesSize[AUDIO]!!
val documentsSize = filesSize[DOCUMENTS]!!
val archivesSize = filesSize[ARCHIVES]!!
val othersSize = filesSize[OTHERS]!!
val fileSizeImages = filesSize[IMAGES]!!
val fileSizeVideos = filesSize[VIDEOS]!!
val fileSizeAudios = filesSize[AUDIO]!!
val fileSizeDocuments = filesSize[DOCUMENTS]!!
val fileSizeArchives = filesSize[ARCHIVES]!!
val fileSizeOthers = filesSize[OTHERS]!!
post {
images_size.text = imagesSize.formatSize()
images_progressbar.progress = (imagesSize / SIZE_DIVIDER).toInt()
binding.apply {
imagesSize.text = fileSizeImages.formatSize()
imagesProgressbar.progress = (fileSizeImages / SIZE_DIVIDER).toInt()
videos_size.text = videosSize.formatSize()
videos_progressbar.progress = (videosSize / SIZE_DIVIDER).toInt()
videosSize.text = fileSizeVideos.formatSize()
videosProgressbar.progress = (fileSizeVideos / SIZE_DIVIDER).toInt()
audio_size.text = audioSize.formatSize()
audio_progressbar.progress = (audioSize / SIZE_DIVIDER).toInt()
audioSize.text = fileSizeAudios.formatSize()
audioProgressbar.progress = (fileSizeAudios / SIZE_DIVIDER).toInt()
documents_size.text = documentsSize.formatSize()
documents_progressbar.progress = (documentsSize / SIZE_DIVIDER).toInt()
documentsSize.text = fileSizeDocuments.formatSize()
documentsProgressbar.progress = (fileSizeDocuments / SIZE_DIVIDER).toInt()
archives_size.text = archivesSize.formatSize()
archives_progressbar.progress = (archivesSize / SIZE_DIVIDER).toInt()
archivesSize.text = fileSizeArchives.formatSize()
archivesProgressbar.progress = (fileSizeArchives / SIZE_DIVIDER).toInt()
others_size.text = othersSize.formatSize()
others_progressbar.progress = (othersSize / SIZE_DIVIDER).toInt()
othersSize.text = fileSizeOthers.formatSize()
othersProgressbar.progress = (fileSizeOthers / SIZE_DIVIDER).toInt()
}
}
}
}
@ -217,23 +232,25 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
// internal storage
val storageStatsManager = context.getSystemService(AppCompatActivity.STORAGE_STATS_SERVICE) as StorageStatsManager
val uuid = StorageManager.UUID_DEFAULT
val totalSpace = storageStatsManager.getTotalBytes(uuid)
val freeSpace = storageStatsManager.getFreeBytes(uuid)
val totalStorageSpace = storageStatsManager.getTotalBytes(uuid)
val freeStorageSpace = storageStatsManager.getFreeBytes(uuid)
post {
arrayOf(
main_storage_usage_progressbar, images_progressbar, videos_progressbar, audio_progressbar, documents_progressbar,
archives_progressbar, others_progressbar
).forEach {
it.max = (totalSpace / SIZE_DIVIDER).toInt()
binding.apply {
arrayOf(
mainStorageUsageProgressbar, imagesProgressbar, videosProgressbar, audioProgressbar, documentsProgressbar,
archivesProgressbar, othersProgressbar
).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 {
// sd card
@ -245,41 +262,42 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
override fun searchQueryChanged(text: String) {
lastSearchedText = text
if (text.isNotEmpty()) {
if (search_holder.alpha < 1f) {
search_holder.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
binding.apply {
if (text.isNotEmpty()) {
if (searchHolder.alpha < 1f) {
searchHolder.fadeIn()
}
} else {
searchHolder.animate().alpha(0f).setDuration(SHORT_ANIMATION_DURATION).withEndAction {
searchHolder.beGone()
(searchResultsList.adapter as? ItemsAdapter)?.updateItems(allDeviceListItems, text)
}.start()
}
(context as? Activity)?.runOnUiThread {
(search_results_list.adapter as? ItemsAdapter)?.updateItems(filtered, text)
search_results_list.beVisible()
search_placeholder.beVisibleIf(filtered.isEmpty())
search_placeholder_2.beGone()
hideProgressBar()
if (text.length == 1) {
searchResultsList.beGone()
searchPlaceholder.beVisible()
searchPlaceholder2.beVisible()
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()
}
search_results_list.adapter = null
binding.searchResultsList.adapter = null
addItems()
}
private fun setupGridLayoutManager() {
val layoutManager = search_results_list.layoutManager as MyGridLayoutManager
val layoutManager = binding.searchResultsList.layoutManager as MyGridLayoutManager
layoutManager.spanCount = context?.config?.fileColumnCnt ?: 3
}
private fun setupListLayoutManager() {
val layoutManager = search_results_list.layoutManager as MyGridLayoutManager
val layoutManager = binding.searchResultsList.layoutManager as MyGridLayoutManager
layoutManager.spanCount = 1
}
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)
}.apply {
search_results_list.adapter = this
binding.searchResultsList.adapter = this
}
}
@ -367,14 +385,14 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
}
private fun showProgressBar() {
progress_bar.show()
binding.progressBar.show()
}
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() {
ensureBackgroundThread {
@ -403,7 +421,7 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
}
override fun columnCountChanged() {
(search_results_list.layoutManager as MyGridLayoutManager).spanCount = context!!.config.fileColumnCnt
(binding.searchResultsList.layoutManager as MyGridLayoutManager).spanCount = context!!.config.fileColumnCnt
getRecyclerAdapter()?.apply {
notifyItemRangeChanged(0, listItems.size)
}