Skip to content

Commit

Permalink
Re-arch base tab navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Feb 4, 2024
1 parent 416a1b6 commit fa6a9c2
Show file tree
Hide file tree
Showing 18 changed files with 159 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ internal fun App() {
)
) {
CompositionLocalProvider(
// TODO: Move to dependency injection
LocalStringManager provides RealStringManager(),
) {
BottomSheetNavigator(
Expand All @@ -67,8 +68,8 @@ internal fun App() {
modifier = Modifier.fillMaxWidth(),
) {
TabItem(Tab.Home)
TabItem(Tab.Lists)
TabItem(Tab.Calendar)
TabItem(Tab.Todo)
TabItem(Tab.Agenda)
}
},
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.greenmiststudios.common.components.screens

import androidx.compose.runtime.Composable
import com.greenmiststudios.common.components.TopAppBarWithContent
import com.greenmiststudios.common.screens.Screen

public object AgendaScreen : Screen<Unit> {
override val params: Unit = Unit

@Composable
override fun Content() {
AgendaScreen(Unit, {})
}
}

@Suppress("UNUSED_PARAMETER")
@Composable
public fun AgendaScreen(viewModel: Unit, onEvent: (Unit) -> Unit) {
TopAppBarWithContent {
//TODO
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ public object HomeScreen : Screen<Unit> {
}
}

@Suppress("UNUSED_PARAMETER")
@Composable
private fun HomeScreen(viewModel: HomeViewModel, onEvent: (HomeViewEvent) -> Unit) {
TopAppBarWithContent {
Box(modifier = Modifier.fillMaxWidth()) {
Text("Home")
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,28 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.unit.dp
import cafe.adriel.voyager.navigator.LocalNavigator
import com.greenmiststudios.common.components.ActionButton
import com.greenmiststudios.common.components.TopAppBarWithContent
import com.greenmiststudios.common.presenters.ListsPresenter
import com.greenmiststudios.common.presenters.TodoPresenter
import com.greenmiststudios.common.screens.Screen
import com.greenmiststudios.common.screens.bindPresenter
import com.greenmiststudios.common.viewmodels.ListsViewEvent
import com.greenmiststudios.common.viewmodels.ListsViewEvent.AddList
import com.greenmiststudios.common.viewmodels.ListsViewModel
import com.greenmiststudios.common.uiutils.LoggingEventReceiver
import com.greenmiststudios.common.viewmodels.TodoViewEvent
import com.greenmiststudios.common.viewmodels.TodoViewEvent.AddList
import com.greenmiststudios.common.viewmodels.TodoViewModel

public object ListsScreen : Screen<Unit> {
public object TodoScreen : Screen<Unit> {
override val params: Unit = Unit

@Composable
override fun Content() {
val binding = bindPresenter<ListsPresenter, ListsViewModel, ListsViewEvent>()
ListsScreen(listsViewModel = binding.viewModel, onEvent = binding.onEvent)
val binding = bindPresenter<TodoPresenter, TodoViewModel, TodoViewEvent>()
TodoScreen(todoViewModel = binding.viewModel, onEvent = binding.onEvent)
}
}

@Composable
public fun ListsScreen(listsViewModel: ListsViewModel, onEvent: (ListsViewEvent) -> Unit) {
val navigator = LocalNavigator.current!!

public fun TodoScreen(todoViewModel: TodoViewModel, onEvent: (TodoViewEvent) -> Unit) {
TopAppBarWithContent(
modifier = Modifier.fillMaxSize(),
actionButtons = listOf(
Expand All @@ -53,16 +51,24 @@ public fun ListsScreen(listsViewModel: ListsViewModel, onEvent: (ListsViewEvent)
modifier = Modifier,
verticalArrangement = spacedBy(16.dp),
) {
items(listsViewModel.lists.size, key = { listsViewModel.lists[it].id }) {
val list = listsViewModel.lists[it]
item(key = TodoListKeys.ListHeader.key) {
Text(
modifier = Modifier.padding(horizontal = 16.dp),
text = "Lists",
style = MaterialTheme.typography.titleMedium
)
}

items(todoViewModel.lists.size, key = { todoViewModel.lists[it].id }) {
val list = todoViewModel.lists[it]

Card(
modifier = Modifier
.padding(horizontal = 16.dp)
.fillMaxWidth()
.wrapContentHeight()
.clickable(onClickLabel = list.name) {
onEvent(ListsViewEvent.OpenList(list.id))
onEvent(TodoViewEvent.OpenList(list.id))
},
) {
Column(
Expand All @@ -81,4 +87,11 @@ public fun ListsScreen(listsViewModel: ListsViewModel, onEvent: (ListsViewEvent)
}
}
}
}

public enum class TodoListKeys {
ListHeader,
;

public val key: String = this.name
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.greenmiststudios.common.di

import com.greenmiststudios.common.persistance.createDatabase
import com.greenmiststudios.common.presenters.ListsPresenter
import org.koin.core.module.Module
import org.koin.dsl.module

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package com.greenmiststudios.common.di
import com.greenmiststudios.common.di.qualifiers.Threading.IO
import com.greenmiststudios.common.presenters.EditListPresenter
import com.greenmiststudios.common.presenters.HomePresenter
import com.greenmiststudios.common.presenters.ListsPresenter
import com.greenmiststudios.common.presenters.TodoPresenter
import org.koin.core.module.Module
import org.koin.dsl.module

public val presenterModule: Module = module {
factory { EditListPresenter(screen = get(), navigator = get(), database = get(), ioContext = get(IO)) }
factory { ListsPresenter(database = get(), navigator = get(), ioContext = get(IO)) }
factory { TodoPresenter(database = get(), navigator = get(), ioContext = get(IO)) }
factory { HomePresenter() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.greenmiststudios.common.viewmodels.HomeViewModel
import kotlinx.coroutines.flow.Flow

public class HomePresenter : MoleculePresenter<HomeViewModel, HomeViewEvent> {

@Suppress("UNUSED_ANONYMOUS_PARAMETER")
@Composable
override fun models(events: Flow<HomeViewEvent>): HomeViewModel {
LaunchedEffect(Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.greenmiststudios.common.presenters
import androidx.compose.runtime.Composable
import kotlinx.coroutines.flow.Flow

public interface MoleculePresenter<Model, Event> {
public interface MoleculePresenter<Model, Event>: Presenter<Model, Event> {
@Composable
public fun models(events: Flow<Event>): Model
}
}

public interface Presenter<Model, Event>
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ import com.greenmiststudios.common.components.screens.EditListScreen
import com.greenmiststudios.common.data.TidyList
import com.greenmiststudios.common.data.TidyListItem
import com.greenmiststudios.common.navigation.Navigator
import com.greenmiststudios.common.viewmodels.ListsViewEvent
import com.greenmiststudios.common.viewmodels.ListsViewModel
import com.greenmiststudios.common.viewmodels.TodoViewEvent
import com.greenmiststudios.common.viewmodels.TodoViewModel
import com.greenmiststudios.tidy.Database
import com.greenmiststudios.tidy.GetAllListsWithItems
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlin.coroutines.CoroutineContext

public class ListsPresenter(
public class TodoPresenter(
private val navigator: Navigator,
private val database: Database,
private val ioContext: CoroutineContext,
) : MoleculePresenter<ListsViewModel, ListsViewEvent> {
) : MoleculePresenter<TodoViewModel, TodoViewEvent> {
@Composable
override fun models(events: Flow<ListsViewEvent>): ListsViewModel {
override fun models(events: Flow<TodoViewEvent>): TodoViewModel {
LaunchedEffect(Unit) {
events.collect {
when (it) {
ListsViewEvent.AddList -> navigator.goTo(EditListScreen(EditListScreen.Config.CreateList))
is ListsViewEvent.OpenList -> navigator.goTo(EditListScreen(EditListScreen.Config.EditList(it.id)))
TodoViewEvent.AddList -> navigator.goTo(EditListScreen(EditListScreen.Config.CreateList))
is TodoViewEvent.OpenList -> navigator.goTo(EditListScreen(EditListScreen.Config.EditList(it.id)))
}
}
}
Expand All @@ -42,7 +42,7 @@ public class ListsPresenter(
.map { it.asTidyList() }
}.collectAsState(emptyList())

return ListsViewModel(lists)
return TodoViewModel(lists)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public interface StringManager {
public enum class StringKey(public val value: String) {
APP_NAME("Tidy"),
TAB_HOME("Home"),
TAB_CALENDAR("Calendar"),
TAB_LISTS("Lists"),
TAB_AGENDA("Agenda"),
TAB_TODO("Todo"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import cafe.adriel.voyager.navigator.CurrentScreen
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.navigator.tab.Tab
import cafe.adriel.voyager.navigator.tab.TabOptions
import com.greenmiststudios.common.components.screens.CalendarScreen
import com.greenmiststudios.common.components.screens.AgendaScreen
import com.greenmiststudios.common.components.screens.HomeScreen
import com.greenmiststudios.common.components.screens.ListsScreen
import com.greenmiststudios.common.components.screens.TodoScreen
import com.greenmiststudios.common.providers.LocalStringManager
import com.greenmiststudios.common.resources.StringKey
import cafe.adriel.voyager.core.screen.Screen as VoyagerScreen
Expand All @@ -37,23 +37,23 @@ public sealed class Tab(
)
}

public data object Lists : com.greenmiststudios.common.screens.Tab(ListsScreen) {
public data object Todo : com.greenmiststudios.common.screens.Tab(TodoScreen) {
override val options: TabOptions
@Composable
get() = TabOptions(
index = 1u,
icon = rememberVectorPainter(Icons.Outlined.Assignment),
title = LocalStringManager.current[StringKey.TAB_LISTS],
title = LocalStringManager.current[StringKey.TAB_TODO],
)
}

public data object Calendar : com.greenmiststudios.common.screens.Tab(CalendarScreen) {
public data object Agenda : com.greenmiststudios.common.screens.Tab(AgendaScreen) {
override val options: TabOptions
@Composable
get() = TabOptions(
index = 2u,
icon = rememberVectorPainter(Icons.Outlined.CalendarMonth),
title = LocalStringManager.current[StringKey.TAB_CALENDAR],
title = LocalStringManager.current[StringKey.TAB_AGENDA],
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.greenmiststudios.common.uiutils

internal fun <T> LoggingEventReceiver(): (T) -> Unit = {
println("PreviewLoggingEventReceiver: $it")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.greenmiststudios.common.uiutils

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import com.greenmiststudios.common.di.startTidyKoin
import com.greenmiststudios.common.providers.LocalStringManager
import com.greenmiststudios.common.resources.RealStringManager
import org.koin.mp.KoinPlatformTools

@Composable
public fun TidyPreview(content: @Composable () -> Unit) {
if (KoinPlatformTools.defaultContext().getOrNull() == null) {
startTidyKoin()
}

CompositionLocalProvider(
// TODO: Move to dependency injection
LocalStringManager provides RealStringManager(),
) {
content()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.greenmiststudios.common.viewmodels

import com.greenmiststudios.common.data.TidyList

public data class TodoViewModel(
val lists: List<TidyList>,
)

public sealed interface TodoViewEvent {
public data object AddList : TodoViewEvent
public data class OpenList(val id: String) : TodoViewEvent
}
Loading

0 comments on commit fa6a9c2

Please sign in to comment.