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 @@ -87,6 +87,7 @@ class ViewsAndVisitorsDetailUseCase constructor(
site: SiteModel,
forced: Boolean
): State<VisitsAndViewsModel> {
AppLog.d(T.STATS, selectedDate.toString() )
val response = visitsAndViewsStore.fetchVisits(
site,
DAYS,
Expand Down Expand Up @@ -147,7 +148,7 @@ class ViewsAndVisitorsDetailUseCase constructor(
val selectedItem = domainModel.dates.getOrNull(index) ?: domainModel.dates.last()

items.add(
viewsAndVisitorsMapper.buildTitle(
viewsAndVisitorsMapper.buildWeekTitle(
domainModel.dates,
DAYS,
selectedItem,
Expand All @@ -165,7 +166,7 @@ class ViewsAndVisitorsDetailUseCase constructor(
)
)
items.add(
viewsAndVisitorsMapper.buildInformation(
viewsAndVisitorsMapper.buildWeeksDetailInformation(
domainModel.dates,
uiState.selectedPosition,
this::onTopTipsLinkClick
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.wordpress.android.ui.stats.refresh.lists.sections.insights.usecases.T
import org.wordpress.android.ui.stats.refresh.lists.sections.insights.usecases.TotalStatsMapper.TotalStatsType.LIKES
import org.wordpress.android.ui.stats.refresh.utils.MILLION
import org.wordpress.android.ui.stats.refresh.utils.StatsUtils
import org.wordpress.android.util.AppLog
import org.wordpress.android.viewmodel.ResourceProvider
import javax.inject.Inject

Expand All @@ -16,10 +17,10 @@ class TotalStatsMapper @Inject constructor(
private val statsUtils: StatsUtils
) {
fun buildTotalLikesValue(dates: List<PeriodData>): ValueWithChartItem {
val currentWeekLikes = getCurrentWeekDays(dates, LIKES)
val currentWeekLikes = getCurrentSevenDays(dates, LIKES)
val currentWeekSumFormatted = sum(currentWeekLikes)

val previousWeekLikes = getPreviousWeekDays(dates, LIKES)
val previousWeekLikes = getPreviousSevenDays(dates, LIKES)
val positive = currentWeekLikes.sum() >= previousWeekLikes.sum()

return ValueWithChartItem(
Expand All @@ -32,10 +33,10 @@ class TotalStatsMapper @Inject constructor(
}

fun buildTotalCommentsValue(dates: List<PeriodData>): ValueWithChartItem {
val currentWeekComments = getCurrentWeekDays(dates, COMMENTS)
val currentWeekComments = getCurrentSevenDays(dates, COMMENTS)
val currentWeekSumFormatted = sum(currentWeekComments)

val previousWeekComments = getPreviousWeekDays(dates, LIKES)
val previousWeekComments = getPreviousSevenDays(dates, LIKES)
val positive = currentWeekComments.sum() >= previousWeekComments.sum()

return ValueWithChartItem(
Expand All @@ -51,33 +52,33 @@ class TotalStatsMapper @Inject constructor(
currentWeekSum != 0L || previousWeekSum != 0L

fun shouldShowCommentsGuideCard(dates: List<PeriodData>): Boolean {
return getCurrentWeekDays(dates, COMMENTS).sum() > 0
return getCurrentSevenDays(dates, COMMENTS).sum() > 0
}

fun shouldShowFollowersGuideCard(domainModel: Int): Boolean {
return domainModel <= 0
}

fun shouldShowLikesGuideCard(dates: List<PeriodData>): Boolean {
return getCurrentWeekDays(dates, LIKES).sum() > 0
return getCurrentSevenDays(dates, LIKES).sum() > 0
}

fun buildTotalLikesInformation(dates: List<PeriodData>) = buildTotalInformation(dates, LIKES)

fun buildTotalCommentsInformation(dates: List<PeriodData>) = buildTotalInformation(dates, COMMENTS)

private fun buildTotalInformation(dates: List<PeriodData>, type: TotalStatsType): Text? {
val value = getCurrentWeekDays(dates, type).sum()
val previousValue = getPreviousWeekDays(dates, type).sum()
val value = getCurrentSevenDays(dates, type).sum()
val previousValue = getPreviousSevenDays(dates, type).sum()
if (!shouldShowTotalInformation(value, previousValue)) {
return null
}
val positive = value >= previousValue
val change = statsUtils.buildChange(previousValue, value, positive, true).toString()
val stringRes = if (positive) {
R.string.stats_insights_total_stats_positive
R.string.stats_insights_total_stats_seven_days_positive
} else {
R.string.stats_insights_total_stats_negative
R.string.stats_insights_total_stats_seven_days_negative
}

return Text(
Expand All @@ -97,33 +98,53 @@ class TotalStatsMapper @Inject constructor(
/**
* Gives list of data with StatsType for the current week.
*/
private fun getCurrentWeekDays(dates: List<PeriodData>, type: TotalStatsType): List<Long> {
val currentWeekDays = dates.takeLast(DAY_COUNT_FOR_CURRENT_WEEK)
return mapToStatsType(currentWeekDays, type)
fun getCurrentSevenDays(dates: List<PeriodData>, type: TotalStatsType): List<Long> {
// taking data for last 14 days excluding today
AppLog.d(AppLog.T.STATS, dates.toString())
return mapToStatsType(dates.dropLast(1).takeLast(DAY_COUNT_FOR_CURRENT_WEEK), type)
}

/**
* Gives list of data with StatsType for previous week.
*/
private fun getPreviousWeekDays(dates: List<PeriodData>, type: TotalStatsType): List<Long> {
if (dates.size < DAY_COUNT_TOTAL) {
return emptyList()
fun getPreviousSevenDays(dates: List<PeriodData>, type: TotalStatsType): List<Long> {
return if (dates.size < DAY_COUNT_TOTAL) {
emptyList()
} else {
mapToStatsType(dates.take(DAY_COUNT_FOR_PREVIOUS_WEEK), type)
}
}

/**
* Gives list of data with StatsType for the current week.
*/
fun getCurrentWeekDays(dates: List<PeriodData>, type: TotalStatsType): List<Long> {
// taking data for last 14 days excluding today
AppLog.d(AppLog.T.STATS, dates.toString())
return mapToStatsType(dates.takeLast(DAY_COUNT_FOR_CURRENT_WEEK), type)
}

/**
* Gives list of data with StatsType for previous week.
*/
fun getPreviousWeekDays(dates: List<PeriodData>, type: TotalStatsType): List<Long> {
return if (dates.size < DAY_COUNT_TOTAL) {
emptyList()
} else {
mapToStatsType(dates.drop(1).take(DAY_COUNT_FOR_PREVIOUS_WEEK), type)
}
val previousWeekDays = dates.subList(
dates.lastIndex - DAY_COUNT_TOTAL + 1,
dates.lastIndex - DAY_COUNT_FOR_PREVIOUS_WEEK + 1
)
return mapToStatsType(previousWeekDays, type)
}

private fun mapToStatsType(dates: List<PeriodData>, type: TotalStatsType) = dates.map {
when (type) {
TotalStatsType.VIEWS -> it.views
TotalStatsType.VISITORS -> it.visitors
LIKES -> it.likes
COMMENTS -> it.comments
}
}

private enum class TotalStatsType { LIKES, COMMENTS }
enum class TotalStatsType { VIEWS, VISITORS, LIKES, COMMENTS }

companion object {
private const val DAY_COUNT_FOR_CURRENT_WEEK = 7 // Last 7 days
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class ViewsAndVisitorsMapper
private val statsDateFormatter: StatsDateFormatter,
private val resourceProvider: ResourceProvider,
private val statsUtils: StatsUtils,
private val contentDescriptionHelper: ContentDescriptionHelper
private val contentDescriptionHelper: ContentDescriptionHelper,
private val totalStatsMapper: TotalStatsMapper
) {
private val units = listOf(
string.stats_views,
Expand All @@ -59,13 +60,13 @@ class ViewsAndVisitorsMapper
}

fun buildChartLegendsBlue() = ChartLegendsBlue(
string.stats_timeframe_this_week,
string.stats_timeframe_previous_week
string.stats_timeframe_last_seven_days,
string.stats_timeframe_previous_seven_days
)

fun buildChartLegendsPurple() = ChartLegendsPurple(
string.stats_timeframe_this_week,
string.stats_timeframe_previous_week
string.stats_timeframe_last_seven_days,
string.stats_timeframe_previous_seven_days
)

fun buildTitle(
Expand Down Expand Up @@ -100,6 +101,38 @@ class ViewsAndVisitorsMapper
)
}

fun buildWeekTitle(
dates: List<PeriodData>,
statsGranularity: StatsGranularity = DAYS,
selectedItem: PeriodData,
selectedPosition: Int,
startValue: Int = MILLION
): ValuesItem {
val (thisWeekCount, prevWeekCount) = mapDatesToWeeksDetail(dates, selectedPosition)

return ValuesItem(
selectedItem = selectedPosition,
value1 = statsUtils.toFormattedString(thisWeekCount, startValue),
unit1 = units[selectedPosition],
contentDescription1 = resourceProvider.getString(
string.stats_overview_content_description,
thisWeekCount,
resourceProvider.getString(units[selectedPosition]),
statsDateFormatter.printGranularDate(selectedItem.period, statsGranularity),
""
),
value2 = statsUtils.toFormattedString(prevWeekCount, startValue),
unit2 = units[selectedPosition],
contentDescription2 = resourceProvider.getString(
string.stats_overview_content_description,
prevWeekCount,
resourceProvider.getString(units[selectedPosition]),
statsDateFormatter.printGranularDate(selectedItem.period, statsGranularity),
""
)
)
}

private fun PeriodData.getValue(selectedPosition: Int) = when (SelectedType.valueOf(selectedPosition)) {
Views -> this.views
Visitors -> this.visitors
Expand Down Expand Up @@ -179,17 +212,68 @@ class ViewsAndVisitorsMapper
val stringRes = when (SelectedType.valueOf(selectedPosition)) {
Views -> {
when {
positive -> string.stats_insights_views_and_visitors_views_positive
else -> string.stats_insights_views_and_visitors_views_negative
positive -> string.stats_insights_views_and_visitors_seven_days_views_positive
else -> string.stats_insights_views_and_visitors_seven_days_views_negative
}
}
Visitors -> {
when {
positive -> string.stats_insights_views_and_visitors_visitors_positive
else -> string.stats_insights_views_and_visitors_visitors_negative
positive -> string.stats_insights_views_and_visitors_seven_days_visitors_positive
else -> string.stats_insights_views_and_visitors_seven_days_visitors_negative
}
}
else -> string.stats_insights_views_and_visitors_views_positive
else -> string.stats_insights_views_and_visitors_seven_days_views_positive
}

return Text(
text = resourceProvider.getString(stringRes, change),
color = when {
positive -> mapOf(color.stats_color_positive to change)
else -> mapOf(color.stats_color_negative to change)
}
)
}

fun buildWeeksDetailInformation(
dates: List<PeriodData>,
selectedPosition: Int,
navigationAction: (() -> Unit?)? = null
): Text {
val (thisWeekCount, prevWeekCount) = mapDatesToWeeksDetail(dates, selectedPosition)

if (thisWeekCount <= 0 || prevWeekCount <= 0) {
return Text(
text = resourceProvider.getString(
string.stats_insights_views_and_visitors_visitors_empty_state,
EXTERNAL_LINK_ICON_TOKEN
),
links = listOf(
Clickable(
icon = R.drawable.ic_external_white_24dp,
navigationAction = ListItemInteraction.create(
action = { navigationAction?.invoke() }
)
)
)
)
}

val positive = thisWeekCount >= prevWeekCount
val change = statsUtils.buildChange(prevWeekCount, thisWeekCount, positive, true).toString()
val stringRes = when (SelectedType.valueOf(selectedPosition)) {
Views -> {
when {
positive -> string.stats_insights_views_and_visitors_seven_days_views_positive
else -> string.stats_insights_views_and_visitors_seven_days_views_negative
}
}
Visitors -> {
when {
positive -> string.stats_insights_views_and_visitors_seven_days_visitors_positive
else -> string.stats_insights_views_and_visitors_seven_days_visitors_negative
}
}
else -> string.stats_insights_views_and_visitors_seven_days_views_positive
}

return Text(
Expand Down Expand Up @@ -228,20 +312,21 @@ class ViewsAndVisitorsMapper
}

private fun mapDatesToWeeks(dates: List<PeriodData>, selectedPosition: Int): Pair<Long, Long> {
val values = dates.map {
val value = it.getValue(selectedPosition)
value.toInt()
}
val statsType = TotalStatsMapper.TotalStatsType.values()[selectedPosition]

val hasData = values.isNotEmpty() && values.size >= 7
val prevWeekData = totalStatsMapper.getPreviousSevenDays(dates, statsType)
val thisWeekData = totalStatsMapper.getCurrentSevenDays(dates, statsType)

return Pair(thisWeekData.sum(), prevWeekData.sum())
}

val prevWeekData = if (hasData) values.subList(0, 7) else values.subList(0, values.size)
val thisWeekData = if (hasData) values.subList(7, values.size) else emptyList()
private fun mapDatesToWeeksDetail(dates: List<PeriodData>, selectedPosition: Int): Pair<Long, Long> {
val statsType = TotalStatsMapper.TotalStatsType.values()[selectedPosition]

val prevWeekCount = prevWeekData.fold(0L) { acc, next -> acc + next }
val thisWeekCount = thisWeekData.fold(0L) { acc, next -> acc + next }
val prevWeekData = totalStatsMapper.getPreviousWeekDays(dates, statsType)
val thisWeekData = totalStatsMapper.getCurrentWeekDays(dates, statsType)

return Pair(thisWeekCount, prevWeekCount)
return Pair(thisWeekData.sum(), prevWeekData.sum())
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:importantForAccessibility="no"
android:text="@string/stats_timeframe_this_week"
android:text="@string/stats_timeframe_last_seven_days"
app:layout_constraintEnd_toStartOf="@id/legend2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
Expand All @@ -22,7 +22,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:importantForAccessibility="no"
android:text="@string/stats_timeframe_previous_week"
android:text="@string/stats_timeframe_previous_seven_days"
app:layout_constraintStart_toEndOf="@id/legend1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:importantForAccessibility="no"
android:text="@string/stats_timeframe_this_week"
android:text="@string/stats_timeframe_last_seven_days"
app:layout_constraintEnd_toStartOf="@id/legend2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
Expand All @@ -22,7 +22,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:importantForAccessibility="no"
android:text="@string/stats_timeframe_previous_week"
android:text="@string/stats_timeframe_previous_seven_days"
app:layout_constraintStart_toEndOf="@id/legend1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
Expand Down
Loading