Skip to content

[Dynamic Dashboard] Navigate to reviews details from reviews card #11503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -2,6 +2,7 @@ package com.woocommerce.android.ui.dashboard.reviews

import android.widget.RatingBar
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand Down Expand Up @@ -47,7 +48,6 @@ import com.woocommerce.android.ui.dashboard.DashboardViewModel.DashboardWidgetMe
import com.woocommerce.android.ui.dashboard.WidgetCard
import com.woocommerce.android.ui.dashboard.WidgetError
import com.woocommerce.android.ui.dashboard.defaultHideMenuEntry
import com.woocommerce.android.ui.dashboard.reviews.DashboardReviewsViewModel.OpenReviewsList
import com.woocommerce.android.ui.reviews.ProductReviewStatus
import com.woocommerce.android.util.StringUtils
import com.woocommerce.android.viewmodel.MultiLiveEvent
Expand All @@ -68,6 +68,7 @@ fun DashboardReviewsCard(
onHideClicked = { parentViewModel.onHideWidgetClicked(DashboardWidget.Type.REVIEWS) },
onFilterSelected = viewModel::onFilterSelected,
onViewAllClicked = viewModel::onViewAllClicked,
onReviewClicked = viewModel::onReviewClicked,
onContactSupportClicked = parentViewModel::onContactSupportClicked,
onRetryClicked = viewModel::onRetryClicked,
modifier = modifier
Expand All @@ -83,9 +84,23 @@ private fun HandleEvents(event: LiveData<MultiLiveEvent.Event>) {
DisposableEffect(event, navController, lifecycleOwner) {
val observer = Observer { event: MultiLiveEvent.Event ->
when (event) {
OpenReviewsList -> navController.navigateSafely(
is DashboardReviewsViewModel.OpenReviewsList -> navController.navigateSafely(
DashboardFragmentDirections.actionDashboardToReviews()
)
is DashboardReviewsViewModel.OpenReviewDetail -> {
// Open the review list screen first as it's responsible for handling review status changes
navController.navigateSafely(
DashboardFragmentDirections.actionDashboardToReviews()
)
// Continue to the details screen
navController.navigateSafely(
directions = DashboardFragmentDirections.actionGlobalReviewDetailFragment(
launchedFromNotification = false,
remoteReviewId = event.review.remoteId
),
skipThrottling = true
)
Comment on lines +91 to +102
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ReviewListFragment is responsible for handling the undo logic of the reviews' operations (moderation and deletion), so to keep this logic while navigating from the reviews card, we needed to either:

  1. Implement the same logic at the dashboard level.
  2. Navigate to the list before showing the details.

For simplicity, I followed 2, and it works well, please let me know what you think.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice hack!

}
}
}

Expand All @@ -103,6 +118,7 @@ private fun DashboardReviewsCard(
onHideClicked: () -> Unit,
onFilterSelected: (ProductReviewStatus) -> Unit,
onViewAllClicked: () -> Unit,
onReviewClicked: (ProductReview) -> Unit,
onContactSupportClicked: () -> Unit,
onRetryClicked: () -> Unit,
modifier: Modifier
Expand Down Expand Up @@ -132,7 +148,8 @@ private fun DashboardReviewsCard(
is DashboardReviewsViewModel.ViewState.Success -> {
ProductReviewsCardContent(
viewState = viewState,
onFilterSelected = onFilterSelected
onFilterSelected = onFilterSelected,
onReviewClicked = onReviewClicked
)
}

Expand All @@ -150,6 +167,7 @@ private fun DashboardReviewsCard(
private fun ProductReviewsCardContent(
viewState: DashboardReviewsViewModel.ViewState.Success,
onFilterSelected: (ProductReviewStatus) -> Unit,
onReviewClicked: (ProductReview) -> Unit,
modifier: Modifier = Modifier
) {
Column(modifier) {
Expand All @@ -161,7 +179,8 @@ private fun ProductReviewsCardContent(
viewState.reviews.forEachIndexed { index, productReview ->
ReviewListItem(
review = productReview,
showDivider = index < viewState.reviews.size - 1
showDivider = index < viewState.reviews.size - 1,
onClicked = { onReviewClicked(productReview) }
)
}
}
Expand All @@ -172,11 +191,15 @@ private fun ProductReviewsCardContent(
private fun ReviewListItem(
review: ProductReview,
showDivider: Boolean,
onClicked: () -> Unit,
modifier: Modifier = Modifier
) {
Row(
horizontalArrangement = Arrangement.spacedBy(16.dp),
modifier = modifier.padding(horizontal = 16.dp, vertical = 8.dp)
modifier = modifier
.fillMaxWidth()
.clickable(onClick = onClicked)
.padding(horizontal = 16.dp, vertical = 8.dp)
) {
Icon(
painter = painterResource(id = R.drawable.ic_comment),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.woocommerce.android.model.ProductReview
import com.woocommerce.android.ui.dashboard.DashboardViewModel
import com.woocommerce.android.ui.reviews.ProductReviewStatus
import com.woocommerce.android.ui.reviews.ReviewListRepository
import com.woocommerce.android.ui.reviews.ReviewModerationHandler
import com.woocommerce.android.ui.reviews.ReviewModerationStatus
import com.woocommerce.android.viewmodel.MultiLiveEvent
import com.woocommerce.android.viewmodel.ScopedViewModel
import com.woocommerce.android.viewmodel.getStateFlow
Expand All @@ -15,29 +17,33 @@ import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.transformLatest
import kotlinx.coroutines.launch

@HiltViewModel(assistedFactory = DashboardReviewsViewModel.Factory::class)
class DashboardReviewsViewModel @AssistedInject constructor(
savedStateHandle: SavedStateHandle,
@Assisted private val parentViewModel: DashboardViewModel,
private val reviewListRepository: ReviewListRepository
private val reviewListRepository: ReviewListRepository,
private val reviewModerationHandler: ReviewModerationHandler
) : ScopedViewModel(savedStateHandle) {
companion object {
val supportedFilters = listOf(
ProductReviewStatus.ALL,
ProductReviewStatus.APPROVED,
ProductReviewStatus.HOLD,
ProductReviewStatus.SPAM
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As explained here p1715612834620439/1715264194.676369-slack-C03L1NF1EA3, supporting the spam filter will require more work, so let's remove it for now.

ProductReviewStatus.HOLD
)
}

private val _refreshTrigger = MutableSharedFlow<DashboardViewModel.RefreshEvent>(extraBufferCapacity = 1)
private val refreshTrigger = merge(parentViewModel.refreshTrigger, _refreshTrigger)
.onStart { emit(DashboardViewModel.RefreshEvent()) }
Expand Down Expand Up @@ -72,40 +78,73 @@ class DashboardReviewsViewModel @AssistedInject constructor(
triggerEvent(OpenReviewsList)
}

fun onReviewClicked(review: ProductReview) {
triggerEvent(OpenReviewDetail(review))
}

fun onRetryClicked() {
_refreshTrigger.tryEmit(DashboardViewModel.RefreshEvent())
}

private fun observeMostRecentReviews(
forceRefresh: Boolean,
status: ProductReviewStatus
) = flow<Result<List<ProductReview>>> {
val fetchBeforeEmit = forceRefresh || getCachedReviews(status).isEmpty()
) = channelFlow<Result<List<ProductReview>>> {
val fetchBeforeEmit = forceRefresh || observeCachedReviews(status).first().isEmpty()

if (fetchBeforeEmit) {
reviewListRepository.fetchMostRecentReviews(status)
.onFailure {
emit(Result.failure(it))
return@flow
send(Result.failure(it))
return@channelFlow
}
}

emit(Result.success(getCachedReviews(status)))
coroutineScope {
val cacheJob = launch {
observeCachedReviews(status)
.collect { cachedReviews ->
send(Result.success(cachedReviews))
}
}

if (!fetchBeforeEmit) {
reviewListRepository.fetchMostRecentReviews(status)
.onFailure {
emit(Result.failure(it))
}
emit(Result.success(getCachedReviews(status)))
if (!fetchBeforeEmit) {
reviewListRepository.fetchMostRecentReviews(status)
.onFailure {
cacheJob.cancel()
send(Result.failure(it))
}
}
}
}

@Suppress("MagicNumber")
private suspend fun getCachedReviews(status: ProductReviewStatus) =
reviewListRepository.getCachedProductReviews()
.filter { status == ProductReviewStatus.ALL || it.status == status.toString() }
.take(3)
private suspend fun observeCachedReviews(status: ProductReviewStatus) =
reviewModerationHandler.pendingModerationStatus.map { moderationStatus ->
val cachedReviews = reviewListRepository.getCachedProductReviews()
.filter { status == ProductReviewStatus.ALL || it.status == status.toString() }
// We need just 3 review, but we will take an additional review to account for
// any pending moderation requests
.take(4)

cachedReviews.applyModerationStatus(moderationStatus)
.take(3)
}

private fun List<ProductReview>.applyModerationStatus(
moderationStatus: List<ReviewModerationStatus>
): List<ProductReview> {
return map { review ->
val status = moderationStatus.firstOrNull { it.review.remoteId == review.remoteId }
if (status != null) {
review.copy(status = status.newStatus.toString())
} else {
review
}
}.filter {
it.status != ProductReviewStatus.TRASH.toString() && it.status != ProductReviewStatus.SPAM.toString()
}
}

sealed interface ViewState {
data class Loading(val selectedFilter: ProductReviewStatus) : ViewState
Expand All @@ -118,6 +157,7 @@ class DashboardReviewsViewModel @AssistedInject constructor(
}

data object OpenReviewsList : MultiLiveEvent.Event()
data class OpenReviewDetail(val review: ProductReview) : MultiLiveEvent.Event()

@AssistedFactory
interface Factory {
Expand Down
2 changes: 1 addition & 1 deletion WooCommerce/src/main/res/navigation/nav_graph_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@
app:argType="long" />
<argument
android:name="tempStatus"
android:defaultValue="null"
android:defaultValue="@null"
app:argType="string"
app:nullable="true" />
<argument
Expand Down
Loading