Skip to content

Commit b7cef3b

Browse files
Use ZonedDateTime instead of Calendar where possible.
1 parent a6d6bb1 commit b7cef3b

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.schabi.newpipe.ktx
2+
3+
import java.time.ZonedDateTime
4+
import java.util.Date
5+
6+
fun ZonedDateTime.toLegacyDate(): Date = Date.from(toInstant())

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import io.reactivex.Flowable
77
import io.reactivex.Maybe
88
import io.reactivex.android.schedulers.AndroidSchedulers
99
import io.reactivex.schedulers.Schedulers
10+
import java.time.LocalDate
11+
import java.time.ZoneId
12+
import java.time.ZonedDateTime
1013
import java.util.Calendar
1114
import java.util.Date
1215
import org.schabi.newpipe.MainActivity.DEBUG
@@ -17,6 +20,7 @@ import org.schabi.newpipe.database.feed.model.FeedLastUpdatedEntity
1720
import org.schabi.newpipe.database.stream.model.StreamEntity
1821
import org.schabi.newpipe.extractor.stream.StreamInfoItem
1922
import org.schabi.newpipe.extractor.stream.StreamType
23+
import org.schabi.newpipe.ktx.toLegacyDate
2024
import org.schabi.newpipe.local.subscription.FeedGroupIcon
2125

2226
class FeedDatabaseManager(context: Context) {
@@ -29,13 +33,8 @@ class FeedDatabaseManager(context: Context) {
2933
/**
3034
* Only items that are newer than this will be saved.
3135
*/
32-
val FEED_OLDEST_ALLOWED_DATE: Calendar = Calendar.getInstance().apply {
33-
add(Calendar.WEEK_OF_YEAR, -13)
34-
set(Calendar.HOUR_OF_DAY, 0)
35-
set(Calendar.MINUTE, 0)
36-
set(Calendar.SECOND, 0)
37-
set(Calendar.MILLISECOND, 0)
38-
}
36+
val FEED_OLDEST_ALLOWED_DATE: ZonedDateTime =
37+
LocalDate.now().minusWeeks(13).atStartOfDay(ZoneId.systemDefault())
3938
}
4039

4140
fun groups() = feedGroupTable.getAll()
@@ -73,7 +72,7 @@ class FeedDatabaseManager(context: Context) {
7372
fun upsertAll(
7473
subscriptionId: Long,
7574
items: List<StreamInfoItem>,
76-
oldestAllowedDate: Date = FEED_OLDEST_ALLOWED_DATE.time
75+
oldestAllowedDate: Date = FEED_OLDEST_ALLOWED_DATE.toLegacyDate()
7776
) {
7877
val itemsToInsert = ArrayList<StreamInfoItem>()
7978
loop@ for (streamItem in items) {
@@ -99,7 +98,7 @@ class FeedDatabaseManager(context: Context) {
9998
feedTable.setLastUpdatedForSubscription(FeedLastUpdatedEntity(subscriptionId, Calendar.getInstance().time))
10099
}
101100

102-
fun removeOrphansOrOlderStreams(oldestAllowedDate: Date = FEED_OLDEST_ALLOWED_DATE.time) {
101+
fun removeOrphansOrOlderStreams(oldestAllowedDate: Date = FEED_OLDEST_ALLOWED_DATE.toLegacyDate()) {
103102
feedTable.unlinkStreamsOlderThan(oldestAllowedDate)
104103
streamTable.deleteOrphans()
105104
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import io.reactivex.functions.Function
4141
import io.reactivex.processors.PublishProcessor
4242
import io.reactivex.schedulers.Schedulers
4343
import java.io.IOException
44-
import java.util.Calendar
44+
import java.time.ZonedDateTime
4545
import java.util.concurrent.TimeUnit
4646
import java.util.concurrent.atomic.AtomicBoolean
4747
import java.util.concurrent.atomic.AtomicInteger
@@ -53,6 +53,7 @@ import org.schabi.newpipe.database.feed.model.FeedGroupEntity
5353
import org.schabi.newpipe.extractor.ListInfo
5454
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException
5555
import org.schabi.newpipe.extractor.stream.StreamInfoItem
56+
import org.schabi.newpipe.ktx.toLegacyDate
5657
import org.schabi.newpipe.local.feed.FeedDatabaseManager
5758
import org.schabi.newpipe.local.feed.service.FeedEventManager.Event.ErrorResultEvent
5859
import org.schabi.newpipe.local.feed.service.FeedEventManager.Event.IdleEvent
@@ -172,9 +173,8 @@ class FeedLoadService : Service() {
172173
private fun startLoading(groupId: Long = FeedGroupEntity.GROUP_ALL_ID, useFeedExtractor: Boolean, thresholdOutdatedSeconds: Int) {
173174
feedResultsHolder = ResultsHolder()
174175

175-
val outdatedThreshold = Calendar.getInstance().apply {
176-
add(Calendar.SECOND, -thresholdOutdatedSeconds)
177-
}.time
176+
val outdatedThreshold = ZonedDateTime.now().minusSeconds(thresholdOutdatedSeconds.toLong())
177+
.toLegacyDate()
178178

179179
val subscriptions = when (groupId) {
180180
FeedGroupEntity.GROUP_ALL_ID -> feedDatabaseManager.outdatedSubscriptions(outdatedThreshold)

0 commit comments

Comments
 (0)