Skip to content

Commit 88b687f

Browse files
Refactor codebase to use Instant instead of OffsetDateTime
1 parent fd3f030 commit 88b687f

25 files changed

Lines changed: 160 additions & 185 deletions

app/src/androidTest/java/org/schabi/newpipe/database/DatabaseMigrationTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class DatabaseMigrationTest {
144144
assertEquals(DEFAULT_THUMBNAIL, streamFromMigratedDatabase.thumbnailUrl)
145145
assertNull(streamFromMigratedDatabase.viewCount)
146146
assertNull(streamFromMigratedDatabase.textualUploadDate)
147-
assertNull(streamFromMigratedDatabase.uploadDate)
147+
assertNull(streamFromMigratedDatabase.uploadInstant)
148148
assertNull(streamFromMigratedDatabase.isUploadDateApproximation)
149149

150150
val secondStreamFromMigratedDatabase = listFromDB[1]
@@ -158,7 +158,7 @@ class DatabaseMigrationTest {
158158
assertEquals("", secondStreamFromMigratedDatabase.thumbnailUrl)
159159
assertNull(secondStreamFromMigratedDatabase.viewCount)
160160
assertNull(secondStreamFromMigratedDatabase.textualUploadDate)
161-
assertNull(secondStreamFromMigratedDatabase.uploadDate)
161+
assertNull(secondStreamFromMigratedDatabase.uploadInstant)
162162
assertNull(secondStreamFromMigratedDatabase.isUploadDateApproximation)
163163
}
164164

app/src/main/java/org/schabi/newpipe/database/Converters.kt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,27 @@ import androidx.room.TypeConverter
44
import org.schabi.newpipe.extractor.stream.StreamType
55
import org.schabi.newpipe.local.subscription.FeedGroupIcon
66
import java.time.Instant
7-
import java.time.OffsetDateTime
8-
import java.time.ZoneOffset
97

108
class Converters {
119
/**
12-
* Convert a long value to a [OffsetDateTime].
10+
* Convert a long value to an [Instant].
1311
*
1412
* @param value the long value
15-
* @return the `OffsetDateTime`
13+
* @return the `Instant`
1614
*/
1715
@TypeConverter
18-
fun offsetDateTimeFromTimestamp(value: Long?): OffsetDateTime? {
19-
return value?.let { OffsetDateTime.ofInstant(Instant.ofEpochMilli(it), ZoneOffset.UTC) }
16+
fun timestampToInstant(value: Long?): Instant? {
17+
return value?.let { Instant.ofEpochMilli(it) }
2018
}
2119

2220
/**
23-
* Convert a [OffsetDateTime] to a long value.
21+
* Convert an [Instant] to a long value.
2422
*
25-
* @param offsetDateTime the `OffsetDateTime`
23+
* @param instant the `Instant`
2624
* @return the long value
2725
*/
2826
@TypeConverter
29-
fun offsetDateTimeToTimestamp(offsetDateTime: OffsetDateTime?): Long? {
30-
return offsetDateTime?.withOffsetSameInstant(ZoneOffset.UTC)?.toInstant()?.toEpochMilli()
31-
}
27+
fun instantToTimestamp(instant: Instant?) = instant?.toEpochMilli()
3228

3329
@TypeConverter
3430
fun streamTypeOf(value: String): StreamType {

app/src/main/java/org/schabi/newpipe/database/feed/dao/FeedDAO.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import org.schabi.newpipe.database.stream.StreamWithState
1515
import org.schabi.newpipe.database.stream.model.StreamStateEntity
1616
import org.schabi.newpipe.database.subscription.NotificationMode
1717
import org.schabi.newpipe.database.subscription.SubscriptionEntity
18-
import java.time.OffsetDateTime
18+
import java.time.Instant
1919

2020
@Dao
2121
abstract class FeedDAO {
@@ -90,7 +90,7 @@ abstract class FeedDAO {
9090
groupId: Long,
9191
includePlayed: Boolean,
9292
includePartiallyPlayed: Boolean,
93-
uploadDateBefore: OffsetDateTime?
93+
uploadDateBefore: Instant?
9494
): Maybe<List<StreamWithState>>
9595

9696
/**
@@ -99,7 +99,7 @@ abstract class FeedDAO {
9999
*
100100
* One stream per uploader is kept because it is needed as reference
101101
* when fetching new streams to check if they are new or not.
102-
* @param offsetDateTime the newest date to keep, older streams are removed
102+
* @param instant the newest date to keep, older streams are removed
103103
*/
104104
@Query(
105105
"""
@@ -115,11 +115,11 @@ abstract class FeedDAO {
115115
INNER JOIN feed f
116116
ON s.uid = f.stream_id
117117
118-
WHERE s.upload_date < :offsetDateTime
118+
WHERE s.upload_date < :instant
119119
AND s.upload_date <> max_upload_date))
120120
"""
121121
)
122-
abstract fun unlinkStreamsOlderThan(offsetDateTime: OffsetDateTime)
122+
abstract fun unlinkStreamsOlderThan(instant: Instant)
123123

124124
@Query(
125125
"""
@@ -168,13 +168,13 @@ abstract class FeedDAO {
168168
ON fgs.subscription_id = lu.subscription_id AND fgs.group_id = :groupId
169169
"""
170170
)
171-
abstract fun oldestSubscriptionUpdate(groupId: Long): Flowable<List<OffsetDateTime>>
171+
abstract fun getOldestSubscriptionUpdate(groupId: Long): Flowable<List<Instant>>
172172

173173
@Query("SELECT MIN(last_updated) FROM feed_last_updated")
174-
abstract fun oldestSubscriptionUpdateFromAll(): Flowable<List<OffsetDateTime>>
174+
abstract fun getOldestSubscriptionUpdateFromAll(): Flowable<List<Instant>>
175175

176176
@Query("SELECT COUNT(*) FROM feed_last_updated WHERE last_updated IS NULL")
177-
abstract fun notLoadedCount(): Flowable<Long>
177+
abstract fun getNotLoadedCount(): Flowable<Long>
178178

179179
@Query(
180180
"""
@@ -189,7 +189,7 @@ abstract class FeedDAO {
189189
WHERE lu.last_updated IS NULL
190190
"""
191191
)
192-
abstract fun notLoadedCountForGroup(groupId: Long): Flowable<Long>
192+
abstract fun getNotLoadedCountForGroup(groupId: Long): Flowable<Long>
193193

194194
@Query(
195195
"""
@@ -201,7 +201,7 @@ abstract class FeedDAO {
201201
WHERE lu.last_updated IS NULL OR lu.last_updated < :outdatedThreshold
202202
"""
203203
)
204-
abstract fun getAllOutdated(outdatedThreshold: OffsetDateTime): Flowable<List<SubscriptionEntity>>
204+
abstract fun getAllOutdated(outdatedThreshold: Instant): Flowable<List<SubscriptionEntity>>
205205

206206
@Query(
207207
"""
@@ -216,7 +216,7 @@ abstract class FeedDAO {
216216
WHERE lu.last_updated IS NULL OR lu.last_updated < :outdatedThreshold
217217
"""
218218
)
219-
abstract fun getAllOutdatedForGroup(groupId: Long, outdatedThreshold: OffsetDateTime): Flowable<List<SubscriptionEntity>>
219+
abstract fun getAllOutdatedForGroup(groupId: Long, outdatedThreshold: Instant): Flowable<List<SubscriptionEntity>>
220220

221221
@Query(
222222
"""
@@ -231,7 +231,7 @@ abstract class FeedDAO {
231231
"""
232232
)
233233
abstract fun getOutdatedWithNotificationMode(
234-
outdatedThreshold: OffsetDateTime,
234+
outdatedThreshold: Instant,
235235
@NotificationMode notificationMode: Int
236236
): Flowable<List<SubscriptionEntity>>
237237
}

app/src/main/java/org/schabi/newpipe/database/feed/model/FeedLastUpdatedEntity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import androidx.room.PrimaryKey
77
import org.schabi.newpipe.database.feed.model.FeedLastUpdatedEntity.Companion.FEED_LAST_UPDATED_TABLE
88
import org.schabi.newpipe.database.feed.model.FeedLastUpdatedEntity.Companion.SUBSCRIPTION_ID
99
import org.schabi.newpipe.database.subscription.SubscriptionEntity
10-
import java.time.OffsetDateTime
10+
import java.time.Instant
1111

1212
@Entity(
1313
tableName = FEED_LAST_UPDATED_TABLE,
@@ -26,7 +26,7 @@ data class FeedLastUpdatedEntity(
2626
var subscriptionId: Long,
2727

2828
@ColumnInfo(name = LAST_UPDATED)
29-
var lastUpdated: OffsetDateTime? = null
29+
var lastUpdated: Instant? = null
3030
) {
3131
companion object {
3232
const val FEED_LAST_UPDATED_TABLE = "feed_last_updated"

app/src/main/java/org/schabi/newpipe/database/history/model/SearchHistoryEntry.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,24 @@ import androidx.room.Entity
55
import androidx.room.Ignore
66
import androidx.room.Index
77
import androidx.room.PrimaryKey
8-
import java.time.OffsetDateTime
8+
import java.time.Instant
99

1010
@Entity(
1111
tableName = SearchHistoryEntry.TABLE_NAME,
1212
indices = [Index(value = [SearchHistoryEntry.SEARCH])]
1313
)
1414
data class SearchHistoryEntry(
15-
@field:ColumnInfo(name = CREATION_DATE) var creationDate: OffsetDateTime?,
16-
@field:ColumnInfo(
17-
name = SERVICE_ID
18-
) var serviceId: Int,
19-
@field:ColumnInfo(name = SEARCH) var search: String?
15+
@ColumnInfo(name = CREATION_DATE) var creationInstant: Instant?,
16+
@ColumnInfo(name = SERVICE_ID) var serviceId: Int,
17+
@ColumnInfo(name = SEARCH) var search: String?
2018
) {
2119
@ColumnInfo(name = ID)
2220
@PrimaryKey(autoGenerate = true)
2321
var id: Long = 0
2422

2523
@Ignore
2624
fun hasEqualValues(otherEntry: SearchHistoryEntry): Boolean {
27-
return (
28-
serviceId == otherEntry.serviceId &&
29-
search == otherEntry.search
30-
)
25+
return serviceId == otherEntry.serviceId && search == otherEntry.search
3126
}
3227

3328
companion object {

app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntity.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import org.schabi.newpipe.database.stream.model.StreamEntity;
1010

11-
import java.time.OffsetDateTime;
11+
import java.time.Instant;
1212

1313
import static androidx.room.ForeignKey.CASCADE;
1414
import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.JOIN_STREAM_ID;
@@ -36,21 +36,21 @@ public class StreamHistoryEntity {
3636

3737
@NonNull
3838
@ColumnInfo(name = STREAM_ACCESS_DATE)
39-
private OffsetDateTime accessDate;
39+
private Instant accessInstant;
4040

4141
@ColumnInfo(name = STREAM_REPEAT_COUNT)
4242
private long repeatCount;
4343

4444
/**
4545
* @param streamUid the stream id this history item will refer to
46-
* @param accessDate the last time the stream was accessed
46+
* @param accessInstant the last time the stream was accessed
4747
* @param repeatCount the total number of views this stream received
4848
*/
4949
public StreamHistoryEntity(final long streamUid,
50-
@NonNull final OffsetDateTime accessDate,
50+
@NonNull final Instant accessInstant,
5151
final long repeatCount) {
5252
this.streamUid = streamUid;
53-
this.accessDate = accessDate;
53+
this.accessInstant = accessInstant;
5454
this.repeatCount = repeatCount;
5555
}
5656

@@ -63,12 +63,12 @@ public void setStreamUid(final long streamUid) {
6363
}
6464

6565
@NonNull
66-
public OffsetDateTime getAccessDate() {
67-
return accessDate;
66+
public Instant getAccessInstant() {
67+
return accessInstant;
6868
}
6969

70-
public void setAccessDate(@NonNull final OffsetDateTime accessDate) {
71-
this.accessDate = accessDate;
70+
public void setAccessInstant(@NonNull final Instant accessInstant) {
71+
this.accessInstant = accessInstant;
7272
}
7373

7474
public long getRepeatCount() {

app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntry.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import androidx.room.Embedded
55
import org.schabi.newpipe.database.stream.model.StreamEntity
66
import org.schabi.newpipe.extractor.stream.StreamInfoItem
77
import org.schabi.newpipe.util.image.ImageStrategy
8-
import java.time.OffsetDateTime
8+
import java.time.Instant
99

1010
data class StreamHistoryEntry(
1111
@Embedded
@@ -15,21 +15,11 @@ data class StreamHistoryEntry(
1515
val streamId: Long,
1616

1717
@ColumnInfo(name = StreamHistoryEntity.STREAM_ACCESS_DATE)
18-
val accessDate: OffsetDateTime,
18+
val accessInstant: Instant,
1919

2020
@ColumnInfo(name = StreamHistoryEntity.STREAM_REPEAT_COUNT)
2121
val repeatCount: Long
2222
) {
23-
24-
fun toStreamHistoryEntity(): StreamHistoryEntity {
25-
return StreamHistoryEntity(streamId, accessDate, repeatCount)
26-
}
27-
28-
fun hasEqualValues(other: StreamHistoryEntry): Boolean {
29-
return this.streamEntity.uid == other.streamEntity.uid && streamId == other.streamId &&
30-
accessDate.isEqual(other.accessDate)
31-
}
32-
3323
fun toStreamInfoItem(): StreamInfoItem =
3424
StreamInfoItem(
3525
streamEntity.serviceId,

app/src/main/java/org/schabi/newpipe/database/stream/StreamStatisticsEntry.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import org.schabi.newpipe.database.stream.model.StreamEntity
88
import org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_PROGRESS_MILLIS
99
import org.schabi.newpipe.extractor.stream.StreamInfoItem
1010
import org.schabi.newpipe.util.image.ImageStrategy
11-
import java.time.OffsetDateTime
11+
import java.time.Instant
1212

1313
class StreamStatisticsEntry(
1414
@Embedded
@@ -21,7 +21,7 @@ class StreamStatisticsEntry(
2121
val streamId: Long,
2222

2323
@ColumnInfo(name = STREAM_LATEST_DATE)
24-
val latestAccessDate: OffsetDateTime,
24+
val latestAccessInstant: Instant,
2525

2626
@ColumnInfo(name = STREAM_WATCH_COUNT)
2727
val watchCount: Long

app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamDAO.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.schabi.newpipe.database.stream.model.StreamEntity
1313
import org.schabi.newpipe.database.stream.model.StreamEntity.Companion.STREAM_ID
1414
import org.schabi.newpipe.extractor.stream.StreamType
1515
import org.schabi.newpipe.util.StreamTypeUtil
16-
import java.time.OffsetDateTime
16+
import java.time.Instant
1717

1818
@Dao
1919
abstract class StreamDAO : BasicDAO<StreamEntity> {
@@ -95,9 +95,9 @@ abstract class StreamDAO : BasicDAO<StreamEntity> {
9595
// Use the existent upload date if the newer stream does not have a better precision
9696
// (i.e. is an approximation). This is done to prevent unnecessary changes.
9797
val hasBetterPrecision =
98-
newerStream.uploadDate != null && newerStream.isUploadDateApproximation != true
99-
if (existentMinimalStream.uploadDate != null && !hasBetterPrecision) {
100-
newerStream.uploadDate = existentMinimalStream.uploadDate
98+
newerStream.uploadInstant != null && newerStream.isUploadDateApproximation != true
99+
if (existentMinimalStream.uploadInstant != null && !hasBetterPrecision) {
100+
newerStream.uploadInstant = existentMinimalStream.uploadInstant
101101
newerStream.textualUploadDate = existentMinimalStream.textualUploadDate
102102
newerStream.isUploadDateApproximation = existentMinimalStream.isUploadDateApproximation
103103
}
@@ -138,7 +138,7 @@ abstract class StreamDAO : BasicDAO<StreamEntity> {
138138
var textualUploadDate: String? = null,
139139

140140
@ColumnInfo(name = StreamEntity.STREAM_UPLOAD_DATE)
141-
var uploadDate: OffsetDateTime? = null,
141+
var uploadInstant: Instant? = null,
142142

143143
@ColumnInfo(name = StreamEntity.STREAM_IS_UPLOAD_DATE_APPROXIMATION)
144144
var isUploadDateApproximation: Boolean? = null,

app/src/main/java/org/schabi/newpipe/database/stream/model/StreamEntity.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import org.schabi.newpipe.extractor.stream.StreamType
1515
import org.schabi.newpipe.player.playqueue.PlayQueueItem
1616
import org.schabi.newpipe.util.image.ImageStrategy
1717
import java.io.Serializable
18-
import java.time.OffsetDateTime
18+
import java.time.Instant
1919

2020
@Entity(
2121
tableName = STREAM_TABLE,
@@ -59,7 +59,7 @@ data class StreamEntity(
5959
var textualUploadDate: String? = null,
6060

6161
@ColumnInfo(name = STREAM_UPLOAD_DATE)
62-
var uploadDate: OffsetDateTime? = null,
62+
var uploadInstant: Instant? = null,
6363

6464
@ColumnInfo(name = STREAM_IS_UPLOAD_DATE_APPROXIMATION)
6565
var isUploadDateApproximation: Boolean? = null
@@ -69,8 +69,9 @@ data class StreamEntity(
6969
serviceId = item.serviceId, url = item.url, title = item.name,
7070
streamType = item.streamType, duration = item.duration, uploader = item.uploaderName,
7171
uploaderUrl = item.uploaderUrl,
72-
thumbnailUrl = ImageStrategy.imageListToDbUrl(item.thumbnails), viewCount = item.viewCount,
73-
textualUploadDate = item.textualUploadDate, uploadDate = item.uploadDate?.offsetDateTime(),
72+
thumbnailUrl = ImageStrategy.imageListToDbUrl(item.thumbnails),
73+
viewCount = item.viewCount, textualUploadDate = item.textualUploadDate,
74+
uploadInstant = item.uploadDate?.instant,
7475
isUploadDateApproximation = item.uploadDate?.isApproximation
7576
)
7677

@@ -79,8 +80,9 @@ data class StreamEntity(
7980
serviceId = info.serviceId, url = info.url, title = info.name,
8081
streamType = info.streamType, duration = info.duration, uploader = info.uploaderName,
8182
uploaderUrl = info.uploaderUrl,
82-
thumbnailUrl = ImageStrategy.imageListToDbUrl(info.thumbnails), viewCount = info.viewCount,
83-
textualUploadDate = info.textualUploadDate, uploadDate = info.uploadDate?.offsetDateTime(),
83+
thumbnailUrl = ImageStrategy.imageListToDbUrl(info.thumbnails),
84+
viewCount = info.viewCount,
85+
textualUploadDate = info.textualUploadDate, uploadInstant = info.uploadDate?.instant,
8486
isUploadDateApproximation = info.uploadDate?.isApproximation
8587
)
8688

@@ -101,7 +103,7 @@ data class StreamEntity(
101103

102104
if (viewCount != null) item.viewCount = viewCount as Long
103105
item.textualUploadDate = textualUploadDate
104-
item.uploadDate = uploadDate?.let {
106+
item.uploadDate = uploadInstant?.let {
105107
DateWrapper(it, isUploadDateApproximation ?: false)
106108
}
107109

0 commit comments

Comments
 (0)