From d7943b54a4b6c88095fdd8af052ab3f208a68280 Mon Sep 17 00:00:00 2001 From: takahirom Date: Mon, 6 Sep 2021 10:17:44 +0900 Subject: [PATCH 1/2] Use navigation scoped ViewModel for FeedViewModel --- .../io/github/droidkaigi/feeder/feed/FeedScreen.kt | 10 +++++----- .../io/github/droidkaigi/feeder/feed/FeedViewModel.kt | 10 ++++++---- .../feeder/viewmodel/ProvideRealViewModels.kt | 4 ++-- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/uicomponent-compose/feed/src/main/java/io/github/droidkaigi/feeder/feed/FeedScreen.kt b/uicomponent-compose/feed/src/main/java/io/github/droidkaigi/feeder/feed/FeedScreen.kt index 360573193..4f8d3866c 100644 --- a/uicomponent-compose/feed/src/main/java/io/github/droidkaigi/feeder/feed/FeedScreen.kt +++ b/uicomponent-compose/feed/src/main/java/io/github/droidkaigi/feeder/feed/FeedScreen.kt @@ -60,15 +60,15 @@ import io.github.droidkaigi.feeder.FeedContents import io.github.droidkaigi.feeder.FeedItem import io.github.droidkaigi.feeder.Filters import io.github.droidkaigi.feeder.Theme -import io.github.droidkaigi.feeder.core.R as CoreR import io.github.droidkaigi.feeder.core.TabIndicator import io.github.droidkaigi.feeder.core.getReadableMessage import io.github.droidkaigi.feeder.core.theme.AppThemeWithBackground import io.github.droidkaigi.feeder.core.theme.greenDroid import io.github.droidkaigi.feeder.core.use import io.github.droidkaigi.feeder.core.util.collectInLaunchedEffect -import kotlin.reflect.KClass import kotlinx.coroutines.launch +import kotlin.reflect.KClass +import io.github.droidkaigi.feeder.core.R as CoreR sealed class FeedTab(val name: String, val routePath: String) { object Home : FeedTab("Home", "home") @@ -452,7 +452,7 @@ fun FilterItemCountRow(count: String) { fun PreviewFeedScreen() { AppThemeWithBackground { CompositionLocalProvider( - feedViewModelProviderValue(fakeFeedViewModel()), + provideFeedViewModelFactory { fakeFeedViewModel() }, fmPlayerViewModelProviderValue(fakeFmPlayerViewModel()) ) { FeedScreen( @@ -473,7 +473,7 @@ fun PreviewDarkFeedScreen() { theme = Theme.DARK ) { CompositionLocalProvider( - feedViewModelProviderValue(fakeFeedViewModel()), + provideFeedViewModelFactory { fakeFeedViewModel() }, fmPlayerViewModelProviderValue(fakeFmPlayerViewModel()) ) { FeedScreen( @@ -492,7 +492,7 @@ fun PreviewDarkFeedScreen() { fun PreviewFeedScreenWithStartBlog() { AppThemeWithBackground { CompositionLocalProvider( - feedViewModelProviderValue(fakeFeedViewModel()), + provideFeedViewModelFactory { fakeFeedViewModel() }, fmPlayerViewModelProviderValue(fakeFmPlayerViewModel()) ) { FeedScreen( diff --git a/uicomponent-compose/feed/src/main/java/io/github/droidkaigi/feeder/feed/FeedViewModel.kt b/uicomponent-compose/feed/src/main/java/io/github/droidkaigi/feeder/feed/FeedViewModel.kt index 35e294cfa..4738fa147 100644 --- a/uicomponent-compose/feed/src/main/java/io/github/droidkaigi/feeder/feed/FeedViewModel.kt +++ b/uicomponent-compose/feed/src/main/java/io/github/droidkaigi/feeder/feed/FeedViewModel.kt @@ -33,12 +33,14 @@ interface FeedViewModel : override fun event(event: Event) } -private val LocalFeedViewModel = compositionLocalOf { - error("not LocalFeedViewModel provided") +private val LocalFeedViewModel = compositionLocalOf<@Composable () -> FeedViewModel> { + { + error("not LocalFeedViewModel provided") + } } -fun feedViewModelProviderValue(viewModel: FeedViewModel) = +fun provideFeedViewModelFactory(viewModel: @Composable () -> FeedViewModel) = LocalFeedViewModel provides viewModel @Composable -fun feedViewModel() = LocalFeedViewModel.current +fun feedViewModel() = LocalFeedViewModel.current() diff --git a/uicomponent-compose/main/src/main/java/io/github/droidkaigi/feeder/viewmodel/ProvideRealViewModels.kt b/uicomponent-compose/main/src/main/java/io/github/droidkaigi/feeder/viewmodel/ProvideRealViewModels.kt index 6c84963c8..7d7b1138a 100644 --- a/uicomponent-compose/main/src/main/java/io/github/droidkaigi/feeder/viewmodel/ProvideRealViewModels.kt +++ b/uicomponent-compose/main/src/main/java/io/github/droidkaigi/feeder/viewmodel/ProvideRealViewModels.kt @@ -6,8 +6,8 @@ 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.feedViewModelProviderValue import io.github.droidkaigi.feeder.feed.fmPlayerViewModelProviderValue +import io.github.droidkaigi.feeder.feed.provideFeedViewModelFactory import io.github.droidkaigi.feeder.provideAppViewModelFactory import io.github.droidkaigi.feeder.setting.settingViewModelProviderValue import io.github.droidkaigi.feeder.staff.staffViewModelProviderValue @@ -18,7 +18,7 @@ fun ProvideViewModels(content: @Composable () -> Unit) { provideAppViewModelFactory { hiltViewModel() }, - feedViewModelProviderValue(viewModel()), + provideFeedViewModelFactory { hiltViewModel() }, settingViewModelProviderValue(viewModel()), staffViewModelProviderValue(viewModel()), contributorViewModelProviderValue(fakeContributorViewModel()), From f4f352b159c0a9329f4f89c1ca8bef96649c6499 Mon Sep 17 00:00:00 2001 From: Takahiro Menju Date: Tue, 7 Sep 2021 09:03:44 +0900 Subject: [PATCH 2/2] Apply format Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../main/java/io/github/droidkaigi/feeder/feed/FeedScreen.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uicomponent-compose/feed/src/main/java/io/github/droidkaigi/feeder/feed/FeedScreen.kt b/uicomponent-compose/feed/src/main/java/io/github/droidkaigi/feeder/feed/FeedScreen.kt index 4f8d3866c..9625cdec1 100644 --- a/uicomponent-compose/feed/src/main/java/io/github/droidkaigi/feeder/feed/FeedScreen.kt +++ b/uicomponent-compose/feed/src/main/java/io/github/droidkaigi/feeder/feed/FeedScreen.kt @@ -60,15 +60,15 @@ import io.github.droidkaigi.feeder.FeedContents import io.github.droidkaigi.feeder.FeedItem import io.github.droidkaigi.feeder.Filters import io.github.droidkaigi.feeder.Theme +import io.github.droidkaigi.feeder.core.R as CoreR import io.github.droidkaigi.feeder.core.TabIndicator import io.github.droidkaigi.feeder.core.getReadableMessage import io.github.droidkaigi.feeder.core.theme.AppThemeWithBackground import io.github.droidkaigi.feeder.core.theme.greenDroid import io.github.droidkaigi.feeder.core.use import io.github.droidkaigi.feeder.core.util.collectInLaunchedEffect -import kotlinx.coroutines.launch import kotlin.reflect.KClass -import io.github.droidkaigi.feeder.core.R as CoreR +import kotlinx.coroutines.launch sealed class FeedTab(val name: String, val routePath: String) { object Home : FeedTab("Home", "home")