Skip to content

Commit 46c8753

Browse files
committed
Add an option to mark a show as watched #509
1 parent a3a7b20 commit 46c8753

10 files changed

Lines changed: 73 additions & 29 deletions

File tree

app/src/main/java/de/christinecoenen/code/zapp/app/mediathek/ui/helper/ShowMenuHelper.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ class ShowMenuHelper(
103103
return true
104104
}
105105

106+
R.id.menu_mark_watched -> {
107+
fragment.lifecycle.coroutineScope.launch {
108+
viewModel.markWatched(show)
109+
}
110+
return true
111+
}
112+
106113
else -> false
107114
}
108115
}

app/src/main/java/de/christinecoenen/code/zapp/app/mediathek/ui/helper/ShowMenuHelperViewModel.kt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ShowMenuHelperViewModel(
2727
R.id.menu_remove_download to false,
2828
R.id.menu_cancel_download to false,
2929
R.id.menu_mark_unwatched to false,
30+
R.id.menu_mark_watched to true,
3031
R.id.menu_add_bookmark to true,
3132
R.id.menu_remove_bookmark to false
3233
)
@@ -40,7 +41,7 @@ class ShowMenuHelperViewModel(
4041

4142
fun getMenuItemsVisibility(show: MediathekShow): Flow<Map<Int, Boolean>> {
4243
return mediathekRepository
43-
.getPersistedShowByApiId(show.apiId)
44+
.persistOrUpdateShow(show)
4445
.mapLatest {
4546
lastMapping = mapOf(
4647
R.id.menu_share to true,
@@ -63,17 +64,12 @@ class ShowMenuHelperViewModel(
6364
DownloadStatus.PAUSED,
6465
)),
6566
R.id.menu_mark_unwatched to (it.playbackPosition > 0),
67+
R.id.menu_mark_watched to (it.playbackPosition < it.videoDuration),
6668
R.id.menu_add_bookmark to !it.isBookmarked,
6769
R.id.menu_remove_bookmark to it.isBookmarked
6870
)
6971
lastMapping
7072
}
71-
.onStart {
72-
// for when the show has not yet been persisted
73-
emit(defaultMapping.toMutableMap().apply {
74-
this[R.id.menu_start_download] = show.hasAnyDownloadQuality()
75-
})
76-
}
7773
.distinctUntilChanged()
7874
}
7975

@@ -99,9 +95,11 @@ class ShowMenuHelperViewModel(
9995
mediathekRepository.resetPlaybackPosition(show.apiId)
10096
}
10197

98+
suspend fun markWatched(show: MediathekShow) {
99+
mediathekRepository.markAsPlayed(show.apiId)
100+
}
101+
102102
suspend fun bookmark(show: MediathekShow) {
103-
// we need to persist this first, because it might not yet be persisted!
104-
mediathekRepository.persistOrUpdateShow(show)
105103
mediathekRepository.setBookmarked(show.apiId, true)
106104
}
107105

@@ -110,8 +108,7 @@ class ShowMenuHelperViewModel(
110108
}
111109

112110
suspend fun startDownload(show: MediathekShow, quality: Quality) {
113-
// we need to persist this first, because it might not yet be persisted!
114-
val persistedShow = mediathekRepository.persistOrUpdateShow(show).first()
111+
val persistedShow = mediathekRepository.getPersistedShowByApiId(show.apiId).first()
115112
downloadController.startDownload(persistedShow.id, quality)
116113
}
117114
}

app/src/main/java/de/christinecoenen/code/zapp/models/shows/PersistedMediathekShow.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,21 @@ data class PersistedMediathekShow(
4848
var mediathekShow: MediathekShow
4949

5050
) {
51+
companion object {
52+
fun newFromMediathekShow(show: MediathekShow): PersistedMediathekShow {
53+
return PersistedMediathekShow(mediathekShow = show).apply {
54+
updateMediathekShow(show)
55+
}
56+
}
57+
}
58+
5159
fun updateMediathekShow(show: MediathekShow) {
5260
this.mediathekShow = show
61+
62+
if (this.videoDuration == 0L) {
63+
this.videoDuration = show.duration?.toLong()?.let { it * 1000 } ?: 0
64+
}
65+
5366
this.showUpdatedAt = DateTime.now()
5467
}
5568
}

