Skip to content
Snippets Groups Projects
Unverified Commit 520aec8d authored by Matthias Urhahn's avatar Matthias Urhahn Committed by GitHub
Browse files

Pass and use context for calendar date formatting. (#2139)

parent 2f96d029
No related branches found
No related tags found
No related merge requests found
package de.rki.coronawarnapp.ui.calendar
import dagger.Reusable
import android.content.Context
import de.rki.coronawarnapp.contactdiary.util.getLocale
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.joda.time.Instant
import org.joda.time.LocalDate
import java.util.Locale
import javax.inject.Inject
@Reusable
class CalendarCalculation @Inject constructor() {
class CalendarCalculation constructor(private val context: Context) {
private val locale: Locale
get() = context.getLocale()
/**
* Get month text view text
......@@ -42,19 +44,19 @@ class CalendarCalculation @Inject constructor() {
fun getMonthText(firstDate: LocalDate, lastDate: LocalDate): String {
val monthText = StringBuilder()
// Append first date month as it would always be displayed
monthText.append(firstDate.monthOfYear().getAsText(Locale.getDefault()))
monthText.append(firstDate.monthOfYear().getAsText(locale))
if (firstDate.monthOfYear() != lastDate.monthOfYear()) {
// Different month
if (firstDate.year() == lastDate.year()) {
// Same year (Case 1)
monthText.append(" - ")
.append(lastDate.monthOfYear().getAsText(Locale.getDefault()))
.append(lastDate.monthOfYear().getAsText(locale))
} else {
// Different year (Case 2)
monthText.append(" ")
.append(firstDate.year().get())
.append(" - ")
.append(lastDate.monthOfYear().getAsText(Locale.getDefault()))
.append(lastDate.monthOfYear().getAsText(locale))
}
// Append last date year
monthText.append(" ")
......
......@@ -122,7 +122,7 @@ class CalendarView @JvmOverloads constructor(
}
// Calculate dates to display
days.addAll(CalendarCalculation().getDates())
days.addAll(CalendarCalculation(context).getDates())
// Set calendar adapter as adapter for recycler view
adapter = CalendarAdapter(onItemClickListener)
......@@ -190,6 +190,6 @@ class CalendarView @JvmOverloads constructor(
val firstDate = days.first().date
val lastDate = days.last().date
monthTextView.text = CalendarCalculation().getMonthText(firstDate, lastDate)
monthTextView.text = CalendarCalculation(context).getMonthText(firstDate, lastDate)
}
}
package de.rki.coronawarnapp.ui.calendar
import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import io.kotest.matchers.shouldBe
import io.mockk.MockKAnnotations
import io.mockk.clearAllMocks
import io.mockk.every
import io.mockk.impl.annotations.MockK
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import org.junit.Test
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import testhelpers.BaseTest
import java.util.Locale
class CalendarCalculationTest {
class CalendarCalculationTest : BaseTest() {
@MockK lateinit var context: Context
@MockK lateinit var resources: Resources
@MockK lateinit var configuration: Configuration
private var pattern = "dd.MM.yyyy"
private val formatter = DateTimeFormat.forPattern(pattern)
@BeforeEach
fun setUp() {
MockKAnnotations.init(this)
every { context.resources } returns resources
every { resources.configuration } returns Configuration().apply {
locale = Locale.ENGLISH
}
}
@AfterEach
fun teardown() {
clearAllMocks()
}
fun createInstance() = CalendarCalculation(context)
@Test
fun calculateSameYearSameMonth() {
var input = "27.08.2020"
val input = "27.08.2020"
val dateTime =
DateTime.parse(input, DateTimeFormat.forPattern(pattern))
val dates = CalendarCalculation().getDates(dateTime)
val dates = createInstance().getDates(dateTime)
// First day - 3 of August
dates.first().date.dayOfMonth shouldBe 3
......@@ -25,7 +57,7 @@ class CalendarCalculationTest {
dates.last().date.dayOfMonth shouldBe 30
dates.last().date.monthOfYear shouldBe 8
CalendarCalculation().getMonthText(
createInstance().getMonthText(
dates.first().date,
dates.last().date
) shouldBe "August 2020"
......@@ -33,10 +65,10 @@ class CalendarCalculationTest {
@Test
fun calculateSameYearDifferentMonth() {
var input = "15.09.2020"
val input = "15.09.2020"
val dateTime =
DateTime.parse(input, DateTimeFormat.forPattern(pattern))
val dates = CalendarCalculation().getDates(dateTime)
val dates = createInstance().getDates(dateTime)
// First day - 24 of August
dates.first().date.dayOfMonth shouldBe 24
......@@ -46,7 +78,7 @@ class CalendarCalculationTest {
dates.last().date.dayOfMonth shouldBe 20
dates.last().date.monthOfYear shouldBe 9
CalendarCalculation().getMonthText(
createInstance().getMonthText(
dates.first().date,
dates.last().date
) shouldBe "August - September 2020"
......@@ -54,10 +86,10 @@ class CalendarCalculationTest {
@Test
fun calculateDifferentYearDifferentMonth() {
var input = "12.01.2021"
val input = "12.01.2021"
val dateTime =
DateTime.parse(input, DateTimeFormat.forPattern(pattern))
val dates = CalendarCalculation().getDates(dateTime)
val dates = createInstance().getDates(dateTime)
// First day - 21 of December 2020
dates.first().date.dayOfMonth shouldBe 21
......@@ -69,7 +101,7 @@ class CalendarCalculationTest {
dates.last().date.monthOfYear shouldBe 1
dates.last().date.year shouldBe 2021
CalendarCalculation().getMonthText(
createInstance().getMonthText(
dates.first().date,
dates.last().date
) shouldBe "December 2020 - January 2021"
......@@ -78,7 +110,7 @@ class CalendarCalculationTest {
@Test
fun calculateEdgeCases() {
// new year
CalendarCalculation().getDates(DateTime.parse("27.12.2021", formatter)).apply {
createInstance().getDates(DateTime.parse("27.12.2021", formatter)).apply {
// First day - 6 of December 2021
first().date.dayOfMonth shouldBe 6
first().date.monthOfYear shouldBe 12
......@@ -89,14 +121,14 @@ class CalendarCalculationTest {
last().date.monthOfYear shouldBe 1
last().date.year shouldBe 2022
CalendarCalculation().getMonthText(
createInstance().getMonthText(
first().date,
last().date
) shouldBe "December 2021 - January 2022"
}
// leap year
CalendarCalculation().getDates(DateTime.parse("29.02.2024", formatter)).apply {
createInstance().getDates(DateTime.parse("29.02.2024", formatter)).apply {
// First day - 5 of February 2024
first().date.dayOfMonth shouldBe 5
first().date.monthOfYear shouldBe 2
......@@ -107,7 +139,7 @@ class CalendarCalculationTest {
last().date.monthOfYear shouldBe 3
last().date.year shouldBe 2024
CalendarCalculation().getMonthText(
createInstance().getMonthText(
first().date,
last().date
) shouldBe "February - March 2024"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment