Skip to content

Commit 13e19e7

Browse files
Merge pull request #15319 from nextcloud/refactor/extendedlistfragment
Refactor ExtendedListFragment
2 parents b103dd4 + b6e8431 commit 13e19e7

1 file changed

Lines changed: 82 additions & 94 deletions

File tree

app/src/main/java/com/owncloud/android/ui/fragment/ExtendedListFragment.kt

Lines changed: 82 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ package com.owncloud.android.ui.fragment
1818

1919
import android.animation.LayoutTransition
2020
import android.annotation.SuppressLint
21-
import android.app.Activity
2221
import android.content.Context
2322
import android.content.res.Configuration
2423
import android.os.Bundle
25-
import android.text.TextUtils
2624
import android.util.DisplayMetrics
2725
import android.view.KeyEvent
2826
import android.view.LayoutInflater
@@ -32,7 +30,6 @@ import android.view.MotionEvent
3230
import android.view.ScaleGestureDetector
3331
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener
3432
import android.view.View
35-
import android.view.View.OnTouchListener
3633
import android.view.ViewGroup
3734
import android.view.inputmethod.InputMethodManager
3835
import android.widget.AdapterView
@@ -44,7 +41,6 @@ import androidx.annotation.DrawableRes
4441
import androidx.annotation.StringRes
4542
import androidx.appcompat.widget.SearchView
4643
import androidx.core.content.ContextCompat
47-
import androidx.core.view.MenuItemCompat
4844
import androidx.fragment.app.Fragment
4945
import androidx.lifecycle.lifecycleScope
5046
import androidx.recyclerview.widget.GridLayoutManager
@@ -121,9 +117,9 @@ open class ExtendedListFragment :
121117
private var mEmptyListIcon: ImageView? = null
122118

123119
// Save the state of the scroll in browsing
124-
private var mIndexes: ArrayList<Int?>? = ArrayList<Int?>()
125-
private var mFirstPositions: ArrayList<Int?>? = ArrayList<Int?>()
126-
private var mTops: ArrayList<Int?>? = ArrayList<Int?>()
120+
private var mIndexes = arrayListOf<Int?>()
121+
private var mFirstPositions = arrayListOf<Int?>()
122+
private var mTops = arrayListOf<Int?>()
127123
private var mHeightCell = 0
128124

129125
private var mOnRefreshListener: OnRefreshListener? = null
@@ -166,18 +162,18 @@ open class ExtendedListFragment :
166162
return recyclerView?.layoutManager is GridLayoutManager
167163
}
168164

165+
@Deprecated("Deprecated in Java")
169166
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
170167
val item = menu.findItem(R.id.action_search)
171-
searchView = MenuItemCompat.getActionView(item) as SearchView?
168+
searchView = item.actionView as SearchView?
172169
viewThemeUtils.androidx.themeToolbarSearchView(searchView!!)
173-
closeButton = searchView?.findViewById<ImageView>(androidx.appcompat.R.id.search_close_btn)
170+
closeButton = searchView?.findViewById(androidx.appcompat.R.id.search_close_btn)
174171
searchView?.setOnQueryTextListener(this)
175172
searchView?.setOnCloseListener(this)
176173

177174
val displayMetrics = DisplayMetrics()
178-
val activity: Activity?
179-
if ((getActivity().also { activity = it }) != null) {
180-
activity?.windowManager?.defaultDisplay?.getMetrics(displayMetrics)
175+
activity?.let { activity ->
176+
activity.windowManager?.defaultDisplay?.getMetrics(displayMetrics)
181177
val width = displayMetrics.widthPixels
182178
if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
183179
searchView?.setMaxWidth((width * 0.4).toInt())
@@ -190,40 +186,37 @@ open class ExtendedListFragment :
190186
}
191187
}
192188

