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..612e411174 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 @@ -102,6 +102,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 +141,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 +408,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 @@ + + + +