Skip to content

Commit e877997

Browse files
Improve constructors
1 parent 491b433 commit e877997

5 files changed

Lines changed: 85 additions & 94 deletions

File tree

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import org.schabi.newpipe.database.LocalItem
66
import org.schabi.newpipe.database.history.model.StreamHistoryEntity
77
import org.schabi.newpipe.database.stream.model.StreamEntity
88
import org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_PROGRESS_MILLIS
9-
import org.schabi.newpipe.extractor.stream.StreamInfoItem
10-
import org.schabi.newpipe.util.image.ImageStrategy
119
import java.time.OffsetDateTime
1210

1311
class StreamStatisticsEntry(
@@ -26,16 +24,6 @@ class StreamStatisticsEntry(
2624
@ColumnInfo(name = STREAM_WATCH_COUNT)
2725
val watchCount: Long
2826
) : LocalItem {
29-
fun toStreamInfoItem(): StreamInfoItem {
30-
val item = StreamInfoItem(streamEntity.serviceId, streamEntity.url, streamEntity.title, streamEntity.streamType)
31-
item.duration = streamEntity.duration
32-
item.uploaderName = streamEntity.uploader
33-
item.uploaderUrl = streamEntity.uploaderUrl
34-
item.thumbnails = ImageStrategy.dbUrlToImageList(streamEntity.thumbnailUrl)
35-
36-
return item
37-
}
38-
3927
override fun getLocalItemType(): LocalItem.LocalItemType {
4028
return LocalItem.LocalItemType.STATISTIC_STREAM_ITEM
4129
}

app/src/main/java/org/schabi/newpipe/local/history/HistoryViewModel.kt

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,12 @@ import kotlinx.coroutines.launch
1616
import kotlinx.coroutines.rx3.await
1717
import org.schabi.newpipe.NewPipeDatabase
1818
import org.schabi.newpipe.ui.components.items.Stream
19-
import org.schabi.newpipe.util.Localization
20-
import org.schabi.newpipe.util.ServiceHelper
21-
import java.time.format.DateTimeFormatter
22-
import java.time.format.FormatStyle
2319

2420
class HistoryViewModel(
2521
application: Application,
2622
private val savedStateHandle: SavedStateHandle,
2723
) : AndroidViewModel(application) {
2824
private val historyDao = NewPipeDatabase.getInstance(getApplication()).streamHistoryDAO()
29-
private val dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
3025

3126
val sortKey = savedStateHandle.getStateFlow(ORDER_KEY, SortKey.MOST_PLAYED)
3227
val historyItems = sortKey
@@ -38,16 +33,7 @@ class HistoryViewModel(
3833
}
3934
}.flow
4035
}
41-
.map { pagingData ->
42-
pagingData.map {
43-
val detail = Localization.concatenateStrings(
44-
Localization.shortViewCount(getApplication(), it.watchCount),
45-
dateTimeFormatter.format(it.latestAccessDate),
46-
ServiceHelper.getNameOfServiceById(it.streamEntity.serviceId),
47-
)
48-
Stream(it.streamEntity, detail, it.streamId)
49-
}
50-
}
36+
.map { pagingData -> pagingData.map { Stream(it) } }
5137
.flowOn(Dispatchers.IO)
5238
.cachedIn(viewModelScope)
5339

app/src/main/java/org/schabi/newpipe/ui/components/items/Info.kt

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,11 @@
11
package org.schabi.newpipe.ui.components.items
22

3-
import android.os.Parcelable
4-
import kotlinx.parcelize.Parcelize
5-
import org.schabi.newpipe.database.stream.model.StreamEntity
63
import org.schabi.newpipe.extractor.Image
74
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem
8-
import org.schabi.newpipe.extractor.stream.StreamInfoItem
9-
import org.schabi.newpipe.extractor.stream.StreamType
105
import org.schabi.newpipe.util.NO_SERVICE_ID
11-
import org.schabi.newpipe.util.image.ImageStrategy
12-
import java.util.concurrent.TimeUnit
136

147
sealed class Info
158

16-
@Parcelize
17-
class Stream(
18-
val serviceId: Int = NO_SERVICE_ID,
19-
val url: String = "",
20-
val name: String = "",
21-
val thumbnails: List<Image> = emptyList(),
22-
val uploaderName: String = "",
23-
val type: StreamType,
24-
val uploaderUrl: String? = null,
25-
val duration: Long = TimeUnit.HOURS.toSeconds(1),
26-
val detailText: String = "",
27-
val streamId: Long = -1,
28-
) : Info(), Parcelable {
29-
30-
constructor(item: StreamInfoItem, detailText: String) : this(
31-
item.serviceId, item.url, item.name, item.thumbnails, item.uploaderName.orEmpty(),
32-
item.streamType, item.uploaderUrl, item.duration, detailText
33-
)
34-
35-
constructor(entity: StreamEntity, detailText: String, streamId: Long) : this(
36-
entity.serviceId, entity.url, entity.title,
37-
ImageStrategy.dbUrlToImageList(entity.thumbnailUrl), entity.uploader,
38-
entity.streamType, entity.uploaderUrl, entity.duration, detailText, streamId
39-
)
40-
41-
fun toStreamInfoItem(): StreamInfoItem {
42-
val item = StreamInfoItem(serviceId, url, name, type)
43-
item.duration = duration
44-
item.uploaderName = uploaderName
45-
item.uploaderUrl = uploaderUrl
46-
item.thumbnails = thumbnails
47-
return item
48-
}
49-
}
50-
519
class Playlist(
5210
val serviceId: Int = NO_SERVICE_ID,
5311
val url: String = "",
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package org.schabi.newpipe.ui.components.items
2+
3+
import android.os.Parcelable
4+
import kotlinx.parcelize.Parcelize
5+
import org.schabi.newpipe.App
6+
import org.schabi.newpipe.database.stream.StreamStatisticsEntry
7+
import org.schabi.newpipe.extractor.Image
8+
import org.schabi.newpipe.extractor.stream.StreamInfoItem
9+
import org.schabi.newpipe.extractor.stream.StreamType
10+
import org.schabi.newpipe.util.Localization
11+
import org.schabi.newpipe.util.NO_SERVICE_ID
12+
import org.schabi.newpipe.util.ServiceHelper
13+
import org.schabi.newpipe.util.image.ImageStrategy
14+
import java.time.format.DateTimeFormatter
15+
import java.time.format.FormatStyle
16+
import java.util.concurrent.TimeUnit
17+
18+
@Parcelize
19+
class Stream(
20+
val serviceId: Int = NO_SERVICE_ID,
21+
val url: String = "",
22+
val name: String = "",
23+
val thumbnails: List<Image> = emptyList(),
24+
val uploaderName: String = "",
25+
val type: StreamType,
26+
val uploaderUrl: String? = null,
27+
val duration: Long = TimeUnit.HOURS.toSeconds(1),
28+
val detailText: String = "",
29+
val streamId: Long = -1,
30+
) : Info(), Parcelable {
31+
32+
constructor(item: StreamInfoItem) : this(
33+
item.serviceId, item.url, item.name, item.thumbnails, item.uploaderName.orEmpty(),
34+
item.streamType, item.uploaderUrl, item.duration, item.detailText
35+
)
36+
37+
constructor(entry: StreamStatisticsEntry) : this(
38+
entry.streamEntity.serviceId, entry.streamEntity.url, entry.streamEntity.title,
39+
ImageStrategy.dbUrlToImageList(entry.streamEntity.thumbnailUrl), entry.streamEntity.uploader,
40+
entry.streamEntity.streamType, entry.streamEntity.uploaderUrl, entry.streamEntity.duration,
41+
entry.detailText, entry.streamId
42+
)
43+
44+
fun toStreamInfoItem(): StreamInfoItem {
45+
val item = StreamInfoItem(serviceId, url, name, type)
46+
item.duration = duration
47+
item.uploaderName = uploaderName
48+
item.uploaderUrl = uploaderUrl
49+
item.thumbnails = thumbnails
50+
return item
51+
}
52+
}
53+
54+
private val StreamInfoItem.detailText: String
55+
get() {
56+
val context = App.instance
57+
val views = if (viewCount >= 0) {
58+
when (streamType) {
59+
StreamType.AUDIO_LIVE_STREAM -> Localization.listeningCount(context, viewCount)
60+
StreamType.LIVE_STREAM -> Localization.shortWatchingCount(context, viewCount)
61+
else -> Localization.shortViewCount(context, viewCount)
62+
}
63+
} else {
64+
""
65+
}
66+
val date = Localization.relativeTimeOrTextual(context, uploadDate, textualUploadDate)
67+
68+
return if (views.isEmpty()) {
69+
date.orEmpty()
70+
} else if (date.isNullOrEmpty()) {
71+
views
72+
} else {
73+
"$views$date"
74+
}
75+
}
76+
77+
private val StreamStatisticsEntry.detailText: String
78+
get() =
79+
Localization.concatenateStrings(
80+
Localization.shortViewCount(App.instance, watchCount),
81+
DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(latestAccessDate),
82+
ServiceHelper.getNameOfServiceById(streamEntity.serviceId),
83+
)

app/src/main/java/org/schabi/newpipe/ui/components/video/RelatedItems.kt

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.schabi.newpipe.ui.components.video
22

3-
import android.content.Context
43
import android.content.res.Configuration
54
import androidx.compose.foundation.layout.Arrangement
65
import androidx.compose.foundation.layout.Row
@@ -36,7 +35,6 @@ import org.schabi.newpipe.ui.components.items.ItemList
3635
import org.schabi.newpipe.ui.components.items.Playlist
3736
import org.schabi.newpipe.ui.components.items.Stream
3837
import org.schabi.newpipe.ui.theme.AppTheme
39-
import org.schabi.newpipe.util.Localization
4038
import org.schabi.newpipe.util.NO_SERVICE_ID
4139
import java.util.concurrent.TimeUnit
4240

@@ -51,7 +49,7 @@ fun RelatedItems(info: StreamInfo) {
5149
}
5250
val displayItems = info.relatedItems.mapNotNull {
5351
when (it) {
54-
is StreamInfoItem -> Stream(it, getStreamDetailText(context, it))
52+
is StreamInfoItem -> Stream(it)
5553
is PlaylistInfoItem -> Playlist(it)
5654
else -> null
5755
}
@@ -90,28 +88,6 @@ fun RelatedItems(info: StreamInfo) {
9088
)
9189
}
9290

93-
private fun getStreamDetailText(context: Context, stream: StreamInfoItem): String {
94-
val count = stream.viewCount
95-
val views = if (count >= 0) {
96-
when (stream.streamType) {
97-
StreamType.AUDIO_LIVE_STREAM -> Localization.listeningCount(context, count)
98-
StreamType.LIVE_STREAM -> Localization.shortWatchingCount(context, count)
99-
else -> Localization.shortViewCount(context, count)
100-
}
101-
} else {
102-
""
103-
}
104-
val date = Localization.relativeTimeOrTextual(context, stream.uploadDate, stream.textualUploadDate)
105-
106-
return if (views.isEmpty()) {
107-
date.orEmpty()
108-
} else if (date.isNullOrEmpty()) {
109-
views
110-
} else {
111-
"$views$date"
112-
}
113-
}
114-
11591
private fun StreamInfoItem(
11692
serviceId: Int = NO_SERVICE_ID,
11793
url: String = "",

0 commit comments

Comments
 (0)