From 4999653683e8523816e38dfea6640296c1ed7edf Mon Sep 17 00:00:00 2001 From: KazemM <76521159+kazemcodes@users.noreply.github.com> Date: Wed, 9 Nov 2022 13:58:44 +0330 Subject: [PATCH] fix video not loaded sometimes --- .../ui/video/VideoPlayerViewModel.kt | 91 ++++++++----------- .../ui/video/component/core/MediaState.kt | 2 +- 2 files changed, 39 insertions(+), 54 deletions(-) diff --git a/presentation/src/main/java/ireader/presentation/ui/video/VideoPlayerViewModel.kt b/presentation/src/main/java/ireader/presentation/ui/video/VideoPlayerViewModel.kt index a1aaa48a0..182ffad69 100644 --- a/presentation/src/main/java/ireader/presentation/ui/video/VideoPlayerViewModel.kt +++ b/presentation/src/main/java/ireader/presentation/ui/video/VideoPlayerViewModel.kt @@ -13,10 +13,12 @@ import ireader.core.source.HttpSource import ireader.core.source.model.MovieUrl import ireader.core.source.model.Subtitles import ireader.domain.catalogs.interactor.GetLocalCatalog +import ireader.domain.models.entities.CatalogLocal import ireader.domain.usecases.files.GetSimpleStorage import ireader.domain.usecases.local.LocalGetChapterUseCase import ireader.domain.usecases.local.LocalInsertUseCases import ireader.domain.usecases.remote.RemoteUseCases +import ireader.domain.utils.extensions.withUIContext import ireader.presentation.core.ui.util.NavigationArgs import ireader.presentation.ui.core.viewmodel.BaseViewModel import ireader.presentation.ui.video.component.PlayerCreator @@ -61,6 +63,8 @@ class VideoScreenViewModel( var chapter by mutableStateOf(null) var currentMovie by mutableStateOf(null) + var appUrl by mutableStateOf(null) + fun subscribeChapter() { viewModelScope.launch { getChapterUseCase.subscribeChapterById(chapterId.value, null).collect { chapter1 -> @@ -68,22 +72,18 @@ class VideoScreenViewModel( chapter1?.content?.let { pages -> val movies = pages.filterIsInstance() val subs = pages.filterIsInstance() - mediaState.subtitleHelper.internalSubtitles.value = subs.map { it.toSubtitleData() }.toSet() + mediaState.subtitleHelper.internalSubtitles.value = + subs.map { it.toSubtitleData() }.toSet() mediaState.subs = emptyList() mediaState.medias = emptyList() - mediaState.subs =subs - mediaState.medias =movies - + mediaState.subs = subs + mediaState.medias = movies } } - } } - - - init { val chapter = runBlocking { getChapterUseCase.findChapterById(chapterId.value) @@ -97,6 +97,32 @@ class VideoScreenViewModel( if (localSource is HttpSource) { source = localSource } + viewModelScope.launch { + if (chapter != null && chapter.content.isEmpty() && catalogLocal != null) { + getRemoteChapter(chapter, catalogLocal) + } else { + loadMedia(chapter) + } + + + } + } + + suspend fun getRemoteChapter(chapter: Chapter, catalogLocal: CatalogLocal) { + remoteUseCases.getRemoteReadingContent(chapter, catalogLocal, onError = { + Log.error(it.toString()) + }, onSuccess = { result -> + withUIContext { + insertUseCases.insertChapter(result) + loadMedia(result) + } + + }, + emptyList() + ) + } + + fun loadMedia(chapter: Chapter?) { val movieUrl = chapter?.content?.filterIsInstance()?.firstOrNull()?.url ?: "" chapter?.content?.let { val subtitles = it.filterIsInstance() @@ -121,55 +147,14 @@ class VideoScreenViewModel( } val link = if (movieUrl.contains("http")) movieUrl else null val data = if (link == null) movieUrl else null - mediaState.loadPlayer(false, link = link,data = data,null, emptySet(),null,true) - this@VideoScreenViewModel.chapter = chapter - viewModelScope.launch { - if (chapter != null && chapter.content.isEmpty()) { - remoteUseCases.getRemoteReadingContent(chapter, catalogLocal, onError = { - Log.error(it.toString()) - }, onSuccess = { result -> - insertUseCases.insertChapter(result) - this@VideoScreenViewModel.chapter = result - }, - emptyList() - ) - } - } - } + mediaState.loadPlayer(false, link = link, data = data, null, emptySet(), null, true) -// fun setMediaItem(): List { -// val movies = chapter?.content?.filterIsInstance() ?: emptyList() -// val subtitles = chapter?.content?.filterIsInstance() ?: emptyList() -// val mediaItems = movies.map { -// MediaItem.Builder().setMediaId(it.url).setUri(it.url).let { mediaItem -> -// val subs = mutableListOf() -// mediaState.subtitleHelper.allSubtitles.map { subtitle -> -// subs.add( -// SubtitleConfiguration.Builder(Uri.parse(subtitle.url)).setLabel("Local") -// .setLanguage("En").setMimeType(MimeTypes.APPLICATION_SUBRIP) -// .setSelectionFlags(C.SELECTION_FLAG_DEFAULT).build() -// ) -// } -// subtitles.map { subtitle -> -// subs.add( -// SubtitleConfiguration.Builder(Uri.parse(subtitle.url)).setLabel("Local") -// .setLanguage("En").setMimeType(MimeTypes.APPLICATION_SUBRIP) -// .setSelectionFlags(C.SELECTION_FLAG_DEFAULT).build() -// ) -// } -// mediaItem.setSubtitleConfigurations(subs.toImmutableList()).build() -// } -// } -// currentMovie = movies.indexOfFirst { it is MovieUrl } -// player?.setMediaItems(mediaItems) -// -// return mediaItems -// } + } override fun onDestroy() { - mediaState?.player?.stop() - mediaState?.player?.release() + mediaState.player?.stop() + mediaState.player?.release() super.onDestroy() } diff --git a/presentation/src/main/java/ireader/presentation/ui/video/component/core/MediaState.kt b/presentation/src/main/java/ireader/presentation/ui/video/component/core/MediaState.kt index 177a0eeb8..f5520d16e 100644 --- a/presentation/src/main/java/ireader/presentation/ui/video/component/core/MediaState.kt +++ b/presentation/src/main/java/ireader/presentation/ui/video/component/core/MediaState.kt @@ -628,7 +628,7 @@ class MediaState( fun releasePlayer(saveTime: Boolean = true) { player?.release() - simpleCache?.release() + //simpleCache?.release() currentTextRenderer = null player = null