Added first data validator to AddBatteryFragment.kt
This commit is contained in:
@@ -14,17 +14,16 @@ import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.constraintlayout.compose.ConstraintLayout
|
||||
import androidx.constraintlayout.compose.Dimension
|
||||
import androidx.lifecycle.asLiveData
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.sockenklaus.batterytracker.R
|
||||
import com.sockenklaus.batterytracker.databinding.FragmentAddBatteryBinding
|
||||
import com.sockenklaus.batterytracker.room.BatteryTrackerDB
|
||||
import com.sockenklaus.batterytracker.room.entities.Battery
|
||||
import com.sockenklaus.batterytracker.ui.theme.BatteryTrackerTheme
|
||||
import com.sockenklaus.batterytracker.ui.theme.Gray500
|
||||
import com.sockenklaus.batterytracker.util.validateDecimal
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -45,13 +44,26 @@ class AddBatteryFragment : Fragment() {
|
||||
): View {
|
||||
|
||||
_binding = FragmentAddBatteryBinding.inflate(inflater, container, false)
|
||||
val db = BatteryTrackerDB.getInstance(requireContext())
|
||||
val view = binding.root
|
||||
|
||||
val batteries = db.batteryDao().getBatteries().asLiveData()
|
||||
var listBatteries = emptyList<Battery>()
|
||||
|
||||
batteries.observe(viewLifecycleOwner) {
|
||||
listBatteries = it
|
||||
}
|
||||
|
||||
binding.root.setContent {
|
||||
BatteryTrackerTheme {
|
||||
var batteryId by remember { mutableStateOf("") }
|
||||
var declaredCapacity by remember { mutableStateOf("")}
|
||||
|
||||
var batteryIsError by remember { mutableStateOf(false) }
|
||||
var batteryHelper: Int? by remember { mutableStateOf(null) }
|
||||
var chargeIsError by remember { mutableStateOf(false) }
|
||||
var chargeHelper: Int? by remember { mutableStateOf(null) }
|
||||
|
||||
val outerPadding = 24.dp
|
||||
val innerPadding = 16.dp
|
||||
|
||||
@@ -60,9 +72,19 @@ class AddBatteryFragment : Fragment() {
|
||||
) {
|
||||
MyOutlinedTextField(
|
||||
value = batteryId,
|
||||
onValueChange = { batteryId = it },
|
||||
onValueChange = { value ->
|
||||
batteryId = value
|
||||
onChangeBatteryId(
|
||||
value = value,
|
||||
batteries = listBatteries,
|
||||
changeHelper = { ch -> batteryHelper = ch },
|
||||
changeIsError = { ce -> batteryIsError = ce }
|
||||
)
|
||||
},
|
||||
labelId = R.string.hint_enter_battery_id,
|
||||
leadingIcon = { Icon(Icons.Default.Tag, "Icon Tag") }
|
||||
leadingIcon = { Icon(Icons.Default.Tag, "Icon Tag") },
|
||||
isError = batteryIsError,
|
||||
helperTextId = batteryHelper
|
||||
)
|
||||
|
||||
Spacer(Modifier.size(innerPadding))
|
||||
@@ -72,7 +94,9 @@ class AddBatteryFragment : Fragment() {
|
||||
onValueChange = { declaredCapacity = validateDecimal(it, declaredCapacity) },
|
||||
labelId = R.string.hint_enter_declared_capacity,
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal),
|
||||
leadingIcon = { Icon(Icons.Default.BatteryFull, "Icon Battery Full") }
|
||||
leadingIcon = { Icon(Icons.Default.BatteryFull, "Icon Battery Full") },
|
||||
isError = chargeIsError,
|
||||
helperTextId = chargeHelper
|
||||
)
|
||||
|
||||
Spacer(Modifier.size(outerPadding))
|
||||
@@ -101,23 +125,49 @@ class AddBatteryFragment : Fragment() {
|
||||
labelId: Int,
|
||||
leadingIcon: (@Composable () -> Unit)? = null,
|
||||
isError: Boolean = false,
|
||||
helperText: String = ""
|
||||
helperTextId: Int? = null
|
||||
) {
|
||||
val helperTextColor = if(isError){
|
||||
MaterialTheme.colors.error
|
||||
} else {
|
||||
Gray500
|
||||
}
|
||||
|
||||
OutlinedTextField(
|
||||
value = value,
|
||||
onValueChange = onValueChange,
|
||||
keyboardOptions = keyboardOptions,
|
||||
label = { Text(stringResource(labelId)) },
|
||||
leadingIcon = leadingIcon,
|
||||
isError = isError,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
)
|
||||
Text(
|
||||
text = helperText,
|
||||
text = stringResource(helperTextId ?: R.string.helper_required),
|
||||
style = MaterialTheme.typography.caption,
|
||||
color = helperTextColor,
|
||||
modifier = Modifier.padding(start = 16.dp)
|
||||
)
|
||||
}
|
||||
|
||||
private fun onChangeBatteryId(
|
||||
value: String,
|
||||
batteries: List<Battery>,
|
||||
changeIsError: (Boolean) -> Unit,
|
||||
changeHelper: (Int?) -> Unit
|
||||
) {
|
||||
changeIsError(false)
|
||||
changeHelper(null)
|
||||
if(batteries.any{ it.id.lowercase() == value.lowercase() }){
|
||||
changeIsError(true)
|
||||
changeHelper(R.string.helper_battery_not_unique)
|
||||
}
|
||||
if(value.isEmpty()){
|
||||
changeIsError(true)
|
||||
changeHelper(null)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
TODO: Implement the action
|
||||
*/
|
||||
|
||||
@@ -5,4 +5,5 @@ import androidx.compose.ui.graphics.Color
|
||||
val Purple200 = Color(0xFFBB86FC)
|
||||
val Purple500 = Color(0xFF6200EE)
|
||||
val Purple700 = Color(0xFF3700B3)
|
||||
val Teal200 = Color(0xFF03DAC5)
|
||||
val Teal200 = Color(0xFF03DAC5)
|
||||
val Gray500 = Color(0xFF9E9E9E)
|
||||
@@ -27,4 +27,6 @@
|
||||
<string name="declared_capacity">Declared Capacity</string>
|
||||
<string name="button_save_battery">Save Battery</string>
|
||||
<string name="title_activity_add_battery">AddBattery</string>
|
||||
<string name="helper_required">* Required</string>
|
||||
<string name="helper_battery_not_unique">Battery-ID not unique!</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user