Skip to content

Commit

Permalink
Trailers: add trailer button to show details.
Browse files Browse the repository at this point in the history
  • Loading branch information
UweTrottmann committed Nov 17, 2023
1 parent b41bd1b commit fa7bd05
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
}
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/fragment_show_narrow.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>

<!-- Copyright 2023 Uwe Trottmann -->
<!-- SPDX-License-Identifier: Apache-2.0 -->

<!-- Enable nested scrolling to scroll app bar, fix fling to top registering. -->
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
Expand Down Expand Up @@ -159,6 +162,15 @@

</com.google.android.material.card.MaterialCardView>

<Button
android:id="@+id/buttonShowTrailer"
style="@style/Widget.SeriesGuide.Button.Borderless.Sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/trailer"
app:icon="@drawable/ic_movie_white_24dp"
app:iconGravity="start" />

<Button
android:id="@+id/buttonShowSimilar"
style="@style/Widget.SeriesGuide.Button.Borderless.Sheet"
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/fragment_show_regular.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>

<!-- Copyright 2023 Uwe Trottmann -->
<!-- SPDX-License-Identifier: Apache-2.0 -->

<!-- Enable nested scrolling to scroll app bar, fix fling to top registering. -->
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
Expand Down Expand Up @@ -152,6 +155,15 @@

</com.google.android.material.card.MaterialCardView>

<Button
android:id="@+id/buttonShowTrailer"
style="@style/Widget.SeriesGuide.Button.Borderless.Sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/trailer"
app:icon="@drawable/ic_movie_white_24dp"
app:iconGravity="start" />

<Button
android:id="@+id/buttonShowSimilar"
style="@style/Widget.SeriesGuide.Button.Borderless.Sheet"
Expand Down

0 comments on commit fa7bd05

Please sign in to comment.