From b2c87f221ec25a207278fd936fed3fc343945bb7 Mon Sep 17 00:00:00 2001 From: Taras Smakula <tarassmakula@gmail.com> Date: Tue, 19 Dec 2023 17:49:03 +0200 Subject: [PATCH] Scroll to new created post --- .../timeline/TimelineDialogFragment.kt | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/futo/circles/feature/timeline/TimelineDialogFragment.kt b/app/src/main/java/org/futo/circles/feature/timeline/TimelineDialogFragment.kt index 817fea499..dd6126b13 100644 --- a/app/src/main/java/org/futo/circles/feature/timeline/TimelineDialogFragment.kt +++ b/app/src/main/java/org/futo/circles/feature/timeline/TimelineDialogFragment.kt @@ -7,6 +7,7 @@ import androidx.fragment.app.viewModels import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.navArgs import androidx.recyclerview.widget.DividerItemDecoration +import androidx.recyclerview.widget.RecyclerView import com.google.android.material.badge.BadgeDrawable import com.google.android.material.badge.BadgeUtils import com.google.android.material.badge.ExperimentalBadgeUtils @@ -15,6 +16,7 @@ import org.futo.circles.R import org.futo.circles.core.base.NetworkObserver import org.futo.circles.core.base.fragment.BaseFullscreenDialogFragment import org.futo.circles.core.extensions.getCurrentUserPowerLevel +import org.futo.circles.core.extensions.gone import org.futo.circles.core.extensions.isCurrentUserAbleToPost import org.futo.circles.core.extensions.observeData import org.futo.circles.core.extensions.observeResponse @@ -74,8 +76,17 @@ class TimelineDialogFragment : BaseFullscreenDialogFragment(DialogFragmentTimeli getCurrentUserPowerLevel(args.roomId), this, isThread - ) { loadMoreDebounce(Unit) }.apply { setHasStableIds(true) } + ) { loadMoreDebounce(Unit) }.apply { + setHasStableIds(true) + registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() { + override fun onItemRangeInserted(positionStart: Int, itemCount: Int) { + super.onItemRangeInserted(positionStart, itemCount) + scrollToTopIfMyNewPostAdded(positionStart, itemCount) + } + }) + } } + private val navigator by lazy { TimelineNavigator(this) } private val knocksCountBadgeDrawable by lazy { BadgeDrawable.create(requireContext()).apply { @@ -279,6 +290,23 @@ class TimelineDialogFragment : BaseFullscreenDialogFragment(DialogFragmentTimeli viewModel.sendReaction(roomId, eventId, emoji) } + private fun scrollToTopIfMyNewPostAdded(positionStart: Int, itemCount: Int) { + val items = viewModel.timelineEventsLiveData.value ?: emptyList() + if (itemCount != 1) return + if (isThread) { + val lastItemPosition = items.size - 1 + if (items.lastOrNull()?.isMyPost() == true && positionStart == lastItemPosition) { + binding.rvTimeline.layoutManager?.scrollToPosition(lastItemPosition) + binding.lCreatePost.gone() + } + } else { + if (items.firstOrNull()?.isMyPost() == true && positionStart == 0) + binding.rvTimeline.layoutManager?.scrollToPosition(0) + } + + + } + private fun onUserAccessLevelChanged(powerLevelsContent: PowerLevelsContent) { if (isGroupMode) onGroupUserAccessLevelChanged(powerLevelsContent) else onCircleUserAccessLeveChanged(powerLevelsContent) -- GitLab