diff --git a/app/src/main/java/com/github/damontecres/wholphin/data/model/HomeRowConfig.kt b/app/src/main/java/com/github/damontecres/wholphin/data/model/HomeRowConfig.kt index 39b82bc827..0252225acf 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/data/model/HomeRowConfig.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/data/model/HomeRowConfig.kt @@ -258,6 +258,13 @@ data class HomeRowViewOptions( val episodeContentScale: PrefContentScale = PrefContentScale.FILL, val episodeAspectRatio: AspectRatio = AspectRatio.TALL, val episodeImageType: ViewOptionImageType = ViewOptionImageType.PRIMARY, + val showcase: Boolean = false, + val showcaseShowLogo: Boolean = true, + val showcaseShowRating: Boolean = true, + val showcaseShowOverview: Boolean = true, + val showcaseShowDetails: Boolean = true, + val showcaseInfiniteScroll: Boolean = true, + val showcaseRotateArtwork: Boolean = false, ) { companion object { val genreDefault = diff --git a/app/src/main/java/com/github/damontecres/wholphin/services/BackdropService.kt b/app/src/main/java/com/github/damontecres/wholphin/services/BackdropService.kt index 66ef284cda..fbf17b18f0 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/services/BackdropService.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/services/BackdropService.kt @@ -23,6 +23,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.update import kotlinx.coroutines.withContext @@ -51,6 +52,17 @@ class BackdropService private val _backdropFlow = MutableStateFlow(BackdropResult.NONE) val backdropFlow = _backdropFlow + private val _showcaseFlow = MutableStateFlow(null) + + /** + * The artwork for a focused showcase row, drawn full screen behind the nav drawer + */ + val showcaseFlow: StateFlow = _showcaseFlow + + fun setShowcase(showcase: ShowcaseArtwork?) { + _showcaseFlow.value = showcase + } + /** * Update the backdrop to use the specified item */ @@ -241,6 +253,14 @@ class BackdropService } } +/** + * The item whose artwork is shown full screen for a focused showcase row + */ +data class ShowcaseArtwork( + val item: BaseItem, + val rotate: Boolean, +) + /** * The result from determining the backdrop URL and extracted colors for the dynamic backdrop */ diff --git a/app/src/main/java/com/github/damontecres/wholphin/services/LibraryRowSettingsService.kt b/app/src/main/java/com/github/damontecres/wholphin/services/LibraryRowSettingsService.kt new file mode 100644 index 0000000000..993aa5c3ee --- /dev/null +++ b/app/src/main/java/com/github/damontecres/wholphin/services/LibraryRowSettingsService.kt @@ -0,0 +1,64 @@ +package com.github.damontecres.wholphin.services + +import android.content.Context +import androidx.annotation.StringRes +import com.github.damontecres.wholphin.data.model.HomeRowViewOptions +import com.github.damontecres.wholphin.ui.toServerString +import dagger.hilt.android.qualifiers.ApplicationContext +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.first +import java.util.UUID +import javax.inject.Inject +import javax.inject.Singleton + +/** + * Stores the display settings for each row on a library's recommended tab, per user and library. + * + * Rows without saved settings use the library's defaults. + */ +@Singleton +class LibraryRowSettingsService + @Inject + constructor( + @param:ApplicationContext private val context: Context, + private val keyValueService: KeyValueService, + ) { + /** + * A stable key for a row, based on the resource name of its title (for example + * "recently_added"), which does not change between app versions + */ + fun rowKey( + @StringRes title: Int, + ): String = context.resources.getResourceEntryName(title) + + fun flow( + userId: UUID, + libraryId: UUID, + ): Flow> = keyValueService.get(userId, key(libraryId), emptyMap()) + + suspend fun get( + userId: UUID, + libraryId: UUID, + ): Map = flow(userId, libraryId).first() + + /** + * Save the settings for one row, or remove them (go back to the defaults) if null + */ + suspend fun save( + userId: UUID, + libraryId: UUID, + rowKey: String, + viewOptions: HomeRowViewOptions?, + ) { + val current = get(userId, libraryId) + val updated = + if (viewOptions == null) { + current - rowKey + } else { + current + (rowKey to viewOptions) + } + keyValueService.save(userId, key(libraryId), updated) + } + + private fun key(libraryId: UUID) = "library_row_settings_${libraryId.toServerString()}" + } diff --git a/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedContent.kt b/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedContent.kt index ca4ae7ff45..44d74586f0 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedContent.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedContent.kt @@ -1,6 +1,7 @@ package com.github.damontecres.wholphin.ui.components import android.content.Context +import androidx.annotation.StringRes import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue @@ -21,6 +22,7 @@ import com.github.damontecres.wholphin.preferences.AppPreferences import com.github.damontecres.wholphin.preferences.UserPreferences import com.github.damontecres.wholphin.services.BackdropService import com.github.damontecres.wholphin.services.FavoriteWatchManager +import com.github.damontecres.wholphin.services.LibraryRowSettingsService import com.github.damontecres.wholphin.services.MediaManagementService import com.github.damontecres.wholphin.services.MusicService import com.github.damontecres.wholphin.services.NavigationManager @@ -81,6 +83,7 @@ class RecommendedViewModel private val mediaManagementService: MediaManagementService, private val suggestionService: SuggestionService, val serverReportService: ServerReportService, + private val libraryRowSettingsService: LibraryRowSettingsService, @Assisted private val parentId: UUID, @Assisted private val suggestionsType: BaseItemKind, @Assisted private val recommendedRows: List>, @@ -99,12 +102,33 @@ class RecommendedViewModel private val _state = MutableStateFlow(RecommendedState()) val state: StateFlow = _state + /** + * The user's saved settings for this library's rows, by row key + */ + private var rowSettings: Map = emptyMap() + + /** + * The display settings for a row: the user's saved settings, or the library's default + */ + private fun viewOptionsFor( + @StringRes title: Int, + ): HomeRowViewOptions = rowSettings[libraryRowSettingsService.rowKey(title)] ?: viewOptions + fun init() { viewModelScope.launchDefault { val limit = userPreferencesService.flow .first() .appPreferences.homePagePreferences.maxItemsPerRow + rowSettings = + try { + serverRepository.currentUser + ?.let { libraryRowSettingsService.get(it.id, parentId) } + .orEmpty() + } catch (ex: Exception) { + Timber.e(ex, "Error loading row settings for %s", parentId) + emptyMap() + } _state.update { it.copy( loading = LoadingState.Loading, @@ -123,7 +147,7 @@ class RecommendedViewModel HomeRowLoadingState.Success( title, items, - viewOptions, + viewOptionsFor(row.title), showViewMore = items.size >= limit, ) } catch (ex: Exception) { @@ -192,7 +216,7 @@ class RecommendedViewModel HomeRowLoadingState.Success( title, resource.items, - viewOptions, + viewOptionsFor(R.string.suggestions), showViewMore = resource.items.size >= limit, ) } @@ -201,7 +225,7 @@ class RecommendedViewModel HomeRowLoadingState.Success( title, emptyList(), - viewOptions, + viewOptionsFor(R.string.suggestions), showViewMore = false, ) } @@ -309,7 +333,7 @@ class RecommendedViewModel initialPosition = row.items.size, viewOptions = ViewOptions( - aspectRatio = viewOptions.aspectRatio, + aspectRatio = row.viewOptions.aspectRatio, ), ), ) @@ -326,7 +350,7 @@ class RecommendedViewModel initialPosition = row.items.size, viewOptions = ViewOptions( - aspectRatio = viewOptions.aspectRatio, + aspectRatio = row.viewOptions.aspectRatio, ), ), ) diff --git a/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedMovie.kt b/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedMovie.kt index febe019997..788c8c146f 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedMovie.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedMovie.kt @@ -18,7 +18,7 @@ import org.jellyfin.sdk.model.api.request.GetResumeItemsRequest import java.time.LocalDateTime import java.util.UUID -private fun getRecommendedRows(parentId: UUID) = +internal fun getRecommendedMovieRows(parentId: UUID) = listOf( RecommendedRow( title = R.string.continue_watching, @@ -104,7 +104,7 @@ fun RecommendedMovie( it.create( parentId = parentId, suggestionsType = BaseItemKind.MOVIE, - recommendedRows = getRecommendedRows(parentId), + recommendedRows = getRecommendedMovieRows(parentId), viewOptions = HomeRowViewOptions(), ) }, diff --git a/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedMusic.kt b/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedMusic.kt index 73e9af448d..cea2b827be 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedMusic.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedMusic.kt @@ -4,21 +4,19 @@ import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel import com.github.damontecres.wholphin.R -import com.github.damontecres.wholphin.data.model.HomeRowViewOptions import com.github.damontecres.wholphin.preferences.UserPreferences -import com.github.damontecres.wholphin.ui.AspectRatio -import com.github.damontecres.wholphin.ui.Cards import com.github.damontecres.wholphin.ui.SlimItemFields import com.github.damontecres.wholphin.ui.data.RowColumn import com.github.damontecres.wholphin.util.GetItemsRequestHandler import org.jellyfin.sdk.model.api.BaseItemKind +import org.jellyfin.sdk.model.api.CollectionType import org.jellyfin.sdk.model.api.ItemSortBy import org.jellyfin.sdk.model.api.SortOrder import org.jellyfin.sdk.model.api.request.GetItemsRequest import java.time.LocalDateTime import java.util.UUID -private fun getRecommendedRows(parentId: UUID) = +internal fun getRecommendedMusicRows(parentId: UUID) = listOf( RecommendedRow( title = R.string.recently_released, @@ -88,13 +86,8 @@ fun RecommendedMusic( it.create( parentId = parentId, suggestionsType = BaseItemKind.MUSIC_ALBUM, - recommendedRows = getRecommendedRows(parentId), - viewOptions = - HomeRowViewOptions( - aspectRatio = AspectRatio.SQUARE, - heightDp = Cards.HEIGHT_EPISODE, - showTitles = true, - ), + recommendedRows = getRecommendedMusicRows(parentId), + viewOptions = recommendedRowDefaultViewOptions(CollectionType.MUSIC), ) }, ), diff --git a/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedRows.kt b/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedRows.kt new file mode 100644 index 0000000000..35f9597716 --- /dev/null +++ b/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedRows.kt @@ -0,0 +1,43 @@ +package com.github.damontecres.wholphin.ui.components + +import com.github.damontecres.wholphin.R +import com.github.damontecres.wholphin.data.model.HomeRowViewOptions +import com.github.damontecres.wholphin.ui.AspectRatio +import com.github.damontecres.wholphin.ui.Cards +import org.jellyfin.sdk.model.api.CollectionType +import java.util.UUID + +/** + * The title of each row on the recommended tab for a type of library, in order. Empty if that + * type of library has no recommended tab. + */ +fun recommendedRowTitles(type: CollectionType): List { + // Only the titles are used, so the library ID does not matter + val anyId = UUID(0, 0) + val rows = + when (type) { + CollectionType.MOVIES -> getRecommendedMovieRows(anyId) + CollectionType.TVSHOWS -> getRecommendedTvShowRows(anyId, enableRewatching = false) + CollectionType.MUSIC -> getRecommendedMusicRows(anyId) + else -> return emptyList() + } + return rows.map { it.title } + R.string.suggestions +} + +/** + * The default display settings for the rows on the recommended tab for a type of library + */ +fun recommendedRowDefaultViewOptions(type: CollectionType): HomeRowViewOptions = + when (type) { + CollectionType.MUSIC -> { + HomeRowViewOptions( + aspectRatio = AspectRatio.SQUARE, + heightDp = Cards.HEIGHT_EPISODE, + showTitles = true, + ) + } + + else -> { + HomeRowViewOptions() + } + } diff --git a/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedTvShow.kt b/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedTvShow.kt index 78caf524ca..4a5aba9ae3 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedTvShow.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/ui/components/RecommendedTvShow.kt @@ -20,7 +20,7 @@ import org.jellyfin.sdk.model.api.request.GetResumeItemsRequest import java.time.LocalDateTime import java.util.UUID -private fun getRecommendedRows( +internal fun getRecommendedTvShowRows( parentId: UUID, enableRewatching: Boolean, ) = listOf( @@ -125,7 +125,7 @@ fun RecommendedTvShow( parentId = parentId, suggestionsType = BaseItemKind.SERIES, recommendedRows = - getRecommendedRows( + getRecommendedTvShowRows( parentId, preferences.appPreferences.homePagePreferences.enableRewatchingNextUp, ), diff --git a/app/src/main/java/com/github/damontecres/wholphin/ui/main/HomePage.kt b/app/src/main/java/com/github/damontecres/wholphin/ui/main/HomePage.kt index bf24ae0087..cf254e35cd 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/ui/main/HomePage.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/ui/main/HomePage.kt @@ -20,6 +20,7 @@ import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue @@ -29,6 +30,7 @@ import androidx.compose.runtime.rememberUpdatedState import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.alpha import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusProperties import androidx.compose.ui.focus.focusRequester @@ -53,6 +55,7 @@ import com.github.damontecres.wholphin.data.model.HomeRowConfig import com.github.damontecres.wholphin.data.model.HomeRowViewOptions import com.github.damontecres.wholphin.data.model.QuickDetailsData import com.github.damontecres.wholphin.preferences.UserPreferences +import com.github.damontecres.wholphin.services.ShowcaseArtwork import com.github.damontecres.wholphin.ui.Cards import com.github.damontecres.wholphin.ui.cards.BannerCard import com.github.damontecres.wholphin.ui.cards.BannerCardWithTitle @@ -299,6 +302,7 @@ fun HomePageContent( ) }, onClickViewMore: (RowColumn, HomeRowLoadingState.Success) -> Unit = { _, _ -> }, + showcaseViewModel: ShowcaseViewModel = hiltViewModel(), ) { val focusedItem = remember(homeRows, position) { @@ -307,6 +311,13 @@ fun HomePageContent( ) } + // When a showcase row is focused, its artwork covers the page + val focusedShowcaseRow = + remember(homeRows, position.row) { + (homeRows.getOrNull(position.row) as? HomeRowLoadingState.Success) + ?.takeIf { it.viewOptions.showcase && it.items.isNotEmpty() } + } + val rowFocusRequesters = remember(homeRows.size) { List(homeRows.size) { FocusRequester() } } var firstFocused by remember { mutableStateOf(false) } @@ -338,7 +349,17 @@ fun HomePageContent( LaunchedEffect(onUpdateBackdrop, focusedItem) { focusedItem?.let { onUpdateBackdrop.invoke(it) } } + // While a showcase row is focused, its artwork is drawn full screen behind the nav drawer + val showcaseItem = focusedShowcaseRow?.let { focusedItem } + val rotateShowcase = focusedShowcaseRow?.viewOptions?.showcaseRotateArtwork == true + LaunchedEffect(showcaseItem, rotateShowcase) { + showcaseViewModel.backdropService.setShowcase(showcaseItem?.let { ShowcaseArtwork(it, rotateShowcase) }) + } + DisposableEffect(Unit) { + onDispose { showcaseViewModel.backdropService.setShowcase(null) } + } Box(modifier = modifier) { + // The page is hidden (but still focusable) while the showcase artwork is shown Column( modifier = Modifier @@ -346,7 +367,8 @@ fun HomePageContent( onEnter = { rowFocusRequesters.getOrNull(currentPosition.row)?.tryRequestFocus() } - }.fillMaxSize(), + }.fillMaxSize() + .alpha(if (focusedShowcaseRow != null) 0f else 1f), ) { headerComposable.invoke(focusedItem) @@ -404,106 +426,132 @@ fun HomePageContent( is HomeRowLoadingState.Success -> { if (row.items.isNotEmpty()) { val viewOptions = row.viewOptions - ItemRow( - title = row.title.getString(), - items = row.items, - onClickItem = - remember(rowIndex, onClickItem) { - { index, item -> - onClickItem.invoke( - RowColumn( - rowIndex, - index, - ), - item, - ) - } + if (viewOptions.showcase) { + ShowcaseRowBanner( + title = row.title.getString(), + items = row.items, + initialIndex = if (currentPosition.row == rowIndex) currentPosition.column else 0, + height = viewOptions.heightDp.dp, + infiniteScroll = viewOptions.showcaseInfiniteScroll, + onFocusIndex = { index -> + currentOnFocusPosition(RowColumn(rowIndex, index)) }, - onLongClickItem = - remember(rowIndex, onLongClickItem) { - { index, item -> - onLongClickItem.invoke( - RowColumn(rowIndex, index), - item, - ) - } + onClickItem = { index, item -> + onClickItem.invoke(RowColumn(rowIndex, index), item) }, - modifier = - rowModifier - .fillMaxWidth() - .focusGroup() - .focusRequester(rowFocusRequesters[rowIndex]), - horizontalPadding = viewOptions.spacing.dp, - cardContent = { index, item, cardModifier, onClick, onLongClick -> - val onFocus = - remember(rowIndex, index) { - { isFocused: Boolean -> - if (isFocused) { - currentOnFocusPosition( - RowColumn( - rowIndex, - index, - ), - ) - } + onLongClickItem = { index, item -> + onLongClickItem.invoke(RowColumn(rowIndex, index), item) + }, + onClickPlay = { index, item -> + currentOnClickPlay(RowColumn(rowIndex, index), item) + }, + modifier = + rowModifier + .fillMaxWidth() + .focusRequester(rowFocusRequesters[rowIndex]), + ) + } else { + ItemRow( + title = row.title.getString(), + items = row.items, + onClickItem = + remember(rowIndex, onClickItem) { + { index, item -> + onClickItem.invoke( + RowColumn( + rowIndex, + index, + ), + item, + ) } - } - val onKey = - remember(item) { - { event: KeyEvent -> - if (isPlayKeyUp(event) && item?.type?.playable == true) { - Timber.v("Clicked play on ${item.id}") - currentOnClickPlay( - currentPosition, - item, - ) - true - } else { - false - } + }, + onLongClickItem = + remember(rowIndex, onLongClickItem) { + { index, item -> + onLongClickItem.invoke( + RowColumn(rowIndex, index), + item, + ) } - } - HomePageCardContent( - index = index, - item = item, - onClick = onClick, - onLongClick = onLongClick, - viewOptions = viewOptions, - modifier = - cardModifier - .onFocusChanged { onFocus(it.isFocused) } - .onKeyEvent { onKey(it) }, - ) - }, - showViewMore = showViewMore && row.showViewMore, - viewMoreCardContent = { mod -> - HomePageViewMoreCard( - isEpisode = row.items.last()?.type == BaseItemKind.EPISODE, - onClick = { - onClickViewMore.invoke( - RowColumn( - rowIndex, - r.items.size, - ), - r, - ) }, - onLongClick = {}, - viewOptions = viewOptions, - modifier = - mod.onFocusChanged { - if (it.isFocused) { - currentOnFocusPosition.invoke( - RowColumn( - rowIndex, - r.items.size, - ), - ) + modifier = + rowModifier + .fillMaxWidth() + .focusGroup() + .focusRequester(rowFocusRequesters[rowIndex]), + horizontalPadding = viewOptions.spacing.dp, + cardContent = { index, item, cardModifier, onClick, onLongClick -> + val onFocus = + remember(rowIndex, index) { + { isFocused: Boolean -> + if (isFocused) { + currentOnFocusPosition( + RowColumn( + rowIndex, + index, + ), + ) + } } + } + val onKey = + remember(item) { + { event: KeyEvent -> + if (isPlayKeyUp(event) && item?.type?.playable == true) { + Timber.v("Clicked play on ${item.id}") + currentOnClickPlay( + currentPosition, + item, + ) + true + } else { + false + } + } + } + HomePageCardContent( + index = index, + item = item, + onClick = onClick, + onLongClick = onLongClick, + viewOptions = viewOptions, + modifier = + cardModifier + .onFocusChanged { onFocus(it.isFocused) } + .onKeyEvent { onKey(it) }, + ) + }, + showViewMore = showViewMore && row.showViewMore, + viewMoreCardContent = { mod -> + HomePageViewMoreCard( + isEpisode = row.items.last()?.type == BaseItemKind.EPISODE, + onClick = { + onClickViewMore.invoke( + RowColumn( + rowIndex, + r.items.size, + ), + r, + ) }, - ) - }, - ) + onLongClick = {}, + viewOptions = viewOptions, + modifier = + mod.onFocusChanged { + if (it.isFocused) { + currentOnFocusPosition.invoke( + RowColumn( + rowIndex, + r.items.size, + ), + ) + } + }, + ) + }, + ) + } } else if (showEmptyRows) { FocusableItemRow( title = r.title.getString(), @@ -518,6 +566,16 @@ fun HomePageContent( } } } + focusedShowcaseRow?.let { showcaseRow -> + ShowcaseHero( + item = focusedItem, + rowTitle = showcaseRow.title.getString(), + viewOptions = showcaseRow.viewOptions, + index = position.column.coerceIn(0, showcaseRow.items.lastIndex), + count = showcaseRow.items.size, + modifier = Modifier.fillMaxSize(), + ) + } when (loadingState) { LoadingState.Pending, LoadingState.Loading, diff --git a/app/src/main/java/com/github/damontecres/wholphin/ui/main/ShowcaseRow.kt b/app/src/main/java/com/github/damontecres/wholphin/ui/main/ShowcaseRow.kt new file mode 100644 index 0000000000..139e813db2 --- /dev/null +++ b/app/src/main/java/com/github/damontecres/wholphin/ui/main/ShowcaseRow.kt @@ -0,0 +1,359 @@ +@file:OptIn(ExperimentalCoilApi::class) + +package com.github.damontecres.wholphin.ui.main + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.alpha +import androidx.compose.ui.draw.drawWithContent +import androidx.compose.ui.focus.onFocusChanged +import androidx.compose.ui.graphics.Brush +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.input.key.Key +import androidx.compose.ui.input.key.KeyEventType +import androidx.compose.ui.input.key.key +import androidx.compose.ui.input.key.onKeyEvent +import androidx.compose.ui.input.key.type +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalLayoutDirection +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.LayoutDirection +import androidx.compose.ui.unit.dp +import androidx.lifecycle.ViewModel +import androidx.tv.material3.MaterialTheme +import androidx.tv.material3.Surface +import androidx.tv.material3.Text +import coil3.annotation.ExperimentalCoilApi +import coil3.compose.AsyncImage +import coil3.request.ImageRequest +import coil3.request.transitionFactory +import com.github.damontecres.wholphin.data.model.BaseItem +import com.github.damontecres.wholphin.data.model.HomeRowViewOptions +import com.github.damontecres.wholphin.services.BackdropService +import com.github.damontecres.wholphin.services.ImageUrlService +import com.github.damontecres.wholphin.ui.CrossFadeFactory +import com.github.damontecres.wholphin.ui.LocalImageUrlService +import com.github.damontecres.wholphin.ui.cards.ItemRowTitle +import com.github.damontecres.wholphin.ui.components.OverviewText +import com.github.damontecres.wholphin.ui.components.QuickDetailsText +import com.github.damontecres.wholphin.ui.components.TimeRemaining +import com.github.damontecres.wholphin.ui.components.TitleOrLogo +import com.github.damontecres.wholphin.ui.components.rememberLogoUrl +import com.github.damontecres.wholphin.ui.components.rememberQuickDetailsContentMap +import com.github.damontecres.wholphin.ui.isNotNullOrBlank +import com.github.damontecres.wholphin.ui.playback.isPlayKeyUp +import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.delay +import org.jellyfin.sdk.model.api.BaseItemKind +import org.jellyfin.sdk.model.api.ImageType +import javax.inject.Inject +import kotlin.time.Duration.Companion.milliseconds +import kotlin.time.Duration.Companion.seconds + +/** + * The details shown over the full screen artwork while a showcase row is focused. The artwork itself + * is drawn behind the nav drawer by [ShowcaseBackdrop], and the rest of the page is hidden until + * focus moves to another row. + * + * @param rowTitle the title of the showcase row, shown faintly so it's clear which row this is + * @param index the position of [item] in the row + * @param count the number of items in the row + */ +@Composable +fun ShowcaseHero( + item: BaseItem?, + rowTitle: String, + viewOptions: HomeRowViewOptions, + index: Int, + count: Int, + modifier: Modifier = Modifier, +) { + Box(modifier = modifier) { + Text( + text = rowTitle, + style = MaterialTheme.typography.titleMedium, + color = MaterialTheme.colorScheme.onSurface, + modifier = + Modifier + .align(Alignment.TopStart) + .padding(start = 32.dp, top = 24.dp) + .alpha(.7f), + ) + ShowcaseDetails( + item = item, + viewOptions = viewOptions, + modifier = + Modifier + .align(Alignment.BottomStart) + .padding(start = 32.dp, bottom = 32.dp) + .fillMaxWidth(.5f), + ) + Text( + text = "${index + 1} / $count", + style = MaterialTheme.typography.titleSmall, + color = MaterialTheme.colorScheme.onSurface, + modifier = + Modifier + .align(Alignment.BottomEnd) + .padding(end = 32.dp, bottom = 32.dp) + .alpha(.7f), + ) + } +} + +/** + * A home row in showcase mode. While focused, [ShowcaseHero] covers the page, and left/right moves + * through the row's items (with [infiniteScroll], right on the last item goes back to the first). + * When not focused, the row shows a single wide banner of its current item's artwork. + * + * Select opens the item, long press shows its menu, and the play button plays it. + */ +@Composable +fun ShowcaseRowBanner( + title: String, + items: List, + initialIndex: Int, + height: Dp, + infiniteScroll: Boolean, + onFocusIndex: (Int) -> Unit, + onClickItem: (Int, BaseItem) -> Unit, + onLongClickItem: (Int, BaseItem) -> Unit, + onClickPlay: (Int, BaseItem) -> Unit, + modifier: Modifier = Modifier, +) { + val lastIndex = (items.size - 1).coerceAtLeast(0) + var index by rememberSaveable { mutableIntStateOf(initialIndex.coerceIn(0, lastIndex)) } + val currentIndex = index.coerceIn(0, lastIndex) + val item = items.getOrNull(currentIndex) + + Column( + verticalArrangement = Arrangement.spacedBy(8.dp), + modifier = modifier, + ) { + ItemRowTitle(title) + Surface( + onClick = { item?.let { onClickItem.invoke(currentIndex, it) } }, + onLongClick = { item?.let { onLongClickItem.invoke(currentIndex, it) } }, + modifier = + Modifier + .padding(horizontal = 16.dp) + .fillMaxWidth() + .height(height) + .onFocusChanged { + if (it.isFocused) onFocusIndex.invoke(currentIndex) + }.onKeyEvent { event -> + if (event.type == KeyEventType.KeyDown && event.key == Key.DirectionLeft) { + if (currentIndex > 0) { + index = currentIndex - 1 + onFocusIndex.invoke(index) + true + } else { + // Let focus move out of the row, such as to the nav drawer + false + } + } else if (event.type == KeyEventType.KeyDown && event.key == Key.DirectionRight) { + if (currentIndex < lastIndex) { + index = currentIndex + 1 + onFocusIndex.invoke(index) + } else if (infiniteScroll && lastIndex > 0) { + // Go back to the first item + index = 0 + onFocusIndex.invoke(index) + } + true + } else if (isPlayKeyUp(event)) { + item?.let { if (it.playable) onClickPlay.invoke(currentIndex, it) } + true + } else { + false + } + }, + ) { + Box(modifier = Modifier.fillMaxSize()) { + ShowcaseBackdrop( + item = item, + rotate = false, + modifier = Modifier.matchParentSize(), + ) + Text( + text = item?.data?.seriesName ?: item?.title ?: "", + style = MaterialTheme.typography.titleMedium, + color = MaterialTheme.colorScheme.onSurface, + maxLines = 1, + overflow = TextOverflow.Ellipsis, + modifier = + Modifier + .align(Alignment.BottomStart) + .padding(16.dp), + ) + } + } + } +} + +/** + * How long each backdrop is shown when rotating artwork + */ +private val ROTATE_ARTWORK_INTERVAL = 8.seconds + +/** + * Provides the [BackdropService] so the home page can set the full screen showcase artwork + */ +@HiltViewModel +class ShowcaseViewModel + @Inject + constructor( + val backdropService: BackdropService, + ) : ViewModel() + +/** + * Shows the item's backdrop. If [rotate] is on and the item has more than one backdrop set in + * Jellyfin, cycles through them in the order they are set. Dimmed while the nav drawer is open. + */ +@Composable +fun ShowcaseBackdrop( + item: BaseItem?, + rotate: Boolean, + modifier: Modifier = Modifier, + drawerIsOpen: Boolean = false, +) { + val imageUrlService = LocalImageUrlService.current + val imageUrls = remember(item) { imageUrlService.backdropUrls(item) } + var imageIndex by remember(item) { mutableIntStateOf(0) } + if (rotate && imageUrls.size > 1) { + LaunchedEffect(item, imageUrls.size) { + while (true) { + delay(ROTATE_ARTWORK_INTERVAL) + imageIndex = (imageIndex + 1) % imageUrls.size + } + } + } + val imageUrl = imageUrls.getOrNull(imageIndex) + val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl + val background = MaterialTheme.colorScheme.background + AsyncImage( + model = + ImageRequest + .Builder(LocalContext.current) + .data(imageUrl) + .transitionFactory(CrossFadeFactory(400.milliseconds)) + .build(), + contentDescription = null, + contentScale = ContentScale.Crop, + alignment = Alignment.TopCenter, + modifier = + modifier.drawWithContent { + drawContent() + // Fade toward the start edge and the bottom so text on top stays readable + val start = if (isRtl) size.width else 0f + val end = if (isRtl) 0f else size.width + drawRect( + brush = + Brush.horizontalGradient( + colors = listOf(background.copy(alpha = .75f), Color.Transparent), + startX = start, + endX = start + (end - start) * .55f, + ), + ) + drawRect( + brush = + Brush.verticalGradient( + colors = listOf(Color.Transparent, background.copy(alpha = .85f)), + startY = size.height * .55f, + endY = size.height, + ), + ) + if (drawerIsOpen) { + drawRect(Color.Black.copy(alpha = .75f)) + } + }, + ) +} + +/** + * All of the backdrop images set for the item in Jellyfin, in order. Episodes and seasons use their + * series' backdrops. + */ +private fun ImageUrlService.backdropUrls(item: BaseItem?): List { + if (item == null) return emptyList() + val data = item.data + val useParent = item.type == BaseItemKind.EPISODE || item.type == BaseItemKind.SEASON + val itemId = if (useParent) data.parentBackdropItemId else item.id + val tags = if (useParent) data.parentBackdropImageTags.orEmpty() else data.backdropImageTags.orEmpty() + return if (itemId == null || tags.isEmpty()) { + listOfNotNull(getItemImageUrl(item, ImageType.BACKDROP)) + } else { + tags.mapIndexedNotNull { index, tag -> + getItemImageUrl(itemId = itemId, imageType = ImageType.BACKDROP, tag = tag, imageIndex = index) + } + } +} + +@Composable +private fun ShowcaseDetails( + item: BaseItem?, + viewOptions: HomeRowViewOptions, + modifier: Modifier = Modifier, +) { + val textStyle = MaterialTheme.typography.titleSmall + val inlineContentMap = rememberQuickDetailsContentMap(textStyle) + val details = item?.ui?.quickDetails + Column( + verticalArrangement = Arrangement.spacedBy(8.dp), + modifier = modifier, + ) { + TitleOrLogo( + title = item?.data?.seriesName ?: item?.title, + logoImageUrl = rememberLogoUrl(item), + showLogo = viewOptions.showcaseShowLogo, + ) + val showDetails = viewOptions.showcaseShowDetails + val showRating = viewOptions.showcaseShowRating + if (details != null && (showDetails || showRating)) { + Row( + horizontalArrangement = Arrangement.spacedBy(4.dp), + verticalAlignment = Alignment.CenterVertically, + ) { + // Episode number & date or year, runtime, etc + if (showDetails) { + QuickDetailsText(details.basic, Modifier, textStyle, inlineContentMap) + } + if (showRating) { + QuickDetailsText(details.communityRating, Modifier, textStyle, inlineContentMap) + QuickDetailsText(details.criticRating, Modifier, textStyle, inlineContentMap) + } + // When it would end if started now, using the time remaining if partly watched + if (showDetails) { + item?.timeRemainingOrRuntime?.let { TimeRemaining(it, textStyle = textStyle) } + } + } + } + val overview = item?.data?.overview + if (viewOptions.showcaseShowOverview && overview.isNotNullOrBlank()) { + // The shared overview component, so app-wide overview settings apply here too + OverviewText( + overview = overview, + maxLines = 3, + onClick = {}, + enabled = false, + ) + } + } +} diff --git a/app/src/main/java/com/github/damontecres/wholphin/ui/main/settings/HomeRowSettings.kt b/app/src/main/java/com/github/damontecres/wholphin/ui/main/settings/HomeRowSettings.kt index 48901b0f1d..fe5f89eb06 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/ui/main/settings/HomeRowSettings.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/ui/main/settings/HomeRowSettings.kt @@ -55,7 +55,7 @@ import org.jellyfin.sdk.model.api.SortOrder @Composable fun HomeRowSettings( title: String, - config: HomeRowConfig, + config: HomeRowConfig?, preferenceOptions: List>, viewOptions: HomeRowViewOptions, onViewOptionsChange: (HomeRowViewOptions) -> Unit, @@ -123,11 +123,14 @@ fun HomeRowSettings( ) } } - addAdditionalConfig( - config = config, - onConfigChange = onConfigChange, - onConfigAction = onConfigAction, - ) + // Home rows have extra settings, such as combining Continue Watching and Next Up + if (config != null) { + addAdditionalConfig( + config = config, + onConfigChange = onConfigChange, + onConfigAction = onConfigAction, + ) + } } } } @@ -334,6 +337,80 @@ internal object Options { valueToIndex = { it.ordinal }, ) + val ViewOptionsShowcase = + AppSwitchPreference( + title = R.string.showcase_view, + defaultValue = false, + getter = { it.showcase }, + setter = { vo, value -> vo.copy(showcase = value) }, + summary = R.string.showcase_view_summary, + ) + + val ViewOptionsShowcaseLogo = + AppSwitchPreference( + title = R.string.show_logo, + defaultValue = true, + getter = { it.showcaseShowLogo }, + setter = { vo, value -> vo.copy(showcaseShowLogo = value) }, + ) + + val ViewOptionsShowcaseRating = + AppSwitchPreference( + title = R.string.show_rating, + defaultValue = true, + getter = { it.showcaseShowRating }, + setter = { vo, value -> vo.copy(showcaseShowRating = value) }, + ) + + val ViewOptionsShowcaseOverview = + AppSwitchPreference( + title = R.string.show_overview, + defaultValue = true, + getter = { it.showcaseShowOverview }, + setter = { vo, value -> vo.copy(showcaseShowOverview = value) }, + ) + + val ViewOptionsShowcaseDetails = + AppSwitchPreference( + title = R.string.show_details, + defaultValue = true, + getter = { it.showcaseShowDetails }, + setter = { vo, value -> vo.copy(showcaseShowDetails = value) }, + ) + + val ViewOptionsShowcaseInfiniteScroll = + AppSwitchPreference( + title = R.string.infinite_scroll, + defaultValue = true, + getter = { it.showcaseInfiniteScroll }, + setter = { vo, value -> vo.copy(showcaseInfiniteScroll = value) }, + summary = R.string.infinite_scroll_summary, + ) + + val ViewOptionsShowcaseRotateArtwork = + AppSwitchPreference( + title = R.string.rotate_artwork, + defaultValue = false, + getter = { it.showcaseRotateArtwork }, + setter = { vo, value -> vo.copy(showcaseRotateArtwork = value) }, + summary = R.string.rotate_artwork_summary, + ) + + private val SHOWCASE_GROUP = + PreferenceGroup( + title = R.string.showcase, + preferences = + listOf( + ViewOptionsShowcase, + ViewOptionsShowcaseLogo, + ViewOptionsShowcaseRating, + ViewOptionsShowcaseOverview, + ViewOptionsShowcaseDetails, + ViewOptionsShowcaseInfiniteScroll, + ViewOptionsShowcaseRotateArtwork, + ), + ) + val ViewOptionsApplyAll = AppClickablePreference( title = R.string.apply_all_rows, @@ -404,6 +481,7 @@ internal object Options { ViewOptionsUseSeries, ), ), + SHOWCASE_GROUP, PreferenceGroup( title = R.string.more, preferences = @@ -439,6 +517,7 @@ internal object Options { ViewOptionsEpisodeContentScale, ), ), + SHOWCASE_GROUP, PreferenceGroup( title = R.string.more, preferences = diff --git a/app/src/main/java/com/github/damontecres/wholphin/ui/nav/ApplicationContent.kt b/app/src/main/java/com/github/damontecres/wholphin/ui/nav/ApplicationContent.kt index 0cf426226b..6739ee6ca0 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/ui/nav/ApplicationContent.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/ui/nav/ApplicationContent.kt @@ -4,9 +4,11 @@ import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel import androidx.lifecycle.ViewModel +import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewmodel.navigation3.rememberViewModelStoreNavEntryDecorator import androidx.navigation3.runtime.NavEntry @@ -21,6 +23,7 @@ import com.github.damontecres.wholphin.services.BackdropService import com.github.damontecres.wholphin.services.NavigationManager import com.github.damontecres.wholphin.ui.components.ErrorMessage import com.github.damontecres.wholphin.ui.launchIO +import com.github.damontecres.wholphin.ui.main.ShowcaseBackdrop import dagger.hilt.android.lifecycle.HiltViewModel import javax.inject.Inject @@ -65,6 +68,16 @@ fun ApplicationContent( enableTopScrim = enableTopScrim, viewModel = viewModel, ) + // Full screen artwork for a focused showcase row, behind the nav drawer + val showcase by viewModel.backdropService.showcaseFlow.collectAsStateWithLifecycle() + showcase?.let { + ShowcaseBackdrop( + item = it.item, + rotate = it.rotate, + drawerIsOpen = drawerState.isOpen, + modifier = Modifier.fillMaxSize(), + ) + } val navDrawerListState = rememberLazyListState() NavDisplay( backStack = navigationManager.backStack, diff --git a/app/src/main/java/com/github/damontecres/wholphin/ui/preferences/LibraryRowsDialog.kt b/app/src/main/java/com/github/damontecres/wholphin/ui/preferences/LibraryRowsDialog.kt new file mode 100644 index 0000000000..03e7b2fd4b --- /dev/null +++ b/app/src/main/java/com/github/damontecres/wholphin/ui/preferences/LibraryRowsDialog.kt @@ -0,0 +1,111 @@ +package com.github.damontecres.wholphin.ui.preferences + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import androidx.tv.material3.ListItem +import androidx.tv.material3.MaterialTheme +import androidx.tv.material3.Text +import com.github.damontecres.wholphin.R +import com.github.damontecres.wholphin.data.model.HomeRowViewOptions +import com.github.damontecres.wholphin.ui.components.BasicDialog +import com.github.damontecres.wholphin.ui.components.recommendedRowDefaultViewOptions +import com.github.damontecres.wholphin.ui.components.recommendedRowTitles +import com.github.damontecres.wholphin.ui.main.settings.HomeRowSettings +import com.github.damontecres.wholphin.ui.main.settings.Options +import com.github.damontecres.wholphin.ui.nav.ServerNavDrawerItem +import org.jellyfin.sdk.model.api.CollectionType + +/** + * Lists the rows on a library's recommended tab. Choosing one shows the same row settings used for + * home page rows (size, image type, showcase, etc). + * + * @param rowSettings the saved settings for this library's rows, by row key + * @param rowKey gets the key for a row from its title + * @param onSave saves a row's settings, or null to go back to the default + */ +@Composable +fun LibraryRowsDialog( + library: ServerNavDrawerItem, + rowSettings: Map, + rowKey: (Int) -> String, + onSave: (rowKey: String, viewOptions: HomeRowViewOptions?) -> Unit, + onDismissRequest: () -> Unit, +) { + val rowTitles = remember(library.type) { recommendedRowTitles(library.type) } + val defaultViewOptions = remember(library.type) { recommendedRowDefaultViewOptions(library.type) } + var selectedRow by remember { mutableStateOf(null) } + + BasicDialog( + onDismissRequest = { + // Back from a row's settings returns to the list of rows + if (selectedRow != null) selectedRow = null else onDismissRequest.invoke() + }, + elevation = 3.dp, + ) { + val row = selectedRow + if (row == null) { + Column( + modifier = + Modifier + .padding(16.dp) + .width(420.dp), + ) { + Text( + text = stringResource(R.string.customize_rows_for, library.name), + style = MaterialTheme.typography.titleLarge, + color = MaterialTheme.colorScheme.onSurface, + modifier = Modifier.padding(bottom = 8.dp), + ) + LazyColumn { + items(rowTitles) { title -> + val showcase = rowSettings[rowKey(title)]?.showcase == true + ListItem( + selected = false, + onClick = { selectedRow = title }, + headlineContent = { Text(stringResource(title)) }, + supportingContent = + if (showcase) { + { Text(stringResource(R.string.showcase)) } + } else { + null + }, + ) + } + } + } + } else { + val key = rowKey(row) + val isEpisodeRow = + library.type == CollectionType.TVSHOWS && + (row == R.string.continue_watching || row == R.string.next_up) + HomeRowSettings( + title = stringResource(row), + config = null, + preferenceOptions = if (isEpisodeRow) Options.OPTIONS_EPISODES else Options.OPTIONS, + viewOptions = rowSettings[key] ?: defaultViewOptions, + onViewOptionsChange = { newViewOptions -> + onSave.invoke(key, newViewOptions.takeIf { it != defaultViewOptions }) + }, + onConfigChange = {}, + onConfigAction = {}, + onApplyApplyAll = {}, + defaultViewOptions = defaultViewOptions, + modifier = + Modifier + .padding(16.dp) + .width(420.dp), + ) + } + } +} diff --git a/app/src/main/java/com/github/damontecres/wholphin/ui/preferences/NavDrawerPreference.kt b/app/src/main/java/com/github/damontecres/wholphin/ui/preferences/NavDrawerPreference.kt index 7deeb95c5c..554c721e57 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/ui/preferences/NavDrawerPreference.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/ui/preferences/NavDrawerPreference.kt @@ -41,9 +41,11 @@ import androidx.tv.material3.Text import com.github.damontecres.wholphin.R import com.github.damontecres.wholphin.data.ServerPreferencesDao import com.github.damontecres.wholphin.data.ServerRepository +import com.github.damontecres.wholphin.data.model.HomeRowViewOptions import com.github.damontecres.wholphin.data.model.NavDrawerPinnedItem import com.github.damontecres.wholphin.data.model.NavPinType import com.github.damontecres.wholphin.services.BackdropService +import com.github.damontecres.wholphin.services.LibraryRowSettingsService import com.github.damontecres.wholphin.services.NavDrawerService import com.github.damontecres.wholphin.services.NavigationManager import com.github.damontecres.wholphin.services.SeerrServerRepository @@ -51,20 +53,25 @@ import com.github.damontecres.wholphin.ui.FontAwesome import com.github.damontecres.wholphin.ui.PreviewTvSpec import com.github.damontecres.wholphin.ui.components.BasicDialog import com.github.damontecres.wholphin.ui.components.Button +import com.github.damontecres.wholphin.ui.components.recommendedRowTitles import com.github.damontecres.wholphin.ui.launchDefault import com.github.damontecres.wholphin.ui.launchIO import com.github.damontecres.wholphin.ui.main.settings.MoveDirection import com.github.damontecres.wholphin.ui.nav.NavDrawerItem +import com.github.damontecres.wholphin.ui.nav.ServerNavDrawerItem import com.github.damontecres.wholphin.ui.theme.WholphinTheme import com.github.damontecres.wholphin.util.ExceptionHandler import com.github.damontecres.wholphin.util.WholphinDispatchers import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.qualifiers.ApplicationContext +import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.firstOrNull +import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import java.util.UUID import javax.inject.Inject data class NavDrawerPin( @@ -113,6 +120,7 @@ fun NavDrawerPreference( ) { val items by viewModel.state.collectAsState() var showDialog by remember { mutableStateOf(false) } + var customizeRowsFor by remember { mutableStateOf(null) } ClickPreference( title = title, summary = summary, @@ -140,6 +148,20 @@ fun NavDrawerPreference( onMoveDown = { index -> viewModel.update(items.move(MoveDirection.DOWN, index)) }, + onCustomizeRows = { customizeRowsFor = it }, + ) + } + customizeRowsFor?.let { library -> + val rowSettings by remember(library.itemId) { viewModel.libraryRowSettings(library.itemId) } + .collectAsState(emptyMap()) + LibraryRowsDialog( + library = library, + rowSettings = rowSettings, + rowKey = viewModel::rowKey, + onSave = { rowKey, viewOptions -> + viewModel.saveLibraryRowSettings(library.itemId, rowKey, viewOptions) + }, + onDismissRequest = { customizeRowsFor = null }, ) } } @@ -151,6 +173,7 @@ fun NavDrawerPreferenceDialog( onClick: (Int) -> Unit, onMoveUp: (Int) -> Unit, onMoveDown: (Int) -> Unit, + onCustomizeRows: (ServerNavDrawerItem) -> Unit = {}, ) { val scope = rememberCoroutineScope() val bringIntoViewRequesters = remember { List(items.size) { BringIntoViewRequester() } } @@ -196,6 +219,11 @@ fun NavDrawerPreferenceDialog( onMoveDown.invoke(index) ensureVisible(index + 1) }, + // Only libraries with a recommended tab have rows to customize + onCustomizeRows = + (item.item as? ServerNavDrawerItem) + ?.takeIf { recommendedRowTitles(it.type).isNotEmpty() } + ?.let { library -> { onCustomizeRows.invoke(library) } }, modifier = Modifier .animateItem() @@ -217,6 +245,7 @@ fun NavDrawerPreferenceListItem( onMoveUp: () -> Unit, onMoveDown: () -> Unit, modifier: Modifier = Modifier, + onCustomizeRows: (() -> Unit)? = null, ) { Box( modifier = modifier, @@ -251,6 +280,9 @@ fun NavDrawerPreferenceListItem( horizontalArrangement = Arrangement.spacedBy(4.dp), modifier = Modifier.wrapContentWidth(), ) { + if (onCustomizeRows != null) { + MoveButton(R.string.fa_sliders, true, onCustomizeRows) + } MoveButton(R.string.fa_caret_up, moveUpAllowed, onMoveUp) MoveButton(R.string.fa_caret_down, moveDownAllowed, onMoveDown) } @@ -305,6 +337,7 @@ class NavDrawerPreferencesViewModel private val navDrawerService: NavDrawerService, private val serverPreferencesDao: ServerPreferencesDao, private val seerrServerRepository: SeerrServerRepository, + private val libraryRowSettingsService: LibraryRowSettingsService, ) : ViewModel() { val state = MutableStateFlow>(listOf()) @@ -345,6 +378,24 @@ class NavDrawerPreferencesViewModel } } + fun rowKey(title: Int): String = libraryRowSettingsService.rowKey(title) + + fun libraryRowSettings(libraryId: UUID): Flow> = + serverRepository.currentUser?.let { libraryRowSettingsService.flow(it.id, libraryId) } + ?: flowOf(emptyMap()) + + fun saveLibraryRowSettings( + libraryId: UUID, + rowKey: String, + viewOptions: HomeRowViewOptions?, + ) { + viewModelScope.launchIO(ExceptionHandler(true)) { + serverRepository.currentUser?.let { + libraryRowSettingsService.save(it.id, libraryId, rowKey, viewOptions) + } + } + } + fun update(items: List) { state.update { items } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 98b35bd3f4..e0cda1ccc9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -341,6 +341,7 @@ Max items on home page rows Choose the default items to show, others will be hidden Customize Navigation Drawer Items + Rows for %1$s Click to switch pages Switch nav drawer pages on focus Passout Protection @@ -579,6 +580,16 @@ Increase size for all cards Decrease size for all cards Use thumb images + Showcase + Showcase view + Show the focused item\'s artwork large behind this row + Show logo + Show rating + Show overview + Infinite scroll + After the last item, go back to the first + Rotate artwork + Cycle through the backdrop images set for each item in Jellyfin Settings saved Display presets Built-in presets to quickly style all rows