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 for AppViewModel #604

Merged
merged 6 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 2 additions & 1 deletion buildSrc/src/main/java/Dep.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object Dep {
const val activity = "androidx.activity:activity-compose:1.3.0"
const val constraintLayout =
"androidx.constraintlayout:constraintlayout-compose:1.0.0-beta02"
const val navigation = "androidx.navigation:navigation-compose:2.4.0-alpha05"
const val navigation = "androidx.navigation:navigation-compose:2.4.0-alpha06"
const val livedata = "androidx.compose.runtime:runtime-livedata:${Versions.compose}"
const val runtime = "androidx.compose.runtime:runtime:${Versions.compose}"
const val foundation = "androidx.compose.foundation:foundation:${Versions.compose}"
Expand All @@ -23,6 +23,7 @@ object Dep {
const val iconsExtended =
"androidx.compose.material:material-icons-extended:${Versions.compose}"
const val animation = "androidx.compose.animation:animation:${Versions.compose}"
const val hiltNavigationCompose = "androidx.hilt:hilt-navigation-compose:1.0.0-alpha03"
}

object Jetpack {
Expand Down
1 change: 1 addition & 0 deletions uicomponent-compose/feed/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ dependencies {
// https://github.com/android/compose-samples/blob/master/JetNews/app/build.gradle#L66
implementation Dep.Compose.constraintLayout
implementation Dep.Compose.navigation
implementation Dep.Compose.hiltNavigationCompose
implementation Dep.Compose.runtime
implementation Dep.Compose.livedata
implementation Dep.Compose.foundation
Expand Down
1 change: 1 addition & 0 deletions uicomponent-compose/main/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ dependencies {
// https://github.com/android/compose-samples/blob/master/JetNews/app/build.gradle#L66
implementation Dep.Compose.activity
implementation Dep.Compose.navigation
implementation Dep.Compose.hiltNavigationCompose
implementation Dep.Compose.runtime
implementation Dep.Compose.livedata
implementation Dep.Compose.foundation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import androidx.navigation.compose.composable
import androidx.navigation.compose.navArgument
import androidx.navigation.navDeepLink
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import io.github.droidkaigi.feeder.core.R as CoreR
import io.github.droidkaigi.feeder.core.navigation.chromeCustomTabs
takahirom marked this conversation as resolved.
Show resolved Hide resolved
import io.github.droidkaigi.feeder.core.navigation.navigateChromeCustomTabs
import io.github.droidkaigi.feeder.core.navigation.rememberCustomNavController
Expand All @@ -32,6 +31,7 @@ import io.github.droidkaigi.feeder.feed.FeedTab
import io.github.droidkaigi.feeder.other.OtherScreen
import io.github.droidkaigi.feeder.other.OtherTab
import kotlinx.coroutines.launch
import io.github.droidkaigi.feeder.core.R as CoreR
takahirom marked this conversation as resolved.
Show resolved Hide resolved

private const val FEED_PATH = "feed/"
private const val OTHER_PATH = "other/"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.droidkaigi.feeder

import androidx.compose.runtime.Composable
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.staticCompositionLocalOf
import io.github.droidkaigi.feeder.core.UnidirectionalViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -26,11 +26,17 @@ interface AppViewModel :
override fun event(event: Event)
}

private val LocalAppViewModel = compositionLocalOf<AppViewModel> {
error("not LocalDroidKaigiAppViewModel provided")
}
private val LocalAppViewModelFactory =
staticCompositionLocalOf<@Composable () -> AppViewModel> {
{
error("not LocalDroidKaigiAppViewModel provided")
}
}

fun appViewModelProviderValue(viewModel: AppViewModel) = LocalAppViewModel provides viewModel
fun provideAppViewModelFactory(viewModelFactory: @Composable () -> AppViewModel) =
LocalAppViewModelFactory provides viewModelFactory

@Composable
fun appViewModel() = LocalAppViewModel.current
fun appViewModel(): AppViewModel {
return LocalAppViewModelFactory.current()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ 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.appViewModelProviderValue
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.provideAppViewModelFactory
import io.github.droidkaigi.feeder.setting.settingViewModelProviderValue
import io.github.droidkaigi.feeder.staff.staffViewModelProviderValue

@Composable
fun ProvideViewModels(content: @Composable () -> Unit) {
CompositionLocalProvider(
appViewModelProviderValue(viewModel<RealAppViewModel>()),
provideAppViewModelFactory {
hiltViewModel<RealAppViewModel>()
},
feedViewModelProviderValue(viewModel<RealFeedViewModel>()),
settingViewModelProviderValue(viewModel<RealSettingViewModel>()),
staffViewModelProviderValue(viewModel<RealStaffViewModel>()),
Expand Down