Skip to content

Commit b3554a6

Browse files
Parse TopRead date as a LocalDate. (#3954)
Co-authored-by: Dmitry Brant <[email protected]>
1 parent b2024e7 commit b3554a6

File tree

4 files changed

+14
-28
lines changed

4 files changed

+14
-28
lines changed
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
package org.wikipedia.feed.topread
22

33
import android.os.Parcelable
4+
import kotlinx.parcelize.IgnoredOnParcel
45
import kotlinx.parcelize.Parcelize
56
import kotlinx.serialization.Serializable
67
import org.wikipedia.dataclient.page.PageSummary
7-
import org.wikipedia.util.DateUtil
8-
import java.lang.Exception
9-
import java.util.*
8+
import java.time.LocalDate
9+
import java.time.format.DateTimeFormatter
10+
import java.time.format.DateTimeParseException
1011

1112
@Serializable
1213
@Parcelize
1314
class TopRead(
1415
val date: String = "",
1516
val articles: List<PageSummary> = emptyList()
1617
) : Parcelable {
17-
fun date(): Date {
18+
@IgnoredOnParcel
19+
val localDate: LocalDate by lazy {
1820
try {
19-
return DateUtil.iso8601ShortDateParse(date)
20-
} catch (e: Exception) {}
21-
return Date()
21+
LocalDate.parse(date, DateTimeFormatter.ISO_OFFSET_DATE)
22+
} catch (e: DateTimeParseException) {
23+
LocalDate.now()
24+
}
2225
}
2326
}

app/src/main/java/org/wikipedia/feed/topread/TopReadListCard.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import org.wikipedia.feed.model.CardType
99
import org.wikipedia.feed.model.ListCard
1010
import org.wikipedia.util.DateUtil
1111
import org.wikipedia.util.L10nUtil
12-
import java.util.concurrent.TimeUnit
1312

1413
@Parcelize
1514
class TopReadListCard(private val articles: TopRead, val site: WikiSite) :
@@ -20,7 +19,7 @@ class TopReadListCard(private val articles: TopRead, val site: WikiSite) :
2019
}
2120

2221
override fun subtitle(): String {
23-
return DateUtil.getFeedCardDateString(articles.date())
22+
return DateUtil.getShortDateString(articles.localDate)
2423
}
2524

2625
override fun type(): CardType {
@@ -32,7 +31,7 @@ class TopReadListCard(private val articles: TopRead, val site: WikiSite) :
3231
}
3332

3433
override fun dismissHashCode(): Int {
35-
return TimeUnit.MILLISECONDS.toDays(articles.date().time).toInt() + wikiSite().hashCode()
34+
return articles.localDate.toEpochDay().toInt() + wikiSite().hashCode()
3635
}
3736

3837
companion object {

app/src/main/java/org/wikipedia/util/DateUtil.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ object DateUtil {
2828
return Date.from(Instant.parse(date))
2929
}
3030

31-
fun iso8601ShortDateParse(date: String): Date {
32-
return getCachedDateFormat("yyyy-MM-dd'Z'", Locale.ROOT, true).parse(date)!!
33-
}
34-
3531
fun iso8601LocalDateTimeParse(timestamp: String): LocalDateTime {
3632
return LocalDateTime.ofInstant(Instant.parse(timestamp), ZoneId.systemDefault())
3733
}
@@ -56,10 +52,6 @@ object DateUtil {
5652
return getShortDateString(date.time)
5753
}
5854

59-
fun getFeedCardDateString(date: Date): String {
60-
return getShortDateString(date)
61-
}
62-
6355
fun getFeedCardShortDateString(date: Calendar): String {
6456
return getExtraShortDateString(date.time)
6557
}

app/src/test/java/org/wikipedia/feed/topread/TopReadTest.kt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,19 @@ import org.junit.runner.RunWith
77
import org.robolectric.RobolectricTestRunner
88
import org.wikipedia.json.JsonUtil
99
import org.wikipedia.test.TestFileUtil
10-
import java.text.SimpleDateFormat
11-
import java.util.*
10+
import java.time.LocalDate
1211

1312
@RunWith(RobolectricTestRunner::class)
1413
class TopReadTest {
1514
@Test
1615
@Throws(Throwable::class)
1716
fun testUnmarshalManyArticles() {
1817
val subject = unmarshal("most_read.json")
19-
MatcherAssert.assertThat(subject.date(), Matchers.`is`(date("2016-06-01Z")))
18+
MatcherAssert.assertThat(subject.localDate, Matchers.`is`(LocalDate.of(2016, 6, 1)))
2019
MatcherAssert.assertThat(subject.articles, Matchers.notNullValue())
2120
MatcherAssert.assertThat(subject.articles.size, Matchers.`is`(40))
2221
}
2322

24-
@Throws(Throwable::class)
25-
private fun date(str: String): Date {
26-
val format = SimpleDateFormat("yyyy-MM-dd'Z'", Locale.ROOT)
27-
format.timeZone = TimeZone.getTimeZone("UTC")
28-
return format.parse(str)!!
29-
}
30-
3123
companion object {
3224
@Throws(Throwable::class)
3325
fun unmarshal(filename: String): TopRead {

0 commit comments

Comments
 (0)