Skip to content

Commit

Permalink
Lifecycle common
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbel committed May 28, 2024
1 parent d1f776c commit 2311fad
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.michaelbel.movies.ui.ktx

import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.flow.StateFlow

@Composable
actual fun <T> StateFlow<T>.collectAsStateCommon(
lifecycleOwner: LifecycleOwner,
minActiveState: Lifecycle.State,
context: CoroutineContext
): State<T> = collectAsStateWithLifecycle()
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.michaelbel.movies.ui.ktx

import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlinx.coroutines.flow.StateFlow

@Composable
expect fun <T> StateFlow<T>.collectAsStateCommon(
lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current,
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
context: CoroutineContext = EmptyCoroutineContext
): State<T>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.michaelbel.movies.ui.ktx

import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.flow.StateFlow

@Composable
actual fun <T> StateFlow<T>.collectAsStateCommon(
lifecycleOwner: LifecycleOwner,
minActiveState: Lifecycle.State,
context: CoroutineContext
): State<T> = collectAsState()
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package org.michaelbel.movies.feed.ui
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.paging.compose.collectAsLazyPagingItems
import org.koin.androidx.compose.koinViewModel
import org.michaelbel.movies.feed.FeedViewModel
import org.michaelbel.movies.persistence.database.ktx.orEmpty
import org.michaelbel.movies.ui.ktx.collectAsStateCommon

@Composable
fun FeedRoute(
Expand All @@ -20,11 +20,11 @@ fun FeedRoute(
viewModel: FeedViewModel = koinViewModel()
) {
val pagingItems = viewModel.pagingDataFlow.collectAsLazyPagingItems()
val account by viewModel.account.collectAsStateWithLifecycle()
val currentFeedView by viewModel.currentFeedView.collectAsStateWithLifecycle()
val currentMovieList by viewModel.currentMovieList.collectAsStateWithLifecycle()
val notificationsPermissionRequired by viewModel.notificationsPermissionRequired.collectAsStateWithLifecycle()
val networkStatus by viewModel.networkStatus.collectAsStateWithLifecycle()
val account by viewModel.account.collectAsStateCommon()
val currentFeedView by viewModel.currentFeedView.collectAsStateCommon()
val currentMovieList by viewModel.currentMovieList.collectAsStateCommon()
val notificationsPermissionRequired by viewModel.notificationsPermissionRequired.collectAsStateCommon()
val networkStatus by viewModel.networkStatus.collectAsStateCommon()
val isAuthFailureSnackbarShowed = viewModel.isAuthFailureSnackbarShowed

FeedScreenContent(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package org.michaelbel.movies.feed

import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.runBlocking
import org.michaelbel.movies.common.list.MovieList
import org.michaelbel.movies.common.viewmodel.BaseViewModel
import org.michaelbel.movies.interactor.Interactor

class FeedViewModel(
private val interactor: Interactor
): BaseViewModel() {


val currentMovieList: StateFlow<MovieList> = interactor.currentMovieList
.stateIn(
scope = viewModelScope,
started = SharingStarted.Lazily,
initialValue = runBlocking { interactor.currentMovieList.first() }
)
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
@file:OptIn(ExperimentalMaterial3Api::class)

package org.michaelbel.movies.feed.ui

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import org.koin.compose.koinInject
import org.michaelbel.movies.feed.FeedViewModel
import org.michaelbel.movies.persistence.database.entity.pojo.AccountPojo
import org.michaelbel.movies.ui.ktx.collectAsStateCommon

@Composable
fun FeedRoute(
Expand All @@ -23,41 +17,15 @@ fun FeedRoute(
modifier: Modifier = Modifier,
viewModel: FeedViewModel = koinInject<FeedViewModel>()
) {
val currentMovieList by viewModel.currentMovieList.collectAsStateCommon()

FeedScreenContent(
currentMovieList = currentMovieList,
onNavigateToSearch = onNavigateToSearch,
onNavigateToAuth = onNavigateToAuth,
onNavigateToAccount = onNavigateToAccount,
onNavigateToSettings = onNavigateToSettings,
onNavigateToDetails = onNavigateToDetails,
modifier = modifier
)
}

@Composable
private fun FeedScreenContent(
onNavigateToSearch: () -> Unit,
onNavigateToAuth: () -> Unit,
onNavigateToAccount: () -> Unit,
onNavigateToSettings: () -> Unit,
onNavigateToDetails: (String, Int) -> Unit,
modifier: Modifier = Modifier
) {
Scaffold(
modifier = modifier.fillMaxSize(),
topBar = {
FeedToolbar(
title = "Now Playing Movies",
account = AccountPojo.Empty,
isTmdbApiKeyEmpty = false,
topAppBarScrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(),
onSearchIconClick = onNavigateToSearch,
onAuthIconClick = onNavigateToAuth,
onAccountIconClick = onNavigateToAccount,
onSettingsIconClick = onNavigateToSettings
)
},
containerColor = MaterialTheme.colorScheme.background
) { innerPadding ->

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@file:OptIn(ExperimentalMaterial3Api::class)

package org.michaelbel.movies.feed.ui

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import org.michaelbel.movies.common.list.MovieList
import org.michaelbel.movies.feed.ktx.titleText
import org.michaelbel.movies.persistence.database.entity.pojo.AccountPojo

@Composable
internal fun FeedScreenContent(
currentMovieList: MovieList,
onNavigateToSearch: () -> Unit,
onNavigateToAuth: () -> Unit,
onNavigateToAccount: () -> Unit,
onNavigateToSettings: () -> Unit,
onNavigateToDetails: (String, Int) -> Unit,
modifier: Modifier = Modifier
) {
Scaffold(
modifier = modifier.fillMaxSize(),
topBar = {
FeedToolbar(
title = currentMovieList.titleText,
account = AccountPojo.Empty,
isTmdbApiKeyEmpty = false,
topAppBarScrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(),
onSearchIconClick = onNavigateToSearch,
onAuthIconClick = onNavigateToAuth,
onAccountIconClick = onNavigateToAccount,
onSettingsIconClick = onNavigateToSettings
)
},
containerColor = MaterialTheme.colorScheme.background
) { innerPadding ->

}
}

0 comments on commit 2311fad

Please sign in to comment.