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

refactor nav abstraction #61

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package st.slex.csplashscreen.di.main

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.stslex93.notes.core.ui.base.ViewModelFactory
import st.slex.csplashscreen.core.ui.base.ViewModelFactory
import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import st.slex.csplashscreen.core.ui.mvi.Router
import st.slex.csplashscreen.core.ui.mvi.Store
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

open class BaseViewModel<out S : State, out E : Event, in A : Action>(
private val store: Store<S, E, A>
open class BaseViewModel<out S : State, out E : Event, in A : Action, in N : Event.Navigation>(
private val store: Store<S, E, A>,
private val router: Router<N>
) : ViewModel() {

val state: StateFlow<S> = store.state
Expand All @@ -24,6 +26,10 @@ open class BaseViewModel<out S : State, out E : Event, in A : Action>(
store.processAction(action)
}

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

override fun onCleared() {
super.onCleared()
store.destroy()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.stslex93.notes.core.ui.base
package st.slex.csplashscreen.core.ui.base

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package st.slex.csplashscreen.core.ui.mvi

fun interface Router<in E : Store.Event.Navigation> {
operator fun invoke(event: E)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ interface Store<out S : State, out E : Event, in A : Action> {
fun destroy()

interface State
interface Event

interface Event {
interface Navigation : Event
}

interface Action
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package st.slex.csplashscreen.feature.collection.di

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.stslex93.notes.core.ui.base.ViewModelFactory
import st.slex.csplashscreen.core.ui.base.ViewModelFactory
import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fun NavGraphBuilder.singleCollectionGraph(

viewModel.event.CollectAsEvent { event ->
when (event) {
is Event.Navigation -> viewModel.processNavigation(event)
is Event.Navigation -> viewModel.navigate(event)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package st.slex.csplashscreen.feature.collection.navigation

interface SingleCollectionRouter {
import st.slex.csplashscreen.core.ui.mvi.Router
import st.slex.csplashscreen.feature.collection.ui.store.SingleCollectionStore.Event.Navigation

fun navToImage(uuid: String)

fun navToProfile(username: String)
}
interface SingleCollectionRouter : Router<Navigation>
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@ 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.Event.Navigation
import javax.inject.Inject

class SingleCollectionRouterImpl @Inject constructor(
private val navigator: Navigator
) : SingleCollectionRouter {

override fun navToImage(uuid: String) {
navigator(NavigationScreen.ImageDetailScreen(uuid))
override fun invoke(event: Navigation) {
when (event) {
is Navigation.ImageDetail -> navToImage(event)
is Navigation.Profile -> navToProfile(event)
}
}

override fun navToProfile(username: String) {
navigator(NavigationScreen.UserScreen(username))
private fun navToImage(event: Navigation.ImageDetail) {
navigator(NavigationScreen.ImageDetailScreen(event.uuid))
}

private fun navToProfile(event: Navigation.Profile) {
navigator(NavigationScreen.UserScreen(event.username))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,5 @@ import javax.inject.Inject

class SingleCollectionViewModel @Inject constructor(
store: SingleCollectionStore,
private val router: SingleCollectionRouter
) : BaseViewModel<State, Event, Action>(store) {

fun processNavigation(event: Navigation) {
when (event) {
is Navigation.ImageDetail -> router.navToImage(event.uuid)
is Navigation.Profile -> router.navToProfile(event.username)
}
}
}
router: SingleCollectionRouter
) : BaseViewModel<State, Event, Action, Navigation>(store, router)
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface SingleCollectionStore : Store<State, Event, Action> {
sealed interface Event : Store.Event {

@Stable
sealed interface Navigation : Event {
sealed interface Navigation : Event, Store.Event.Navigation {

@Stable
data class Profile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package st.slex.csplashscreen.feature.favourite.di

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.stslex93.notes.core.ui.base.ViewModelFactory
import st.slex.csplashscreen.core.ui.base.ViewModelFactory
import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fun NavGraphBuilder.favouriteGraph(

viewModel.event.CollectAsEvent { event ->
when (event) {
is Event.Navigation -> viewModel.processNavigation(event)
is Event.Navigation -> viewModel.navigate(event)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package st.slex.csplashscreen.feature.favourite.navigation

interface FavouriteRouter {

fun navToUser(username: String)

fun navToImage(uuid: String)

fun navHome()
}
import st.slex.csplashscreen.core.ui.mvi.Router
import st.slex.csplashscreen.feature.favourite.ui.store.FavouriteStore

interface FavouriteRouter : Router<FavouriteStore.Event.Navigation>
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@ package st.slex.csplashscreen.feature.favourite.navigation

import st.slex.csplashscreen.core.navigation.NavigationScreen
import st.slex.csplashscreen.core.ui.di.Navigator
import st.slex.csplashscreen.feature.favourite.ui.store.FavouriteStore
import javax.inject.Inject

class FavouriteRouterImpl @Inject constructor(
private val navigator: Navigator
) : FavouriteRouter {

override fun navToUser(username: String) {
navigator(NavigationScreen.UserScreen(username))
override fun invoke(event: FavouriteStore.Event.Navigation) {
when (event) {
FavouriteStore.Event.Navigation.Home -> navHome()
is FavouriteStore.Event.Navigation.Image -> navToImage(event)
is FavouriteStore.Event.Navigation.User -> navToUser(event)
}
}

override fun navToImage(uuid: String) {
navigator(NavigationScreen.ImageDetailScreen(uuid))
private fun navToUser(event: FavouriteStore.Event.Navigation.User) {
navigator(NavigationScreen.UserScreen(event.username))
}

override fun navHome() {
private fun navToImage(event: FavouriteStore.Event.Navigation.Image) {
navigator(NavigationScreen.ImageDetailScreen(event.uuid))
}

private fun navHome() {
navigator(NavigationScreen.Home)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@ import st.slex.csplashscreen.feature.favourite.navigation.FavouriteRouter
import st.slex.csplashscreen.feature.favourite.ui.store.FavouriteStore
import st.slex.csplashscreen.feature.favourite.ui.store.FavouriteStore.Action
import st.slex.csplashscreen.feature.favourite.ui.store.FavouriteStore.Event
import st.slex.csplashscreen.feature.favourite.ui.store.FavouriteStore.Event.Navigation
import st.slex.csplashscreen.feature.favourite.ui.store.FavouriteStore.State
import javax.inject.Inject

class FavouriteViewModel @Inject constructor(
private val router: FavouriteRouter,
router: FavouriteRouter,
store: FavouriteStore
) : BaseViewModel<State, Event, Action>(store) {

fun processNavigation(event: Event.Navigation) {
when (event) {
Event.Navigation.Home -> router.navHome()
is Event.Navigation.Image -> router.navToImage(event.uuid)
is Event.Navigation.User -> router.navToUser(event.username)
}
}
}
) : BaseViewModel<State, Event, Action, Navigation>(store, router)
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface FavouriteStore : Store<State, Event, Action> {
sealed interface Event : Store.Event {

@Stable
sealed interface Navigation : Event {
sealed interface Navigation : Event, Store.Event.Navigation {

@Stable
data class User(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package st.slex.csplashscreen.feature.home.di

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.stslex93.notes.core.ui.base.ViewModelFactory
import st.slex.csplashscreen.core.ui.base.ViewModelFactory
import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fun NavGraphBuilder.homeGraph(

viewModel.event.CollectAsEvent { event ->
when (event) {
is HomeStore.Event.Navigation -> viewModel.processNavigation(event)
is HomeStore.Event.Navigation -> viewModel.navigate(event)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package st.slex.csplashscreen.feature.home.navigation

interface HomeRouter {

fun navToProfile(username: String)

fun navToImage(uuid: String)

fun navToCollection(uuid: String)
}
import st.slex.csplashscreen.core.ui.mvi.Router
import st.slex.csplashscreen.feature.home.ui.store.HomeStore

interface HomeRouter : Router<HomeStore.Event.Navigation>
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@ package st.slex.csplashscreen.feature.home.navigation

import st.slex.csplashscreen.core.navigation.NavigationScreen
import st.slex.csplashscreen.core.ui.di.Navigator
import st.slex.csplashscreen.feature.home.ui.store.HomeStore.Event.Navigation
import javax.inject.Inject

class HomeRouterImpl @Inject constructor(
private val navigator: Navigator
) : HomeRouter {

override fun navToProfile(username: String) {
navigator(NavigationScreen.UserScreen(username))
override fun invoke(event: Navigation) {
when (event) {
is Navigation.Collection -> navToCollection(event)
is Navigation.Image -> navToImage(event)
is Navigation.User -> navToProfile(event)
}
}

override fun navToImage(uuid: String) {
navigator(NavigationScreen.ImageDetailScreen(uuid))
private fun navToProfile(event: Navigation.User) {
navigator(NavigationScreen.UserScreen(event.username))
}

override fun navToCollection(uuid: String) {
navigator(NavigationScreen.CollectionScreen(uuid))
private fun navToImage(event: Navigation.Image) {
navigator(NavigationScreen.ImageDetailScreen(event.uuid))
}

private fun navToCollection(event: Navigation.Collection) {
navigator(NavigationScreen.CollectionScreen(event.uuid))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@ import st.slex.csplashscreen.feature.home.navigation.HomeRouter
import st.slex.csplashscreen.feature.home.ui.store.HomeStore
import st.slex.csplashscreen.feature.home.ui.store.HomeStore.Action
import st.slex.csplashscreen.feature.home.ui.store.HomeStore.Event
import st.slex.csplashscreen.feature.home.ui.store.HomeStore.Event.Navigation
import st.slex.csplashscreen.feature.home.ui.store.HomeStore.State
import javax.inject.Inject

class HomeViewModel @Inject constructor(
private val router: HomeRouter,
router: HomeRouter,
store: HomeStore
) : BaseViewModel<State, Event, Action>(store) {

fun processNavigation(event: Event.Navigation) {
when (event) {
is Event.Navigation.Collection -> router.navToCollection(event.uuid)
is Event.Navigation.Image -> router.navToImage(event.uuid)
is Event.Navigation.User -> router.navToProfile(event.username)
}
}
}
) : BaseViewModel<State, Event, Action, Navigation>(store, router)
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface HomeStore : Store<State, Event, Action> {
sealed interface Event : Store.Event {

@Stable
sealed interface Navigation : Event {
sealed interface Navigation : Event, Store.Event.Navigation {

@Stable
data class User(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package st.slex.csplashscreen.feature.feature_photo_detail.di

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.stslex93.notes.core.ui.base.ViewModelFactory
import st.slex.csplashscreen.core.ui.base.ViewModelFactory
import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fun NavGraphBuilder.imageDetailGraph(

viewModel.event.CollectAsEvent { event ->
when (event) {
is ImageDetailStore.Event.Navigation -> viewModel.processNavigation(event)
is ImageDetailStore.Event.Navigation -> viewModel.navigate(event)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package st.slex.csplashscreen.feature.feature_photo_detail.navigation

interface ImageDetailRouter {

fun navToSearch(tag: String)

fun navToProfile(username: String)
}
import st.slex.csplashscreen.core.ui.mvi.Router
import st.slex.csplashscreen.feature.feature_photo_detail.ui.store.ImageDetailStore.Event.Navigation

interface ImageDetailRouter : Router<Navigation>
Loading
Loading