[Android] Set resolver as singleton objects

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


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Mar  3 10:55:50 2020 +0100| [881c10c2d48d2c1f62ac22638b6722094da9fb40] | committer: Geoffrey Métais

Set resolver as singleton objects

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

 .../provider/MediaScrapingTvshowProvider.kt        | 75 ++++++++--------------
 1 file changed, 26 insertions(+), 49 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 8038d14e6..1fcde8a93 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
@@ -64,16 +64,10 @@ class MediaScrapingTvshowProvider(private val context: Context) {
             }
         }
 
-        seasons.forEach {
-            it.episodes.forEach { episode ->
-                episode.media?.let { media ->
-                    if (media.seen < 1) {
-                        return episode
-                    }
+        return seasons.flatMap { it.episodes }
+                .firstOrNull {
+                    it.media?.let { media -> media.seen < 1 } ?: false
                 }
-            }
-        }
-        return null
     }
 
     suspend fun getAllSeasons(tvShow: MediaMetadataWithImages): List<Season> {
@@ -140,53 +134,36 @@ class MediaScrapingTvshowProvider(private val context: Context) {
     }
 
     suspend fun getAllEpisodesForShow(id: String): List<MediaMetadataWithImages> {
-        val medias = ArrayList<MediaMetadataWithImages>()
         val mediaMetadataRepository = MediaMetadataRepository.getInstance(context)
-        mediaMetadataRepository.getTvshow(id)?.let { tvShow ->
-            val season = getAllSeasons(tvShow)
-            season.forEach {
-                it.episodes.forEach { episode ->
-                    medias.add(episode)
-                }
-            }
-
-        }
-
-        return medias
+        return mediaMetadataRepository.getTvshow(id)?.let { tvShow ->
+            getAllSeasons(tvShow).flatMap { it.episodes }
+        } ?: emptyList()
     }
 
     fun getAllMedias(seasons: List<Season>?): List<MediaWrapper> {
-        val mediasToPlay = ArrayList<MediaWrapper>()
-        seasons?.forEach {
-            it.episodes.forEach { episode ->
-                episode.media?.let { media ->
-                    mediasToPlay.add(media)
-                }
-            }
-        }
-        return mediasToPlay
+        return seasons?.flatMap { it.episodes }?.mapNotNull { it.media } ?: emptyList()
     }
 
     companion object {
-        fun getProviders() : List<IMediaContentResolver> = mutableListOf<IMediaContentResolver>().apply {
-            add(object : IMediaContentResolver {
-                override val prefix = CONTENT_RESUME
-                override suspend fun getList(context: Context, id: String): Pair<List<MediaWrapper>, Int>? {
-                    val provider = MediaScrapingTvshowProvider(context)
-                    return withContext(Dispatchers.IO) { Pair(provider.getResumeMediasById(id.substringAfter(prefix)), 0) }
-                }
-            })
-            add(object : IMediaContentResolver {
-                override val prefix = CONTENT_EPISODE
-                override suspend fun getList(context: Context, id: String): Pair<List<MediaWrapper>, Int>? {
-                    val provider = MediaScrapingTvshowProvider(context)
-                    val moviepediaId = id.substringAfter(prefix)
-                    return withContext(Dispatchers.IO) { provider.getShowIdForEpisode(moviepediaId)?.let { provider.getAllEpisodesForShow(it) } }?.let {
-                        Pair(it.mapNotNull { episode -> episode.media }, it.indexOfFirst { it.metadata.moviepediaId == moviepediaId })
-                    }
-                }
-            })
-        }
+        fun getProviders() = listOf(ResumeResolver, TvShowResolver)
+    }
+}
 
+private object ResumeResolver : IMediaContentResolver {
+    override val prefix = CONTENT_RESUME
+    override suspend fun getList(context: Context, id: String): Pair<List<MediaWrapper>, Int>? {
+        val provider = MediaScrapingTvshowProvider(context)
+        return withContext(Dispatchers.IO) { Pair(provider.getResumeMediasById(id.substringAfter(prefix)), 0) }
+    }
+}
+
+private object TvShowResolver : IMediaContentResolver {
+    override val prefix = CONTENT_EPISODE
+    override suspend fun getList(context: Context, id: String): Pair<List<MediaWrapper>, Int>? {
+        val provider = MediaScrapingTvshowProvider(context)
+        val moviepediaId = id.substringAfter(prefix)
+        return withContext(Dispatchers.IO) { provider.getShowIdForEpisode(moviepediaId)?.let { provider.getAllEpisodesForShow(it) } }?.let {
+            Pair(it.mapNotNull { episode -> episode.media }, it.indexOfFirst { it.metadata.moviepediaId == moviepediaId })
+        }
     }
 }



More information about the Android mailing list