Fixed AddBattery Composables
This commit is contained in:
@@ -51,6 +51,7 @@ dependencies {
|
|||||||
implementation 'androidx.room:room-runtime:2.5.0-alpha02'
|
implementation 'androidx.room:room-runtime:2.5.0-alpha02'
|
||||||
implementation "androidx.compose.ui:ui:1.3.0-alpha01"
|
implementation "androidx.compose.ui:ui:1.3.0-alpha01"
|
||||||
implementation "androidx.compose.ui:ui-tooling-preview:1.3.0-alpha01"
|
implementation "androidx.compose.ui:ui-tooling-preview:1.3.0-alpha01"
|
||||||
|
implementation 'androidx.compose.material:material-icons-extended:1.1.1'
|
||||||
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.0'
|
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.0'
|
||||||
annotationProcessor 'androidx.room:room-compiler:2.5.0-alpha02'
|
annotationProcessor 'androidx.room:room-compiler:2.5.0-alpha02'
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package com.sockenklaus.batterytracker.ui.fragments
|
package com.sockenklaus.batterytracker.ui.fragments
|
||||||
|
|
||||||
import android.inputmethodservice.Keyboard
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
@@ -8,18 +7,16 @@ import android.view.View
|
|||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.compose.foundation.layout.*
|
import androidx.compose.foundation.layout.*
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
import androidx.compose.foundation.text.KeyboardOptions
|
||||||
import androidx.compose.material.OutlinedTextField
|
import androidx.compose.material.*
|
||||||
import androidx.compose.material.Text
|
import androidx.compose.material.icons.Icons
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.material.icons.filled.*
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.text.input.KeyboardType
|
import androidx.compose.ui.text.input.KeyboardType
|
||||||
import androidx.compose.ui.tooling.preview.Preview
|
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.constraintlayout.compose.ConstraintLayout
|
import androidx.constraintlayout.compose.ConstraintLayout
|
||||||
import androidx.constraintlayout.compose.Dimension
|
import androidx.constraintlayout.compose.Dimension
|
||||||
@@ -27,12 +24,6 @@ import com.sockenklaus.batterytracker.R
|
|||||||
import com.sockenklaus.batterytracker.databinding.FragmentAddBatteryBinding
|
import com.sockenklaus.batterytracker.databinding.FragmentAddBatteryBinding
|
||||||
import com.sockenklaus.batterytracker.ui.theme.BatteryTrackerTheme
|
import com.sockenklaus.batterytracker.ui.theme.BatteryTrackerTheme
|
||||||
|
|
||||||
|
|
||||||
// TODO: Rename parameter arguments, choose names that match
|
|
||||||
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
|
|
||||||
private const val ARG_PARAM1 = "param1"
|
|
||||||
private const val ARG_PARAM2 = "param2"
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple [Fragment] subclass.
|
* A simple [Fragment] subclass.
|
||||||
* Use the [AddBatteryFragment.newInstance] factory method to
|
* Use the [AddBatteryFragment.newInstance] factory method to
|
||||||
@@ -43,14 +34,10 @@ class AddBatteryFragment : Fragment() {
|
|||||||
private var _binding: FragmentAddBatteryBinding? = null
|
private var _binding: FragmentAddBatteryBinding? = null
|
||||||
private val binding get() = _binding!!
|
private val binding get() = _binding!!
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
|
||||||
super.onCreate(savedInstanceState)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
inflater: LayoutInflater, container: ViewGroup?,
|
inflater: LayoutInflater, container: ViewGroup?,
|
||||||
savedInstanceState: Bundle?
|
savedInstanceState: Bundle?
|
||||||
): View? {
|
): View {
|
||||||
|
|
||||||
_binding = FragmentAddBatteryBinding.inflate(inflater, container, false)
|
_binding = FragmentAddBatteryBinding.inflate(inflater, container, false)
|
||||||
val view = binding.root
|
val view = binding.root
|
||||||
@@ -61,56 +48,53 @@ class AddBatteryFragment : Fragment() {
|
|||||||
var declaredCapacity by remember { mutableStateOf("")}
|
var declaredCapacity by remember { mutableStateOf("")}
|
||||||
|
|
||||||
ConstraintLayout {
|
ConstraintLayout {
|
||||||
val ( labelBatteryId, textBatteryId, labelDeclaredCapacity, textDeclaredCapacity, fab ) = createRefs()
|
val (textBatteryId, textDeclaredCapacity, fab ) = createRefs()
|
||||||
val outer_margin = 24.dp
|
val outerMargin = 24.dp
|
||||||
val inner_margin = 16.dp
|
val innerMargin = 16.dp
|
||||||
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.battery_id),
|
|
||||||
Modifier.constrainAs(labelBatteryId) {
|
|
||||||
end.linkTo(labelDeclaredCapacity.end)
|
|
||||||
baseline.linkTo(textBatteryId.baseline)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
// TODO: Stop baseline from moving!!!!
|
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
value = batteryId,
|
value = batteryId,
|
||||||
onValueChange = { batteryId = it },
|
onValueChange = { batteryId = it },
|
||||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
|
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
|
||||||
label = { Text(stringResource(R.string.hint_enter_battery_id)) },
|
label = { Text(stringResource(R.string.hint_enter_battery_id)) },
|
||||||
|
leadingIcon = { Icon(Icons.Default.Tag, null) },
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.constrainAs(textBatteryId) {
|
.constrainAs(textBatteryId) {
|
||||||
top.linkTo(parent.top, margin = outer_margin)
|
top.linkTo(parent.top, margin = outerMargin)
|
||||||
start.linkTo(textDeclaredCapacity.start)
|
start.linkTo(parent.start, margin = outerMargin)
|
||||||
end.linkTo(parent.end, margin = outer_margin)
|
end.linkTo(parent.end, margin = outerMargin)
|
||||||
width = Dimension.fillToConstraints
|
width = Dimension.fillToConstraints
|
||||||
}
|
}
|
||||||
.wrapContentHeight()
|
.wrapContentHeight()
|
||||||
)
|
)
|
||||||
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.declared_capacity),
|
|
||||||
Modifier
|
|
||||||
.constrainAs(labelDeclaredCapacity) {
|
|
||||||
baseline.linkTo(textDeclaredCapacity.baseline)
|
|
||||||
start.linkTo(parent.start, margin = outer_margin)
|
|
||||||
}
|
|
||||||
.wrapContentSize()
|
|
||||||
)
|
|
||||||
|
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
value = declaredCapacity,
|
value = declaredCapacity,
|
||||||
onValueChange = { declaredCapacity = verifyDecimal(declaredCapacity, it) },
|
onValueChange = { declaredCapacity = verifyDecimal(declaredCapacity, it) },
|
||||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal),
|
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal),
|
||||||
label = { Text(stringResource(R.string.hint_enter_declared_capacity)) },
|
label = { Text(stringResource(R.string.hint_enter_declared_capacity)) },
|
||||||
|
leadingIcon = { Icon(Icons.Default.BatteryFull, null) },
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.constrainAs(textDeclaredCapacity) {
|
.constrainAs(textDeclaredCapacity) {
|
||||||
top.linkTo(textBatteryId.bottom, margin = inner_margin)
|
top.linkTo(textBatteryId.bottom, margin = innerMargin)
|
||||||
start.linkTo(labelDeclaredCapacity.end, margin = inner_margin)
|
start.linkTo(parent.start, margin = outerMargin)
|
||||||
end.linkTo(parent.end, margin = outer_margin)
|
end.linkTo(parent.end, margin = outerMargin)
|
||||||
width = Dimension.fillToConstraints
|
width = Dimension.fillToConstraints
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
ExtendedFloatingActionButton(
|
||||||
|
onClick = { saveBattery() },
|
||||||
|
icon = { Icon(Icons.Default.Save, null) },
|
||||||
|
text = { Text(stringResource(R.string.button_save_battery)) },
|
||||||
|
modifier = Modifier
|
||||||
|
.constrainAs(fab) {
|
||||||
|
top.linkTo(textDeclaredCapacity.bottom, margin = 24.dp)
|
||||||
|
end.linkTo(parent.end, margin = outerMargin)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -119,6 +103,10 @@ class AddBatteryFragment : Fragment() {
|
|||||||
return view
|
return view
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun saveBattery() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private fun verifyDecimal(originalValue: String, it: String): String {
|
private fun verifyDecimal(originalValue: String, it: String): String {
|
||||||
val reg = Regex("^[+-]?([0-9]+\\.?[0-9]*|\\.[0-9]+)\$")
|
val reg = Regex("^[+-]?([0-9]+\\.?[0-9]*|\\.[0-9]+)\$")
|
||||||
return if(it.matches(reg) || it.isEmpty()) it
|
return if(it.matches(reg) || it.isEmpty()) it
|
||||||
|
|||||||
Reference in New Issue
Block a user