193-
searchView?.setOnQueryTextFocusChangeListener(
194-
View.OnFocusChangeListener { v: View?, hasFocus: Boolean ->
195-
lifecycleScope.launch(Dispatchers.Main) {
196-
if (getActivity() != null &&
197-
(getActivity() !is FolderPickerActivity) &&
198-
(getActivity() !is UploadFilesActivity)
199-
) {
200-
if (getActivity() is FileDisplayActivity) {
201-
val fragment = (getActivity() as FileDisplayActivity).leftFragment
202-
if (fragment is OCFileListFragment) {
203-
fragment.setFabVisible(!hasFocus)
204-
}
205-
}
189+
searchView?.setOnQueryTextFocusChangeListener { _: View?, hasFocus: Boolean ->
190+
lifecycleScope.launch(Dispatchers.Main) {
191+
val activity = activity
206192

207-
if (TextUtils.isEmpty(searchView?.query)) {
208-
closeButton?.setVisibility(View.INVISIBLE)
209-
}
193+
if (activity == null || (activity is FolderPickerActivity) || (activity is UploadFilesActivity)) {
194+
return@launch
195+
}
196+
197+
if (activity is FileDisplayActivity) {
198+
val fragment = activity.leftFragment
199+
if (fragment is OCFileListFragment) {
200+
fragment.setFabVisible(!hasFocus)
210201
}
211202
}
203+
204+
if (searchView?.query.isNullOrEmpty()) {
205+
closeButton?.visibility = View.INVISIBLE
206+
}
212207
}
213-
)
208+
}
214209

215210
// On close -> empty field, show keyboard and
216-
closeButton?.setOnClickListener(
217-
View.OnClickListener { view: View? ->
218-
searchView?.setQuery("", true)
219-
searchView?.requestFocus()
220-
searchView?.onActionViewExpanded()
221-
222-
val inputMethodManager =
223-
getActivity()?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
224-
inputMethodManager?.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT)
225-
}
226-
)
211+
closeButton?.setOnClickListener {
212+
searchView?.setQuery("", true)
213+
searchView?.requestFocus()
214+
searchView?.onActionViewExpanded()
215+
216+
val inputMethodManager =
217+
activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
218+
inputMethodManager?.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT)
219+
}
227220

228221
val searchBar = searchView?.findViewById<LinearLayout>(androidx.appcompat.R.id.search_bar)
229222
searchBar?.setLayoutTransition(LayoutTransition())
@@ -232,9 +225,9 @@ open class ExtendedListFragment :
232225
override fun onQueryTextChange(query: String): Boolean {
233226
// After 300 ms, set the query
234227

235-
closeButton?.setVisibility(View.VISIBLE)
228+
closeButton?.visibility = View.VISIBLE
236229
if (query.isEmpty()) {
237-
closeButton?.setVisibility(View.INVISIBLE)
230+
closeButton?.visibility = View.INVISIBLE
238231
}
239232
return false
240233
}
@@ -248,22 +241,21 @@ open class ExtendedListFragment :
248241
return true
249242
}
250243
if (adapter is LocalFileListAdapter) {
251-
performSearch(query, ArrayList<String?>(), false)
244+
performSearch(query, ArrayList(), false)
252245
return true
253246
}
254247
return false
255248
}
256249

