Skip to content

Commit 4caf77b

Browse files
committed
migrate fragments to viewbinding
1 parent 1ddf7ff commit 4caf77b

4 files changed

Lines changed: 291 additions & 222 deletions

File tree

app/src/main/kotlin/com/simplemobiletools/filemanager/pro/fragments/ItemsFragment.kt

Lines changed: 108 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ import com.simplemobiletools.filemanager.pro.R
1717
import com.simplemobiletools.filemanager.pro.activities.MainActivity
1818
import com.simplemobiletools.filemanager.pro.activities.SimpleActivity
1919
import com.simplemobiletools.filemanager.pro.adapters.ItemsAdapter
20+
import com.simplemobiletools.filemanager.pro.databinding.ItemsFragmentBinding
2021
import com.simplemobiletools.filemanager.pro.dialogs.CreateNewItemDialog
2122
import com.simplemobiletools.filemanager.pro.extensions.config
2223
import com.simplemobiletools.filemanager.pro.extensions.isPathOnRoot
2324
import com.simplemobiletools.filemanager.pro.helpers.MAX_COLUMN_COUNT
2425
import com.simplemobiletools.filemanager.pro.helpers.RootHelpers
2526
import com.simplemobiletools.filemanager.pro.interfaces.ItemOperationsListener
2627
import com.simplemobiletools.filemanager.pro.models.ListItem
27-
import kotlinx.android.synthetic.main.items_fragment.view.*
2828
import java.io.File
2929

