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

Update prettytime. #5230

Merged
merged 2 commits into from
Mar 18, 2021
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
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
29 changes: 0 additions & 29 deletions app/src/main/java/org/schabi/newpipe/ktx/OffsetDateTime.kt

This file was deleted.

14 changes: 6 additions & 8 deletions app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<FeedState, Unit>() {
private var _feedBinding: FragmentFeedBinding? = null
Expand All @@ -58,7 +58,7 @@ class FeedFragment : BaseListFragment<FeedState, Unit>() {

private var groupId = FeedGroupEntity.GROUP_ALL_ID
private var groupName = ""
private var oldestSubscriptionUpdate: Calendar? = null
private var oldestSubscriptionUpdate: OffsetDateTime? = null
Isira-Seneviratne marked this conversation as resolved.
Show resolved Hide resolved

init {
setHasOptionsMenu(true)
Expand Down Expand Up @@ -275,12 +275,10 @@ class FeedFragment : BaseListFragment<FeedState, Unit>() {
}

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) } ?: "—"
)
}

// /////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/schabi/newpipe/local/feed/FeedState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -13,7 +13,7 @@ sealed class FeedState {

data class LoadedState(
val items: List<StreamInfoItem>,
val oldestUpdate: Calendar? = null,
val oldestUpdate: OffsetDateTime? = null,
val notLoadedCount: Long,
val itemsErrors: List<Throwable> = emptyList()
) : FeedState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
)
Expand Down
8 changes: 1 addition & 7 deletions app/src/main/java/org/schabi/newpipe/util/Localization.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
Expand All @@ -30,7 +29,6 @@
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;

Expand Down Expand Up @@ -314,11 +312,7 @@ public static PrettyTime resolvePrettyTime(final Context context) {
}

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) {
Expand Down

This file was deleted.

22 changes: 5 additions & 17 deletions app/src/test/java/org/schabi/newpipe/util/LocalizationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down