mirror of
https://github.com/smyalygames/checklist-tester.git
synced 2025-05-18 14:34:12 +02:00
feat(connector): create ScreenModel for Procedures
This commit is contained in:
parent
43a4be4852
commit
371ccf8511
@ -1,8 +1,10 @@
|
||||
package di
|
||||
|
||||
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.ProcedureScreenModel
|
||||
import tab.project.ProjectsScreenModel
|
||||
|
||||
fun commonModule() = module {
|
||||
@ -13,10 +15,18 @@ fun commonModule() = module {
|
||||
single<ProjectTransaction> {
|
||||
ProjectTransaction(driverFactory = get<DriverFactory>())
|
||||
}
|
||||
|
||||
single<ProcedureTransaction> {
|
||||
ProcedureTransaction(driverFactory = get<DriverFactory>())
|
||||
}
|
||||
}
|
||||
|
||||
fun viewModelModule() = module {
|
||||
single<ProjectsScreenModel> {
|
||||
ProjectsScreenModel(db = get())
|
||||
}
|
||||
|
||||
single<ProcedureScreenModel> {
|
||||
ProcedureScreenModel(db = get())
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxHeight
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.foundation.rememberScrollbarAdapter
|
||||
import androidx.compose.material.icons.Icons
|
||||
@ -19,8 +20,11 @@ import androidx.compose.ui.Modifier
|
||||
import cafe.adriel.voyager.core.screen.Screen
|
||||
import cafe.adriel.voyager.navigator.LocalNavigator
|
||||
import cafe.adriel.voyager.navigator.currentOrThrow
|
||||
import io.anthonyberg.connector.shared.entity.Procedure
|
||||
|
||||
class ListProcedures : Screen {
|
||||
class ListProcedures (
|
||||
private val procedures: List<Procedure>
|
||||
) : Screen {
|
||||
@Composable
|
||||
override fun Content() {
|
||||
val navigator = LocalNavigator.currentOrThrow
|
||||
@ -45,20 +49,8 @@ class ListProcedures : Screen {
|
||||
modifier = Modifier.fillMaxWidth(0.7F),
|
||||
) {
|
||||
LazyColumn(state = state) {
|
||||
items(50) { index ->
|
||||
ListItem(
|
||||
modifier = Modifier
|
||||
.clickable(
|
||||
enabled = true,
|
||||
onClick = {
|
||||
// TODO add procedure editor
|
||||
}
|
||||
),
|
||||
overlineContent = { Text("Emergency") },
|
||||
headlineContent = { Text("Procedure $index") },
|
||||
trailingContent = { Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, "Open Procedure") }
|
||||
)
|
||||
HorizontalDivider()
|
||||
items(procedures) { procedure ->
|
||||
procedureItem(procedure)
|
||||
}
|
||||
}
|
||||
VerticalScrollbar(
|
||||
@ -73,4 +65,21 @@ class ListProcedures : Screen {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun procedureItem(procedure: Procedure) {
|
||||
ListItem(
|
||||
modifier = Modifier
|
||||
.clickable(
|
||||
enabled = true,
|
||||
onClick = {
|
||||
// TODO add procedure editor
|
||||
}
|
||||
),
|
||||
overlineContent = { Text(procedure.type) },
|
||||
headlineContent = { Text(procedure.name) },
|
||||
trailingContent = { Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, "Open Procedure") }
|
||||
)
|
||||
HorizontalDivider()
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ import cafe.adriel.voyager.core.screen.Screen
|
||||
import cafe.adriel.voyager.navigator.LocalNavigator
|
||||
import cafe.adriel.voyager.navigator.currentOrThrow
|
||||
|
||||
object NoProcedures : Screen {
|
||||
class NoProcedures : Screen {
|
||||
@Composable
|
||||
override fun Content() {
|
||||
val navigator = LocalNavigator.currentOrThrow
|
||||
|
@ -0,0 +1,37 @@
|
||||
package tab.procedure
|
||||
|
||||
import cafe.adriel.voyager.core.model.StateScreenModel
|
||||
import cafe.adriel.voyager.core.model.screenModelScope
|
||||
import io.anthonyberg.connector.shared.ProcedureTransaction
|
||||
import io.anthonyberg.connector.shared.entity.Procedure
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class ProcedureScreenModel (
|
||||
private val db: ProcedureTransaction
|
||||
) : StateScreenModel<ProcedureState>(ProcedureState.Loading) {
|
||||
|
||||
fun procedureExists() {
|
||||
screenModelScope.launch {
|
||||
val exists = db.proceduresExist()
|
||||
|
||||
if (exists) {
|
||||
val procedures = getProcedures()
|
||||
mutableState.value = ProcedureState.Result(procedures = procedures)
|
||||
} else {
|
||||
mutableState.value = ProcedureState.Init
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getProcedures(): List<Procedure> {
|
||||
val procedures = db.getProcedures()
|
||||
|
||||
return procedures
|
||||
}
|
||||
}
|
||||
|
||||
sealed class ProcedureState {
|
||||
data object Init : ProcedureState()
|
||||
data object Loading : ProcedureState()
|
||||
data class Result(val procedures: List<Procedure>) : ProcedureState()
|
||||
}
|
@ -29,7 +29,7 @@ class Procedures : Tab {
|
||||
|
||||
@Composable
|
||||
override fun Content() {
|
||||
Navigator(ListProcedures()) {
|
||||
Navigator(ProceduresContent()) {
|
||||
CurrentScreen()
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,24 @@
|
||||
package tab.procedure
|
||||
|
||||
import androidx.compose.runtime.*
|
||||
import cafe.adriel.voyager.core.screen.Screen
|
||||
import cafe.adriel.voyager.koin.getScreenModel
|
||||
import tab.LoadingScreen
|
||||
|
||||
class ProceduresContent : Screen {
|
||||
@Composable
|
||||
override fun Content() {
|
||||
val screenModel = getScreenModel<ProcedureScreenModel>()
|
||||
val state by screenModel.state.collectAsState()
|
||||
|
||||
when (val s = state) {
|
||||
is ProcedureState.Loading -> LoadingScreen("Procedures").Content()
|
||||
is ProcedureState.Init -> NoProcedures().Content()
|
||||
is ProcedureState.Result -> ListProcedures(s.procedures).Content()
|
||||
}
|
||||
|
||||
LaunchedEffect(currentCompositeKeyHash) {
|
||||
screenModel.procedureExists()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user