From 6c0bbe8eaca4be815b243897c5b22ccc6785a16d Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Thu, 23 Mar 2023 07:47:43 +0530 Subject: [PATCH] Replace the UtcDate class with DateUtil methods. --- .../aggregated/AggregatedFeedContentClient.kt | 4 ++-- .../java/org/wikipedia/feed/model/UtcDate.kt | 18 ------------------ .../java/org/wikipedia/feed/news/NewsCard.kt | 9 ++------- .../main/java/org/wikipedia/util/DateUtil.kt | 17 ++++++++--------- .../widgets/WidgetProviderFeaturedPage.kt | 5 +++-- 5 files changed, 15 insertions(+), 38 deletions(-) delete mode 100644 app/src/main/java/org/wikipedia/feed/model/UtcDate.kt diff --git a/app/src/main/java/org/wikipedia/feed/aggregated/AggregatedFeedContentClient.kt b/app/src/main/java/org/wikipedia/feed/aggregated/AggregatedFeedContentClient.kt index 93ec1aaaa42..8ba3bf1bf73 100644 --- a/app/src/main/java/org/wikipedia/feed/aggregated/AggregatedFeedContentClient.kt +++ b/app/src/main/java/org/wikipedia/feed/aggregated/AggregatedFeedContentClient.kt @@ -138,11 +138,11 @@ class AggregatedFeedContentClient { private fun requestAggregated() { aggregatedClient.cancel() - val date = DateUtil.getUtcRequestDateFor(age) + val (year, month, day) = DateUtil.getYearMonthAndDayForAge(age) aggregatedClient.disposables.add(Observable.fromIterable(FeedContentType.aggregatedLanguages) .flatMap({ lang -> ServiceFactory.getRest(WikiSite.forLanguageCode(lang)) - .getAggregatedFeed(date.year, date.month, date.day) + .getAggregatedFeed(year, month, day) .subscribeOn(Schedulers.io()) }, { first, second -> Pair(first, second) }) .toList() diff --git a/app/src/main/java/org/wikipedia/feed/model/UtcDate.kt b/app/src/main/java/org/wikipedia/feed/model/UtcDate.kt deleted file mode 100644 index 43880cb7c46..00000000000 --- a/app/src/main/java/org/wikipedia/feed/model/UtcDate.kt +++ /dev/null @@ -1,18 +0,0 @@ -package org.wikipedia.feed.model - -import java.util.Calendar -import java.util.TimeZone - -class UtcDate(private val age: Int) { - - val baseCalendar: Calendar - get() = Calendar.getInstance(TimeZone.getTimeZone("UTC")).also { - it.add(Calendar.DATE, -age) - } - - val year get() = baseCalendar[Calendar.YEAR].toString() - - val month get() = (baseCalendar[Calendar.MONTH] + 1).toString().padStart(2, '0') - - val day get() = baseCalendar[Calendar.DATE].toString().padStart(2, '0') -} diff --git a/app/src/main/java/org/wikipedia/feed/news/NewsCard.kt b/app/src/main/java/org/wikipedia/feed/news/NewsCard.kt index 8ba2d7ab05a..36a0650bed4 100644 --- a/app/src/main/java/org/wikipedia/feed/news/NewsCard.kt +++ b/app/src/main/java/org/wikipedia/feed/news/NewsCard.kt @@ -3,10 +3,9 @@ package org.wikipedia.feed.news import org.wikipedia.R import org.wikipedia.dataclient.WikiSite import org.wikipedia.feed.model.CardType -import org.wikipedia.feed.model.UtcDate import org.wikipedia.feed.model.WikiSiteCard +import org.wikipedia.util.DateUtil import org.wikipedia.util.L10nUtil -import java.util.concurrent.TimeUnit class NewsCard(private val news: List, private val age: Int, @@ -21,11 +20,7 @@ class NewsCard(private val news: List, } override fun dismissHashCode(): Int { - return TimeUnit.MILLISECONDS.toDays(date().baseCalendar.time.time).toInt() + wikiSite().hashCode() - } - - fun date(): UtcDate { - return UtcDate(age) + return DateUtil.getRequestDateForAge(age).toEpochDay().toInt() + wikiSite().hashCode() } fun news(): List { diff --git a/app/src/main/java/org/wikipedia/util/DateUtil.kt b/app/src/main/java/org/wikipedia/util/DateUtil.kt index de34394d820..f14c284a4d6 100644 --- a/app/src/main/java/org/wikipedia/util/DateUtil.kt +++ b/app/src/main/java/org/wikipedia/util/DateUtil.kt @@ -6,7 +6,6 @@ import android.os.Build import android.text.format.DateFormat import org.wikipedia.R import org.wikipedia.WikipediaApp -import org.wikipedia.feed.model.UtcDate import java.text.SimpleDateFormat import java.time.Instant import java.time.LocalDate @@ -41,15 +40,11 @@ object DateUtil { } fun getFeedCardDayHeaderDate(age: Int): String { - return getDateStringWithSkeletonPattern(UtcDate(age).baseCalendar.time, "MMMM d") + return getDateStringWithSkeletonPattern(getRequestDateForAge(age), "MMMM d") } fun getFeedCardDateString(age: Int): String { - return getFeedCardDateString(UtcDate(age).baseCalendar) - } - - private fun getFeedCardDateString(date: Calendar): String { - return getShortDateString(date.time) + return getShortDateString(getRequestDateForAge(age)) } fun getFeedCardShortDateString(date: Calendar): String { @@ -153,8 +148,12 @@ object DateUtil { return DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(localDate) } - fun getUtcRequestDateFor(age: Int): UtcDate { - return UtcDate(age) + fun getRequestDateForAge(age: Int): LocalDate { + return LocalDate.now().minusDays(age.toLong()) + } + + fun getYearMonthAndDayForAge(age: Int): List { + return getRequestDateForAge(age).toString().split("-") } fun getDefaultDateFor(age: Int): Calendar { diff --git a/app/src/main/java/org/wikipedia/widgets/WidgetProviderFeaturedPage.kt b/app/src/main/java/org/wikipedia/widgets/WidgetProviderFeaturedPage.kt index 0c4e4b065be..a8be9360aef 100644 --- a/app/src/main/java/org/wikipedia/widgets/WidgetProviderFeaturedPage.kt +++ b/app/src/main/java/org/wikipedia/widgets/WidgetProviderFeaturedPage.kt @@ -67,8 +67,9 @@ class WidgetProviderFeaturedPage : AppWidgetProvider() { val mainPageTitle = PageTitle( MainPageNameData.valueFor(app.appOrSystemLanguageCode), app.wikiSite) - val date = DateUtil.getUtcRequestDateFor(0) - ServiceFactory.getRest(WikipediaApp.instance.wikiSite).getAggregatedFeed(date.year, date.month, date.day) + val (year, month, day) = DateUtil.getYearMonthAndDayForAge(0) + ServiceFactory.getRest(WikipediaApp.instance.wikiSite) + .getAggregatedFeed(year, month, day) .flatMap { response: AggregatedFeedContent -> if (response.tfa != null) { Observable.just(response.tfa)