-
Notifications
You must be signed in to change notification settings - Fork 132
[Dynamic Dashboard] Reviews card: business logic #11477
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
6 commits
Select commit
Hold shift + click to select a range
d0ebd8e
Declare a new dashboard type for reviews
hichamboushaba beb00d9
Add classes for the reviews card
hichamboushaba 205ec7b
Logic of fetching and updating the view state of the reviews card
hichamboushaba aedb35d
Refactor notifications fetch to dispatchAndAwait
hichamboushaba 5b1befd
Fix detekt issues
hichamboushaba f873268
Merge branch 'trunk' into issue/11474-dynamic-dashboard-reviews-card
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
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
64 changes: 64 additions & 0 deletions
64
...erce/src/main/kotlin/com/woocommerce/android/ui/dashboard/reviews/DashboardReviewsCard.kt
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.woocommerce.android.ui.dashboard.reviews | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.material.CircularProgressIndicator | ||
import androidx.compose.material.Divider | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.livedata.observeAsState | ||
import androidx.compose.ui.Modifier | ||
import com.woocommerce.android.model.DashboardWidget | ||
import com.woocommerce.android.ui.compose.viewModelWithFactory | ||
import com.woocommerce.android.ui.dashboard.DashboardViewModel | ||
import com.woocommerce.android.ui.dashboard.DashboardViewModel.DashboardWidgetMenu | ||
import com.woocommerce.android.ui.dashboard.WidgetCard | ||
import com.woocommerce.android.ui.dashboard.WidgetError | ||
import com.woocommerce.android.ui.dashboard.defaultHideMenuEntry | ||
|
||
@Composable | ||
fun DashboardReviewsCard( | ||
parentViewModel: DashboardViewModel, | ||
modifier: Modifier = Modifier, | ||
viewModel: DashboardReviewsViewModel = viewModelWithFactory { factory: DashboardReviewsViewModel.Factory -> | ||
factory.create(parentViewModel = parentViewModel) | ||
} | ||
) { | ||
WidgetCard( | ||
titleResource = DashboardWidget.Type.REVIEWS.titleResource, | ||
menu = DashboardWidgetMenu( | ||
listOf( | ||
DashboardWidget.Type.REVIEWS.defaultHideMenuEntry { | ||
parentViewModel.onHideWidgetClicked( | ||
DashboardWidget.Type.REVIEWS | ||
) | ||
} | ||
) | ||
), | ||
isError = false, | ||
modifier = modifier | ||
) { | ||
viewModel.viewState.observeAsState().value?.let { viewState -> | ||
when (viewState) { | ||
is DashboardReviewsViewModel.ViewState.Loading -> { | ||
CircularProgressIndicator() | ||
} | ||
|
||
is DashboardReviewsViewModel.ViewState.Success -> { | ||
Column { | ||
viewState.reviews.forEach { review -> | ||
Text(text = review.review) | ||
Divider() | ||
} | ||
} | ||
} | ||
|
||
is DashboardReviewsViewModel.ViewState.Error -> { | ||
WidgetError( | ||
onContactSupportClicked = { /*TODO*/ }, | ||
onRetryClicked = { /*TODO*/ } | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...src/main/kotlin/com/woocommerce/android/ui/dashboard/reviews/DashboardReviewsViewModel.kt
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.woocommerce.android.ui.dashboard.reviews | ||
|
||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.asLiveData | ||
import androidx.lifecycle.viewModelScope | ||
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.viewmodel.ScopedViewModel | ||
import com.woocommerce.android.viewmodel.getStateFlow | ||
import dagger.assisted.Assisted | ||
import dagger.assisted.AssistedFactory | ||
import dagger.assisted.AssistedInject | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.combine | ||
import kotlinx.coroutines.flow.emitAll | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.onStart | ||
import kotlinx.coroutines.flow.transformLatest | ||
|
||
@HiltViewModel(assistedFactory = DashboardReviewsViewModel.Factory::class) | ||
class DashboardReviewsViewModel @AssistedInject constructor( | ||
savedStateHandle: SavedStateHandle, | ||
@Assisted private val parentViewModel: DashboardViewModel, | ||
private val reviewListRepository: ReviewListRepository | ||
) : ScopedViewModel(savedStateHandle) { | ||
private val refreshTrigger = parentViewModel.refreshTrigger | ||
.onStart { emit(DashboardViewModel.RefreshEvent()) } | ||
private val status = savedStateHandle.getStateFlow(viewModelScope, ProductReviewStatus.ALL) | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
val viewState = combine(refreshTrigger, status) { refresh, status -> Pair(refresh, status) } | ||
.transformLatest { (refresh, status) -> | ||
emit(ViewState.Loading) | ||
emitAll( | ||
observeMostRecentReviews(forceRefresh = refresh.isForced, status = status) | ||
.map { result -> | ||
result.fold( | ||
onSuccess = { reviews -> | ||
ViewState.Success(reviews) | ||
}, | ||
onFailure = { ViewState.Error } | ||
) | ||
} | ||
) | ||
} | ||
.asLiveData() | ||
|
||
private fun observeMostRecentReviews( | ||
forceRefresh: Boolean, | ||
status: ProductReviewStatus | ||
) = flow<Result<List<ProductReview>>> { | ||
if (forceRefresh) { | ||
reviewListRepository.fetchMostRecentReviews(status) | ||
.onFailure { | ||
emit(Result.failure(it)) | ||
return@flow | ||
} | ||
} | ||
|
||
emit(Result.success(getCachedReviews(status))) | ||
|
||
if (!forceRefresh) { | ||
reviewListRepository.fetchMostRecentReviews(status) | ||
.onFailure { | ||
emit(Result.failure(it)) | ||
} | ||
emit(Result.success(getCachedReviews(status))) | ||
} | ||
} | ||
|
||
@Suppress("MagicNumber") | ||
private suspend fun getCachedReviews(status: ProductReviewStatus) = | ||
reviewListRepository.getCachedProductReviews() | ||
.filter { status == ProductReviewStatus.ALL || it.status == status.toString() } | ||
.take(3) | ||
|
||
sealed interface ViewState { | ||
data object Loading : ViewState | ||
data class Success(val reviews: List<ProductReview>) : ViewState | ||
data object Error : ViewState | ||
} | ||
|
||
@AssistedFactory | ||
interface Factory { | ||
fun create(parentViewModel: DashboardViewModel): DashboardReviewsViewModel | ||
} | ||
} |
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
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.
This logic follows approach
2
on what we decided, as it's quite simple to implement for cards with existing caching, I think it would make sense to use it even if iOS team don't, WDYT?