Work on AddChargeFragment.kt

This commit is contained in:
sockenklaus
2022-07-16 18:10:42 +02:00
parent 5e03930d6f
commit 8f316d98fd
6 changed files with 117 additions and 98 deletions

View File

@@ -24,6 +24,7 @@ import androidx.constraintlayout.compose.Dimension
import com.sockenklaus.batterytracker.R
import com.sockenklaus.batterytracker.databinding.FragmentAddBatteryBinding
import com.sockenklaus.batterytracker.ui.theme.BatteryTrackerTheme
import com.sockenklaus.batterytracker.util.validateDecimal
/**
* A simple [Fragment] subclass.
@@ -43,7 +44,7 @@ class AddBatteryFragment : Fragment() {
_binding = FragmentAddBatteryBinding.inflate(inflater, container, false)
val view = binding.root
binding.composeAddBattery.setContent {
binding.root.setContent {
BatteryTrackerTheme {
var batteryId by remember { mutableStateOf("") }
var declaredCapacity by remember { mutableStateOf("")}
@@ -71,7 +72,7 @@ class AddBatteryFragment : Fragment() {
OutlinedTextField(
value = declaredCapacity,
onValueChange = { declaredCapacity = verifyDecimal(declaredCapacity, it) },
onValueChange = { declaredCapacity = validateDecimal(it, declaredCapacity) },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal),
label = { Text(stringResource(R.string.hint_enter_declared_capacity)) },
leadingIcon = { Icon(Icons.Default.BatteryFull, null) },
@@ -111,12 +112,6 @@ class AddBatteryFragment : Fragment() {
}
private fun verifyDecimal(originalValue: String, it: String): String {
val reg = Regex("^[+-]?([0-9]+\\.?[0-9]*|\\.[0-9]+)\$")
return if(it.matches(reg) || it.isEmpty()) it
else originalValue
}
/*companion object {
* Use this factory method to create a new instance of

View File

@@ -8,9 +8,24 @@ import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Tag
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
import com.google.android.material.datepicker.MaterialDatePicker
import com.sockenklaus.batterytracker.R
import com.sockenklaus.batterytracker.databinding.FragmentAddChargeBinding
import com.sockenklaus.batterytracker.ui.theme.BatteryTrackerTheme
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
@@ -26,6 +41,7 @@ class AddChargeFragment : Fragment() {
private lateinit var viewModel: AddChargeViewModel
private val binding get() = _binding!!
@OptIn(ExperimentalMaterialApi::class)
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
@@ -39,7 +55,79 @@ class AddChargeFragment : Fragment() {
val items = listOf("E1", "E2", "E3", "E4", "Bonsai", "Bonai")
val adapter = ArrayAdapter(requireContext(), R.layout.list_item, items)
(binding.editBatteryId.editText as? AutoCompleteTextView)?.setAdapter(adapter)
binding.composeView.setContent{
BatteryTrackerTheme {
ConstraintLayout {
val (textBatteryId, dbgFilterOptions, dbgExp, btnDate, textCharge, fab) = createRefs()
val outerMargin = 24.dp
val innerMargin = 16.dp
var selectedBatteryId by remember { mutableStateOf(TextFieldValue(""))}
var date by remember { mutableStateOf(LocalDate.now())}
var charge by remember { mutableStateOf("") }
var bIdExpanded by remember { mutableStateOf(false)}
ExposedDropdownMenuBox(
expanded = bIdExpanded,
onExpandedChange = { bIdExpanded = !bIdExpanded},
modifier = Modifier
.constrainAs(textBatteryId) {
top.linkTo(parent.top, margin = outerMargin)
start.linkTo(parent.start, margin = outerMargin)
end.linkTo(parent.end, margin = outerMargin)
width = Dimension.fillToConstraints
}
) {
OutlinedTextField(
value = selectedBatteryId,
onValueChange = { selectedBatteryId = it },
label = { Text(stringResource(R.string.select_battery_id)) },
leadingIcon = { Icon(Icons.Default.Tag, null) },
trailingIcon = {
ExposedDropdownMenuDefaults.TrailingIcon(expanded = bIdExpanded)
},
colors = ExposedDropdownMenuDefaults.outlinedTextFieldColors()
)
val filteringOptions = items.filter { it.contains(selectedBatteryId.text, ignoreCase = true)}
if(filteringOptions.isNotEmpty()) {
ExposedDropdownMenu(
expanded = bIdExpanded,
onDismissRequest = { bIdExpanded = false }
) {
for (filteringOption in filteringOptions) {
DropdownMenuItem(
onClick = {
selectedBatteryId = TextFieldValue(
text = filteringOption,
selection = TextRange(filteringOption.length)
)
bIdExpanded = false
}
) {
Text(filteringOption)
}
}
}
}
}
/*Text(
filteringOptions.toString(),
Modifier.constrainAs(dbgFilterOptions) {
top.linkTo(textBatteryId.bottom, margin = 40.dp)
}
)*/
Text(
"expanded: $bIdExpanded",
Modifier.constrainAs(dbgExp) {
top.linkTo(dbgFilterOptions.bottom, margin = 40.dp)
}
)
}
}
}
/*(binding.editBatteryId.editText as? AutoCompleteTextView)?.setAdapter(adapter)
val current: LocalDate = LocalDate.now()
@@ -54,7 +142,7 @@ class AddChargeFragment : Fragment() {
binding.editDate.setOnClickListener {
openDatePicker()
}
}*/
return binding.root
}

