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] Add year to date picker for weeks. #20589

Merged
merged 3 commits into from
Apr 9, 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 @@ -9,6 +9,7 @@ import org.wordpress.android.fluxc.network.utils.StatsGranularity.WEEKS
import org.wordpress.android.fluxc.network.utils.StatsGranularity.YEARS
import org.wordpress.android.fluxc.utils.SiteUtils
import org.wordpress.android.util.LocaleManagerWrapper
import org.wordpress.android.util.config.StatsTrafficTabFeatureConfig
import org.wordpress.android.viewmodel.ResourceProvider
import java.text.DateFormat
import java.text.ParseException
Expand All @@ -30,7 +31,11 @@ private const val YEARS_FORMAT = "MMM"
private const val REMOVE_YEAR = "([^\\p{Alpha}']|('[\\p{Alpha}]+'))*y+([^\\p{Alpha}']|('[\\p{Alpha}]+'))*"

class StatsDateFormatter
@Inject constructor(private val localeManagerWrapper: LocaleManagerWrapper, val resourceProvider: ResourceProvider) {
@Inject constructor(
private val localeManagerWrapper: LocaleManagerWrapper,
val resourceProvider: ResourceProvider,
val statsTrafficTabFeatureConfig: StatsTrafficTabFeatureConfig
) {
private val inputFormat: SimpleDateFormat
get() {
return SimpleDateFormat(STATS_INPUT_FORMAT, localeManagerWrapper.getLocale())
Expand Down Expand Up @@ -110,7 +115,7 @@ class StatsDateFormatter
val startCalendar = Calendar.getInstance()
startCalendar.time = endCalendar.time
startCalendar.add(Calendar.DAY_OF_WEEK, -6)
return printWeek(startCalendar, endCalendar)
return printWeek(startCalendar, endCalendar, statsTrafficTabFeatureConfig.isEnabled())
}
MONTHS -> outputMonthFormat.format(date)
.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.wordpress.android.fluxc.network.utils.StatsGranularity.MONTHS
import org.wordpress.android.fluxc.network.utils.StatsGranularity.WEEKS
import org.wordpress.android.fluxc.network.utils.StatsGranularity.YEARS
import org.wordpress.android.util.LocaleManagerWrapper
import org.wordpress.android.util.config.StatsTrafficTabFeatureConfig
import org.wordpress.android.viewmodel.ResourceProvider
import java.util.Calendar
import java.util.Locale
Expand All @@ -27,14 +28,18 @@ class StatsDateFormatterTest : BaseUnitTest() {
@Mock
lateinit var localeManagerWrapper: LocaleManagerWrapper

@Mock
lateinit var statsTrafficTabFeatureConfig: StatsTrafficTabFeatureConfig

@Mock
lateinit var resourceProvider: ResourceProvider
private lateinit var statsDateFormatter: StatsDateFormatter

@Before
fun setUp() {
whenever(localeManagerWrapper.getLocale()).thenReturn(Locale.US)
statsDateFormatter = StatsDateFormatter(localeManagerWrapper, resourceProvider)
whenever(statsTrafficTabFeatureConfig.isEnabled()).thenReturn(false)
statsDateFormatter = StatsDateFormatter(localeManagerWrapper, resourceProvider, statsTrafficTabFeatureConfig)
}

@Test
Expand Down Expand Up @@ -72,6 +77,42 @@ class StatsDateFormatterTest : BaseUnitTest() {
assertThat(parsedDate).isEqualTo("Dec 17 - Dec 23")
}

@Test
fun `prints a week date in the same year in string format with stats traffic tab enabled`() {
whenever(statsTrafficTabFeatureConfig.isEnabled()).thenReturn(true)
irfano marked this conversation as resolved.
Show resolved Hide resolved
val unparsedDate = "2018W12W19"
val result = "Dec 17 - Dec 23, 2018"
whenever(
resourceProvider.getString(
R.string.stats_from_to_dates_in_week_label,
"Dec 17",
"Dec 23, 2018"
)
).thenReturn(result)

val parsedDate = statsDateFormatter.printGranularDate(unparsedDate, WEEKS)

assertThat(parsedDate).isEqualTo("Dec 17 - Dec 23, 2018")
}

@Test
fun `prints a week date in two different years in string format with traffic tab enabled`() {
whenever(statsTrafficTabFeatureConfig.isEnabled()).thenReturn(true)
val unparsedDate = "2018W12W31"
val result = "Dec 31, 2018 - Jan 6, 2019"
whenever(
resourceProvider.getString(
R.string.stats_from_to_dates_in_week_label,
"Dec 31, 2018",
"Jan 6, 2019"
)
).thenReturn(result)

val parsedDate = statsDateFormatter.printGranularDate(unparsedDate, WEEKS)

assertThat(parsedDate).isEqualTo("Dec 31, 2018 - Jan 6, 2019")
}

@Test
fun `prints a week date`() {
val calendar = Calendar.getInstance()
Expand Down
Loading