Skip to content

Commit b6e8431

Browse files
committed
Further refactor ExtendedListFragment.kt
- implement review feedback - use non-nullable types - improve nullability handling - remove unnecessary casts - simplify expressions - use property access notation Signed-off-by: ZetaTom <70907959+ZetaTom@users.noreply.github.com>
1 parent 30ff136 commit b6e8431

1 file changed

Lines changed: 57 additions & 58 deletions

File tree

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

Lines changed: 57 additions & 58 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
@@ -43,7 +41,6 @@ import androidx.annotation.DrawableRes
4341
import androidx.annotation.StringRes
4442
import androidx.appcompat.widget.SearchView
4543
import androidx.core.content.ContextCompat
46-
import androidx.core.view.MenuItemCompat
4744
import androidx.fragment.app.Fragment
4845
import androidx.lifecycle.lifecycleScope
4946
import androidx.recyclerview.widget.GridLayoutManager
@@ -120,9 +117,9 @@ open class ExtendedListFragment :
120117
private var mEmptyListIcon: ImageView? = null
121118

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

128125
private var mOnRefreshListener: OnRefreshListener? = null
@@ -168,16 +165,15 @@ open class ExtendedListFragment :
168165
@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!!)
173170
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())
@@ -192,21 +188,22 @@ open class ExtendedListFragment :
192188

193189
searchView?.setOnQueryTextFocusChangeListener { _: View?, hasFocus: Boolean ->
194190
lifecycleScope.launch(Dispatchers.Main) {
195-
if (getActivity() != null &&
196-
(getActivity() !is FolderPickerActivity) &&
197-
(getActivity() !is UploadFilesActivity)
198-
) {
199-
if (getActivity() is FileDisplayActivity) {
200-
val fragment = (getActivity() as FileDisplayActivity).leftFragment
201-
if (fragment is OCFileListFragment) {
202-
fragment.setFabVisible(!hasFocus)
203-
}
204-
}
191+
val activity = activity
192+
193+
if (activity == null || (activity is FolderPickerActivity) || (activity is UploadFilesActivity)) {
194+
return@launch
195+
}
205196

206-
if (TextUtils.isEmpty(searchView?.query)) {
207-
closeButton?.setVisibility(View.INVISIBLE)
197+
if (activity is FileDisplayActivity) {
198+
val fragment = activity.leftFragment
199+
if (fragment is OCFileListFragment) {
200+
fragment.setFabVisible(!hasFocus)
208201
}
209202
}
203+
204+
if (searchView?.query.isNullOrEmpty()) {
205+
closeButton?.visibility = View.INVISIBLE
206+
}
210207
}
211208
}
212209

@@ -217,7 +214,7 @@ open class ExtendedListFragment :
217214
searchView?.onActionViewExpanded()
218215

219216
val inputMethodManager =
220-
getActivity()?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
217+
activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
221218
inputMethodManager?.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT)
222219
}
223220

@@ -228,9 +225,9 @@ open class ExtendedListFragment :
228225
override fun onQueryTextChange(query: String): Boolean {
229226
// After 300 ms, set the query
230227

231-
closeButton?.setVisibility(View.VISIBLE)
228+
closeButton?.visibility = View.VISIBLE
232229
if (query.isEmpty()) {
233-
closeButton?.setVisibility(View.INVISIBLE)
230+
closeButton?.visibility = View.INVISIBLE
234231
}
235232
return false
236233
}
@@ -252,11 +249,11 @@ open class ExtendedListFragment :
252249