app/src/main/java/de/christinecoenen/code/zapp/persistence/MediathekShowDao.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,7 @@ interface MediathekShowDao {
132132

133133
if (existingPersistedShow == null) {
134134
// insert new show
135-
val newPersistedShow = PersistedMediathekShow(
136-
mediathekShow = show
137-
)
135+
val newPersistedShow = PersistedMediathekShow.newFromMediathekShow(show)
138136
insert(newPersistedShow)
139137
} else {
140138
// update existing show
@@ -166,6 +164,9 @@ interface MediathekShowDao {
166164
@Query("UPDATE PersistedMediathekShow SET playbackPosition=0 WHERE apiId=:apiId")
167165
suspend fun resetPlaybackPosition(apiId: String)
168166

167+
@Query("UPDATE PersistedMediathekShow SET playbackPosition=(SELECT videoDuration WHERE apiId=:apiId), lastPlayedBackAt=:playedAt WHERE apiId=:apiId")
168+
suspend fun markAsPlayed(apiId: String, playedAt: DateTime)
169+
169170
@Query("UPDATE PersistedMediathekShow SET playbackPosition=0")
170171
suspend fun resetAllPlaybackPositions()
171172

app/src/main/java/de/christinecoenen/code/zapp/repositories/MediathekRepository.kt

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ class MediathekRepository(private val database: Database) {
3232

3333
return database
3434
.mediathekShowDao()
35-
.getPersonalShows("%$searchQuery%", channelIds, minDurationSeconds, maxDurationSeconds, limit)
35+
.getPersonalShows(
36+
"%$searchQuery%",
37+
channelIds,
38+
minDurationSeconds,
39+
maxDurationSeconds,
40+
limit
41+
)
3642
}
3743

3844
fun getPersonalShowsCount(
@@ -47,7 +53,12 @@ class MediathekRepository(private val database: Database) {
4753

4854
return database
4955
.mediathekShowDao()
50-
.getPersonalShowsCount("%$searchQuery%", channelIds, minDurationSeconds, maxDurationSeconds)
56+
.getPersonalShowsCount(
57+
"%$searchQuery%",
58+
channelIds,
59+
minDurationSeconds,
60+
maxDurationSeconds
61+
)
5162
.distinctUntilChanged()
5263
.flowOn(Dispatchers.IO)
5364
}
@@ -94,19 +105,17 @@ class MediathekRepository(private val database: Database) {
94105
.getAllBookarked("%$searchQuery%")
95106
}
96107

97-
suspend fun persistOrUpdateShow(show: MediathekShow): Flow<PersistedMediathekShow> =
98-
withContext(Dispatchers.IO) {
99-
database
100-
.mediathekShowDao()
101-
.insertOrUpdate(show)
102-
103-
database
104-
.mediathekShowDao()
105-
.getFromApiId(show.apiId)
106-
.distinctUntilChanged()
107-
.filterNotNull()
108-
.flowOn(Dispatchers.IO)
109-
}
108+
fun persistOrUpdateShow(show: MediathekShow): Flow<PersistedMediathekShow> {
109+
return database
110+
.mediathekShowDao()
111+
.getFromApiId(show.apiId)
112+
.onStart {
113+
database.mediathekShowDao().insertOrUpdate(show)
114+
}
115+
.distinctUntilChanged()
116+
.filterNotNull()
117+
.flowOn(Dispatchers.IO)
118+
}
110119

111120
suspend fun updateShow(show: PersistedMediathekShow?) = withContext(Dispatchers.IO) {
112121
database
@@ -238,6 +247,12 @@ class MediathekRepository(private val database: Database) {
238247
.setPlaybackPosition(showId, positionMillis, durationMillis, DateTime.now())
239248
}
240249

250+
suspend fun markAsPlayed(apiId: String) = withContext(Dispatchers.IO) {
251+
database
252+
.mediathekShowDao()
253+
.markAsPlayed(apiId, DateTime.now())
254+
}
255+
241256
suspend fun resetPlaybackPosition(apiId: String) = withContext(Dispatchers.IO) {
242257
database
243258
.mediathekShowDao()

app/src/main/res/menu/mediathek_show.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
android:visible="false"
3030
app:showAsAction="never" />
3131

32+
<item
33+
android:id="@+id/menu_mark_watched"
34+
android:icon="@drawable/ic_outline_play_circle_24"
35+
android:title="@string/action_mark_watched"
36+
android:visible="false"
37+
app:showAsAction="never" />
38+
3239
<item
3340
android:id="@+id/menu_start_download"
3441
android:icon="@drawable/ic_baseline_save_alt_24"

app/src/main/res/raw-en/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# v-next
2+
* Added an option to mark a show as watched ([#509](https://github.com/mediathekview/zapp/issues/509))
23
* Added an option to reset the progress of all started shows ([#507](https://github.com/mediathekview/zapp/issues/507))
34
* Fixed channel order list being covered by the navigation bar ([#496](https://github.com/mediathekview/zapp/issues/496))
45
* Restricted local result list for the mediathek search ([#486](https://github.com/mediathekview/zapp/issues/486))

app/src/main/res/raw/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# v-next
2+
* Option hinzugefügt, um Videos als gesehen zu markieren ([#509](https://github.com/mediathekview/zapp/issues/509))
23
* Option hinzugefügt, um den Fortschritt aller angefangenen Videos zurückzusetzen ([#507](https://github.com/mediathekview/zapp/issues/507))
34
* Behoben, dass die Sender-Anordnen-Liste von der Navigation überdeckt wird ([#496](https://github.com/mediathekview/zapp/issues/496))
45
* Lokale Ergebnisliste in der Mediathek-Suche begrenzt ([#486](https://github.com/mediathekview/zapp/issues/486))

app/src/main/res/values-en/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<string name="action_delete_download">Delete download</string>
8989
<string name="action_cancel_download">Cancel download</string>
9090
<string name="action_mark_unwatched">Mark as unwatched</string>
91+
<string name="action_mark_watched">Mark as watched</string>
9192
<string name="action_view_all">View all</string>
9293

9394
<string name="search_query_hint_zapp">Search in Zapp…</string>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<string name="action_delete_download">Download löschen</string>
8989
<string name="action_cancel_download">Download abbrechen</string>
9090
<string name="action_mark_unwatched">Als ungesehen markieren</string>
91+
<string name="action_mark_watched">Als gesehen markieren</string>
9192
<string name="action_view_all">Alle ansehen</string>
9293

9394
<string name="search_query_hint_zapp">Zapp durchsuchen…</string>

0 commit comments

Comments
 (0)