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 1 commit
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
1 change: 1 addition & 0 deletions buildSrc/src/main/java/Dep.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -16,6 +16,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[spotless] reported by reviewdog 🐶

Suggested change
import androidx.lifecycle.viewmodel.compose.viewModel

import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
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,18 @@ 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
@Composable
fun appViewModelFactoryProviderValue(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,8 +2,9 @@ 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.appViewModelFactoryProviderValue
import io.github.droidkaigi.feeder.contributor.contributorViewModelProviderValue
import io.github.droidkaigi.feeder.contributor.fakeContributorViewModel
import io.github.droidkaigi.feeder.feed.feedViewModelProviderValue
Expand All @@ -14,7 +15,9 @@ import io.github.droidkaigi.feeder.staff.staffViewModelProviderValue
@Composable
fun ProvideViewModels(content: @Composable () -> Unit) {
CompositionLocalProvider(
appViewModelProviderValue(viewModel<RealAppViewModel>()),
appViewModelFactoryProviderValue {
hiltViewModel<RealAppViewModel>()
},
feedViewModelProviderValue(viewModel<RealFeedViewModel>()),
settingViewModelProviderValue(viewModel<RealSettingViewModel>()),
staffViewModelProviderValue(viewModel<RealStaffViewModel>()),
Expand Down