Skip to content
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 @@ -6,11 +6,22 @@ import org.wordpress.android.R
enum class InsightsCardType(
@StringRes val displayNameResId: Int
) {
YEAR_IN_REVIEW(R.string.stats_insights_year_in_review);
YEAR_IN_REVIEW(
R.string.stats_insights_year_in_review
),
ALL_TIME_STATS(
R.string.stats_insights_all_time_stats_title
),
MOST_POPULAR_DAY(
R.string.stats_insights_most_popular_day
);

companion object {
fun defaultCards(): List<InsightsCardType> = listOf(
YEAR_IN_REVIEW
)
fun defaultCards(): List<InsightsCardType> =
listOf(
YEAR_IN_REVIEW,
ALL_TIME_STATS,
MOST_POPULAR_DAY
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package org.wordpress.android.ui.newstats

data class InsightsCardsConfiguration(
val visibleCards: List<InsightsCardType> =
InsightsCardType.defaultCards()
InsightsCardType.defaultCards(),
val hiddenCards: List<InsightsCardType> = emptyList()
) {
fun hiddenCards(): List<InsightsCardType> {
fun computeHiddenCards(): List<InsightsCardType> {
return InsightsCardType.entries
.filter { it !in visibleCards }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import javax.inject.Inject

@HiltViewModel
class InsightsViewModel @Inject constructor(
private val selectedSiteRepository: SelectedSiteRepository,
private val selectedSiteRepository:
SelectedSiteRepository,
private val cardConfigurationRepository:
InsightsCardsConfigurationRepository,
private val networkUtilsWrapper: NetworkUtilsWrapper
Expand Down Expand Up @@ -52,7 +53,8 @@ class InsightsViewModel @Inject constructor(
}

fun checkNetworkStatus(): Boolean {
val isAvailable = networkUtilsWrapper.isNetworkAvailable()
val isAvailable =
networkUtilsWrapper.isNetworkAvailable()
_isNetworkAvailable.value = isAvailable
return isAvailable
}
Expand Down Expand Up @@ -91,7 +93,7 @@ class InsightsViewModel @Inject constructor(
config: InsightsCardsConfiguration
) {
_visibleCards.value = config.visibleCards
_hiddenCards.value = config.hiddenCards()
_hiddenCards.value = config.computeHiddenCards()
_cardsToLoad.value = config.visibleCards
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@
import org.wordpress.android.ui.newstats.viewsstats.ViewsStatsCard
import org.wordpress.android.ui.newstats.viewsstats.ViewsStatsViewModel
import android.widget.Toast
import org.wordpress.android.ui.newstats.alltimestats.AllTimeStatsCard
import org.wordpress.android.ui.newstats.alltimestats.AllTimeStatsViewModel
import org.wordpress.android.ui.newstats.mostpopularday.MostPopularDayCard
import org.wordpress.android.ui.newstats.mostpopularday.MostPopularDayViewModel
import org.wordpress.android.ui.newstats.yearinreview.YearInReviewCard
import org.wordpress.android.ui.newstats.yearinreview.YearInReviewDetailActivity
import org.wordpress.android.ui.newstats.yearinreview.YearInReviewViewModel
Expand Down Expand Up @@ -874,16 +878,33 @@

@OptIn(ExperimentalMaterial3Api::class)
@Composable
@Suppress("LongMethod")
@Suppress("LongMethod", "LongParameterList")
private fun InsightsTabContent(

Check failure on line 882 in WordPress/src/main/java/org/wordpress/android/ui/newstats/NewStatsActivity.kt

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 18 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=wordpress-mobile_WordPress-Android&issues=AZzYsiK0bjN_7UHLhctz&open=AZzYsiK0bjN_7UHLhctz&pullRequest=22673
yearInReviewViewModel: YearInReviewViewModel = viewModel(),
yearInReviewViewModel: YearInReviewViewModel =
viewModel(),
allTimeStatsViewModel: AllTimeStatsViewModel =
viewModel(),
mostPopularDayViewModel: MostPopularDayViewModel =
viewModel(),
insightsViewModel: InsightsViewModel = viewModel()
) {
val context = LocalContext.current
val yearInReviewUiState by yearInReviewViewModel
.uiState.collectAsState()
val isRefreshing by yearInReviewViewModel
val allTimeStatsUiState by allTimeStatsViewModel
.uiState.collectAsState()
val mostPopularDayUiState by
mostPopularDayViewModel
.uiState.collectAsState()
val yearRefreshing by yearInReviewViewModel
.isRefreshing.collectAsState()
val allTimeRefreshing by allTimeStatsViewModel
.isRefreshing.collectAsState()
val popularDayRefreshing by
mostPopularDayViewModel
.isRefreshing.collectAsState()
val isRefreshing = yearRefreshing ||
allTimeRefreshing || popularDayRefreshing
val pullToRefreshState = rememberPullToRefreshState()

val visibleCards by insightsViewModel
Expand All @@ -900,7 +921,16 @@
LaunchedEffect(cardsToLoad) {
cardsToLoad.dispatchInsightsToVisibleCards(
onYearInReview = {
yearInReviewViewModel.loadDataIfNeeded()
yearInReviewViewModel
.loadDataIfNeeded()
},
onAllTimeStats = {
allTimeStatsViewModel
.loadDataIfNeeded()
},
onMostPopularDay = {
mostPopularDayViewModel
.loadDataIfNeeded()
}
)
}
Expand All @@ -922,7 +952,15 @@

val loadVisibleCards = {
visibleCards.dispatchInsightsToVisibleCards(
onYearInReview = { yearInReviewViewModel.loadData() }
onYearInReview = {
yearInReviewViewModel.loadData()
},
onAllTimeStats = {
allTimeStatsViewModel.loadData()
},
onMostPopularDay = {
mostPopularDayViewModel.loadData()
}
)
}

Expand Down Expand Up @@ -960,6 +998,12 @@
visibleCards.dispatchInsightsToVisibleCards(
onYearInReview = {
yearInReviewViewModel.refresh()
},
onAllTimeStats = {
allTimeStatsViewModel.refresh()
},
onMostPopularDay = {
mostPopularDayViewModel.refresh()
}
)
},
Expand Down Expand Up @@ -1009,6 +1053,78 @@
visibleCards.forEachIndexed { index, cardType ->
val cardPosition = cardPositions[index]
when (cardType) {
InsightsCardType.ALL_TIME_STATS ->
AllTimeStatsCard(
uiState = allTimeStatsUiState,
onRemoveCard = {
insightsViewModel
.removeCard(cardType)
},
onRetry = {
allTimeStatsViewModel
.onRetry()
},
cardPosition = cardPosition,
onMoveUp = {
insightsViewModel
.moveCardUp(cardType)
},
onMoveToTop = {
insightsViewModel
.moveCardToTop(cardType)
},
onMoveDown = {
insightsViewModel
.moveCardDown(cardType)
},
onMoveToBottom = {
insightsViewModel
.moveCardToBottom(
cardType
)
}
)
InsightsCardType.MOST_POPULAR_DAY ->
MostPopularDayCard(
uiState =
mostPopularDayUiState,
onRemoveCard = {
insightsViewModel
.removeCard(
cardType
)
},
onRetry = {
mostPopularDayViewModel
.onRetry()
},
cardPosition =
cardPosition,
onMoveUp = {
insightsViewModel
.moveCardUp(
cardType
)
},
onMoveToTop = {
insightsViewModel
.moveCardToTop(
cardType
)
},
onMoveDown = {
insightsViewModel
.moveCardDown(
cardType
)
},
onMoveToBottom = {
insightsViewModel
.moveCardToBottom(
cardType
)
}
)
InsightsCardType.YEAR_IN_REVIEW ->
YearInReviewCard(
uiState = yearInReviewUiState,
Expand Down Expand Up @@ -1053,11 +1169,19 @@
}

private fun List<InsightsCardType>.dispatchInsightsToVisibleCards(
onYearInReview: () -> Unit
onYearInReview: () -> Unit,
onAllTimeStats: () -> Unit,
onMostPopularDay: () -> Unit
) {
if (InsightsCardType.YEAR_IN_REVIEW in this) {
onYearInReview()
}
if (InsightsCardType.ALL_TIME_STATS in this) {
onAllTimeStats()
}
if (InsightsCardType.MOST_POPULAR_DAY in this) {
onMostPopularDay()
}
}

@OptIn(ExperimentalMaterial3Api::class)
Expand Down
Loading
Loading