Skip to content

Commit

Permalink
APP - page create device (#26)
Browse files Browse the repository at this point in the history
APP - page create device
  • Loading branch information
Puvikaran2001 authored Apr 5, 2024
1 parent ba6d61d commit e642c0c
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package dk.scheduling.schedulingfrontend.device

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.em
import dk.scheduling.schedulingfrontend.sharedcomponents.FilledButton
import dk.scheduling.schedulingfrontend.sharedcomponents.OutlinedButton
import dk.scheduling.schedulingfrontend.sharedcomponents.StandardTextField

@Composable
fun CreateDevicePage(
modifier: Modifier = Modifier,
navigateOnValidCreation: () -> Unit,
navigateOnCancelCreation: () -> Unit,
) {
var device by remember {
mutableStateOf(Device(-1, "", null))
}

var isEffectSet by remember {
mutableStateOf(true)
}
Column(
modifier =
modifier
.fillMaxSize()
.padding(all = 50.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Top,
) {
Spacer(modifier = Modifier.height(90.dp))
Text(
text = "Create a Device",
fontSize = 7.em,
textAlign = TextAlign.Center,
color = MaterialTheme.colorScheme.primary,
fontWeight = FontWeight(700),
)
}

Column(
modifier =
modifier
.fillMaxSize()
.padding(all = 50.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
StandardTextField(
label = "Device Name",
value = device.name,
onValueChange = { device = Device(device.id, it, device.effect) },
)

Spacer(modifier = Modifier.height(20.dp))

StandardTextField(
label = "Effect (W)",
value = if (device.effect != null) device.effect.toString() else "",
onValueChange = {
var effect: Int? = null
if (it == "") {
isEffectSet = false
} else {
effect = it.toInt()
isEffectSet = true
}

device = Device(device.id, device.name, effect)
},
keyboardOptions =
KeyboardOptions(
keyboardType = KeyboardType.Number,
),
isError = !isEffectSet,
)

Spacer(modifier = Modifier.height(30.dp))

FilledButton(
onClick = {
if (createDevice(device)) {
navigateOnValidCreation()
}
},
text = "Create Device",
)

Spacer(modifier = Modifier.height(10.dp))

OutlinedButton(
onClick = {
navigateOnCancelCreation()
},
text = "Cancel",
)
}
}

fun createDevice(device: Device): Boolean {
// Send info to the server and if all goes well return true otherwise false
return false
}

@Preview(showBackground = true, device = "spec:id=reference_phone,shape=Normal,width=411,height=891,unit=dp,dpi=420")
@Composable
fun CreateDevicePagePreview() {
CreateDevicePage(navigateOnValidCreation = {}, navigateOnCancelCreation = {})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dk.scheduling.schedulingfrontend.device

data class Device(
var id: Int,
var name: String,
var effect: Int?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dk.scheduling.schedulingfrontend.sharedcomponents

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier

@Composable
fun FilledButton(
modifier: Modifier = Modifier,
onClick: () -> Unit,
text: String,
) {
Column {
Button(
onClick = onClick,
modifier = modifier.fillMaxWidth(),
) {
Text(text = text)
}
}
}

@Composable
fun OutlinedButton(
modifier: Modifier = Modifier,
onClick: () -> Unit,
text: String,
) {
Column {
androidx.compose.material3.OutlinedButton(
onClick = onClick,
modifier = modifier.fillMaxWidth(),
) {
Text(text = text)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dk.scheduling.schedulingfrontend.sharedcomponents

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier

@Composable
fun StandardTextField(
modifier: Modifier = Modifier,
label: String,
value: String,
onValueChange: (String) -> Unit,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
isError: Boolean = false,
) {
OutlinedTextField(
label = { Text(label) },
value = value,
onValueChange = onValueChange,
modifier =
modifier
.fillMaxWidth(),
singleLine = true,
keyboardOptions = keyboardOptions,
isError = isError,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ fun SignUpPagePreviewLightMode() {
@Preview(showBackground = true, device = "spec:id=reference_phone,shape=Normal,width=411,height=891,unit=dp,dpi=420")
@Composable
fun SignUpPagePreviewDarkMode() {
SchedulingFrontendTheme(darkTheme = true) {
SchedulingFrontendTheme(darkTheme = true, dynamicColor = false) {
SignUpPage(navigateOnValidSignUp = {}, navigateToLoginPage = {})
}
}

0 comments on commit e642c0c

Please sign in to comment.