From ff3398908bcd271b2507a4c72e16f4011e14fa9f Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Wed, 24 Jun 2026 12:59:48 -0400 Subject: [PATCH] Optimize ViewPostDetailFragmentNew.UiState and it's observer in ViewPostDetailFragmentNew. --- .../CommentsStatusRecyclerViewAdapter.kt | 12 ++++----- .../fragments/ViewPostDetailFragmentNew.java | 26 +++++++------------ .../ViewPostDetailFragmentViewModelNew.kt | 25 +++++++++--------- 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsStatusRecyclerViewAdapter.kt b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsStatusRecyclerViewAdapter.kt index dee937e7..58ea7d4b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsStatusRecyclerViewAdapter.kt +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsStatusRecyclerViewAdapter.kt @@ -29,8 +29,8 @@ class CommentsStatusRecyclerViewAdapter( } var isSingleCommentThreadMode: Boolean = false - var isInitiallyLoading: Boolean = false - var isInitiallyLoadingFailed: Boolean = false + var isInitialLoading: Boolean = false + var isInitialLoadingFailed: Boolean = false var emptyComments: Boolean = false private val circularProgressBarBackgroundColor = activity.customThemeWrapper.circularProgressBarBackground @@ -39,9 +39,9 @@ class CommentsStatusRecyclerViewAdapter( private val commentBackgroundColor = activity.customThemeWrapper.commentBackgroundColor override fun getItemViewType(position: Int): Int { - if (isInitiallyLoading) { + if (isInitialLoading) { return VIEW_TYPE_FIRST_LOADING - } else if (isInitiallyLoadingFailed) { + } else if (isInitialLoadingFailed) { if (isSingleCommentThreadMode && position == 0) { return Constants.VIEW_TYPE_VIEW_ALL_COMMENTS } @@ -110,11 +110,11 @@ class CommentsStatusRecyclerViewAdapter( } override fun getItemCount(): Int { - if (isInitiallyLoading) { + if (isInitialLoading) { return 1 } - if (isInitiallyLoadingFailed || emptyComments) { + if (isInitialLoadingFailed || emptyComments) { return if (isSingleCommentThreadMode) 2 else 1 } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragmentNew.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragmentNew.java index 31c05019..12b8e264 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragmentNew.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragmentNew.java @@ -532,24 +532,25 @@ public class ViewPostDetailFragmentNew extends Fragment implements FragmentCommu viewPostDetailFragmentViewModel.getUiState().observe(getViewLifecycleOwner(), uiState -> { RecyclerView recyclerView = mCommentsRecyclerView != null ? mCommentsRecyclerView : binding.postDetailRecyclerViewViewPostDetailFragment; mCommentsStatusAdapter.setSingleCommentThreadMode(uiState.getSingleCommentId() != null && !uiState.getSingleCommentId().isEmpty()); - mCommentsStatusAdapter.setInitiallyLoading(uiState.isInitialLoading()); - mCommentsStatusAdapter.setInitiallyLoadingFailed(uiState.isInitialLoadingFailed()); + mCommentsStatusAdapter.setInitialLoading(uiState.isInitialLoading()); + mCommentsStatusAdapter.setInitialLoadingFailed(uiState.isInitialLoadingFailed()); recyclerView.post(() -> mCommentsStatusAdapter.notifyDataSetChanged()); mCommentsFooterAdapter.setLoadingMoreChildren(uiState.isLoadingMoreChildren()); mCommentsFooterAdapter.setLoadMoreChildrenSuccess(uiState.getLoadMoreChildrenSuccess()); recyclerView.post(() -> mCommentsFooterAdapter.notifyDataSetChanged()); + binding.swipeRefreshLayoutViewPostDetailFragment.setRefreshing((mPost == null && uiState.isInitialLoading()) || uiState.isRefreshing()); + if (uiState.isInitialLoading()) { binding.fetchPostInfoLinearLayoutViewPostDetailFragment.setVisibility(View.GONE); - binding.swipeRefreshLayoutViewPostDetailFragment.setRefreshing(true); mGlide.clear(binding.fetchPostInfoImageViewViewPostDetailFragment); - } else { - binding.swipeRefreshLayoutViewPostDetailFragment.setRefreshing(false); + mCommentsAdapter.initiallyLoading(); + } else { if (uiState.getShouldShowErrorView()) { showErrorView(viewPostDetailFragmentViewModel.getDerivedPostId()); - } else { + } else if (!uiState.isInitialLoadingFailed()) { if (!renderContent()) { return; } @@ -562,17 +563,9 @@ public class ViewPostDetailFragmentNew extends Fragment implements FragmentCommu showMessage(R.string.refresh_post_failed); } - if (uiState.isFetchingComments()) { - if (mCommentsAdapter != null) { - mCommentsAdapter.initiallyLoading(); - } - } - if (uiState.getSortType() != null) { SortType.Type sortType = uiState.getSortType(); mActivity.setTitle(sortType.fullName); - binding.fetchPostInfoLinearLayoutViewPostDetailFragment.setVisibility(View.GONE); - mGlide.clear(binding.fetchPostInfoImageViewViewPostDetailFragment); if (mSharedPreferences.getBoolean(SharedPreferencesUtils.SAVE_SORT_TYPE, true)) { mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_POST_COMMENT, sortType.name()).apply(); @@ -591,7 +584,6 @@ public class ViewPostDetailFragmentNew extends Fragment implements FragmentCommu } EventBus.getDefault().post(new PostUpdateEventToPostList(dataState.getPost(), postListPosition)); setupMenu(); - binding.swipeRefreshLayoutViewPostDetailFragment.setRefreshing(false); } comments = dataState.getComments(); @@ -664,6 +656,8 @@ public class ViewPostDetailFragmentNew extends Fragment implements FragmentCommu return new PlaybackInfo(INDEX_UNSET, TIME_UNSET, volumeInfo); }); + binding.fetchPostInfoLinearLayoutViewPostDetailFragment.setOnClickListener(view -> viewPostDetailFragmentViewModel.fetchPostAndCommentsById(postId)); + viewPostDetailFragmentViewModel.getPostModerationEventLiveData().observe(getViewLifecycleOwner(), moderationEvent -> { showMessage(moderationEvent.getToastMessageResId()); @@ -1140,9 +1134,7 @@ public class ViewPostDetailFragmentNew extends Fragment implements FragmentCommu } private void showErrorView(String postId) { - binding.swipeRefreshLayoutViewPostDetailFragment.setRefreshing(false); binding.fetchPostInfoLinearLayoutViewPostDetailFragment.setVisibility(View.VISIBLE); - binding.fetchPostInfoLinearLayoutViewPostDetailFragment.setOnClickListener(view -> viewPostDetailFragmentViewModel.fetchPostAndCommentsById(postId)); binding.fetchPostInfoTextViewViewPostDetailFragment.setText(R.string.load_post_error); mGlide.load(R.drawable.error_image).into(binding.fetchPostInfoImageViewViewPostDetailFragment); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/viewmodels/ViewPostDetailFragmentViewModelNew.kt b/app/src/main/java/ml/docilealligator/infinityforreddit/viewmodels/ViewPostDetailFragmentViewModelNew.kt index 85ea3e3d..603eb5bc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/viewmodels/ViewPostDetailFragmentViewModelNew.kt +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/viewmodels/ViewPostDetailFragmentViewModelNew.kt @@ -77,7 +77,6 @@ class ViewPostDetailFragmentViewModelNew( val isInitialLoading: Boolean, val isInitialLoadingFailed: Boolean, val fetchPostFailed: Boolean, - val isFetchingComments: Boolean, val isRefreshing: Boolean, val isLoadingMoreChildren: Boolean, val loadMoreChildrenSuccess: Boolean, @@ -113,7 +112,6 @@ class ViewPostDetailFragmentViewModelNew( isInitialLoading = false, isInitialLoadingFailed = false, fetchPostFailed = false, - isFetchingComments = false, isRefreshing = false, isLoadingMoreChildren = false, loadMoreChildrenSuccess = true, @@ -277,7 +275,7 @@ class ViewPostDetailFragmentViewModelNew( _uiState.value = _uiState.value.copy( isInitialLoading = true, isInitialLoadingFailed = false, - isFetchingComments = true, + fetchPostFailed = false, shouldShowErrorView = false ) @@ -286,7 +284,6 @@ class ViewPostDetailFragmentViewModelNew( _uiState.value = _uiState.value.copy( isInitialLoading = false, isInitialLoadingFailed = true, - isFetchingComments = false, isRefreshing = if (changeRefreshState) false else _uiState.value.isRefreshing ) return @@ -326,7 +323,6 @@ class ViewPostDetailFragmentViewModelNew( _uiState.value = _uiState.value.copy( isInitialLoading = false, isInitialLoadingFailed = false, - isFetchingComments = false, isRefreshing = if (changeRefreshState) false else _uiState.value.isRefreshing ) _dataState.value = _dataState.value.copy( @@ -338,7 +334,6 @@ class ViewPostDetailFragmentViewModelNew( _uiState.value = _uiState.value.copy( isInitialLoading = false, isInitialLoadingFailed = true, - isFetchingComments = false, isRefreshing = if (changeRefreshState) false else _uiState.value.isRefreshing ) } @@ -347,7 +342,6 @@ class ViewPostDetailFragmentViewModelNew( _uiState.value = _uiState.value.copy( isInitialLoading = false, isInitialLoadingFailed = true, - isFetchingComments = false, isRefreshing = if (changeRefreshState) false else _uiState.value.isRefreshing ) } @@ -356,7 +350,6 @@ class ViewPostDetailFragmentViewModelNew( _uiState.value = _uiState.value.copy( isInitialLoading = false, isInitialLoadingFailed = true, - isFetchingComments = false, isRefreshing = if (changeRefreshState) false else _uiState.value.isRefreshing ) } @@ -371,7 +364,7 @@ class ViewPostDetailFragmentViewModelNew( _uiState.value = _uiState.value.copy( isInitialLoading = true, isInitialLoadingFailed = false, - isFetchingComments = true, + fetchPostFailed = false, shouldShowErrorView = false ) @@ -439,8 +432,7 @@ class ViewPostDetailFragmentViewModelNew( ) _uiState.value = _uiState.value.copy( isInitialLoading = false, - isInitialLoadingFailed = false, - isFetchingComments = false + isInitialLoadingFailed = false ) } is AppResult.Error<*> -> { @@ -453,22 +445,30 @@ class ViewPostDetailFragmentViewModelNew( } } ?: run { _uiState.value = _uiState.value.copy( + isInitialLoading = false, + isInitialLoadingFailed = true, shouldShowErrorView = true ) } } else { _uiState.value = _uiState.value.copy( + isInitialLoading = false, + isInitialLoadingFailed = true, shouldShowErrorView = true ) } } catch (e: Exception) { e.printStackTrace() _uiState.value = _uiState.value.copy( + isInitialLoading = false, + isInitialLoadingFailed = true, shouldShowErrorView = true ) } } ?: run { _uiState.value = _uiState.value.copy( + isInitialLoading = false, + isInitialLoadingFailed = true, shouldShowErrorView = true ) } @@ -477,7 +477,7 @@ class ViewPostDetailFragmentViewModelNew( fun fetchMoreComments() { viewModelScope.launch { - if (_uiState.value.isFetchingComments || _uiState.value.isLoadingMoreChildren || !_uiState.value.loadMoreChildrenSuccess) { + if (_uiState.value.isInitialLoading || _uiState.value.isLoadingMoreChildren || !_uiState.value.loadMoreChildrenSuccess) { return@launch } @@ -803,6 +803,7 @@ class ViewPostDetailFragmentViewModelNew( if (!_uiState.value.isRefreshing) { _uiState.value = _uiState.value.copy( isRefreshing = true, + fetchPostFailed = false, shouldShowErrorView = false )