253250
fun performSearch(query: String, listOfHiddenFiles: ArrayList<String?>?, isBackPressed: Boolean) {
254251
val adapter = recyclerView?.adapter
255-
val activity: Activity? = activity
252+
val activity = activity ?: return
256253

257-
if (activity != null) {
258-
if (activity is FileDisplayActivity) {
259-
if (isBackPressed && TextUtils.isEmpty(query)) {
254+
when (activity) {
255+
is FileDisplayActivity -> {
256+
if (isBackPressed && query.isEmpty()) {
260257
activity.resetSearchView()
261258
activity.updateListOfFilesFragment(true)
262259
} else {
@@ -280,13 +277,17 @@ open class ExtendedListFragment :
280277
}
281278
searchView?.clearFocus()
282279
}
283-
} else if (activity is UploadFilesActivity) {
284-
val localFileListAdapter = adapter as LocalFileListAdapter?
285-
if (localFileListAdapter != null) {
280+
}
281+
282+
is UploadFilesActivity -> {
283+
val localFileListAdapter = adapter
284+
if (adapter is LocalFileListAdapter) {
286285
localFileListAdapter.filter(query)
287286
activity.fileListFragment.setLoading(false)
288287
}
289-
} else if (activity is FolderPickerActivity) {
288+
}
289+
290+
is FolderPickerActivity -> {
290291
activity.search(query)
291292
}
292293
}
@@ -369,14 +370,14 @@ open class ExtendedListFragment :
369370

370371
@SuppressLint("NotifyDataSetChanged")
371372
protected open fun setGridViewColumns(scaleFactor: Float) {
372-
if (mRecyclerView?.layoutManager is GridLayoutManager) {
373-
val gridLayoutManager = mRecyclerView?.layoutManager as GridLayoutManager
373+
val gridLayoutManager = mRecyclerView?.layoutManager
374+
if (gridLayoutManager is GridLayoutManager) {
374375
if (mScale == -1f) {
375376
gridLayoutManager.setSpanCount(GridView.AUTO_FIT)
376377
mScale = gridLayoutManager.spanCount.toFloat()
377378
}
378379
mScale *= 2f - scaleFactor
379-
mScale = max(MIN_COLUMN_SIZE.toDouble(), min(mScale.toDouble(), maxColumnSize.toDouble())).toFloat()
380+
mScale = max(MIN_COLUMN_SIZE, min(mScale, maxColumnSize.toFloat()))
380381
val scaleInt = mScale.roundToInt()
381382
gridLayoutManager.setSpanCount(scaleInt)
382383
mRecyclerView?.adapter?.notifyDataSetChanged()
@@ -407,9 +408,9 @@ open class ExtendedListFragment :
407408
return
408409
}
409410

410-
mIndexes = savedInstanceState.getIntegerArrayList(KEY_INDEXES)
411-
mFirstPositions = savedInstanceState.getIntegerArrayList(KEY_FIRST_POSITIONS)
412-
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 }
413414
mHeightCell = savedInstanceState.getInt(KEY_HEIGHT_CELL)
414415
setMessageForEmptyList(savedInstanceState.getString(KEY_EMPTY_LIST_MESSAGE))
415416

@@ -450,15 +451,15 @@ open class ExtendedListFragment :
450451
* Restore index and position
451452
*/
452453
protected fun restoreIndexAndTopPosition() {
453-
if (mIndexes == null || mIndexes?.isEmpty() == true) {
454+
if (mIndexes.isEmpty()) {
454455
Log_OC.d(TAG, "Indexes is null or empty")
455456
return
456457
}
457458

458459
// needs to be checked; not every browse-up had a browse-down before
459-
val index = mIndexes?.removeAt(mIndexes!!.size - 1)
460-
val firstPosition = mFirstPositions?.removeAt(mFirstPositions!!.size - 1)!!
461-
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)
462463

463464
Log_OC.v(
464465
TAG,
@@ -468,26 +469,24 @@ open class ExtendedListFragment :
468469
)
469470
)
470471

471-
scrollToPosition(firstPosition)
472+
firstPosition?.let { scrollToPosition(it) }
472473
}
473474

474475
private fun scrollToPosition(position: Int) {
475-
val linearLayoutManager = mRecyclerView?.layoutManager as LinearLayoutManager?
476+
val layoutManager = mRecyclerView?.layoutManager
476477

477-
if (linearLayoutManager != null) {
478-
val visibleItemCount = linearLayoutManager.findLastCompletelyVisibleItemPosition() -
479-
linearLayoutManager.findFirstCompletelyVisibleItemPosition()
480-
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)
481482
}
482483
}
483484

484485
/*
485486
* Save index and top position
486487
*/
487488
protected fun saveIndexAndTopPosition(index: Int) {
488-
if (mIndexes != null) {
489-
mIndexes?.add(index)
490-
}
489+
mIndexes.add(index)
491490

492491
val layoutManager = mRecyclerView?.layoutManager
493492
val firstPosition: Int = if (layoutManager is GridLayoutManager) {
@@ -496,12 +495,12 @@ open class ExtendedListFragment :
496495
(layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition()
497496
}
498497

499-
mFirstPositions?.add(firstPosition)
498+
mFirstPositions.add(firstPosition)
500499

501500
val view = mRecyclerView?.getChildAt(0)
502501
val top = view?.top ?: 0
503502

504-
mTops?.add(top)
503+
mTops.add(top)
505504

506505
// Save the height of a cell
507506
mHeightCell = if (view == null || mHeightCell != 0) mHeightCell else view.height
@@ -515,8 +514,8 @@ open class ExtendedListFragment :
515514
if (searchView != null) {
516515
searchView?.onActionViewCollapsed()
517516

518-
val activity: Activity?
519-
if ((getActivity().also { activity = it }) != null && activity is FileDisplayActivity) {
517+
val activity = activity
518+
if (activity is FileDisplayActivity) {
520519
activity.setDrawerIndicatorEnabled(activity.isDrawerIndicatorAvailable)
521520
activity.hideSearchView(activity.getCurrentDir())
522521
}
@@ -594,7 +593,7 @@ open class ExtendedListFragment :
594593
mEmptyListIcon?.setImageResource(icon)
595594
}
596595

597-
mEmptyListIcon?.setVisibility(View.VISIBLE)
596+
mEmptyListIcon?.visibility = View.VISIBLE
598597
mEmptyListMessage?.visibility = View.VISIBLE
599598
}
600599
}
@@ -693,7 +692,7 @@ open class ExtendedListFragment :
693692
}
694693
mEmptyListHeadline?.setText(R.string.file_list_loading)
695694
mEmptyListMessage?.text = ""
696-
mEmptyListIcon?.setVisibility(View.GONE)
695+
mEmptyListIcon?.visibility = View.GONE
697696
}
698697
)
699698
}

0 commit comments

Comments
 (0)