Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(tv): convert libraries screen to media screen
Browse files Browse the repository at this point in the history
jarnedemeulemeester committed Dec 27, 2024
1 parent c20c460 commit f4dfc18
Showing 2 changed files with 37 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.jdtech.jellyfin.ui
package dev.jdtech.jellyfin.presentation.film

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
@@ -9,63 +9,65 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
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.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.tv.material3.MaterialTheme
import dev.jdtech.jellyfin.core.presentation.dummy.dummyCollections
import dev.jdtech.jellyfin.film.presentation.media.MediaAction
import dev.jdtech.jellyfin.film.presentation.media.MediaState
import dev.jdtech.jellyfin.film.presentation.media.MediaViewModel
import dev.jdtech.jellyfin.models.CollectionType
import dev.jdtech.jellyfin.models.FindroidCollection
import dev.jdtech.jellyfin.presentation.theme.FindroidTheme
import dev.jdtech.jellyfin.presentation.theme.spacings
import dev.jdtech.jellyfin.ui.components.Direction
import dev.jdtech.jellyfin.ui.components.ItemCard
import dev.jdtech.jellyfin.viewmodels.MediaViewModel
import java.util.UUID

@Composable
fun LibrariesScreen(
fun MediaScreen(
navigateToLibrary: (libraryId: UUID, libraryName: String, libraryType: CollectionType) -> Unit,
isLoading: (Boolean) -> Unit,
mediaViewModel: MediaViewModel = hiltViewModel(),
viewModel: MediaViewModel = hiltViewModel(),
) {
val delegatedUiState by mediaViewModel.uiState.collectAsState()
val state by viewModel.state.collectAsState()

LaunchedEffect(true) {
viewModel.loadData()
}

LaunchedEffect(state.isLoading) {
isLoading(state.isLoading)
}

LibrariesScreenLayout(
uiState = delegatedUiState,
isLoading = isLoading,
onClick = navigateToLibrary,
state = state,
onAction = { action ->
when (action) {
is MediaAction.OnItemClick -> {
navigateToLibrary(action.item.id, action.item.name, action.item.type)
}
else -> Unit
}
viewModel.onAction(action)
},
)
}

@Composable
private fun LibrariesScreenLayout(
uiState: MediaViewModel.UiState,
isLoading: (Boolean) -> Unit,
onClick: (UUID, String, CollectionType) -> Unit,
state: MediaState,
onAction: (MediaAction) -> Unit,
) {
var collections: List<FindroidCollection> by remember {
mutableStateOf(emptyList())
}
val focusRequester = remember { FocusRequester() }

when (uiState) {
is MediaViewModel.UiState.Normal -> {
collections = uiState.collections
isLoading(false)
}
is MediaViewModel.UiState.Loading -> {
isLoading(true)
}
else -> Unit
LaunchedEffect(state.libraries) {
focusRequester.requestFocus()
}

val focusRequester = remember { FocusRequester() }

LazyVerticalGrid(
columns = GridCells.Fixed(3),
horizontalArrangement = Arrangement.spacedBy(MaterialTheme.spacings.large),
@@ -78,29 +80,25 @@ private fun LibrariesScreenLayout(
),
modifier = Modifier.focusRequester(focusRequester),
) {
items(collections, key = { it.id }) { collection ->
items(state.libraries, key = { it.id }) { library ->
ItemCard(
item = collection,
item = library,
direction = Direction.HORIZONTAL,
onClick = {
onClick(collection.id, collection.name, collection.type)
onAction(MediaAction.OnItemClick(library))
},
)
}
}
LaunchedEffect(collections) {
focusRequester.requestFocus()
}
}

@Preview(device = "id:tv_1080p")
@Composable
private fun LibrariesScreenLayoutPreview() {
FindroidTheme {
LibrariesScreenLayout(
uiState = MediaViewModel.UiState.Normal(dummyCollections),
isLoading = {},
onClick = { _, _, _ -> },
state = MediaState(libraries = dummyCollections),
onAction = {},
)
}
}
3 changes: 2 additions & 1 deletion app/tv/src/main/java/dev/jdtech/jellyfin/ui/MainScreen.kt
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@ import dev.jdtech.jellyfin.models.CollectionType
import dev.jdtech.jellyfin.models.PlayerItem
import dev.jdtech.jellyfin.models.User
import dev.jdtech.jellyfin.presentation.film.HomeScreen
import dev.jdtech.jellyfin.presentation.film.MediaScreen
import dev.jdtech.jellyfin.presentation.theme.FindroidTheme
import dev.jdtech.jellyfin.presentation.theme.spacings
import dev.jdtech.jellyfin.ui.components.LoadingIndicator
@@ -202,7 +203,7 @@ private fun MainScreenLayout(
)
}
2 -> {
LibrariesScreen(
MediaScreen(
navigateToLibrary = navigateToLibrary,
isLoading = { isLoading = it },
)

0 comments on commit f4dfc18

Please sign in to comment.