[Android] TV: Use sequences to find first resumable episode

Geoffrey Métais git at videolan.org
Tue Mar 3 13:30:11 CET 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Mar  3 11:52:53 2020 +0100| [e445f0fe2b9ec87690c7d7c847424c2570c6ff18] | committer: Geoffrey Métais

TV: Use sequences to find first resumable episode

This reduces the number of medialibrary calls

> https://code.videolan.org/videolan/vlc-android/commit/e445f0fe2b9ec87690c7d7c847424c2570c6ff18
---

 .../provider/MediaScrapingTvshowProvider.kt        | 31 +++++++---------------
 1 file changed, 9 insertions(+), 22 deletions(-)

diff --git a/application/moviepedia/src/main/java/org/videolan/moviepedia/provider/MediaScrapingTvshowProvider.kt b/application/moviepedia/src/main/java/org/videolan/moviepedia/provider/MediaScrapingTvshowProvider.kt
index 1fcde8a93..7e93290cc 100644
--- a/application/moviepedia/src/main/java/org/videolan/moviepedia/provider/MediaScrapingTvshowProvider.kt
+++ b/application/moviepedia/src/main/java/org/videolan/moviepedia/provider/MediaScrapingTvshowProvider.kt
@@ -25,6 +25,7 @@
 package org.videolan.moviepedia.provider
 
 import android.content.Context
+import androidx.annotation.MainThread
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.withContext
 import org.videolan.medialibrary.interfaces.Medialibrary
@@ -33,41 +34,27 @@ import org.videolan.moviepedia.database.models.MediaMetadataWithImages
 import org.videolan.moviepedia.repository.MediaMetadataRepository
 import org.videolan.moviepedia.viewmodel.Season
 import org.videolan.resources.CONTENT_EPISODE
-import org.videolan.resources.CONTENT_PREFIX
 import org.videolan.resources.CONTENT_RESUME
 import org.videolan.resources.interfaces.IMediaContentResolver
 import org.videolan.resources.util.getFromMl
 
 class MediaScrapingTvshowProvider(private val context: Context) {
 
+    @MainThread
     fun getFirstResumableEpisode(medialibrary: Medialibrary, mediaMetadataEpisodes: List<MediaMetadataWithImages>): MediaMetadataWithImages? {
         val seasons = ArrayList<Season>()
         mediaMetadataEpisodes.forEach { episode ->
-            val existingSeason = seasons.firstOrNull { it.seasonNumber == episode.metadata.season }
-            val season = if (existingSeason == null) {
-                val newSeason = Season(episode.metadata.season ?: 0)
-                seasons.add(newSeason)
-                newSeason
-            } else existingSeason
+            val season = seasons.firstOrNull { it.seasonNumber == episode.metadata.season }
+                    ?: Season(episode.metadata.season ?: 0).also { seasons.add(it) }
             season.episodes.add(episode)
         }
-        seasons.forEach { season ->
+        return seasons.asSequence().mapNotNull { season ->
             season.episodes.sortBy { episode -> episode.metadata.episode }
-            //retrieve ML media
-            season.episodes.forEach { episode ->
-                if (episode.media == null) {
-                    episode.metadata.mlId?.let {
-                        val fromMl = medialibrary.getMedia(it)
-                        episode.media = fromMl
-                    }
-                }
+            season.episodes.asSequence().firstOrNull { episode ->
+                if (episode.media == null) episode.media = episode.metadata.mlId?.let { medialibrary.getMedia(it) }
+                episode.media?.let { media -> media.seen < 1 } == true
             }
-        }
-
-        return seasons.flatMap { it.episodes }
-                .firstOrNull {
-                    it.media?.let { media -> media.seen < 1 } ?: false
-                }
+        }.firstOrNull()
     }
 
     suspend fun getAllSeasons(tvShow: MediaMetadataWithImages): List<Season> {



More information about the Android mailing list