From 8cf4ba25f57813609042b6c9ac175fe7b30f4536 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sun, 20 Dec 2020 09:45:14 +0530 Subject: [PATCH 1/2] Update prettytime to 5.0.0.Final. --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 0b8de4d41..20d6ab003 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -233,7 +233,7 @@ dependencies { implementation "io.reactivex.rxjava3:rxandroid:3.0.0" implementation "com.jakewharton.rxbinding4:rxbinding:4.0.0" - implementation "org.ocpsoft.prettytime:prettytime:4.0.6.Final" + implementation "org.ocpsoft.prettytime:prettytime:5.0.0.Final" testImplementation 'junit:junit:4.13.1' testImplementation "org.mockito:mockito-core:${mockitoVersion}" From 74e20a8c52c90693f76037ad547606ec232a66f9 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sun, 20 Dec 2020 09:53:05 +0530 Subject: [PATCH 2/2] Use PrettyTime's new formatUnrounded(OffsetDateTime) method. Also change the types of the relevant variables from Calendar to OffsetDateTime. --- .../org/schabi/newpipe/ktx/OffsetDateTime.kt | 29 ------------------ .../schabi/newpipe/local/feed/FeedFragment.kt | 14 ++++----- .../schabi/newpipe/local/feed/FeedState.kt | 4 +-- .../newpipe/local/feed/FeedViewModel.kt | 7 ++--- .../org/schabi/newpipe/util/Localization.java | 8 +---- .../ktx/OffsetDateTimeToCalendarTest.kt | 30 ------------------- .../schabi/newpipe/util/LocalizationTest.kt | 22 ++++---------- 7 files changed, 16 insertions(+), 98 deletions(-) delete mode 100644 app/src/main/java/org/schabi/newpipe/ktx/OffsetDateTime.kt delete mode 100644 app/src/test/java/org/schabi/newpipe/ktx/OffsetDateTimeToCalendarTest.kt diff --git a/app/src/main/java/org/schabi/newpipe/ktx/OffsetDateTime.kt b/app/src/main/java/org/schabi/newpipe/ktx/OffsetDateTime.kt deleted file mode 100644 index 0d1a534b9..000000000 --- a/app/src/main/java/org/schabi/newpipe/ktx/OffsetDateTime.kt +++ /dev/null @@ -1,29 +0,0 @@ -package org.schabi.newpipe.ktx - -import java.time.OffsetDateTime -import java.time.ZoneOffset -import java.time.temporal.ChronoField -import java.util.Calendar -import java.util.Date -import java.util.GregorianCalendar -import java.util.TimeZone - -// This method is a modified version of GregorianCalendar.from(ZonedDateTime). -// Math.addExact() and Math.multiplyExact() are desugared even though lint displays a warning. -@SuppressWarnings("NewApi") -fun OffsetDateTime.toCalendar(): Calendar { - val cal = GregorianCalendar(TimeZone.getTimeZone("UTC")) - val offsetDateTimeUTC = withOffsetSameInstant(ZoneOffset.UTC) - cal.gregorianChange = Date(Long.MIN_VALUE) - cal.firstDayOfWeek = Calendar.MONDAY - cal.minimalDaysInFirstWeek = 4 - try { - cal.timeInMillis = Math.addExact( - Math.multiplyExact(offsetDateTimeUTC.toEpochSecond(), 1000), - offsetDateTimeUTC[ChronoField.MILLI_OF_SECOND].toLong() - ) - } catch (ex: ArithmeticException) { - throw IllegalArgumentException(ex) - } - return cal -} diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt b/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt index 33bed7d7e..1df999144 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt @@ -45,7 +45,7 @@ import org.schabi.newpipe.ktx.animate import org.schabi.newpipe.ktx.animateHideRecyclerViewAllowingScrolling import org.schabi.newpipe.local.feed.service.FeedLoadService import org.schabi.newpipe.util.Localization -import java.util.Calendar +import java.time.OffsetDateTime class FeedFragment : BaseListFragment() { private var _feedBinding: FragmentFeedBinding? = null @@ -58,7 +58,7 @@ class FeedFragment : BaseListFragment() { private var groupId = FeedGroupEntity.GROUP_ALL_ID private var groupName = "" - private var oldestSubscriptionUpdate: Calendar? = null + private var oldestSubscriptionUpdate: OffsetDateTime? = null init { setHasOptionsMenu(true) @@ -275,12 +275,10 @@ class FeedFragment : BaseListFragment() { } private fun updateRefreshViewState() { - val oldestSubscriptionUpdateText = when { - oldestSubscriptionUpdate != null -> Localization.relativeTime(oldestSubscriptionUpdate!!) - else -> "—" - } - - feedBinding.refreshText.text = getString(R.string.feed_oldest_subscription_update, oldestSubscriptionUpdateText) + feedBinding.refreshText.text = getString( + R.string.feed_oldest_subscription_update, + oldestSubscriptionUpdate?.let { Localization.relativeTime(it) } ?: "—" + ) } // ///////////////////////////////////////////////////////////////////////// diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/FeedState.kt b/app/src/main/java/org/schabi/newpipe/local/feed/FeedState.kt index 00ca76b8e..dec2773e1 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/FeedState.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/FeedState.kt @@ -2,7 +2,7 @@ package org.schabi.newpipe.local.feed import androidx.annotation.StringRes import org.schabi.newpipe.extractor.stream.StreamInfoItem -import java.util.Calendar +import java.time.OffsetDateTime sealed class FeedState { data class ProgressState( @@ -13,7 +13,7 @@ sealed class FeedState { data class LoadedState( val items: List, - val oldestUpdate: Calendar? = null, + val oldestUpdate: OffsetDateTime? = null, val notLoadedCount: Long, val itemsErrors: List = emptyList() ) : FeedState() diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt b/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt index 84f913409..e516cdaca 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt @@ -11,7 +11,6 @@ import io.reactivex.rxjava3.functions.Function4 import io.reactivex.rxjava3.schedulers.Schedulers import org.schabi.newpipe.database.feed.model.FeedGroupEntity import org.schabi.newpipe.extractor.stream.StreamInfoItem -import org.schabi.newpipe.ktx.toCalendar import org.schabi.newpipe.local.feed.service.FeedEventManager import org.schabi.newpipe.local.feed.service.FeedEventManager.Event.ErrorResultEvent import org.schabi.newpipe.local.feed.service.FeedEventManager.Event.IdleEvent @@ -48,13 +47,11 @@ class FeedViewModel(applicationContext: Context, val groupId: Long = FeedGroupEn .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { (event, listFromDB, notLoadedCount, oldestUpdate) -> - val oldestUpdateCalendar = oldestUpdate?.toCalendar() - mutableStateLiveData.postValue( when (event) { - is IdleEvent -> FeedState.LoadedState(listFromDB, oldestUpdateCalendar, notLoadedCount) + is IdleEvent -> FeedState.LoadedState(listFromDB, oldestUpdate, notLoadedCount) is ProgressEvent -> FeedState.ProgressState(event.currentProgress, event.maxProgress, event.progressMessage) - is SuccessResultEvent -> FeedState.LoadedState(listFromDB, oldestUpdateCalendar, notLoadedCount, event.itemsErrors) + is SuccessResultEvent -> FeedState.LoadedState(listFromDB, oldestUpdate, notLoadedCount, event.itemsErrors) is ErrorResultEvent -> FeedState.ErrorState(event.error) } ) diff --git a/app/src/main/java/org/schabi/newpipe/util/Localization.java b/app/src/main/java/org/schabi/newpipe/util/Localization.java index f62f959c4..674c7844f 100644 --- a/app/src/main/java/org/schabi/newpipe/util/Localization.java +++ b/app/src/main/java/org/schabi/newpipe/util/Localization.java @@ -20,7 +20,6 @@ import org.ocpsoft.prettytime.units.Decade; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.ListExtractor; import org.schabi.newpipe.extractor.localization.ContentCountry; -import org.schabi.newpipe.ktx.OffsetDateTimeKt; import java.math.BigDecimal; import java.math.RoundingMode; @@ -30,7 +29,6 @@ import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.util.Arrays; -import java.util.Calendar; import java.util.List; import java.util.Locale; @@ -314,11 +312,7 @@ public final class Localization { } public static String relativeTime(final OffsetDateTime offsetDateTime) { - return relativeTime(OffsetDateTimeKt.toCalendar(offsetDateTime)); - } - - public static String relativeTime(final Calendar calendarTime) { - return prettyTime.formatUnrounded(calendarTime); + return prettyTime.formatUnrounded(offsetDateTime); } private static void changeAppLanguage(final Locale loc, final Resources res) { diff --git a/app/src/test/java/org/schabi/newpipe/ktx/OffsetDateTimeToCalendarTest.kt b/app/src/test/java/org/schabi/newpipe/ktx/OffsetDateTimeToCalendarTest.kt deleted file mode 100644 index 74da07998..000000000 --- a/app/src/test/java/org/schabi/newpipe/ktx/OffsetDateTimeToCalendarTest.kt +++ /dev/null @@ -1,30 +0,0 @@ -package org.schabi.newpipe.ktx - -import org.junit.Assert.assertEquals -import org.junit.Test -import java.time.LocalDate -import java.time.OffsetDateTime -import java.time.ZoneOffset -import java.util.Calendar -import java.util.TimeZone - -class OffsetDateTimeToCalendarTest { - @Test - fun testRelativeTimeWithCurrentOffsetDateTime() { - val calendar = LocalDate.of(2020, 1, 1).atStartOfDay().atOffset(ZoneOffset.UTC) - .toCalendar() - - assertEquals(2020, calendar[Calendar.YEAR]) - assertEquals(0, calendar[Calendar.MONTH]) - assertEquals(1, calendar[Calendar.DAY_OF_MONTH]) - assertEquals(0, calendar[Calendar.HOUR]) - assertEquals(0, calendar[Calendar.MINUTE]) - assertEquals(0, calendar[Calendar.SECOND]) - assertEquals(TimeZone.getTimeZone("UTC"), calendar.timeZone) - } - - @Test(expected = IllegalArgumentException::class) - fun testRelativeTimeWithFarOffOffsetDateTime() { - OffsetDateTime.MAX.minusYears(1).toCalendar() - } -} diff --git a/app/src/test/java/org/schabi/newpipe/util/LocalizationTest.kt b/app/src/test/java/org/schabi/newpipe/util/LocalizationTest.kt index 9f7e74649..ab6396951 100644 --- a/app/src/test/java/org/schabi/newpipe/util/LocalizationTest.kt +++ b/app/src/test/java/org/schabi/newpipe/util/LocalizationTest.kt @@ -3,34 +3,22 @@ package org.schabi.newpipe.util import org.junit.Assert.assertEquals import org.junit.Test import org.ocpsoft.prettytime.PrettyTime -import java.text.SimpleDateFormat +import java.time.LocalDate import java.time.OffsetDateTime import java.time.ZoneOffset -import java.util.GregorianCalendar import java.util.Locale class LocalizationTest { - - @Test - fun `After initializing pretty time relativeTime() with a Calendar must work`() { - val reference = SimpleDateFormat("yyyy/MM/dd").parse("2021/1/1") - Localization.initPrettyTime(PrettyTime(reference, Locale.ENGLISH)) - - val actual = Localization.relativeTime(GregorianCalendar(2021, 1, 6)) - - // yes this assertion is true, even if it should be 5 days, it works as it is. Future research required. - assertEquals("1 month from now", actual) - } - @Test(expected = NullPointerException::class) fun `relativeTime() must fail without initializing pretty time`() { - Localization.relativeTime(GregorianCalendar(2021, 1, 6)) + Localization.relativeTime(OffsetDateTime.of(2021, 1, 6, 0, 0, 0, 0, ZoneOffset.UTC)) } @Test fun `relativeTime() with a OffsetDateTime must work`() { - val reference = SimpleDateFormat("yyyy/MM/dd").parse("2021/1/1") - Localization.initPrettyTime(PrettyTime(reference, Locale.ENGLISH)) + val prettyTime = PrettyTime(LocalDate.of(2021, 1, 1), ZoneOffset.UTC) + prettyTime.locale = Locale.ENGLISH + Localization.initPrettyTime(prettyTime) val offset = OffsetDateTime.of(2021, 1, 6, 0, 0, 0, 0, ZoneOffset.UTC) val actual = Localization.relativeTime(offset)