Skip to content

Commit

Permalink
Merge pull request #69 from stslex/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
stslex authored Nov 17, 2023
2 parents d853691 + dd5a819 commit c9eed61
Show file tree
Hide file tree
Showing 59 changed files with 195 additions and 369 deletions.
12 changes: 5 additions & 7 deletions app/src/main/java/st/slex/csplashscreen/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.core.view.WindowCompat
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import st.slex.csplashscreen.core.ui.base.daggerViewModel
import st.slex.csplashscreen.core.ui.di.MainUiApi
Expand All @@ -26,10 +25,10 @@ class MainActivity : ComponentActivity(), MainUiProvider {
WindowCompat.setDecorFitsSystemWindows(window, false)
setContent {
AppTheme {
val navController = rememberNavController()
val viewModel = buildViewModel(navController)
val viewModel = buildViewModel()

InitialApp(
navController = navController,
navController = api.navigator.controller,
onBottomAppBarClick = remember {
{ viewModel.navigate(it) }
}
Expand All @@ -39,9 +38,8 @@ class MainActivity : ComponentActivity(), MainUiProvider {
}

@Composable
private fun buildViewModel(
navHostController: NavHostController
): InitialAppViewModel {
private fun buildViewModel(): InitialAppViewModel {
val navHostController = rememberNavController()
val component = buildMainUIApi(
navHostController = navHostController
)
Expand Down
4 changes: 2 additions & 2 deletions build-logic/dependencies/src/main/kotlin/AppVersions.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
object AppVersions {
const val VERSION_NAME = "1.67"
const val VERSION_CODE = 13
const val VERSION_NAME = "1.69"
const val VERSION_CODE = 15
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class NavigatorImpl @Inject constructor(
private val navController: NavHostController
) : Navigator {

override val controller: NavHostController
get() = navController

override fun invoke(screen: Screen) {
when (screen) {
is NavigationScreen.PopBackStack -> navController.popBackStack()
Expand Down
1 change: 1 addition & 0 deletions core/ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
dependencies {
implementation(project(":core:core"))
implementation(project(":core:network"))
api(libs.androidx.compose.navigation)

implementation(libs.androidx.paging.runtime)
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package st.slex.csplashscreen.core.ui.di

fun interface Navigator {
import androidx.navigation.NavHostController

interface Navigator {

val controller: NavHostController

operator fun invoke(screen: Screen)
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package st.slex.csplashscreen.core.ui.mvi

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingData
import androidx.paging.cachedIn
import androidx.paging.map
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn
Expand All @@ -27,45 +29,34 @@ import st.slex.csplashscreen.core.ui.mvi.Store.Event
import st.slex.csplashscreen.core.ui.mvi.Store.Navigation
import st.slex.csplashscreen.core.ui.mvi.Store.State

abstract class BaseStore<S : State, E : Event, A : Action, N : Navigation>(
abstract class BaseViewModel<S : State, E : Event, A : Action, N : Navigation>(
private val router: Router<N>,
private val appDispatcher: AppDispatcher
) : Store<S, E, A> {
) : ViewModel() {

abstract val initialState: S
abstract fun sendAction(action: A)

private var _scope: CoroutineScope? = null
private val scope: CoroutineScope
get() = requireNotNull(_scope)
protected open val _state: MutableStateFlow<S> = MutableStateFlow(initialState)
val state: StateFlow<S>
get() = _state.asStateFlow()

override val state: MutableStateFlow<S>
get() = MutableStateFlow(initialState)

override val event: MutableSharedFlow<E> = MutableSharedFlow()
val event: MutableSharedFlow<E> = MutableSharedFlow()

fun updateState(update: (S) -> S) {
state.update(update)
_state.update(update)
}

fun sendEvent(event: E) {
scope.launch(appDispatcher.default) {
this@BaseStore.event.emit(event)
viewModelScope.launch(appDispatcher.default) {
this@BaseViewModel.event.emit(event)
}
}

fun navigate(event: N) {
router(event)
}

override fun init(scope: CoroutineScope) {
_scope = scope
}

override fun destroy() {
_scope?.cancel()
_scope = null
}

fun <K : Any, T : Any, R : Any> Pager<K, T>.state(
transform: suspend (value: T) -> R
): StateFlow<PagingData<R>> = this
Expand All @@ -75,14 +66,16 @@ abstract class BaseStore<S : State, E : Event, A : Action, N : Navigation>(

fun <T : Any> Flow<PagingData<T>>.state(): StateFlow<PagingData<T>> = this
.flowOn(appDispatcher.default)
.cachedIn(scope)
.cachedIn(viewModelScope)
.stateIn(
initialValue = PagingData.empty(),
scope = scope,
scope = viewModelScope,
started = SharingStarted.Lazily
)

fun launch(block: suspend CoroutineScope.() -> Unit): Job = scope.launch(
fun launch(
block: suspend CoroutineScope.() -> Unit
): Job = viewModelScope.launch(
context = appDispatcher.default,
block = block
)
Expand All @@ -91,7 +84,7 @@ abstract class BaseStore<S : State, E : Event, A : Action, N : Navigation>(
block: suspend CoroutineScope.() -> T,
onFailure: suspend (Throwable) -> Unit = {},
onSuccess: (T) -> Unit,
): Job = scope.launch(appDispatcher.default) {
): Job = viewModelScope.launch(appDispatcher.default) {
runCatching {
block()
}
Expand All @@ -112,5 +105,5 @@ abstract class BaseStore<S : State, E : Event, A : Action, N : Navigation>(
Logger.exception(error)
}
.onEach(each)
.launchIn(scope)
.launchIn(viewModelScope)
}
18 changes: 1 addition & 17 deletions core/ui/src/main/java/st/slex/csplashscreen/core/ui/mvi/Store.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
package st.slex.csplashscreen.core.ui.mvi

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import st.slex.csplashscreen.core.ui.mvi.Store.Action
import st.slex.csplashscreen.core.ui.mvi.Store.Event
import st.slex.csplashscreen.core.ui.mvi.Store.State

interface Store<out S : State, out E : Event, in A : Action> {

val state: StateFlow<S>
val event: SharedFlow<E>

fun processAction(action: A)

fun init(scope: CoroutineScope)

fun destroy()
interface Store {

interface State

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ package st.slex.csplashscreen.feature.collection.di

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import st.slex.csplashscreen.core.ui.base.ViewModelFactory
import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoMap
import st.slex.csplashscreen.core.ui.base.ViewModelFactory
import st.slex.csplashscreen.core.ui.di.ViewModelKey
import st.slex.csplashscreen.feature.collection.data.SingleCollectionRepository
import st.slex.csplashscreen.feature.collection.data.SingleCollectionRepositoryImpl
import st.slex.csplashscreen.feature.collection.domain.SingleCollectionInteractor
import st.slex.csplashscreen.feature.collection.domain.SingleCollectionInteractorImpl
import st.slex.csplashscreen.feature.collection.navigation.SingleCollectionRouter
import st.slex.csplashscreen.feature.collection.navigation.SingleCollectionRouterImpl
import st.slex.csplashscreen.feature.collection.ui.SingleCollectionViewModel
import st.slex.csplashscreen.feature.collection.ui.store.SingleCollectionStore
import st.slex.csplashscreen.feature.collection.ui.store.SingleCollectionStoreImpl
import st.slex.csplashscreen.feature.collection.ui.presenter.SingleCollectionViewModel

@Module
interface SingleCollectionModule {
Expand All @@ -28,10 +26,6 @@ interface SingleCollectionModule {
@SingleCollectionScope
fun bindsInteractor(impl: SingleCollectionInteractorImpl): SingleCollectionInteractor

@Binds
@SingleCollectionScope
fun bindsStore(impl: SingleCollectionStoreImpl): SingleCollectionStore

@Binds
@SingleCollectionScope
fun bindRouter(impl: SingleCollectionRouterImpl): SingleCollectionRouter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import st.slex.csplashscreen.core.ui.base.setupComponent
import st.slex.csplashscreen.core.ui.utils.CollectAsEvent
import st.slex.csplashscreen.feature.collection.di.SingleCollectionBuilder
import st.slex.csplashscreen.feature.collection.ui.CollectionScreen
import st.slex.csplashscreen.feature.collection.ui.SingleCollectionViewModel
import st.slex.csplashscreen.feature.collection.ui.store.SingleCollectionStore.Action
import st.slex.csplashscreen.feature.collection.ui.presenter.SingleCollectionStore.Action
import st.slex.csplashscreen.feature.collection.ui.presenter.SingleCollectionViewModel

fun NavGraphBuilder.singleCollectionGraph(
modifier: Modifier = Modifier,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package st.slex.csplashscreen.feature.collection.navigation

import st.slex.csplashscreen.core.ui.mvi.Router
import st.slex.csplashscreen.feature.collection.ui.store.SingleCollectionStore.Navigation
import st.slex.csplashscreen.feature.collection.ui.presenter.SingleCollectionStore.Navigation

interface SingleCollectionRouter : Router<Navigation>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package st.slex.csplashscreen.feature.collection.navigation

import st.slex.csplashscreen.core.navigation.NavigationScreen
import st.slex.csplashscreen.core.ui.di.Navigator
import st.slex.csplashscreen.feature.collection.ui.store.SingleCollectionStore.Navigation
import st.slex.csplashscreen.feature.collection.ui.presenter.SingleCollectionStore.Navigation
import javax.inject.Inject

class SingleCollectionRouterImpl @Inject constructor(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package st.slex.csplashscreen.feature.collection.ui.store
package st.slex.csplashscreen.feature.collection.ui.presenter

import androidx.compose.runtime.Stable
import androidx.paging.PagingData
import kotlinx.coroutines.flow.StateFlow
import st.slex.csplashscreen.core.photos.ui.model.PhotoModel
import st.slex.csplashscreen.core.ui.mvi.Store
import st.slex.csplashscreen.feature.collection.ui.store.SingleCollectionStore.Action
import st.slex.csplashscreen.feature.collection.ui.store.SingleCollectionStore.Event
import st.slex.csplashscreen.feature.collection.ui.store.SingleCollectionStore.State

interface SingleCollectionStore : Store<State, Event, Action> {
interface SingleCollectionStore {

@Stable
data class State(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package st.slex.csplashscreen.feature.collection.ui.store
package st.slex.csplashscreen.feature.collection.ui.presenter

import androidx.paging.Pager
import androidx.paging.PagingConfig
Expand All @@ -14,31 +14,30 @@ import st.slex.csplashscreen.core.core.coroutine.AppDispatcher
import st.slex.csplashscreen.core.core.coroutine.CoroutineExt.mapState
import st.slex.csplashscreen.core.photos.ui.model.PhotoModel
import st.slex.csplashscreen.core.photos.ui.model.toPresentation
import st.slex.csplashscreen.core.ui.mvi.BaseStore
import st.slex.csplashscreen.core.ui.mvi.BaseViewModel
import st.slex.csplashscreen.core.ui.paging.PagingSource
import st.slex.csplashscreen.feature.collection.domain.SingleCollectionInteractor
import st.slex.csplashscreen.feature.collection.navigation.SingleCollectionRouter
import st.slex.csplashscreen.feature.collection.ui.store.SingleCollectionStore.Action
import st.slex.csplashscreen.feature.collection.ui.store.SingleCollectionStore.Event
import st.slex.csplashscreen.feature.collection.ui.store.SingleCollectionStore.Navigation
import st.slex.csplashscreen.feature.collection.ui.store.SingleCollectionStore.State
import st.slex.csplashscreen.feature.collection.ui.presenter.SingleCollectionStore.Action
import st.slex.csplashscreen.feature.collection.ui.presenter.SingleCollectionStore.Event
import st.slex.csplashscreen.feature.collection.ui.presenter.SingleCollectionStore.Navigation
import st.slex.csplashscreen.feature.collection.ui.presenter.SingleCollectionStore.State
import javax.inject.Inject

class SingleCollectionStoreImpl @Inject constructor(
class SingleCollectionViewModel @Inject constructor(
private val interactor: SingleCollectionInteractor,
appDispatcher: AppDispatcher,
router: SingleCollectionRouter
) : SingleCollectionStore, BaseStore<State, Event, Action, Navigation>(router, appDispatcher) {
) : BaseViewModel<State, Event, Action, Navigation>(router, appDispatcher) {

override val initialState: State
get() = State(
photos = ::allPhotos,
collectionId = ""
)
override val initialState: State = State(
photos = ::allPhotos,
collectionId = ""
)

override val state: MutableStateFlow<State> = MutableStateFlow(initialState)
override val _state: MutableStateFlow<State> = MutableStateFlow(initialState)

override fun processAction(action: Action) {
override fun sendAction(action: Action) {
when (action) {
is Action.Init -> actionInit(action)
is Action.OnProfileClick -> actionProfileClick(action)
Expand Down
Loading

0 comments on commit c9eed61

Please sign in to comment.