diff --git a/app/src/main/java/com/example/abe/ui/form_transaction/FormTransaction.kt b/app/src/main/java/com/example/abe/ui/form_transaction/FormTransaction.kt index d22f32ee28399cf6cd8acb10692ff4cc8c50db2c..d29c659c64b508b2c3badddea6bf84b787757016 100644 --- a/app/src/main/java/com/example/abe/ui/form_transaction/FormTransaction.kt +++ b/app/src/main/java/com/example/abe/ui/form_transaction/FormTransaction.kt @@ -9,6 +9,7 @@ import android.location.Address import android.location.Geocoder import android.location.Location import android.location.LocationManager +import android.net.Uri import android.os.Bundle import android.provider.Settings import android.util.Log @@ -19,6 +20,7 @@ import android.widget.AdapterView import android.widget.ArrayAdapter import android.widget.Toast import androidx.activity.addCallback +import androidx.constraintlayout.widget.ConstraintSet import androidx.core.app.ActivityCompat import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels @@ -81,6 +83,7 @@ class FormTransaction : Fragment() { titleFocusListener() amountFocusListener() categoryFocusListener() + openInMapListener() locationFocusListener() saveButtonListener() @@ -101,15 +104,25 @@ class FormTransaction : Fragment() { displayTrx(trxId) } else if (args.containsKey("random_amount")) { viewModel.setRandomAmount(args.getInt("random_amount")) - binding.btnDelete.visibility = View.GONE + useNewTrxLayout() } } else { - binding.btnDelete.visibility = View.GONE + useNewTrxLayout() } return binding.root } + private fun useNewTrxLayout() { + binding.btnDelete.visibility = View.GONE + binding.btnOpenMap.visibility = View.GONE + + ConstraintSet().apply { + clone(binding.formTransaction) + connect(R.id.formLocationContainer, ConstraintSet.TOP, R.id.formCategoryContainer, ConstraintSet.BOTTOM, 5) + applyTo(binding.formTransaction) + } + } private fun displayTrx(idTrx: Int) { id = idTrx @@ -150,6 +163,17 @@ class FormTransaction : Fragment() { } } + private fun openInMapListener() { + binding.btnOpenMap.setOnClickListener { + val destinationLatitude = viewModel.latitude.value.toString() + val destinationLongitude = viewModel.longitude.value.toString() + + val mapUri = Uri.parse("https://maps.google.com/maps?daddr=$destinationLatitude,$destinationLongitude") + val intent = Intent(Intent.ACTION_VIEW, mapUri) + startActivity(intent) + } + } + private fun locationFocusListener() { binding.formLocationEditText.setOnFocusChangeListener { _, focused -> if (!focused) { diff --git a/app/src/main/res/layout/fragment_form_transaction.xml b/app/src/main/res/layout/fragment_form_transaction.xml index b3c08de536f52f69764affdbd37965f40e197c93..6bec0c5bd2b7036ce7e296316aa728af398e5ba6 100644 --- a/app/src/main/res/layout/fragment_form_transaction.xml +++ b/app/src/main/res/layout/fragment_form_transaction.xml @@ -75,33 +75,49 @@ android:id="@+id/categoryAutocomplete" android:layout_width="match_parent" android:layout_height="wrap_content" - android:text="@={viewModel.category}" android:inputType="none" - android:labelFor="@+id/formCategoryContainer" /> + android:labelFor="@+id/formCategoryContainer" + android:text="@={viewModel.category}" /> </com.google.android.material.textfield.TextInputLayout> + <Button + android:id="@+id/btnOpenMap" + android:minWidth="0dp" + android:minHeight="0dp" + android:layout_width="wrap_content" + android:layout_height="0dp" + android:paddingTop="1dp" + android:paddingBottom="1dp" + android:layout_marginTop="8dp" + android:text="Open in Map" + android:textColor="@color/primary" + android:background="@android:color/transparent" + android:textSize="14sp" + android:textStyle="bold" + app:layout_constraintEnd_toEndOf="@+id/formCategoryContainer" + app:layout_constraintTop_toBottomOf="@+id/formCategoryContainer" /> + <com.google.android.material.textfield.TextInputLayout android:id="@+id/formLocationContainer" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginHorizontal="20dp" - android:layout_marginTop="5dp" app:helperTextTextColor="@color/destructive" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toBottomOf="@id/formCategoryContainer"> + app:layout_constraintTop_toBottomOf="@id/btnOpenMap"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/formLocationEditText" android:layout_width="match_parent" android:layout_height="match_parent" - android:text="@={viewModel.location}" android:hint="Location" android:inputType="text" - android:lines="1" /> + android:lines="1" + android:text="@={viewModel.location}" /> </com.google.android.material.textfield.TextInputLayout>