|
| 1 | +package org.jellyfin.androidtv.ui.home |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.content.Context |
| 5 | +import androidx.leanback.widget.ArrayObjectAdapter |
| 6 | +import androidx.leanback.widget.HeaderItem |
| 7 | +import androidx.leanback.widget.ListRow |
| 8 | +import androidx.leanback.widget.OnItemViewClickedListener |
| 9 | +import androidx.leanback.widget.Presenter |
| 10 | +import androidx.leanback.widget.Row |
| 11 | +import androidx.leanback.widget.RowPresenter |
| 12 | +import kotlinx.coroutines.CoroutineScope |
| 13 | +import kotlinx.coroutines.Dispatchers |
| 14 | +import kotlinx.coroutines.launch |
| 15 | +import kotlinx.coroutines.withContext |
| 16 | +import org.jellyfin.androidtv.R |
| 17 | +import org.jellyfin.androidtv.auth.repository.UserRepository |
| 18 | +import org.jellyfin.androidtv.constant.LiveTvOption |
| 19 | +import org.jellyfin.androidtv.ui.GridButton |
| 20 | +import org.jellyfin.androidtv.ui.navigation.Destinations |
| 21 | +import org.jellyfin.androidtv.ui.navigation.NavigationRepository |
| 22 | +import org.jellyfin.androidtv.ui.presentation.CardPresenter |
| 23 | +import org.jellyfin.androidtv.ui.presentation.GridButtonPresenter |
| 24 | +import org.jellyfin.androidtv.ui.presentation.MutableObjectAdapter |
| 25 | +import org.jellyfin.androidtv.util.LiveTvDefaultViewHelper |
| 26 | +import org.jellyfin.androidtv.util.Utils |
| 27 | +import org.jellyfin.sdk.api.client.ApiClient |
| 28 | +import org.jellyfin.sdk.api.client.extensions.userViewsApi |
| 29 | +import org.jellyfin.sdk.model.api.BaseItemDto |
| 30 | +import org.jellyfin.sdk.model.api.CollectionType |
| 31 | +import timber.log.Timber |
| 32 | + |
| 33 | +/** |
| 34 | + * Enhanced version of HomeFragmentLiveTVRow that respects the user's default Live TV view preference |
| 35 | + */ |
| 36 | +class HomeFragmentLiveTVRowWithUserPreferenceCheck( |
| 37 | + private val activity: Activity, |
| 38 | + private val userRepository: UserRepository, |
| 39 | + private val navigationRepository: NavigationRepository, |
| 40 | + private val api: ApiClient, |
| 41 | + private val coroutineScope: CoroutineScope |
| 42 | +) : HomeFragmentRow, OnItemViewClickedListener { |
| 43 | + override fun addToRowsAdapter(context: Context, cardPresenter: CardPresenter, rowsAdapter: MutableObjectAdapter<Row>) { |
| 44 | + val header = HeaderItem(rowsAdapter.size().toLong(), activity.getString(R.string.pref_live_tv_cat)) |
| 45 | + val adapter = ArrayObjectAdapter(GridButtonPresenter()) |
| 46 | + |
| 47 | + // Live TV Guide button |
| 48 | + adapter.add(GridButton(LiveTvOption.LIVE_TV_GUIDE_OPTION_ID, activity.getString(R.string.lbl_live_tv_guide))) |
| 49 | + // Live TV Recordings button |
| 50 | + adapter.add(GridButton(LiveTvOption.LIVE_TV_RECORDINGS_OPTION_ID, activity.getString(R.string.lbl_recorded_tv))) |
| 51 | + if (Utils.canManageRecordings(userRepository.currentUser.value)) { |
| 52 | + // Recording Schedule button |
| 53 | + adapter.add(GridButton(LiveTvOption.LIVE_TV_SCHEDULE_OPTION_ID, activity.getString(R.string.lbl_schedule))) |
| 54 | + // Recording Series button |
| 55 | + adapter.add(GridButton(LiveTvOption.LIVE_TV_SERIES_OPTION_ID, activity.getString(R.string.lbl_series))) |
| 56 | + } |
| 57 | + |
| 58 | + rowsAdapter.add(ListRow(header, adapter)) |
| 59 | + } |
| 60 | + |
| 61 | + override fun onItemClicked(itemViewHolder: Presenter.ViewHolder?, item: Any?, rowViewHolder: RowPresenter.ViewHolder?, row: Row?) { |
| 62 | + if (item !is GridButton) return |
| 63 | + |
| 64 | + when (item.id) { |
| 65 | + LiveTvOption.LIVE_TV_GUIDE_OPTION_ID -> navigationRepository.navigate(Destinations.liveTvGuide) |
| 66 | + LiveTvOption.LIVE_TV_SCHEDULE_OPTION_ID -> navigationRepository.navigate(Destinations.liveTvSchedule) |
| 67 | + LiveTvOption.LIVE_TV_RECORDINGS_OPTION_ID -> navigationRepository.navigate(Destinations.liveTvRecordings) |
| 68 | + LiveTvOption.LIVE_TV_SERIES_OPTION_ID -> navigationRepository.navigate(Destinations.liveTvSeriesRecordings) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Handle click on the Live TV card from the home screen |
| 74 | + * Checks user preferences and navigates directly to the preferred view if set |
| 75 | + */ |
| 76 | + fun onLiveTvCardClicked() { |
| 77 | + coroutineScope.launch { |
| 78 | + try { |
| 79 | + // First, get the Live TV library item |
| 80 | + val liveTvItem = getLiveTvLibraryItem() |
| 81 | + |
| 82 | + // Then check for default view preference |
| 83 | + val defaultViewId = withContext(Dispatchers.IO) { |
| 84 | + LiveTvDefaultViewHelper.getDefaultLiveTvView(api) |
| 85 | + } |
| 86 | + |
| 87 | + withContext(Dispatchers.Main) { |
| 88 | + if (defaultViewId != null) { |
| 89 | + // Navigate directly to the preferred view |
| 90 | + when (defaultViewId) { |
| 91 | + LiveTvOption.LIVE_TV_GUIDE_OPTION_ID -> navigationRepository.navigate(Destinations.liveTvGuide) |
| 92 | + LiveTvOption.LIVE_TV_SCHEDULE_OPTION_ID -> navigationRepository.navigate(Destinations.liveTvSchedule) |
| 93 | + LiveTvOption.LIVE_TV_RECORDINGS_OPTION_ID -> navigationRepository.navigate(Destinations.liveTvRecordings) |
| 94 | + LiveTvOption.LIVE_TV_SERIES_OPTION_ID -> navigationRepository.navigate(Destinations.liveTvSeriesRecordings) |
| 95 | + else -> { |
| 96 | + // Fallback to the standard Live TV selection screen |
| 97 | + if (liveTvItem != null) { |
| 98 | + navigationRepository.navigate(Destinations.librarySmartScreen(liveTvItem)) |
| 99 | + } else { |
| 100 | + // If we couldn't get the Live TV item, navigate to the guide as a fallback |
| 101 | + navigationRepository.navigate(Destinations.liveTvGuide) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } else { |
| 106 | + // No preference set, show the standard Live TV selection screen |
| 107 | + if (liveTvItem != null) { |
| 108 | + navigationRepository.navigate(Destinations.librarySmartScreen(liveTvItem)) |
| 109 | + } else { |
| 110 | + // If we couldn't get the Live TV item, navigate to the guide as a fallback |
| 111 | + navigationRepository.navigate(Destinations.liveTvGuide) |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } catch (e: Exception) { |
| 116 | + // Fallback to the Live TV guide on error |
| 117 | + navigationRepository.navigate(Destinations.liveTvGuide) |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Gets the Live TV library item from the user's views |
| 124 | + */ |
| 125 | + private suspend fun getLiveTvLibraryItem(): BaseItemDto? { |
| 126 | + return try { |
| 127 | + val userViews = api.userViewsApi.getUserViews().content?.items ?: emptyList() |
| 128 | + userViews.find { it.collectionType == CollectionType.LIVETV } |
| 129 | + } catch (e: Exception) { |
| 130 | + null |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments