Skip to content

Commit 061c4bb

Browse files
Use PrettyTime's formatUnrounded(OffsetDateTime) method.
1 parent b8bcfb7 commit 061c4bb

File tree

7 files changed

+16
-98
lines changed

7 files changed

+16
-98
lines changed

app/src/main/java/org/schabi/newpipe/ktx/OffsetDateTime.kt

-29
This file was deleted.

app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt

+6-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import org.schabi.newpipe.ktx.animate
4646
import org.schabi.newpipe.local.feed.service.FeedLoadService
4747
import org.schabi.newpipe.report.UserAction
4848
import org.schabi.newpipe.util.Localization
49-
import java.util.Calendar
49+
import java.time.OffsetDateTime
5050

5151
class FeedFragment : BaseListFragment<FeedState, Unit>() {
5252
private var _feedBinding: FragmentFeedBinding? = null
@@ -63,7 +63,7 @@ class FeedFragment : BaseListFragment<FeedState, Unit>() {
6363

6464
private var groupId = FeedGroupEntity.GROUP_ALL_ID
6565
private var groupName = ""
66-
private var oldestSubscriptionUpdate: Calendar? = null
66+
private var oldestSubscriptionUpdate: OffsetDateTime? = null
6767

6868
init {
6969
setHasOptionsMenu(true)
@@ -304,12 +304,10 @@ class FeedFragment : BaseListFragment<FeedState, Unit>() {
304304
}
305305

306306
private fun updateRefreshViewState() {
307-
val oldestSubscriptionUpdateText = when {
308-
oldestSubscriptionUpdate != null -> Localization.relativeTime(oldestSubscriptionUpdate!!)
309-
else -> ""
310-
}
311-
312-
feedBinding.refreshText.text = getString(R.string.feed_oldest_subscription_update, oldestSubscriptionUpdateText)
307+
feedBinding.refreshText.text = getString(
308+
R.string.feed_oldest_subscription_update,
309+
oldestSubscriptionUpdate?.let { Localization.relativeTime(it) } ?: ""
310+
)
313311
}
314312

315313
// /////////////////////////////////////////////////////////////////////////

app/src/main/java/org/schabi/newpipe/local/feed/FeedState.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package org.schabi.newpipe.local.feed
22

33
import androidx.annotation.StringRes
44
import org.schabi.newpipe.extractor.stream.StreamInfoItem
5-
import java.util.Calendar
5+
import java.time.OffsetDateTime
66

77
sealed class FeedState {
88
data class ProgressState(
@@ -13,7 +13,7 @@ sealed class FeedState {
1313

1414
data class LoadedState(
1515
val items: List<StreamInfoItem>,
16-
val oldestUpdate: Calendar? = null,
16+
val oldestUpdate: OffsetDateTime? = null,
1717
val notLoadedCount: Long,
1818
val itemsErrors: List<Throwable> = emptyList()
1919
) : FeedState()

app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt

+2-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import io.reactivex.rxjava3.functions.Function4
1111
import io.reactivex.rxjava3.schedulers.Schedulers
1212
import org.schabi.newpipe.database.feed.model.FeedGroupEntity
1313
import org.schabi.newpipe.extractor.stream.StreamInfoItem
14-
import org.schabi.newpipe.ktx.toCalendar
1514
import org.schabi.newpipe.local.feed.service.FeedEventManager
1615
import org.schabi.newpipe.local.feed.service.FeedEventManager.Event.ErrorResultEvent
1716
import org.schabi.newpipe.local.feed.service.FeedEventManager.Event.IdleEvent
@@ -48,13 +47,11 @@ class FeedViewModel(applicationContext: Context, val groupId: Long = FeedGroupEn
4847
.subscribeOn(Schedulers.io())
4948
.observeOn(AndroidSchedulers.mainThread())
5049
.subscribe { (event, listFromDB, notLoadedCount, oldestUpdate) ->
51-
val oldestUpdateCalendar = oldestUpdate?.toCalendar()
52-
5350
mutableStateLiveData.postValue(
5451
when (event) {
55-
is IdleEvent -> FeedState.LoadedState(listFromDB, oldestUpdateCalendar, notLoadedCount)
52+
is IdleEvent -> FeedState.LoadedState(listFromDB, oldestUpdate, notLoadedCount)
5653
is ProgressEvent -> FeedState.ProgressState(event.currentProgress, event.maxProgress, event.progressMessage)
57-
is SuccessResultEvent -> FeedState.LoadedState(listFromDB, oldestUpdateCalendar, notLoadedCount, event.itemsErrors)
54+
is SuccessResultEvent -> FeedState.LoadedState(listFromDB, oldestUpdate, notLoadedCount, event.itemsErrors)
5855
is ErrorResultEvent -> FeedState.ErrorState(event.error)
5956
}
6057
)

app/src/main/java/org/schabi/newpipe/util/Localization.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.schabi.newpipe.R;
2121
import org.schabi.newpipe.extractor.ListExtractor;
2222
import org.schabi.newpipe.extractor.localization.ContentCountry;
23-
import org.schabi.newpipe.ktx.OffsetDateTimeKt;
2423

2524
import java.math.BigDecimal;
2625
import java.math.RoundingMode;
@@ -30,7 +29,6 @@
3029
import java.time.format.DateTimeFormatter;
3130
import java.time.format.FormatStyle;
3231
import java.util.Arrays;
33-
import java.util.Calendar;
3432
import java.util.List;
3533
import java.util.Locale;
3634

@@ -314,11 +312,7 @@ public static PrettyTime resolvePrettyTime(final Context context) {
314312
}
315313

316314
public static String relativeTime(final OffsetDateTime offsetDateTime) {
317-
return relativeTime(OffsetDateTimeKt.toCalendar(offsetDateTime));
318-
}
319-
320-
public static String relativeTime(final Calendar calendarTime) {
321-
return prettyTime.formatUnrounded(calendarTime);
315+
return prettyTime.formatUnrounded(offsetDateTime);
322316
}
323317

324318
private static void changeAppLanguage(final Locale loc, final Resources res) {

app/src/test/java/org/schabi/newpipe/ktx/OffsetDateTimeToCalendarTest.kt

-30
This file was deleted.

app/src/test/java/org/schabi/newpipe/util/LocalizationTest.kt

+5-17
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,22 @@ package org.schabi.newpipe.util
33
import org.junit.Assert.assertEquals
44
import org.junit.Test
55
import org.ocpsoft.prettytime.PrettyTime
6-
import java.text.SimpleDateFormat
6+
import java.time.LocalDate
77
import java.time.OffsetDateTime
88
import java.time.ZoneOffset
9-
import java.util.GregorianCalendar
109
import java.util.Locale
1110

1211
class LocalizationTest {
13-
14-
@Test
15-
fun `After initializing pretty time relativeTime() with a Calendar must work`() {
16-
val reference = SimpleDateFormat("yyyy/MM/dd").parse("2021/1/1")
17-
Localization.initPrettyTime(PrettyTime(reference, Locale.ENGLISH))
18-
19-
val actual = Localization.relativeTime(GregorianCalendar(2021, 1, 6))
20-
21-
// yes this assertion is true, even if it should be 5 days, it works as it is. Future research required.
22-
assertEquals("1 month from now", actual)
23-
}
24-
2512
@Test(expected = NullPointerException::class)
2613
fun `relativeTime() must fail without initializing pretty time`() {
27-
Localization.relativeTime(GregorianCalendar(2021, 1, 6))
14+
Localization.relativeTime(OffsetDateTime.of(2021, 1, 6, 0, 0, 0, 0, ZoneOffset.UTC))
2815
}
2916

3017
@Test
3118
fun `relativeTime() with a OffsetDateTime must work`() {
32-
val reference = SimpleDateFormat("yyyy/MM/dd").parse("2021/1/1")
33-
Localization.initPrettyTime(PrettyTime(reference, Locale.ENGLISH))
19+
val prettyTime = PrettyTime(LocalDate.of(2021, 1, 1), ZoneOffset.UTC)
20+
prettyTime.locale = Locale.ENGLISH
21+
Localization.initPrettyTime(prettyTime)
3422

3523
val offset = OffsetDateTime.of(2021, 1, 6, 0, 0, 0, 0, ZoneOffset.UTC)
3624
val actual = Localization.relativeTime(offset)

0 commit comments

Comments
 (0)