feat(connector): add ActionsScreenModel to get actions from database

This commit is contained in:
Anthony Berg
2024-04-26 18:15:09 +01:00
parent 8d8c140b35
commit 07b3cfdb26
4 changed files with 77 additions and 9 deletions

View File

@@ -1,10 +1,12 @@
package di package di
import InterfaceState import InterfaceState
import io.anthonyberg.connector.shared.ActionTransaction
import io.anthonyberg.connector.shared.ProcedureTransaction import io.anthonyberg.connector.shared.ProcedureTransaction
import io.anthonyberg.connector.shared.ProjectTransaction import io.anthonyberg.connector.shared.ProjectTransaction
import io.anthonyberg.connector.shared.database.DriverFactory import io.anthonyberg.connector.shared.database.DriverFactory
import org.koin.dsl.module import org.koin.dsl.module
import tab.procedure.ActionsScreenModel
import tab.procedure.ProcedureScreenModel import tab.procedure.ProcedureScreenModel
import tab.project.ProjectsScreenModel import tab.project.ProjectsScreenModel
@@ -20,6 +22,10 @@ fun commonModule() = module {
single<ProcedureTransaction> { single<ProcedureTransaction> {
ProcedureTransaction(driverFactory = get<DriverFactory>()) ProcedureTransaction(driverFactory = get<DriverFactory>())
} }
single<ActionTransaction> {
ActionTransaction(driverFactory = get<DriverFactory>())
}
} }
fun viewModelModule() = module { fun viewModelModule() = module {
@@ -34,4 +40,8 @@ fun viewModelModule() = module {
single<ProcedureScreenModel> { single<ProcedureScreenModel> {
ProcedureScreenModel(db = get(), interfaceState = get()) ProcedureScreenModel(db = get(), interfaceState = get())
} }
single<ActionsScreenModel> {
ActionsScreenModel(db = get(), interfaceState = get())
}
} }

View File

@@ -17,6 +17,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import cafe.adriel.voyager.core.screen.Screen import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.koin.getScreenModel
import cafe.adriel.voyager.navigator.LocalNavigator import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.Navigator import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.navigator.currentOrThrow import cafe.adriel.voyager.navigator.currentOrThrow
@@ -27,18 +28,26 @@ import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.painterResource import org.jetbrains.compose.resources.painterResource
import org.koin.compose.koinInject import org.koin.compose.koinInject
class Actions : Screen { class Actions (dbActions: List<Action>) : Screen {
private val columnPadding = 24.dp private val columnPadding = 24.dp
private val itemPadding = 24.dp private val itemPadding = 24.dp
private var inputs = mutableStateListOf<Action>() private var inputs = mutableStateListOf<Action>()
init {
inputs.addAll(dbActions)
}
@Composable @Composable
override fun Content() { override fun Content() {
val navigator = LocalNavigator.currentOrThrow val navigator = LocalNavigator.currentOrThrow
val viewModel = koinInject<InterfaceState>() val viewModel = koinInject<InterfaceState>()
val state = rememberLazyListState(0) val state = rememberLazyListState(0)
// Sends to screen model that Actions has been loaded
val screenModel = getScreenModel<ActionsScreenModel>()
screenModel.loadedActions()
// Checks if a project has been selected before viewing contents // Checks if a project has been selected before viewing contents
if (viewModel.procedureId == null) { if (viewModel.procedureId == null) {
navigator.pop() navigator.pop()

View File

@@ -0,0 +1,35 @@
package tab.procedure
import InterfaceState
import cafe.adriel.voyager.core.model.StateScreenModel
import cafe.adriel.voyager.core.model.screenModelScope
import io.anthonyberg.connector.shared.ActionTransaction
import io.anthonyberg.connector.shared.entity.Action
import kotlinx.coroutines.launch
class ActionsScreenModel (
private val db: ActionTransaction,
private val interfaceState: InterfaceState
) : StateScreenModel<ActionsState>(ActionsState.Idle) {
fun getActions() {
screenModelScope.launch {
mutableState.value = ActionsState.Loading
val procedureId = interfaceState.procedureId ?: return@launch
val actions = db.getActions(procedureId = procedureId)
mutableState.value = ActionsState.Result(actions = actions)
}
}
fun loadedActions() {
mutableState.value = ActionsState.Idle
}
}
sealed class ActionsState {
data object Idle : ActionsState()
data object Loading : ActionsState()
data class Result(val actions: List<Action>) : ActionsState()
}

View File

@@ -16,14 +16,17 @@ import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight
import androidx.compose.material.icons.outlined.Add import androidx.compose.material.icons.outlined.Add
import androidx.compose.material3.* import androidx.compose.material3.*
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import cafe.adriel.voyager.core.screen.Screen import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.koin.getScreenModel
import cafe.adriel.voyager.navigator.LocalNavigator import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.navigator.currentOrThrow import cafe.adriel.voyager.navigator.currentOrThrow
import io.anthonyberg.connector.shared.entity.Procedure import io.anthonyberg.connector.shared.entity.Procedure
import org.koin.compose.koinInject import org.koin.compose.koinInject
import tab.LoadingScreen
class ListProcedures ( class ListProcedures (
private val procedures: List<Procedure> private val procedures: List<Procedure>
@@ -32,8 +35,20 @@ class ListProcedures (
override fun Content() { override fun Content() {
val navigator = LocalNavigator.currentOrThrow val navigator = LocalNavigator.currentOrThrow
val viewModel = koinInject<InterfaceState>() val viewModel = koinInject<InterfaceState>()
val screenModel = getScreenModel<ActionsScreenModel>()
val state by screenModel.state.collectAsState()
val state = rememberLazyListState(0) when (val s = state) {
is ActionsState.Idle -> { }
is ActionsState.Loading -> navigator.push(LoadingScreen("Actions"))
is ActionsState.Result -> {
navigator.pop()
navigator.push(Actions(s.actions))
}
}
val lazyState = rememberLazyListState(0)
Scaffold ( Scaffold (
floatingActionButton = { floatingActionButton = {
@@ -53,9 +68,9 @@ class ListProcedures (
Box( Box(
modifier = Modifier.fillMaxWidth(0.7F), modifier = Modifier.fillMaxWidth(0.7F),
) { ) {
LazyColumn(state = state) { LazyColumn(state = lazyState) {
items(procedures) { procedure -> items(procedures) { procedure ->
procedureItem(procedure, viewModel, navigator) procedureItem(procedure, viewModel, screenModel)
} }
} }
VerticalScrollbar( VerticalScrollbar(
@@ -63,7 +78,7 @@ class ListProcedures (
.align(Alignment.CenterEnd) .align(Alignment.CenterEnd)
.fillMaxHeight(), .fillMaxHeight(),
adapter = rememberScrollbarAdapter( adapter = rememberScrollbarAdapter(
scrollState = state, scrollState = lazyState,
), ),
) )
} }
@@ -72,15 +87,14 @@ class ListProcedures (
} }
@Composable @Composable
private fun procedureItem(procedure: Procedure, viewModel: InterfaceState, navigator: Navigator) { private fun procedureItem(procedure: Procedure, viewModel: InterfaceState, screenModel: ActionsScreenModel) {
ListItem( ListItem(
modifier = Modifier modifier = Modifier
.clickable( .clickable(
enabled = true, enabled = true,
onClick = { onClick = {
viewModel.procedureId = procedure.id viewModel.procedureId = procedure.id
screenModel.getActions()
navigator.push(Actions())
} }
), ),
overlineContent = { Text(procedure.type) }, overlineContent = { Text(procedure.type) },