Skip to content
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

Use navigation scoped viewmodel #614

Merged
merged 6 commits into from
Sep 14, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ fun PreviewFeedScreen() {
AppThemeWithBackground {
CompositionLocalProvider(
provideFeedViewModelFactory { fakeFeedViewModel() },
fmPlayerViewModelProviderValue(fakeFmPlayerViewModel())
provideFmPlayerViewModelFactory { fakeFmPlayerViewModel() }
) {
FeedScreen(
selectedTab = FeedTab.Home,
Expand All @@ -479,7 +479,7 @@ fun PreviewDarkFeedScreen() {
) {
CompositionLocalProvider(
provideFeedViewModelFactory { fakeFeedViewModel() },
fmPlayerViewModelProviderValue(fakeFmPlayerViewModel())
provideFmPlayerViewModelFactory { fakeFmPlayerViewModel() }
) {
FeedScreen(
selectedTab = FeedTab.Home,
Expand All @@ -498,7 +498,7 @@ fun PreviewFeedScreenWithStartBlog() {
AppThemeWithBackground {
CompositionLocalProvider(
provideFeedViewModelFactory { fakeFeedViewModel() },
fmPlayerViewModelProviderValue(fakeFmPlayerViewModel())
provideFmPlayerViewModelFactory { fakeFmPlayerViewModel() }
) {
FeedScreen(
selectedTab = FeedTab.FilteredFeed.Blog,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ interface FmPlayerViewModel : UnidirectionalViewModel<FmPlayerViewModel.Event,
override fun event(event: Event)
}

private val LocalFmPlayerViewModel = compositionLocalOf<FmPlayerViewModel> {
error("not LocalFmPlayerViewModel provided")
}
private val LocalFmPlayerViewModelFactory =
compositionLocalOf<@Composable () -> FmPlayerViewModel> {
{
error("not LocalFmPlayerViewModel provided")
}
}

fun fmPlayerViewModelProviderValue(viewModel: FmPlayerViewModel) =
LocalFmPlayerViewModel provides viewModel
fun provideFmPlayerViewModelFactory(viewModelFactory: @Composable () -> FmPlayerViewModel) =
LocalFmPlayerViewModelFactory provides viewModelFactory

@Composable
fun fmPlayerViewModel() = LocalFmPlayerViewModel.current
fun fmPlayerViewModel() = LocalFmPlayerViewModelFactory.current()
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@ package io.github.droidkaigi.feeder.viewmodel
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import io.github.droidkaigi.feeder.contributor.contributorViewModelProviderValue
import io.github.droidkaigi.feeder.contributor.fakeContributorViewModel
import io.github.droidkaigi.feeder.feed.fmPlayerViewModelProviderValue
import io.github.droidkaigi.feeder.contributor.provideContributorViewModelFactory
import io.github.droidkaigi.feeder.feed.provideFeedViewModelFactory
import io.github.droidkaigi.feeder.feed.provideFmPlayerViewModelFactory
import io.github.droidkaigi.feeder.provideAppViewModelFactory
import io.github.droidkaigi.feeder.setting.provideSettingViewModelFactory
import io.github.droidkaigi.feeder.staff.staffViewModelProviderValue
import io.github.droidkaigi.feeder.staff.provideStaffViewModelFactory

@Composable
fun ProvideViewModels(content: @Composable () -> Unit) {
CompositionLocalProvider(
provideAppViewModelFactory { hiltViewModel<RealAppViewModel>() },
provideFeedViewModelFactory { hiltViewModel<RealFeedViewModel>() },
provideSettingViewModelFactory { hiltViewModel<RealSettingViewModel>() },
staffViewModelProviderValue(viewModel<RealStaffViewModel>()),
contributorViewModelProviderValue(fakeContributorViewModel()),
fmPlayerViewModelProviderValue(viewModel<RealFmPlayerViewModel>()),
provideStaffViewModelFactory { (hiltViewModel<RealStaffViewModel>()) },
provideContributorViewModelFactory { fakeContributorViewModel() },
provideFmPlayerViewModelFactory { hiltViewModel<RealFmPlayerViewModel>() },
content = content
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ fun ContributorItem(contributor: Contributor, onClickItem: (Contributor) -> Unit
fun PreviewContributorItem() {
ConferenceAppFeederTheme {
val contributor = fakeContributors().first()
CompositionLocalProvider(contributorViewModelProviderValue(fakeContributorViewModel())) {
CompositionLocalProvider(
provideContributorViewModelFactory {
fakeContributorViewModel()
}
) {
ContributorItem(contributor = contributor) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ fun ContributorList(onContributorClick: (Contributor) -> Unit) {
@Composable
fun PreviewContributorScreen() {
ConferenceAppFeederTheme {
CompositionLocalProvider(contributorViewModelProviderValue(fakeContributorViewModel())) {
CompositionLocalProvider(
provideContributorViewModelFactory {
fakeContributorViewModel()
}
) {
ContributorList() {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ interface ContributorViewModel :
override fun event(event: Event)
}

private val LocalContributeViewModel = compositionLocalOf<ContributorViewModel> {
error("Not view model provided")
}
private val LocalContributeViewModelFactory =
compositionLocalOf<@Composable () -> ContributorViewModel> {
{
error("Not view model provided")
}
}

fun contributorViewModelProviderValue(viewModel: ContributorViewModel) =
LocalContributeViewModel provides viewModel
fun provideContributorViewModelFactory(viewModelFactory: @Composable () -> ContributorViewModel) =
LocalContributeViewModelFactory provides viewModelFactory

@Composable
fun contributeViewModel() = LocalContributeViewModel.current
fun contributeViewModel() = LocalContributeViewModelFactory.current()
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fun StaffItem(staff: Staff, onClickItem: (Staff) -> Unit) {
fun PreviewStaffItem() {
ConferenceAppFeederTheme {
val staff = fakeStaffs().first()
CompositionLocalProvider(staffViewModelProviderValue(fakeStaffViewModel())) {
CompositionLocalProvider(provideStaffViewModelFactory { fakeStaffViewModel() }) {
StaffItem(staff = staff) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fun StaffList(onStaffClick: (Staff) -> Unit) {
@Composable
fun PreviewStaffScreen() {
ConferenceAppFeederTheme {
CompositionLocalProvider(staffViewModelProviderValue(fakeStaffViewModel())) {
CompositionLocalProvider(provideStaffViewModelFactory { fakeStaffViewModel() }) {
StaffList {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ interface StaffViewModel :
override fun event(event: Event)
}

private val LocalStaffViewModel = compositionLocalOf<StaffViewModel> {
error("Not view model provided")
private val LocalStaffViewModelFactory = compositionLocalOf<@Composable () -> StaffViewModel> {
{
error("Not view model provided")
}
}

fun staffViewModelProviderValue(viewModel: StaffViewModel) = LocalStaffViewModel provides viewModel
fun provideStaffViewModelFactory(viewModelFactory: @Composable () -> StaffViewModel) =
LocalStaffViewModelFactory provides viewModelFactory

@Composable
fun staffViewModel() = LocalStaffViewModel.current
fun staffViewModel() = LocalStaffViewModelFactory.current()