View File

@@ -0,0 +1,7 @@
package com.sockenklaus.batterytracker.util
fun validateDecimal(newVal: String, oldVal: String): String {
val reg = Regex("^[+-]?([0-9]+\\.?[0-9]*|\\.[0-9]+)\$")
return if(newVal.matches(reg) || newVal.isEmpty()) newVal
else oldVal
}

View File

@@ -2,92 +2,8 @@
<androidx.compose.ui.platform.ComposeView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".ui.fragments.AddBatteryFragment"
android:id="@+id/compose_add_battery"
/>
<!--<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragments.AddBatteryFragment">
<TextView
android:id="@+id/text_battery_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="@string/battery_id"
app:layout_constraintBaseline_toBaselineOf="@+id/battery_id"
app:layout_constraintEnd_toEndOf="@+id/text_declared_capacity" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/battery_id"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginTop="24dp"
android:hint="@string/hint_enter_battery_id"
app:layout_constraintStart_toStartOf="@id/declared_capacity"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:id="@+id/text_declared_capacity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="@string/declared_capacity"
app:layout_constraintBaseline_toBaselineOf="@+id/declared_capacity"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/declared_capacity"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:hint="@string/hint_enter_declared_capacity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/text_declared_capacity"
app:layout_constraintTop_toBottomOf="@id/battery_id">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginTop="32dp"
android:layout_marginEnd="24dp"
android:contentDescription="@string/button_save_battery"
android:text="@string/button_save_battery"
app:icon="@drawable/ic_baseline_check_24"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/declared_capacity" />
</androidx.constraintlayout.widget.ConstraintLayout>-->
/>

View File

@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
@@ -6,7 +9,16 @@
android:layout_height="match_parent"
tools:context=".ui.fragments.add_charge.AddChargeFragment">
<com.google.android.material.textfield.TextInputLayout
<androidx.compose.ui.platform.ComposeView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".ui.fragments.AddChargeFragment"
android:id="@+id/compose_view"
app:layout_constraintTop_toTopOf="parent"
/>
<!--<com.google.android.material.textfield.TextInputLayout
android:id="@+id/edit_battery_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
@@ -18,7 +30,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/text_battery_id"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintTop_toBottomOf="@+id/compose_view"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
>
@@ -100,6 +112,6 @@
android:text="@string/button_save_charge"
app:icon="@drawable/ic_baseline_check_24"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_charge" />
app:layout_constraintTop_toBottomOf="@+id/edit_charge" />-->
</androidx.constraintlayout.widget.ConstraintLayout>