@@ -1,6 +1,7 @@
|
||||
package com.sockenklaus.batterytracker.ui.composables
|
||||
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material.*
|
||||
import androidx.compose.material.icons.Icons
|
||||
@@ -91,6 +92,11 @@ fun AddBattery(navController: NavController){
|
||||
keyboardType = KeyboardType.Decimal,
|
||||
imeAction = ImeAction.Done
|
||||
),
|
||||
keyboardActions = KeyboardActions(
|
||||
onDone = {
|
||||
saveBattery(model = model, navController = navController)
|
||||
}
|
||||
),
|
||||
leadingIcon = { Icon(Icons.Default.BatteryFull, "Icon Battery Full") },
|
||||
suffix = "Ah",
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
@@ -99,16 +105,21 @@ fun AddBattery(navController: NavController){
|
||||
Spacer(Modifier.size(outerPadding))
|
||||
|
||||
ExtendedFloatingActionButton(
|
||||
onClick = {
|
||||
if(model.batteryName.isBlank()) model.batteryHasError = true
|
||||
|
||||
if(!model.batteryHasError && model.saveBattery(model.batteryName, model.declaredCapacity)){
|
||||
navController.navigate(Routes.HOME)
|
||||
}
|
||||
},
|
||||
onClick = { saveBattery(model = model, navController = navController) },
|
||||
icon = { Icon(Icons.Default.Save, "Icon Save") },
|
||||
text = { Text(stringResource(R.string.button_save_battery)) },
|
||||
modifier = Modifier.align(Alignment.End)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun saveBattery(
|
||||
model: AddBatteryViewModel,
|
||||
navController: NavController
|
||||
){
|
||||
if(model.batteryName.isBlank()) model.batteryHasError = true
|
||||
|
||||
if(!model.batteryHasError && model.saveBattery(model.batteryName, model.declaredCapacity)) {
|
||||
navController.navigate(Routes.HOME)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.sockenklaus.batterytracker.ui.composables
|
||||
|
||||
import android.app.DatePickerDialog
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material.*
|
||||
import androidx.compose.material.icons.Icons
|
||||
@@ -23,6 +24,7 @@ import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavController
|
||||
import com.sockenklaus.batterytracker.R
|
||||
import com.sockenklaus.batterytracker.room.entities.Battery
|
||||
import com.sockenklaus.batterytracker.ui.Routes
|
||||
import com.sockenklaus.batterytracker.ui.composables.util.MyOutlinedTextFieldWithSuffix
|
||||
import com.sockenklaus.batterytracker.ui.models.AddChargeViewModel
|
||||
@@ -71,6 +73,12 @@ fun AddCharge(navController: NavController){
|
||||
keyboardOptions = KeyboardOptions(
|
||||
imeAction = ImeAction.Next
|
||||
),
|
||||
keyboardActions = KeyboardActions(
|
||||
onNext = {
|
||||
bIdExpanded = false
|
||||
defaultKeyboardAction(ImeAction.Next)
|
||||
}
|
||||
),
|
||||
singleLine = true
|
||||
)
|
||||
|
||||
@@ -114,6 +122,11 @@ fun AddCharge(navController: NavController){
|
||||
keyboardType = KeyboardType.Decimal,
|
||||
imeAction = ImeAction.Done
|
||||
),
|
||||
keyboardActions = KeyboardActions(
|
||||
onDone = {
|
||||
saveCharge(batteries, model, navController)
|
||||
}
|
||||
),
|
||||
labelId = R.string.hint_charge,
|
||||
leadingIcon = { Icon(Icons.Default.BatteryChargingFull, "Icon Battery Charging Full") },
|
||||
isError = model.chargeHasError,
|
||||
@@ -134,21 +147,7 @@ fun AddCharge(navController: NavController){
|
||||
ExtendedFloatingActionButton(
|
||||
text = { Text(stringResource(R.string.button_save_charge)) },
|
||||
onClick = {
|
||||
if(!batteries.any{ it.name == model.batteryId.text }){
|
||||
model.batteryHasError = true
|
||||
model.batteryHelper = R.string.helper_battery_not_found
|
||||
}
|
||||
if(model.batteryId.text.isBlank()){
|
||||
model.batteryHasError = true
|
||||
model.batteryHelper = R.string.helper_required
|
||||
}
|
||||
if(model.charge.isBlank()){
|
||||
model.chargeHasError = true
|
||||
}
|
||||
|
||||
if(!model.batteryHasError && !model.chargeHasError && model.saveCharge(batteries, model.batteryId.text, model.charge, model.date)){
|
||||
navController.navigate(Routes.HOME)
|
||||
}
|
||||
saveCharge(batteries, model, navController)
|
||||
},
|
||||
icon = { Icon(Icons.Default.Save, "Icon Save") },
|
||||
modifier = Modifier.align(Alignment.End)
|
||||
@@ -157,6 +156,27 @@ fun AddCharge(navController: NavController){
|
||||
}
|
||||
}
|
||||
|
||||
fun saveCharge(
|
||||
batteries: List<Battery>,
|
||||
model: AddChargeViewModel,
|
||||
navController: NavController
|
||||
) {
|
||||
if(!batteries.any{ it.name == model.batteryId.text }){
|
||||
model.batteryHasError = true
|
||||
model.batteryHelper = R.string.helper_battery_not_found
|
||||
}
|
||||
if(model.batteryId.text.isBlank()){
|
||||
model.batteryHasError = true
|
||||
model.batteryHelper = R.string.helper_required
|
||||
}
|
||||
if(model.charge.isBlank()){
|
||||
model.chargeHasError = true
|
||||
}
|
||||
|
||||
if(!model.batteryHasError && !model.chargeHasError && model.saveCharge(batteries, model.batteryId.text, model.charge, model.date)){
|
||||
navController.navigate(Routes.HOME)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ChargeDatePicker(
|
||||
|
||||
@@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.LazyListState
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material.*
|
||||
import androidx.compose.runtime.*
|
||||
@@ -49,6 +50,11 @@ fun Home(
|
||||
singleLine = true,
|
||||
keyboardOptions = KeyboardOptions(
|
||||
imeAction = ImeAction.Search
|
||||
),
|
||||
keyboardActions = KeyboardActions(
|
||||
onSearch = {
|
||||
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
package com.sockenklaus.batterytracker.ui.models
|
||||
|
||||
import android.app.Application
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.livedata.observeAsState
|
||||
import androidx.compose.ui.text.input.TextFieldValue
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.LiveData
|
||||
|
||||
Reference in New Issue
Block a user