-
Notifications
You must be signed in to change notification settings - Fork 132
[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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3c8e903
Fix value of null in the navigation graph
hichamboushaba 48128a9
Handle navigation to the review details screen
hichamboushaba c013402
Observer moderation status and update status of reviews on the card
hichamboushaba 442c79f
Remove the Spam option from the dropdown
hichamboushaba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As explained here p1715612834620439/1715264194.676369-slack-C03L1NF1EA3, supporting the |
||
ProductReviewStatus.HOLD | ||
) | ||
} | ||
|
||
private val _refreshTrigger = MutableSharedFlow<DashboardViewModel.RefreshEvent>(extraBufferCapacity = 1) | ||
private val refreshTrigger = merge(parentViewModel.refreshTrigger, _refreshTrigger) | ||
.onStart { emit(DashboardViewModel.RefreshEvent()) } | ||
|
@@ -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 | ||
|
@@ -118,6 +157,7 @@ class DashboardReviewsViewModel @AssistedInject constructor( | |
} | ||
|
||
data object OpenReviewsList : MultiLiveEvent.Event() | ||
data class OpenReviewDetail(val review: ProductReview) : MultiLiveEvent.Event() | ||
|
||
@AssistedFactory | ||
interface Factory { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theundo
logic of the reviews' operations (moderation and deletion), so to keep this logic while navigating from the reviews card, we needed to either:For simplicity, I followed
2
, and it works well, please let me know what you think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice hack!