mirror of
https://github.com/smyalygames/checklist-tester.git
synced 2025-05-18 06:24:12 +02:00
feat(connector): add ActionsScreenModel to get actions from database
This commit is contained in:
parent
8d8c140b35
commit
07b3cfdb26
@ -1,10 +1,12 @@
|
||||
package di
|
||||
|
||||
import InterfaceState
|
||||
import io.anthonyberg.connector.shared.ActionTransaction
|
||||
import io.anthonyberg.connector.shared.ProcedureTransaction
|
||||
import io.anthonyberg.connector.shared.ProjectTransaction
|
||||
import io.anthonyberg.connector.shared.database.DriverFactory
|
||||
import org.koin.dsl.module
|
||||
import tab.procedure.ActionsScreenModel
|
||||
import tab.procedure.ProcedureScreenModel
|
||||
import tab.project.ProjectsScreenModel
|
||||
|
||||
@ -20,6 +22,10 @@ fun commonModule() = module {
|
||||
single<ProcedureTransaction> {
|
||||
ProcedureTransaction(driverFactory = get<DriverFactory>())
|
||||
}
|
||||
|
||||
single<ActionTransaction> {
|
||||
ActionTransaction(driverFactory = get<DriverFactory>())
|
||||
}
|
||||
}
|
||||
|
||||
fun viewModelModule() = module {
|
||||
@ -34,4 +40,8 @@ fun viewModelModule() = module {
|
||||
single<ProcedureScreenModel> {
|
||||
ProcedureScreenModel(db = get(), interfaceState = get())
|
||||
}
|
||||
|
||||
single<ActionsScreenModel> {
|
||||
ActionsScreenModel(db = get(), interfaceState = get())
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cafe.adriel.voyager.core.screen.Screen
|
||||
import cafe.adriel.voyager.koin.getScreenModel
|
||||
import cafe.adriel.voyager.navigator.LocalNavigator
|
||||
import cafe.adriel.voyager.navigator.Navigator
|
||||
import cafe.adriel.voyager.navigator.currentOrThrow
|
||||
@ -27,18 +28,26 @@ import org.jetbrains.compose.resources.ExperimentalResourceApi
|
||||
import org.jetbrains.compose.resources.painterResource
|
||||
import org.koin.compose.koinInject
|
||||
|
||||
class Actions : Screen {
|
||||
class Actions (dbActions: List<Action>) : Screen {
|
||||
private val columnPadding = 24.dp
|
||||
private val itemPadding = 24.dp
|
||||
|
||||
private var inputs = mutableStateListOf<Action>()
|
||||
|
||||
init {
|
||||
inputs.addAll(dbActions)
|
||||
}
|
||||
|
||||
@Composable
|
||||
override fun Content() {
|
||||
val navigator = LocalNavigator.currentOrThrow
|
||||
val viewModel = koinInject<InterfaceState>()
|
||||
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
|
||||
if (viewModel.procedureId == null) {
|
||||
navigator.pop()
|
||||
|
@ -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()
|
||||
}
|
@ -16,14 +16,17 @@ import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight
|
||||
import androidx.compose.material.icons.outlined.Add
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import cafe.adriel.voyager.core.screen.Screen
|
||||
import cafe.adriel.voyager.koin.getScreenModel
|
||||
import cafe.adriel.voyager.navigator.LocalNavigator
|
||||
import cafe.adriel.voyager.navigator.Navigator
|
||||
import cafe.adriel.voyager.navigator.currentOrThrow
|
||||
import io.anthonyberg.connector.shared.entity.Procedure
|
||||
import org.koin.compose.koinInject
|
||||
import tab.LoadingScreen
|
||||
|
||||
class ListProcedures (
|
||||
private val procedures: List<Procedure>
|
||||
@ -32,8 +35,20 @@ class ListProcedures (
|
||||
override fun Content() {
|
||||
val navigator = LocalNavigator.currentOrThrow
|
||||
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 (
|
||||
floatingActionButton = {
|
||||
@ -53,9 +68,9 @@ class ListProcedures (
|
||||
Box(
|
||||
modifier = Modifier.fillMaxWidth(0.7F),
|
||||
) {
|
||||
LazyColumn(state = state) {
|
||||
LazyColumn(state = lazyState) {
|
||||
items(procedures) { procedure ->
|
||||
procedureItem(procedure, viewModel, navigator)
|
||||
procedureItem(procedure, viewModel, screenModel)
|
||||
}
|
||||
}
|
||||
VerticalScrollbar(
|
||||
@ -63,7 +78,7 @@ class ListProcedures (
|
||||
.align(Alignment.CenterEnd)
|
||||
.fillMaxHeight(),
|
||||
adapter = rememberScrollbarAdapter(
|
||||
scrollState = state,
|
||||
scrollState = lazyState,
|
||||
),
|
||||
)
|
||||
}
|
||||
@ -72,15 +87,14 @@ class ListProcedures (
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun procedureItem(procedure: Procedure, viewModel: InterfaceState, navigator: Navigator) {
|
||||
private fun procedureItem(procedure: Procedure, viewModel: InterfaceState, screenModel: ActionsScreenModel) {
|
||||
ListItem(
|
||||
modifier = Modifier
|
||||
.clickable(
|
||||
enabled = true,
|
||||
onClick = {
|
||||
viewModel.procedureId = procedure.id
|
||||
|
||||
navigator.push(Actions())
|
||||
screenModel.getActions()
|
||||
}
|
||||
),
|
||||
overlineContent = { Text(procedure.type) },
|
||||
|
Loading…
x
Reference in New Issue
Block a user