feat(connector): link projects to database

This commit is contained in:
Anthony
2024-04-04 18:27:32 +02:00
parent 0215ba1ef4
commit 4e040516db
6 changed files with 76 additions and 47 deletions

View File

@@ -10,6 +10,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.currentOrThrow
@@ -17,6 +18,7 @@ class CreateProject : Screen {
@Composable
override fun Content() {
val navigator = LocalNavigator.currentOrThrow
val screenModel = getScreenModel<ProjectsScreenModel>()
var projectName by remember { mutableStateOf("") }
var aircraftType by remember { mutableStateOf("") }
@@ -61,8 +63,13 @@ class CreateProject : Screen {
Button(
contentPadding = ButtonDefaults.ButtonWithIconContentPadding,
onClick = {
// TODO save new project and make sure it redirects to list page
navigator.pop()
if (projectName.isNotBlank() and aircraftType.isNotBlank()) {
screenModel.createProject(
projectName = projectName,
aircraftType = aircraftType
)
navigator.pop()
}
}
) {
Icon(Icons.Outlined.Add, "Create Project", modifier = Modifier.size(18.dp))

View File

@@ -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,9 @@ 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.Project
class ListProjects : Screen {
class ListProjects(private val projects: List<Project>) : Screen {
@Composable
override fun Content() {
val navigator = LocalNavigator.currentOrThrow
@@ -45,20 +47,8 @@ class ListProjects : Screen {
modifier = Modifier.fillMaxWidth(0.7F),
) {
LazyColumn(state = state) {
items(50) { index ->
ListItem(
modifier = Modifier
.clickable(
enabled = true,
onClick = {
// TODO add loading project
}
),
overlineContent = { Text("Boeing 737-800") },
headlineContent = { Text("Project $index") },
trailingContent = { Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, "Open Project") }
)
HorizontalDivider()
items(projects) { project ->
projectItem(project)
}
}
VerticalScrollbar(
@@ -73,4 +63,22 @@ class ListProjects : Screen {
}
}
}
@Composable
private fun projectItem(project: Project) {
ListItem(
modifier = Modifier
.clickable(
enabled = true,
onClick = {
// TODO add loading project
}
),
overlineContent = { Text(project.aircraftType) },
headlineContent = { Text(project.name) },
trailingContent = { Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, "Open Project") }
)
HorizontalDivider()
}
}

View File

@@ -1,9 +1,6 @@
package tab.project
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.*
import cafe.adriel.voyager.koin.getScreenModel
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.navigator.tab.Tab
@@ -36,11 +33,14 @@ class Projects : Tab {
val screenModel = getScreenModel<ProjectsScreenModel>()
val state by screenModel.state.collectAsState()
when (state) {
is ProjectsScreenModel.State.UnInit -> screenModel.projectExists()
is ProjectsScreenModel.State.Init -> Navigator(NoProjects())
is ProjectsScreenModel.State.Loading -> Navigator(LoadingScreen("Projects"))
is ProjectsScreenModel.State.Result -> Navigator(ListProjects())
when (val s = state) {
is ProjectState.Init -> Navigator(NoProjects())
is ProjectState.Loading -> Navigator(LoadingScreen("Projects"))
is ProjectState.Result -> Navigator(ListProjects(s.projects))
}
LaunchedEffect(currentCompositeKeyHash) {
screenModel.projectExists()
}
}
}

View File

@@ -8,33 +8,44 @@ import kotlinx.coroutines.launch
class ProjectsScreenModel (
private val db: ProjectTransaction
) : StateScreenModel<ProjectsScreenModel.State>(State.UnInit) {
sealed class State {
object UnInit : State()
object Init : State()
object Loading : State()
data class Result(val projects: List<Project>) : State()
}
) : StateScreenModel<ProjectState>(ProjectState.Loading) {
fun projectExists() {
screenModelScope.launch {
mutableState.value = State.Loading
val exists = db.projectExists()
if (exists) {
getProjects()
val projects = getProjects()
mutableState.value = ProjectState.Result(projects = projects)
} else {
mutableState.value = State.Init
mutableState.value = ProjectState.Init
}
}
}
fun getProjects() {
mutableState.value = State.Loading
val projects = db.getProjects()
private fun getProjects(): List<Project> {
val projects = db.getProjects()
println(projects)
mutableState.value = State.Result(projects)
return projects
}
fun createProject(projectName : String, aircraftType : String) {
screenModelScope.launch {
mutableState.value = ProjectState.Loading
// Add project to database
db.createProject(projectName = projectName, aircraftType = aircraftType)
// Load new projects
val projects = getProjects()
mutableState.value = ProjectState.Result(projects = projects)
}
}
}
sealed class ProjectState {
data object Init : ProjectState()
data object Loading : ProjectState()
data class Result(val projects: List<Project>) : ProjectState()
}