257250
fun performSearch(query: String, listOfHiddenFiles: ArrayList<String?>?, isBackPressed: Boolean) {
258251
val adapter = recyclerView?.adapter
259-
val activity: Activity? = getActivity()
252+
val activity = activity ?: return
260253

261-
if (activity != null) {
262-
if (activity is FileDisplayActivity) {
263-
if (isBackPressed && TextUtils.isEmpty(query)) {
264-
val fileDisplayActivity = activity
265-
fileDisplayActivity.resetSearchView()
266-
fileDisplayActivity.updateListOfFilesFragment(true)
254+
when (activity) {
255+
is FileDisplayActivity -> {
256+
if (isBackPressed && query.isEmpty()) {
257+
activity.resetSearchView()
258+
activity.updateListOfFilesFragment(true)
267259
} else {
268260
lifecycleScope.launch(Dispatchers.Main) {
269261
if (adapter is OCFileListAdapter) {
@@ -285,13 +277,17 @@ open class ExtendedListFragment :
285277
}
286278
searchView?.clearFocus()
287279
}
288-
} else if (activity is UploadFilesActivity) {
289-
val localFileListAdapter = adapter as LocalFileListAdapter?
290-
if (localFileListAdapter != null) {
280+
}
281+
282+
is UploadFilesActivity -> {
283+
val localFileListAdapter = adapter
284+
if (adapter is LocalFileListAdapter) {
291285
localFileListAdapter.filter(query)
292286
activity.fileListFragment.setLoading(false)
293287
}
294-
} else if (activity is FolderPickerActivity) {
288+
}
289+
290+
is FolderPickerActivity -> {
295291
activity.search(query)
296292
}
297293
}
@@ -307,10 +303,6 @@ open class ExtendedListFragment :
307303
return true
308304
}
309305

310-
override fun onAttach(context: Context) {
311-
super.onAttach(context)
312-
}
313-
314306
@SuppressLint("ClickableViewAccessibility")
315307
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
316308
Log_OC.d(TAG, "onCreateView")
@@ -331,15 +323,13 @@ open class ExtendedListFragment :
331323

332324
mScaleGestureDetector = ScaleGestureDetector(MainApp.getAppContext(), ScaleListener())
333325

334-
recyclerView?.setOnTouchListener(
335-
OnTouchListener { view: View, motionEvent: MotionEvent ->
336-
mScaleGestureDetector?.onTouchEvent(motionEvent)
337-
if (motionEvent.action == MotionEvent.ACTION_UP) {
338-
view.performClick()
339-
}
340-
false
326+
recyclerView?.setOnTouchListener { view: View, motionEvent: MotionEvent ->
327+
mScaleGestureDetector?.onTouchEvent(motionEvent)
328+
if (motionEvent.action == MotionEvent.ACTION_UP) {
329+
view.performClick()
341330
}
342-
)
331+
false
332+
}
343333

344334
mRefreshListLayout = binding?.swipeContainingList
345335
mRefreshListLayout?.let {
@@ -380,14 +370,14 @@ open class ExtendedListFragment :
380370

381371
@SuppressLint("NotifyDataSetChanged")
382372
protected open fun setGridViewColumns(scaleFactor: Float) {
383-
if (mRecyclerView?.layoutManager is GridLayoutManager) {
384-
val gridLayoutManager = mRecyclerView?.layoutManager as GridLayoutManager
373+
val gridLayoutManager = mRecyclerView?.layoutManager
374+
if (gridLayoutManager is GridLayoutManager) {
385375
if (mScale == -1f) {
386376
gridLayoutManager.setSpanCount(GridView.AUTO_FIT)
387377
mScale = gridLayoutManager.spanCount.toFloat()
388378
}
389-
mScale *= 1f - (scaleFactor - 1f)
390-
mScale = max(MIN_COLUMN_SIZE.toDouble(), min(mScale.toDouble(), maxColumnSize.toDouble())).toFloat()
379+
mScale *= 2f - scaleFactor
380+
mScale = max(MIN_COLUMN_SIZE, min(mScale, maxColumnSize.toFloat()))
391381
val scaleInt = mScale.roundToInt()
392382
gridLayoutManager.setSpanCount(scaleInt)
393383
mRecyclerView?.adapter?.notifyDataSetChanged()
@@ -418,9 +408,9 @@ open class ExtendedListFragment :
418408
return
419409
}
420410

421-
mIndexes = savedInstanceState.getIntegerArrayList(KEY_INDEXES)
422-
mFirstPositions = savedInstanceState.getIntegerArrayList(KEY_FIRST_POSITIONS)
423-
mTops = savedInstanceState.getIntegerArrayList(KEY_TOPS)
411+
savedInstanceState.getIntegerArrayList(KEY_INDEXES)?.let { mIndexes = it }
412+
savedInstanceState.getIntegerArrayList(KEY_FIRST_POSITIONS)?.let { mFirstPositions = it }
413+
savedInstanceState.getIntegerArrayList(KEY_TOPS)?.let { mTops = it }
424414
mHeightCell = savedInstanceState.getInt(KEY_HEIGHT_CELL)
425415
setMessageForEmptyList(savedInstanceState.getString(KEY_EMPTY_LIST_MESSAGE))
426416

@@ -461,15 +451,15 @@ open class ExtendedListFragment :
461451
* Restore index and position
462452
*/
463453
protected fun restoreIndexAndTopPosition() {
464-
if (mIndexes == null || mIndexes?.isEmpty() == true) {
454+
if (mIndexes.isEmpty()) {
465455
Log_OC.d(TAG, "Indexes is null or empty")
466456
return
467457
}
468458

469459
// needs to be checked; not every browse-up had a browse-down before
470-
val index = mIndexes?.removeAt(mIndexes!!.size - 1)
471-
val firstPosition = mFirstPositions?.removeAt(mFirstPositions!!.size - 1)!!
472-
val top = mTops?.removeAt(mTops!!.size - 1)
460+
val index = mIndexes.removeAt(mIndexes.size - 1)
461+
val firstPosition = mFirstPositions.removeAt(mFirstPositions.size - 1)
462+
val top = mTops.removeAt(mTops.size - 1)
473463

474464
Log_OC.v(
475465
TAG,
@@ -479,26 +469,24 @@ open class ExtendedListFragment :
479469
)
480470
)
481471

482-
scrollToPosition(firstPosition)
472+
firstPosition?.let { scrollToPosition(it) }
483473
}
484474

485475
private fun scrollToPosition(position: Int) {
486-
val linearLayoutManager = mRecyclerView?.layoutManager as LinearLayoutManager?
476+
val layoutManager = mRecyclerView?.layoutManager
487477

488-
if (linearLayoutManager != null) {
489-
val visibleItemCount = linearLayoutManager.findLastCompletelyVisibleItemPosition() -
490-
linearLayoutManager.findFirstCompletelyVisibleItemPosition()
491-
linearLayoutManager.scrollToPositionWithOffset(position, (visibleItemCount / 2) * mHeightCell)
478+
if (layoutManager is LinearLayoutManager) {
479+
val visibleItemCount = layoutManager.findLastCompletelyVisibleItemPosition() -
480+
layoutManager.findFirstCompletelyVisibleItemPosition()
481+
layoutManager.scrollToPositionWithOffset(position, (visibleItemCount / 2) * mHeightCell)
492482
}
493483
}
494484

495485
/*
496486
* Save index and top position
497487
*/
498488
protected fun saveIndexAndTopPosition(index: Int) {
499-
if (mIndexes != null) {
500-
mIndexes?.add(index)
501-
}
489+
mIndexes.add(index)
502490

503491
val layoutManager = mRecyclerView?.layoutManager
504492
val firstPosition: Int = if (layoutManager is GridLayoutManager) {
@@ -507,12 +495,12 @@ open class ExtendedListFragment :
507495
(layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition()
508496
}
509497

510-
mFirstPositions?.add(firstPosition)
498+
mFirstPositions.add(firstPosition)
511499

512500
val view = mRecyclerView?.getChildAt(0)
513501
val top = view?.top ?: 0
514502

515-
mTops?.add(top)
503+
mTops.add(top)
516504

517505
// Save the height of a cell
518506
mHeightCell = if (view == null || mHeightCell != 0) mHeightCell else view.height
@@ -526,8 +514,8 @@ open class ExtendedListFragment :
526514
if (searchView != null) {
527515
searchView?.onActionViewCollapsed()
528516

529-
val activity: Activity?
530-
if ((getActivity().also { activity = it }) != null && activity is FileDisplayActivity) {
517+
val activity = activity
518+
if (activity is FileDisplayActivity) {
531519
activity.setDrawerIndicatorEnabled(activity.isDrawerIndicatorAvailable)
532520
activity.hideSearchView(activity.getCurrentDir())
533521
}
@@ -605,7 +593,7 @@ open class ExtendedListFragment :
605593
mEmptyListIcon?.setImageResource(icon)
606594
}
607595

608-
mEmptyListIcon?.setVisibility(View.VISIBLE)
596+
mEmptyListIcon?.visibility = View.VISIBLE
609597
mEmptyListMessage?.visibility = View.VISIBLE
610598
}
611599
}
@@ -704,7 +692,7 @@ open class ExtendedListFragment :
704692
}
705693
mEmptyListHeadline?.setText(R.string.file_list_loading)
706694
mEmptyListMessage?.text = ""
707-
mEmptyListIcon?.setVisibility(View.GONE)
695+
mEmptyListIcon?.visibility = View.GONE
708696
}
709697
)
710698
}
@@ -751,10 +739,10 @@ open class ExtendedListFragment :
751739
mSwitchGridViewButton?.let {
752740
if (isGridEnabled) {
753741
it.setContentDescription(getString(R.string.action_switch_list_view))
754-
it.setIcon(ContextCompat.getDrawable(requireContext(), R.drawable.ic_view_list))
742+
it.icon = ContextCompat.getDrawable(requireContext(), R.drawable.ic_view_list)
755743
} else {
756744
it.setContentDescription(getString(R.string.action_switch_grid_view))
757-
it.setIcon(ContextCompat.getDrawable(requireContext(), R.drawable.ic_view_module))
745+
it.icon = ContextCompat.getDrawable(requireContext(), R.drawable.ic_view_module)
758746
}
759747
}
760748
}

0 commit comments

Comments
 (0)