Skip to content

[Jetcaster]: Provide PodcastDetailsViewModel via factory. #1310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions Jetcaster/app/src/main/java/com/example/jetcaster/ui/home/Home.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalSavedStateRegistryOwner
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Devices
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.core.os.bundleOf
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.AsyncImage
Expand All @@ -99,6 +101,7 @@ import com.example.jetcaster.core.data.model.LibraryInfo
import com.example.jetcaster.core.data.model.PlayerEpisode
import com.example.jetcaster.core.data.model.PodcastCategoryFilterResult
import com.example.jetcaster.core.data.model.PodcastInfo
import com.example.jetcaster.ui.Screen
import com.example.jetcaster.ui.home.discover.discoverItems
import com.example.jetcaster.ui.home.library.libraryItems
import com.example.jetcaster.ui.podcast.PodcastDetailsScreen
Expand All @@ -123,9 +126,7 @@ fun MainScreen(
viewModel: HomeViewModel = viewModel()
) {
val viewState by viewModel.state.collectAsStateWithLifecycle()
val navigator = rememberSupportingPaneScaffoldNavigator<String>(
isDestinationHistoryAware = false
)
val navigator = rememberSupportingPaneScaffoldNavigator<String>()
BackHandler(enabled = navigator.canNavigateBack()) {
navigator.navigateBack()
}
Expand All @@ -137,8 +138,14 @@ fun MainScreen(
val podcastUri = navigator.currentDestination?.content
?: viewState.featuredPodcasts.firstOrNull()?.uri
if (!podcastUri.isNullOrEmpty()) {
val podcastDetailsViewModel = PodcastDetailsViewModel(
podcastUri = podcastUri
val podcastDetailsViewModel: PodcastDetailsViewModel = viewModel(
key = podcastUri,
factory = PodcastDetailsViewModel.provideFactory(
owner = LocalSavedStateRegistryOwner.current,
defaultArgs = bundleOf(
Screen.ARG_PODCAST_URI to podcastUri
)
)
)
PodcastDetailsScreen(
viewModel = podcastDetailsViewModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,10 @@ class PodcastDetailsViewModel(
private val episodeStore: EpisodeStore = Graph.episodeStore,
private val episodePlayer: EpisodePlayer = Graph.episodePlayer,
private val podcastStore: PodcastStore = Graph.podcastStore,
private val podcastUri: String
savedStateHandle: SavedStateHandle
) : ViewModel() {

constructor(
episodeStore: EpisodeStore = Graph.episodeStore,
episodePlayer: EpisodePlayer = Graph.episodePlayer,
podcastStore: PodcastStore = Graph.podcastStore,
savedStateHandle: SavedStateHandle
) : this(
episodeStore = episodeStore,
episodePlayer = episodePlayer,
podcastStore = podcastStore,
podcastUri = Uri.decode(savedStateHandle.get<String>(Screen.ARG_PODCAST_URI)!!)
)
private val podcastUri = Uri.decode(savedStateHandle.get<String>(Screen.ARG_PODCAST_URI)!!)

val state: StateFlow<PodcastUiState> =
combine(
Expand Down