30-
class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), ItemOperationsListener,
30+
class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment<MyViewPagerFragment.ItemsInnerBinding>(context, attributeSet),
31+
ItemOperationsListener,
3132
Breadcrumbs.BreadcrumbsListener {
3233
private var showHidden = false
3334
private var lastSearchedText = ""
@@ -36,17 +37,26 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
3637

3738
private var storedItems = ArrayList<ListItem>()
3839
private var itemsIgnoringSearch = ArrayList<ListItem>()
40+
private lateinit var binding: ItemsFragmentBinding
41+
42+
override fun onFinishInflate() {
43+
super.onFinishInflate()
44+
binding = ItemsFragmentBinding.bind(this)
45+
innerBinding = ItemsInnerBinding(binding)
46+
}
3947

4048
override fun setupFragment(activity: SimpleActivity) {
4149
if (this.activity == null) {
4250
this.activity = activity
43-
breadcrumbs.listener = this@ItemsFragment
44-
items_swipe_refresh.setOnRefreshListener { refreshFragment() }
45-
items_fab.setOnClickListener {
46-
if (isCreateDocumentIntent) {
47-
(activity as MainActivity).createDocumentConfirmed(currentPath)
48-
} else {
49-
createNewItem()
51+
binding.apply {
52+
breadcrumbs.listener = this@ItemsFragment
53+
itemsSwipeRefresh.setOnRefreshListener { refreshFragment() }
54+
itemsFab.setOnClickListener {
55+
if (isCreateDocumentIntent) {
56+
(activity as MainActivity).createDocumentConfirmed(currentPath)
57+
} else {
58+
createNewItem()
59+
}
5060
}
5161
}
5262
}
@@ -60,22 +70,24 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
6070
initDrawables()
6171
}
6272

63-
val properPrimaryColor = context!!.getProperPrimaryColor()
64-
items_fastscroller.updateColors(properPrimaryColor)
65-
progress_bar.setIndicatorColor(properPrimaryColor)
66-
progress_bar.trackColor = properPrimaryColor.adjustAlpha(LOWER_ALPHA)
73+
binding.apply {
74+
val properPrimaryColor = context!!.getProperPrimaryColor()
75+
itemsFastscroller.updateColors(properPrimaryColor)
76+
progressBar.setIndicatorColor(properPrimaryColor)
77+
progressBar.trackColor = properPrimaryColor.adjustAlpha(LOWER_ALPHA)
6778

68-
if (currentPath != "") {
69-
breadcrumbs.updateColor(textColor)
70-
}
79+
if (currentPath != "") {
80+
breadcrumbs.updateColor(textColor)
81+
}
7182

72-
items_swipe_refresh.isEnabled = lastSearchedText.isEmpty() && activity?.config?.enablePullToRefresh != false
83+
itemsSwipeRefresh.isEnabled = lastSearchedText.isEmpty() && activity?.config?.enablePullToRefresh != false
84+
}
7385
}
7486

7587
override fun setupFontSize() {
7688
getRecyclerAdapter()?.updateFontSizes()
7789
if (currentPath != "") {
78-
breadcrumbs.updateFontSize(context!!.getTextSize(), false)
90+
binding.breadcrumbs.updateFontSize(context!!.getTextSize(), false)
7991
}
8092
}
8193

@@ -133,18 +145,18 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
133145

134146
private fun addItems(items: ArrayList<ListItem>, forceRefresh: Boolean = false) {
135147
activity?.runOnUiThread {
136-
items_swipe_refresh?.isRefreshing = false
137-
breadcrumbs.setBreadcrumb(currentPath)
148+
binding.itemsSwipeRefresh?.isRefreshing = false
149+
binding.breadcrumbs.setBreadcrumb(currentPath)
138150
if (!forceRefresh && items.hashCode() == storedItems.hashCode()) {
139151
return@runOnUiThread
140152
}
141153

142154
storedItems = items
143-
if (items_list.adapter == null) {
144-
breadcrumbs.updateFontSize(context!!.getTextSize(), true)
155+
if (binding.itemsList.adapter == null) {
156+
binding.breadcrumbs.updateFontSize(context!!.getTextSize(), true)
145157
}
146158

147-
ItemsAdapter(activity as SimpleActivity, storedItems, this, items_list, isPickMultipleIntent, items_swipe_refresh) {
159+
ItemsAdapter(activity as SimpleActivity, storedItems, this, binding.itemsList, isPickMultipleIntent, binding.itemsSwipeRefresh) {
148160
if ((it as? ListItem)?.isSectionTitle == true) {
149161
openDirectory(it.mPath)
150162
searchClosed()
@@ -153,11 +165,11 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
153165
}
154166
}.apply {
155167
setupZoomListener(zoomListener)
156-
items_list.adapter = this
168+
binding.itemsList.adapter = this
157169
}
158170

159171
if (context.areSystemAnimationsEnabled) {
160-
items_list.scheduleLayoutAnimation()
172+
binding.itemsList.scheduleLayoutAnimation()
161173
}
162174

163175
getRecyclerLayoutManager().onRestoreInstanceState(scrollStates[currentPath])
@@ -166,7 +178,7 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
166178

167179
private fun getScrollState() = getRecyclerLayoutManager().onSaveInstanceState()
168180

169-
private fun getRecyclerLayoutManager() = (items_list.layoutManager as MyGridLayoutManager)
181+
private fun getRecyclerLayoutManager() = (binding.itemsList.layoutManager as MyGridLayoutManager)
170182

171183
@SuppressLint("NewApi")
172184
private fun getItems(path: String, callback: (originalPath: String, items: ArrayList<ListItem>) -> Unit) {
@@ -295,59 +307,63 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
295307
return
296308
}
297309

298-
items_swipe_refresh.isEnabled = text.isEmpty() && activity?.config?.enablePullToRefresh != false
299-
when {
300-
text.isEmpty() -> {
301-
items_fastscroller.beVisible()
302-
getRecyclerAdapter()?.updateItems(itemsIgnoringSearch)
303-
items_placeholder.beGone()
304-
items_placeholder_2.beGone()
305-
hideProgressBar()
306-
}
307-
text.length == 1 -> {
308-
items_fastscroller.beGone()
309-
items_placeholder.beVisible()
310-
items_placeholder_2.beVisible()
311-
hideProgressBar()
312-
}
313-
else -> {
314-
showProgressBar()
315-
ensureBackgroundThread {
316-
val files = searchFiles(text, currentPath)
317-
files.sortBy { it.getParentPath() }
318-
319-
if (lastSearchedText != text) {
320-
return@ensureBackgroundThread
321-
}
310+
binding.apply {
311+
itemsSwipeRefresh.isEnabled = text.isEmpty() && activity?.config?.enablePullToRefresh != false
312+
when {
313+
text.isEmpty() -> {
314+
itemsFastscroller.beVisible()
315+
getRecyclerAdapter()?.updateItems(itemsIgnoringSearch)
316+
itemsPlaceholder.beGone()
317+
itemsPlaceholder2.beGone()
318+
hideProgressBar()
319+
}
322320

323-
val listItems = ArrayList<ListItem>()
321+
text.length == 1 -> {
322+
itemsFastscroller.beGone()
323+
itemsPlaceholder.beVisible()
324+
itemsPlaceholder2.beVisible()
325+
hideProgressBar()
326+
}
324327

325-
var previousParent = ""
326-
files.forEach {
327-
val parent = it.mPath.getParentPath()
328-
if (!it.isDirectory && parent != previousParent && context != null) {
329-
val sectionTitle = ListItem(parent, context!!.humanizePath(parent), false, 0, 0, 0, true, false)
330-
listItems.add(sectionTitle)
331-
previousParent = parent
332-
}
328+
else -> {
329+
showProgressBar()
330+
ensureBackgroundThread {
331+
val files = searchFiles(text, currentPath)
332+
files.sortBy { it.getParentPath() }
333333

334-
if (it.isDirectory) {
335-
val sectionTitle = ListItem(it.path, context!!.humanizePath(it.path), true, 0, 0, 0, true, false)
336-
listItems.add(sectionTitle)
337-
previousParent = parent
334+
if (lastSearchedText != text) {
335+
return@ensureBackgroundThread
338336
}
339337

340-
if (!it.isDirectory) {
341-
listItems.add(it)
338+
val listItems = ArrayList<ListItem>()
339+
340+
var previousParent = ""
341+
files.forEach {
342+
val parent = it.mPath.getParentPath()
343+
if (!it.isDirectory && parent != previousParent && context != null) {
344+
val sectionTitle = ListItem(parent, context!!.humanizePath(parent), false, 0, 0, 0, true, false)
345+
listItems.add(sectionTitle)
346+
previousParent = parent
347+
}
348+
349+
if (it.isDirectory) {
350+
val sectionTitle = ListItem(it.path, context!!.humanizePath(it.path), true, 0, 0, 0, true, false)
351+
listItems.add(sectionTitle)
352+
previousParent = parent
353+
}
354+
355+
if (!it.isDirectory) {
356+
listItems.add(it)
357+
}
342358
}
343-
}
344359

345-
activity?.runOnUiThread {
346-
getRecyclerAdapter()?.updateItems(listItems, text)
347-
items_fastscroller.beVisibleIf(listItems.isNotEmpty())
348-
items_placeholder.beVisibleIf(listItems.isEmpty())
349-
items_placeholder_2.beGone()
350-
hideProgressBar()
360+
activity?.runOnUiThread {
361+
getRecyclerAdapter()?.updateItems(listItems, text)
362+
itemsFastscroller.beVisibleIf(listItems.isNotEmpty())
363+
itemsPlaceholder.beVisibleIf(listItems.isEmpty())
364+
itemsPlaceholder2.beGone()
365+
hideProgressBar()
366+
}
351367
}
352368
}
353369
}
@@ -390,12 +406,14 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
390406
}
391407

392408
private fun searchClosed() {
393-
lastSearchedText = ""
394-
items_swipe_refresh.isEnabled = activity?.config?.enablePullToRefresh != false
395-
items_fastscroller.beVisible()
396-
items_placeholder.beGone()
397-
items_placeholder_2.beGone()
398-
hideProgressBar()
409+
binding.apply {
410+
lastSearchedText = ""
411+
itemsSwipeRefresh.isEnabled = activity?.config?.enablePullToRefresh != false
412+
itemsFastscroller.beVisible()
413+
itemsPlaceholder.beGone()
414+
itemsPlaceholder2.beGone()
415+
hideProgressBar()
416+
}
399417
}
400418

401419
private fun createNewItem() {
@@ -408,7 +426,7 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
408426
}
409427
}
410428

411-
private fun getRecyclerAdapter() = items_list.adapter as? ItemsAdapter
429+
private fun getRecyclerAdapter() = binding.itemsList.adapter as? ItemsAdapter
412430

413431
private fun setupLayoutManager() {
414432
if (context!!.config.getFolderViewType(currentPath) == VIEW_TYPE_GRID) {
@@ -419,13 +437,13 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
419437
setupListLayoutManager()
420438
}
421439

422-
items_list.adapter = null
440+
binding.itemsList.adapter = null
423441
initZoomListener()
424442
addItems(storedItems, true)
425443
}
426444

427445
private fun setupGridLayoutManager() {
428-
val layoutManager = items_list.layoutManager as MyGridLayoutManager
446+
val layoutManager = binding.itemsList.layoutManager as MyGridLayoutManager
429447
layoutManager.spanCount = context?.config?.fileColumnCnt ?: 3
430448

431449
layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
@@ -440,14 +458,14 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
440458
}
441459

442460
private fun setupListLayoutManager() {
443-
val layoutManager = items_list.layoutManager as MyGridLayoutManager
461+
val layoutManager = binding.itemsList.layoutManager as MyGridLayoutManager
444462
layoutManager.spanCount = 1
445463
zoomListener = null
446464
}
447465

448466
private fun initZoomListener() {
449467
if (context?.config?.getFolderViewType(currentPath) == VIEW_TYPE_GRID) {
450-
val layoutManager = items_list.layoutManager as MyGridLayoutManager
468+
val layoutManager = binding.itemsList.layoutManager as MyGridLayoutManager
451469
zoomListener = object : MyRecyclerView.MyZoomListener {
452470
override fun zoomIn() {
453471
if (layoutManager.spanCount > 1) {
@@ -483,21 +501,23 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
483501
}
484502

485503
override fun columnCountChanged() {
486-
(items_list.layoutManager as MyGridLayoutManager).spanCount = context!!.config.fileColumnCnt
504+
(binding.itemsList.layoutManager as MyGridLayoutManager).spanCount = context!!.config.fileColumnCnt
487505
(activity as? MainActivity)?.refreshMenuItems()
488506
getRecyclerAdapter()?.apply {
489507
notifyItemRangeChanged(0, listItems.size)
490508
}
491509
}
492510

493-
private fun showProgressBar() {
494-
progress_bar.show()
511+
fun showProgressBar() {
512+
binding.progressBar.show()
495513
}
496514

497515
private fun hideProgressBar() {
498-
progress_bar.hide()
516+
binding.progressBar.hide()
499517
}
500518

519+
fun getBreadcrumbs() = binding.breadcrumbs
520+
501521
override fun toggleFilenameVisibility() {
502522
getRecyclerAdapter()?.updateDisplayFilenamesInGrid()
503523
}
@@ -509,7 +529,7 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
509529
openPath(it)
510530
}
511531
} else {
512-
val item = breadcrumbs.getItem(id)
532+
val item = binding.breadcrumbs.getItem(id)
513533
openPath(item.path)
514534
}
515535
}

0 commit comments

Comments
 (0)