Skip to content

Commit

Permalink
Remove viewModel and load theme from root presenter.
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaskioko committed Dec 2, 2023
1 parent 43f78c3 commit ec96967
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 80 deletions.
60 changes: 16 additions & 44 deletions app/src/main/kotlin/com/thomaskioko/tvmaniac/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,24 @@ import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.WindowCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.lifecycle.viewmodel.viewModelFactory
import com.thomaskioko.tvmaniac.MainActivityUiState.DataLoaded
import com.thomaskioko.tvmaniac.MainActivityUiState.Loading
import com.thomaskioko.tvmaniac.compose.theme.TvManiacTheme
import com.thomaskioko.tvmaniac.datastore.api.Theme
import com.thomaskioko.tvmaniac.inject.MainActivityComponent
import com.thomaskioko.tvmaniac.inject.create
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import com.thomaskioko.tvmaniac.navigation.Loading
import com.thomaskioko.tvmaniac.navigation.ThemeLoaded
import com.thomaskioko.tvmaniac.navigation.ThemeState

class MainActivity : ComponentActivity() {
private lateinit var component: MainActivityComponent

private val viewModel: MainActivityViewModel by viewModels {
viewModelFactory {
addInitializer(MainActivityViewModel::class) { component.viewModel() }
}
}

override fun onCreate(savedInstanceState: Bundle?) {
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
Expand All @@ -48,29 +33,18 @@ class MainActivity : ComponentActivity() {

component.traktAuthManager.registerResult()

var uiState: MainActivityUiState by mutableStateOf(Loading)

lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.state
.onEach {
uiState = it
}
.collect()
}
}

splashScreen.setKeepOnScreenCondition {
when (uiState) {
Loading -> true
is DataLoaded -> false
}
}

enableEdgeToEdge()

setContent {
val darkTheme = shouldUseDarkTheme(uiState)
val themeState by component.presenter.state.collectAsState()
val darkTheme = shouldUseDarkTheme(themeState)

splashScreen.setKeepOnScreenCondition {
when (themeState) {
Loading -> true
is ThemeLoaded -> false
}
}

DisposableEffect(darkTheme) {
enableEdgeToEdge(
Expand All @@ -87,9 +61,7 @@ class MainActivity : ComponentActivity() {
}

TvManiacTheme(darkTheme = darkTheme) {
Surface {
component.rootScreen()
}
component.rootScreen()
}
}
}
Expand All @@ -101,10 +73,10 @@ class MainActivity : ComponentActivity() {
*/
@Composable
private fun shouldUseDarkTheme(
uiState: MainActivityUiState,
uiState: ThemeState,
): Boolean = when (uiState) {
Loading -> isSystemInDarkTheme()
is DataLoaded -> when (uiState.theme) {
is ThemeLoaded -> when (uiState.theme) {
Theme.LIGHT -> false
Theme.DARK -> true
Theme.SYSTEM -> isSystemInDarkTheme()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.thomaskioko.tvmaniac.inject
import androidx.activity.ComponentActivity
import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.defaultComponentContext
import com.thomaskioko.tvmaniac.MainActivityViewModel
import com.thomaskioko.tvmaniac.navigation.RootNavigationPresenter
import com.thomaskioko.tvmaniac.navigation.RootScreen
import com.thomaskioko.tvmaniac.traktauth.api.TraktAuthManager
import com.thomaskioko.tvmaniac.traktauth.implementation.TraktAuthManagerComponent
Expand All @@ -19,6 +19,6 @@ abstract class MainActivityComponent(
@Component val applicationComponent: ApplicationComponent = ApplicationComponent.from(activity),
) : TraktAuthManagerComponent {
abstract val traktAuthManager: TraktAuthManager
abstract val viewModel: () -> MainActivityViewModel
abstract val presenter: RootNavigationPresenter
abstract val rootScreen: RootScreen
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.arkivanov.decompose.router.stack.childStack
import com.arkivanov.decompose.router.stack.pop
import com.arkivanov.decompose.router.stack.pushNew
import com.arkivanov.decompose.value.Value
import com.thomaskioko.tvmaniac.datastore.api.DatastoreRepository
import com.thomaskioko.tvmaniac.presentation.discover.DiscoverShowsPresenterFactory
import com.thomaskioko.tvmaniac.presentation.moreshows.MoreShowsPresenterFactory
import com.thomaskioko.tvmaniac.presentation.search.SearchPresenterFactory
Expand All @@ -18,7 +19,14 @@ import com.thomaskioko.tvmaniac.presentation.showdetails.ShowDetailsPresenterPre
import com.thomaskioko.tvmaniac.presentation.trailers.TrailersPresenterFactory
import com.thomaskioko.tvmaniac.presentation.watchlist.LibraryPresenterFactory
import com.thomaskioko.tvmaniac.traktauth.api.TraktAuthManager
import com.thomaskioko.tvmaniac.util.model.AppCoroutineDispatchers
import com.thomaskioko.tvmaniac.util.scope.ActivityScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.serialization.Serializable
import me.tatarka.inject.annotations.Inject

Expand All @@ -36,8 +44,12 @@ class RootNavigationPresenter(
private val seasonDetailsPresenterFactory: SeasonDetailsPresenterFactory,
private val trailersPresenterFactory: TrailersPresenterFactory,
private val traktAuthManager: TraktAuthManager,
datastoreRepository: DatastoreRepository,
dispatchers: AppCoroutineDispatchers,
) : ComponentContext by componentContext {

private val coroutineScope = CoroutineScope(SupervisorJob() + dispatchers.main)

private val navigation = StackNavigation<Config>()

internal val screenStack: Value<ChildStack<*, Screen>> =
Expand All @@ -49,6 +61,16 @@ class RootNavigationPresenter(
childFactory = ::createScreen,
)

val state: StateFlow<ThemeState> = datastoreRepository.observeTheme()
.map { theme ->
ThemeLoaded(theme = theme)
}
.stateIn(
scope = coroutineScope,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = Loading,
)

internal fun bringToFront(config: Config) {
navigation.bringToFront(config)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.thomaskioko.tvmaniac.navigation

import com.thomaskioko.tvmaniac.datastore.api.Theme

sealed interface ThemeState

data object Loading : ThemeState
data class ThemeLoaded(
val theme: Theme = Theme.SYSTEM,
) : ThemeState

0 comments on commit ec96967

Please sign in to comment.