Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ package com.nextcloud.utils
object TimeConstants {
const val MILLIS_PER_SECOND = 1000
}

object DelayConstants {
const val FIFTY = 50L
const val ONE_HUNDRED = 100L
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import com.nextcloud.client.account.UserAccountManager
import com.nextcloud.client.di.Injectable
import com.nextcloud.client.preferences.AppPreferences
import com.nextcloud.client.preferences.AppPreferencesImpl
import com.nextcloud.utils.DelayConstants
import com.nextcloud.utils.extensions.getTypedActivity
import com.nextcloud.utils.extensions.mainThread
import com.owncloud.android.MainApp
Expand Down Expand Up @@ -416,9 +417,9 @@ open class ExtendedListFragment :
switchToGridView()
}

val referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION)
Log_OC.v(TAG, "Setting grid position $referencePosition")
scrollToPosition(referencePosition)
mainThread(delay = DelayConstants.FIFTY) {
restoreIndexAndTopPosition()
}
}

override fun onSaveInstanceState(savedInstanceState: Bundle) {
Expand All @@ -445,16 +446,12 @@ open class ExtendedListFragment :
return mScale.roundToInt()
}

/*
* Restore index and position
*/
protected fun restoreIndexAndTopPosition() {
if (mIndexes.isEmpty()) {
Log_OC.d(TAG, "Indexes is null or empty")
return
}

// needs to be checked; not every browse-up had a browse-down before
val index = mIndexes.removeAt(mIndexes.size - 1)
val firstPosition = mFirstPositions.removeAt(mFirstPositions.size - 1)
val top = mTops.removeAt(mTops.size - 1)
Expand All @@ -467,30 +464,41 @@ open class ExtendedListFragment :
)
)

firstPosition?.let { scrollToPosition(it) }
if (firstPosition != null && firstPosition >= 0) {
mainThread(delay = DelayConstants.ONE_HUNDRED) {
scrollToPosition(firstPosition, top ?: 0)
}
}
}

private fun scrollToPosition(position: Int) {
private fun scrollToPosition(position: Int, top: Int = 0) {
val layoutManager = mRecyclerView?.layoutManager

if (layoutManager is LinearLayoutManager) {
val visibleItemCount = layoutManager.findLastCompletelyVisibleItemPosition() -
layoutManager.findFirstCompletelyVisibleItemPosition()
layoutManager.scrollToPositionWithOffset(position, (visibleItemCount / 2) * mHeightCell)
if (position < 0) {
return
}

when (layoutManager) {
is GridLayoutManager -> {
layoutManager.scrollToPositionWithOffset(position, top)
}

is LinearLayoutManager -> {
val visibleItemCount = layoutManager.findLastCompletelyVisibleItemPosition() -
layoutManager.findFirstCompletelyVisibleItemPosition()
val offsetTop = if (top != 0) top else (visibleItemCount / 2) * mHeightCell
layoutManager.scrollToPositionWithOffset(position, offsetTop)
}
}
}

/*
* Save index and top position
*/
protected fun saveIndexAndTopPosition(index: Int) {
mIndexes.add(index)

val layoutManager = mRecyclerView?.layoutManager
val firstPosition: Int = if (layoutManager is GridLayoutManager) {
layoutManager.findFirstCompletelyVisibleItemPosition()
} else {
(layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition()
val firstPosition: Int = when (val layoutManager = mRecyclerView?.layoutManager) {
is GridLayoutManager -> layoutManager.findFirstCompletelyVisibleItemPosition()
is LinearLayoutManager -> layoutManager.findFirstCompletelyVisibleItemPosition()
else -> RecyclerView.NO_POSITION
}

mFirstPositions.add(firstPosition)
Expand All @@ -504,9 +512,7 @@ open class ExtendedListFragment :
mHeightCell = if (view == null || mHeightCell != 0) mHeightCell else view.height
}

override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
// to be @overridden
}
override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) = Unit

override fun onRefresh() {
if (searchView != null) {
Expand Down Expand Up @@ -803,8 +809,6 @@ open class ExtendedListFragment :
companion object {
protected val TAG: String = ExtendedListFragment::class.java.getSimpleName()

protected const val KEY_SAVED_LIST_POSITION: String = "SAVED_LIST_POSITION"

private const val KEY_INDEXES = "INDEXES"
private const val KEY_FIRST_POSITIONS = "FIRST_POSITIONS"
private const val KEY_TOPS = "TOPS"
Expand Down
Loading