From bcb1aba091df584a240872c5ce8ec1f7858d527c Mon Sep 17 00:00:00 2001 From: Uwe Trottmann Date: Fri, 17 Nov 2023 08:28:48 +0100 Subject: [PATCH] Trailers: add trailer button to show details. --- .../shows/overview/ShowFragment.kt | 18 ++++++- .../shows/overview/ShowViewModel.kt | 48 +++++++++++++------ .../main/res/layout/fragment_show_narrow.xml | 12 +++++ .../main/res/layout/fragment_show_regular.xml | 12 +++++ 4 files changed, 74 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/battlelancer/seriesguide/shows/overview/ShowFragment.kt b/app/src/main/java/com/battlelancer/seriesguide/shows/overview/ShowFragment.kt index c3d4b144c9..bc39b06f80 100644 --- a/app/src/main/java/com/battlelancer/seriesguide/shows/overview/ShowFragment.kt +++ b/app/src/main/java/com/battlelancer/seriesguide/shows/overview/ShowFragment.kt @@ -1,5 +1,4 @@ -// Copyright 2013-2023 Uwe Trottmann -// Copyright 2013 Andrew Neal +// Copyright 2013, 2023 Uwe Trottmann // SPDX-License-Identifier: Apache-2.0 package com.battlelancer.seriesguide.shows.overview @@ -102,6 +101,7 @@ class ShowFragment() : Fragment() { val buttonShortcut: Button val buttonLanguage: Button val buttonRate: View + val buttonTrailer: Button val buttonSimilar: Button val buttonImdb: Button val buttonShowMetacritic: Button @@ -140,6 +140,7 @@ class ShowFragment() : Fragment() { buttonShortcut = view.findViewById(R.id.buttonShowShortcut) buttonLanguage = view.findViewById(R.id.buttonShowLanguage) buttonRate = view.findViewById(R.id.containerRatings) + buttonTrailer = view.findViewById(R.id.buttonShowTrailer) buttonSimilar = view.findViewById(R.id.buttonShowSimilar) buttonImdb = view.findViewById(R.id.buttonShowImdb) buttonShowMetacritic = view.findViewById(R.id.buttonShowMetacritic) @@ -406,6 +407,19 @@ class ShowFragment() : Fragment() { binding.textViewRatingVotes.text = showForUi.traktVotes binding.textViewRatingUser.text = showForUi.traktUserRating + // Trailer button + binding.buttonTrailer.apply { + if (showForUi.trailerVideoId != null) { + setOnClickListener { + ServiceUtils.openYoutube(showForUi.trailerVideoId, requireContext()) + } + isEnabled = true + } else { + setOnClickListener(null) + isEnabled = false + } + } + // Similar shows button. binding.buttonSimilar.setOnClickListener { show.tmdbId?.also { diff --git a/app/src/main/java/com/battlelancer/seriesguide/shows/overview/ShowViewModel.kt b/app/src/main/java/com/battlelancer/seriesguide/shows/overview/ShowViewModel.kt index 575d935952..0cbeca158b 100644 --- a/app/src/main/java/com/battlelancer/seriesguide/shows/overview/ShowViewModel.kt +++ b/app/src/main/java/com/battlelancer/seriesguide/shows/overview/ShowViewModel.kt @@ -20,6 +20,7 @@ import com.battlelancer.seriesguide.util.LanguageTools import com.battlelancer.seriesguide.util.TextTools import com.battlelancer.seriesguide.util.TimeTools import com.battlelancer.seriesguide.util.Utils +import com.github.michaelbull.result.onSuccess import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -45,7 +46,8 @@ class ShowViewModel(application: Application) : AndroidViewModel(application) { val genres: String, val traktRating: String, val traktVotes: String, - val traktUserRating: String + val traktUserRating: String, + val trailerVideoId: String? ) // Mediator to compute some additional data for the UI in the background. @@ -99,20 +101,38 @@ class ShowViewModel(application: Application) : AndroidViewModel(application) { val traktUserRating = TraktTools.buildUserRatingString(application, show.ratingUser) + val databaseValues = ShowForUi( + show, + timeOrNull, + baseInfo, + overviewStyled, + languageData, + country, + releaseYear, + lastUpdated, + genres, + traktRating, traktVotes, traktUserRating, + null + ) + withContext(Dispatchers.Main) { - showForUi.value = - ShowForUi( - show, - timeOrNull, - baseInfo, - overviewStyled, - languageData, - country, - releaseYear, - lastUpdated, - genres, - traktRating, traktVotes, traktUserRating - ) + showForUi.value = databaseValues + } + + // Do network request after returning data from the database + val showTmdbId = show.tmdbId + if (showTmdbId != null && languageData != null) { + TmdbTools2().getShowTrailerYoutubeId( + application, + show.tmdbId, + languageData.languageCode + ).onSuccess { + if (it != null) { + withContext(Dispatchers.Main) { + showForUi.value = databaseValues.copy(trailerVideoId = it) + } + } + } } } } diff --git a/app/src/main/res/layout/fragment_show_narrow.xml b/app/src/main/res/layout/fragment_show_narrow.xml index 3abaded9d8..f2a72e4e34 100644 --- a/app/src/main/res/layout/fragment_show_narrow.xml +++ b/app/src/main/res/layout/fragment_show_narrow.xml @@ -1,5 +1,8 @@ + + + +