Skip to content
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

Stats traffic fix graph issues #20336

Merged
merged 3 commits into from
Mar 1, 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 @@ -15,6 +15,7 @@ import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.TabsI
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.ACTION_CARD
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.ACTIVITY_ITEM
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.BAR_CHART
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.TRAFFIC_BAR_CHART
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.BIG_TITLE
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.CHART_LEGEND
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.CHART_LEGENDS_BLUE
Expand Down Expand Up @@ -69,6 +70,7 @@ class BlockDiffCallback(
COLUMNS,
CHIPS,
BAR_CHART,
TRAFFIC_BAR_CHART,
PIE_CHART,
LINE_CHART,
ACTIVITY_ITEM,
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.BlockDiffCallback.BlockListPayload
import org.wordpress.android.ui.stats.refresh.BlockDiffCallback.BlockListPayload.TAB_CHANGED
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.ActivityItem
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.BarChartItem
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.TrafficBarChartItem
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.BigTitle
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.ChartLegend
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.ChartLegendsBlue
Expand Down Expand Up @@ -41,6 +42,7 @@ import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Title
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.ACTION_CARD
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.ACTIVITY_ITEM
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.BAR_CHART
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.TRAFFIC_BAR_CHART
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.BIG_TITLE
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.CHART_LEGEND
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.CHART_LEGENDS_BLUE
Expand Down Expand Up @@ -150,7 +152,8 @@ class BlockListAdapter(
COLUMNS -> if (trafficTabEnabled) TrafficFourColumnsViewHolder(parent) else FourColumnsViewHolder(parent)
CHIPS -> ChipsViewHolder(parent)
LINK -> LinkViewHolder(parent)
BAR_CHART -> if (trafficTabEnabled) TrafficBarChartViewHolder(parent) else BarChartViewHolder(parent)
BAR_CHART -> BarChartViewHolder(parent)
TRAFFIC_BAR_CHART -> TrafficBarChartViewHolder(parent)
PIE_CHART -> PieChartViewHolder(parent)
LINE_CHART -> LineChartViewHolder(parent)
CHART_LEGEND -> ChartLegendViewHolder(parent)
Expand Down Expand Up @@ -202,7 +205,7 @@ class BlockListAdapter(
is ChipsViewHolder -> holder.bind(item as Chips)
is LinkViewHolder -> holder.bind(item as Link)
is BarChartViewHolder -> holder.bind(item as BarChartItem)
is TrafficBarChartViewHolder -> holder.bind(item as BarChartItem)
is TrafficBarChartViewHolder -> holder.bind(item as TrafficBarChartItem)
is PieChartViewHolder -> holder.bind(item as PieChartItem)
is LineChartViewHolder -> holder.bind(item as LineChartItem)
is ChartLegendViewHolder -> holder.bind(item as ChartLegend)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.TEXT
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.TITLE
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.TITLE_WITH_MORE
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.TRAFFIC_BAR_CHART
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.VALUES_ITEM
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.VALUE_ITEM
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.VALUE_WITH_CHART_ITEM
Expand Down Expand Up @@ -71,6 +72,7 @@ sealed class BlockListItem(val type: Type) {
CHIPS,
LINK,
BAR_CHART,
TRAFFIC_BAR_CHART,
PIE_CHART,
LINE_CHART,
CHART_LEGEND,
Expand Down Expand Up @@ -266,6 +268,20 @@ sealed class BlockListItem(val type: Type) {
get() = entries.hashCode()
}

data class TrafficBarChartItem(
val entries: List<Bar>,
val overlappingEntries: List<Bar>? = null,
val selectedItem: String? = null,
val onBarSelected: ((period: String?) -> Unit)? = null,
val onBarChartDrawn: ((visibleBarCount: Int) -> Unit)? = null,
val entryContentDescriptions: List<String>
) : BlockListItem(TRAFFIC_BAR_CHART) {
data class Bar(val label: String, val id: String, val value: Int)

override val itemId: Int
get() = entries.hashCode()
}

data class PieChartItem(
val entries: List<Pie>,
val totalLabel: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.wordpress.android.R
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.BarChartItem.Bar
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.TrafficBarChartItem.Bar
import org.wordpress.android.ui.stats.refresh.lists.sections.viewholders.BlockListItemViewHolder
import org.wordpress.android.ui.stats.refresh.utils.BarChartAccessibilityHelper
import org.wordpress.android.ui.stats.refresh.utils.BarChartLabelFormatter
Expand All @@ -35,7 +35,7 @@ class TrafficBarChartViewHolder(parent: ViewGroup) : BlockListItemViewHolder(

private lateinit var accessibilityHelper: BarChartAccessibilityHelper

fun bind(item: BlockListItem.BarChartItem) {
fun bind(item: BlockListItem.TrafficBarChartItem) {
chart.setNoDataText("")
coroutineScope.launch {
delay(50)
Expand Down Expand Up @@ -67,7 +67,7 @@ class TrafficBarChartViewHolder(parent: ViewGroup) : BlockListItemViewHolder(
}
}

private fun BarChart.draw(item: BlockListItem.BarChartItem): Int {
private fun BarChart.draw(item: BlockListItem.TrafficBarChartItem): Int {
resetChart()

data = BarData(getData(item))
Expand All @@ -82,7 +82,7 @@ class TrafficBarChartViewHolder(parent: ViewGroup) : BlockListItemViewHolder(

private fun hasData(entries: List<Bar>) = entries.isNotEmpty() && entries.any { it.value > 0 }

private fun getData(item: BlockListItem.BarChartItem): List<IBarDataSet> {
private fun getData(item: BlockListItem.TrafficBarChartItem): List<IBarDataSet> {
val minColumnCount = 5

val graphWidth = DisplayUtils.pxToDp(chart.context, chart.width)
Expand Down Expand Up @@ -122,7 +122,7 @@ class TrafficBarChartViewHolder(parent: ViewGroup) : BlockListItemViewHolder(
}
}

private fun configureYAxis(item: BlockListItem.BarChartItem) {
private fun configureYAxis(item: BlockListItem.TrafficBarChartItem) {
val minYValue = 4f
val maxYValue = item.entries.maxByOrNull { it.value }?.value ?: 0

Expand Down Expand Up @@ -159,7 +159,7 @@ class TrafficBarChartViewHolder(parent: ViewGroup) : BlockListItemViewHolder(
}
}

private fun configureXAxis(item: BlockListItem.BarChartItem) {
private fun configureXAxis(item: BlockListItem.TrafficBarChartItem) {
chart.xAxis.apply {
setDrawAxisLine(false)
setDrawGridLines(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class TrafficOverviewMapper @Inject constructor(
Comments -> it.comments
else -> 0L
}
BlockListItem.BarChartItem.Bar(
BlockListItem.TrafficBarChartItem.Bar(
statsDateFormatter.printTrafficGranularDate(it.period, statsGranularity),
it.period,
value.toInt()
Expand All @@ -111,13 +111,13 @@ class TrafficOverviewMapper @Inject constructor(
else -> R.string.stats_views
}

val contentDescriptions = statsUtils.getBarChartEntryContentDescriptions(
val contentDescriptions = statsUtils.getTrafficBarChartEntryContentDescriptions(
entryType,
chartItems
)

result.add(
BlockListItem.BarChartItem(
BlockListItem.TrafficBarChartItem(
chartItems,
selectedItem = selectedItemPeriod,
onBarSelected = onBarSelected,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.wordpress.android.ui.stats.refresh.utils

import com.github.mikephil.charting.components.AxisBase
import com.github.mikephil.charting.formatter.ValueFormatter
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.BarChartItem.Bar
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.TrafficBarChartItem.Bar
import javax.inject.Inject

class BarChartLabelFormatter @Inject constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.wordpress.android.ui.stats.refresh.utils

import androidx.annotation.StringRes
import org.wordpress.android.R
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.BarChartItem.Bar
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.LineChartItem.Line
import org.wordpress.android.util.LocaleManagerWrapper
Expand Down Expand Up @@ -168,6 +169,36 @@ class StatsUtils @Inject constructor(
return contentDescriptions
}

fun getTrafficBarChartEntryContentDescriptions(
@StringRes entryType: Int,
entries: List<BlockListItem.TrafficBarChartItem.Bar>,
@StringRes overlappingEntryType: Int? = null,
overlappingEntries: List<BlockListItem.TrafficBarChartItem.Bar>? = null
): List<String> {
val contentDescriptions = mutableListOf<String>()
entries.forEachIndexed { index, bar ->
var contentDescription = resourceProvider.getString(
R.string.stats_bar_chart_accessibility_entry,
bar.label,
bar.value,
resourceProvider.getString(entryType)
)

overlappingEntries?.getOrNull(index)?.let { overlappingBar ->
overlappingEntryType?.let {
contentDescription += resourceProvider.getString(
R.string.stats_bar_chart_accessibility_overlapping_entry,
overlappingBar.value,
resourceProvider.getString(overlappingEntryType)
)
}
}

contentDescriptions.add(contentDescription)
}
return contentDescriptions
}

fun getLineChartEntryContentDescriptions(
@StringRes entryType: Int,
entries: List<Line>
Expand Down
Loading