Commit 1553dae6 authored by Jan Grigat's avatar Jan Grigat
Browse files

added moebel support

stores can now have moebel inserted
parent dfaf0d4a
package de.kompo.common
data class Store(
val Name: String,
val id: Int = getNextId()
){
companion object{
private var nextid = 0
private fun getNextId(): Int{
nextid += 1
return nextid
}
}
}
val name: String,
val items: List<Moebel> = listOf()
)
data class Moebel(
val name: String
)
......@@ -5,4 +5,6 @@ interface StoreRepo {
suspend fun getStoresAsync(): List<Store>
fun addStore(store: Store)
fun addMoebel(store: Store,moebel: Moebel,onComplete: (Store)->Unit,onFail:()->Unit)
}
\ No newline at end of file
package de.kompo.repo.mongo
import de.kompo.common.Moebel
import de.kompo.common.Store
import de.kompo.common.StoreRepo
import kotlinx.coroutines.*
import org.litote.kmongo.coroutine.*
import org.litote.kmongo.eq
import org.litote.kmongo.reactivestreams.*
import org.litote.kmongo.setValue
class MongoStoreRepo: StoreRepo {
......@@ -28,4 +31,11 @@ class MongoStoreRepo: StoreRepo {
db.getCollection<Store>().insertOne(store)
}
}
override fun addMoebel(store: Store,moebel: Moebel,onComplete: (Store)->Unit,onFail:()->Unit) {
scope.launch {
db.getCollection<Store>().updateOne(Store::name eq store.name, setValue(Store::items, store.items.toMutableList().apply { add(moebel) }))
db.getCollection<Store>().findOne(Store::name eq store.name)?.let { onComplete(it) } ?: onFail()
}
}
}
\ No newline at end of file
package de.kompo.ui
import androidx.compose.material.Colors
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import de.kompo.common.Store
import de.kompo.common.StoreRepo
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
......@@ -8,4 +11,5 @@ import org.koin.core.component.inject
class MainApplication : KoinComponent {
val model by inject<StoreRepo>()
val style by inject<Colors>()
val items = mutableStateOf(mutableListOf<Store>())
}
\ No newline at end of file
import androidx.compose.desktop.Window
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import de.kompo.common.Moebel
import de.kompo.common.Store
import de.kompo.ui.MainApplication
import de.kompo.ui.StoreDetailView
......@@ -13,39 +16,94 @@ import de.kompo.ui.StoreDetailView
fun MainWindow(app: MainApplication) = Window {
MaterialTheme(colors = app.style) {
Surface(modifier = Modifier.fillMaxSize()) {
var selected = remember { mutableStateOf<Store?>(null) }
val stores = mutableStateListOf<Store>()
stores.addAll(app.model.getStores())
val showAddStore = remember { mutableStateOf(false) }
if (showAddStore.value){
Dialog({}){
Dialog({
showAddStore.value = false
}){
MaterialTheme(colors = app.style){
val storeName = remember { mutableStateOf("") }
Column {
Text("")
TextField("", label = { Text("Store Name") }, onValueChange = {
storeName.value = it
})
Row {
Button({
app.model.addStore(Store(storeName.value))
}) {
Text("Ok")
Surface(modifier = Modifier.fillMaxSize()) {
val storeName = remember { mutableStateOf(TextFieldValue()) }
Column {
Text("")
TextField(
value = storeName.value,
onValueChange = { storeName.value = it },
label = { Text("Label") }
)
Row {
Button({
app.model.addStore(Store(storeName.value.text))
showAddStore.value = false
storeName.value = TextFieldValue()
stores.clear()
stores.addAll(app.model.getStores())
}) {
Text("Ok")
}
}
}
}
}
}
}
Row {
Row {
Button({
showAddStore.value= true
}){
Text("New Store")
val showAddMoebel = remember { mutableStateOf(false) }
if (showAddMoebel.value){
Dialog({
showAddMoebel.value = false
}){
MaterialTheme(colors = app.style){
Surface(modifier = Modifier.fillMaxSize()) {
val moebelName = remember { mutableStateOf(TextFieldValue()) }
Column {
Text("")
TextField(
value = moebelName.value,
onValueChange = { moebelName.value = it },
label = { Text("Label") }
)
Row {
Button({
selected.value?.let { store ->
app.model.addMoebel(store, Moebel(moebelName.value.text),{
selected.value = it
}){
}
}
showAddMoebel.value = false
moebelName.value = TextFieldValue()
stores.clear()
stores.addAll(app.model.getStores())
}) {
Text("Ok")
}
}
}
}
}
}
}
Row {
Column {
Row {
Button({
showAddStore.value = true
}) {
Text("New Store")
}
}
Row {
var selected = remember { mutableStateOf<Store?>(null) }
StoreTreeView(app.model, modifier = Modifier.padding(4.dp).wrapContentWidth().fillMaxHeight()){
selected.value = it
StoreTreeView(app.model, modifier = Modifier.padding(4.dp).wrapContentWidth().fillMaxHeight(),stores) {
selected.value = it
}
}
}
Divider(
Modifier.padding(4.dp)
......@@ -54,7 +112,9 @@ fun MainWindow(app: MainApplication) = Window {
)
Card(modifier = Modifier.padding(8.dp), elevation = 4.dp) {
selected.value?.let { store ->
StoreDetailView(store)
StoreDetailView(store){
showAddMoebel.value = true
}
} ?: run {
Text("no Store Selected")
}
......@@ -62,4 +122,4 @@ fun MainWindow(app: MainApplication) = Window {
}
}
}
}
\ No newline at end of file
}
package de.kompo.ui
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
......@@ -11,10 +15,18 @@ import de.kompo.common.Store
@Composable
fun StoreView(item: Store,modifier: Modifier = Modifier.wrapContentSize()) = Box(modifier) {
Text(item.Name,Modifier.padding(4.dp).wrapContentSize())
Text(item.name,Modifier.padding(4.dp).wrapContentSize())
}
@Composable
fun StoreDetailView(item: Store,modifier: Modifier = Modifier.wrapContentSize()) = Box(modifier) {
Text(item.Name,Modifier.padding(4.dp).wrapContentSize())
fun StoreDetailView(item: Store,modifier: Modifier = Modifier.wrapContentSize(),OnAddMoebel: ()->Unit) = Column (modifier) {
Text(item.name,Modifier.padding(4.dp).wrapContentSize())
Button(OnAddMoebel){
Text("add möbelstück")
}
LazyColumn {
items(item.items){
Text(it.name,Modifier.padding(4.dp).wrapContentSize())
}
}
}
\ No newline at end of file
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
......@@ -7,11 +6,9 @@ import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.selection.selectable
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.*
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import de.kompo.common.Store
import de.kompo.common.StoreRepo
......@@ -19,7 +16,7 @@ import de.kompo.ui.StoreView
import de.kompo.ui.util.withoutWidthConstraints
@Composable
fun StoreTreeView(model: StoreRepo,modifier: Modifier = Modifier.fillMaxSize().withoutWidthConstraints(), onSelect : (Store) -> Unit = {}) = Surface (
fun StoreTreeView(model: StoreRepo,modifier: Modifier = Modifier.fillMaxSize().withoutWidthConstraints(),list : SnapshotStateList<Store> , onSelect : (Store) -> Unit = {}) = Surface (
color = MaterialTheme.colors.surface
) {
val scrollState = rememberLazyListState()
......@@ -27,12 +24,13 @@ fun StoreTreeView(model: StoreRepo,modifier: Modifier = Modifier.fillMaxSize().w
val fontSize = 14.sp
val lineHeight = fontSize * 1.5f
LazyColumn(
modifier = modifier,
state = scrollState,
state = scrollState
) {
items(model.getStores()){ store ->
StoreView(store,modifier = Modifier.wrapContentSize().selectable(store.id == selectedId.value?.id){
items(list){ store ->
StoreView(store,modifier = Modifier.wrapContentSize().selectable(store.name == selectedId.value?.name){
selectedId.value = store
onSelect(store)
})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment