Optimize ViewPostDetailFragmentNew.UiState and it's observer in ViewPostDetailFragmentNew.

This commit is contained in:
Docile-Alligator
2026-06-24 12:59:48 -04:00
parent 8cdea4b66c
commit ff3398908b
3 changed files with 28 additions and 35 deletions

View File

@ -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
}

View File

@ -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);
}

View File

@ -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
)