Skip to content
Snippets Groups Projects
Commit fbaa37a5 authored by THONG Sylvain's avatar THONG Sylvain
Browse files

Merge branch 'main' into 'feat/GoogleMaps'

# Conflicts:
#   app/src/main/res/values-FR/strings.xml
#   app/src/main/res/values/strings.xml
parents 9c6cc0e1 542f1393
No related branches found
No related tags found
1 merge request!1feat(UI): GoogleMaps Fragment
......@@ -34,6 +34,7 @@ public class DatePickerFragment extends Fragment {
private DatePicker datePicker;
private Button validateDateButton;
private Button cancelButton;
private boolean hasPickedDate = false;
private onUserPickingDateListener mListener;
public DatePickerFragment() {
......@@ -120,6 +121,7 @@ public class DatePickerFragment extends Fragment {
int month = datePicker.getMonth() + 1;
int year = datePicker.getYear();
hasPickedDate = true;
mListener.onDateSubmittedListener(day, month, year, firstName, lastName, birthCity, birthDept, phoneNumbers);
}
});
......@@ -134,4 +136,12 @@ public class DatePickerFragment extends Fragment {
return view;
}
@Override
public void onDetach() {
super.onDetach();
if (!hasPickedDate && mListener != null) {
mListener.onDateCancelledListener(firstName, lastName, birthCity, birthDept, phoneNumbers, previousBirthDate);
}
}
}
\ No newline at end of file
......@@ -7,6 +7,7 @@ import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
......@@ -18,6 +19,7 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
......@@ -146,4 +148,16 @@ public class DisplayUserFragment extends Fragment {
.create()
.show();
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(ARG_FIRST_NAME, mFirstName);
outState.putString(ARG_LAST_NAME, mLastName);
outState.putString(ARG_BIRTH_CITY, mBirthCity);
outState.putString(ARG_BIRTH_DEPT, mBirthDept);
outState.putString(ARG_BIRTH_DATE, mBirthDate);
outState.putStringArrayList(ARG_PHONE_NUMBERS_LIST, mPhoneNumbersList);
}
}
\ No newline at end of file
......@@ -8,8 +8,10 @@ import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
......@@ -170,14 +172,13 @@ public class EditUserFragment extends Fragment {
birthDeptSpinner.setSelection(savedInstanceState.getInt(ARG_BIRTHDEPT, 0));
phoneNumbersList = savedInstanceState.getStringArrayList(ARG_PHONES);
restorePhoneNumbers();
Log.i("RestoredList", "onViewCreated: " + phoneNumbersList);
} else {
firstNameEditText.setText(firstName);
lastNameEditText.setText(lastName);
birthCityEditText.setText(birthCity);
birthDateButton.setText(birthDate);
birthDeptSpinner.setSelection(birthDept);
restorePhoneNumbers();
}
if (getArguments() != null) {
......@@ -191,9 +192,11 @@ public class EditUserFragment extends Fragment {
lastNameEditText.setText(lastName);
birthCityEditText.setText(birthCity);
this.phoneNumbersList = phoneNumbersList;
restorePhoneNumbers();
}
Log.i("RestoredList", "onViewCreated 2: " + phoneNumbersList);
restorePhoneNumbers();
birthDateButton.setOnClickListener(v -> {
getPhoneNumbersList();
callback.onEditDateRequested(birthDate, firstNameEditText.getText().toString(), lastNameEditText.getText().toString(), birthCityEditText.getText().toString(), birthDeptSpinner.getSelectedItemPosition(), phoneNumbersList, birthDateButton.getText().toString());
......@@ -234,15 +237,18 @@ public class EditUserFragment extends Fragment {
}
private void addPhoneNumberToLayout(String phoneNumber) {
Log.i("RestoredList", phoneNumber);
LayoutInflater inflater = LayoutInflater.from(getContext());
View phoneView = inflater.inflate(R.layout.phone_number_input, phonesLayout, false);
EditText phoneEditText = phoneView.findViewById(R.id.phoneNumberEditText);
ConstraintLayout phoneLayout = (ConstraintLayout) phoneView;
EditText phoneEditText = (EditText) phoneLayout.getChildAt(1);
phoneEditText.setId(View.generateViewId());
phoneEditText.setText(phoneNumber);
Button removeButton = phoneView.findViewById(R.id.removePhoneNumberButton);
removeButton.setOnClickListener(v -> removePhoneNumber(phoneView));
Log.i("RestoredList", "Phone view"+phoneView);
phonesLayout.addView(phoneView);
}
......@@ -260,17 +266,24 @@ public class EditUserFragment extends Fragment {
birthDate = birthDateButton.getText().toString();
birthDept = birthDeptSpinner.getSelectedItemPosition();
phoneNumbersList.clear();
for (int i = 1; i < phonesLayout.getChildCount(); i++) {
View phoneView = phonesLayout.getChildAt(i);
EditText phoneEditText = phoneView.findViewById(R.id.phoneNumberEditText);
String phoneNumber = phoneEditText.getText().toString();
if (!phoneNumber.isEmpty()) {
phoneNumbersList.add(phoneNumber);
}
Date birthDateObject = new Date();
if (!this.birthDate.equals(getString(R.string.pick_date_hint))) {
String[] dateParts = this.birthDate.split("/");
birthDateObject = new Date(
Integer.parseInt(dateParts[2]) - 1900, // année
Integer.parseInt(dateParts[1]) - 1, // mois (0-based)
Integer.parseInt(dateParts[0]) // jour
);
}
Date today = new Date();
if (birthDateObject.after(today)) {
Snackbar.make(requireView(), getString(R.string.date_in_future), Snackbar.LENGTH_LONG).show();
return;
}
if (firstName.isEmpty() || lastName.isEmpty() || birthCity.isEmpty() || birthDate.equals(getString(R.string.pick_date_hint)+"TOREMOVE")) {
getPhoneNumbersList();
if (firstName.isEmpty() || lastName.isEmpty() || birthCity.isEmpty() || birthDate.equals(getString(R.string.pick_date_hint))) {
Snackbar.make(requireView(), getString(R.string.empty_warning), Snackbar.LENGTH_LONG).show();
return;
}
......@@ -286,7 +299,8 @@ public class EditUserFragment extends Fragment {
phoneNumbersList.clear();
for (int i = 1; i < phonesLayout.getChildCount(); i++) {
View phoneView = phonesLayout.getChildAt(i);
EditText phoneEditText = phoneView.findViewById(R.id.phoneNumberEditText);
ConstraintLayout phoneLayout = (ConstraintLayout) phoneView;
EditText phoneEditText = (EditText) phoneLayout.getChildAt(1);
String phoneNumber = phoneEditText.getText().toString();
if (!phoneNumber.isEmpty()) {
phoneNumbersList.add(phoneNumber);
......@@ -310,16 +324,17 @@ public class EditUserFragment extends Fragment {
}
private void restorePhoneNumbers() {
while (phonesLayout.getChildCount() > 1) {
/*while (phonesLayout.getChildCount() > 1) { // Nous avons une ligne d'ajout en plus du premier numéro
phonesLayout.removeViewAt(1);
}
}*/
if (phoneNumbersList != null) {
for (String phoneNumber : phoneNumbersList) {
addPhoneNumberToLayout(phoneNumber);
Log.i("RestoredList", "Numéro ajouté: "+phoneNumber);
}
}
}
/**
* Restaure les données utilisateur depuis SharedPreferences.
*/
......@@ -338,21 +353,38 @@ public class EditUserFragment extends Fragment {
super.onSaveInstanceState(outState);
// Sauvegarde les valeurs avant la rotation
outState.putString(ARG_FIRSTNAME, firstNameEditText.getText().toString());
outState.putString(ARG_LASTNAME, lastNameEditText.getText().toString());
outState.putString(ARG_BIRTHCITY, birthCityEditText.getText().toString());
outState.putString(ARG_BIRTHDATE, birthDateButton.getText().toString());
outState.putInt(ARG_BIRTHDEPT, birthDeptSpinner.getSelectedItemPosition());
if (firstNameEditText != null) {
outState.putString(ARG_FIRSTNAME, firstNameEditText.getText().toString());
}
if (lastNameEditText != null) {
outState.putString(ARG_LASTNAME, lastNameEditText.getText().toString());
}
if (birthCityEditText != null) {
outState.putString(ARG_BIRTHCITY, birthCityEditText.getText().toString());
}
if (birthDateButton != null) {
outState.putString(ARG_BIRTHDATE, birthDateButton.getText().toString());
}
if (birthDeptSpinner != null) {
outState.putInt(ARG_BIRTHDEPT, birthDeptSpinner.getSelectedItemPosition());
}
// Récupérer les numéros affichés à l'écran
ArrayList<String> tempPhoneNumbers = new ArrayList<>();
for (int i = 0; i < phonesLayout.getChildCount(); i++) {
View phoneView = phonesLayout.getChildAt(i);
EditText phoneEditText = phoneView.findViewById(R.id.phoneNumberEditText);
if (phoneEditText != null) {
tempPhoneNumbers.add(phoneEditText.getText().toString());
if (phonesLayout != null) {
ArrayList<String> tempPhoneNumbers = new ArrayList<>();
for (int i = 1; i < phonesLayout.getChildCount(); i++) {
View phoneView = phonesLayout.getChildAt(i);
ConstraintLayout phoneLayout = (ConstraintLayout) phoneView;
EditText phoneEditText = (EditText) phoneLayout.getChildAt(1);
if (phoneEditText != null) {
String phoneNumber = phoneEditText.getText().toString();
if (!phoneNumber.isEmpty()) {
tempPhoneNumbers.add(phoneNumber);
}
}
}
Log.i("RestoredList", "onSaveInstanceState: " + tempPhoneNumbers);
outState.putStringArrayList(ARG_PHONES, tempPhoneNumbers);
}
outState.putStringArrayList(ARG_PHONES, tempPhoneNumbers);
}
}
\ No newline at end of file
......@@ -2,6 +2,8 @@ package fr.imt_atlantique.myfirstapplication;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.MenuItem;
......@@ -63,6 +65,8 @@ public class MainActivity extends AppCompatActivity implements EditUserFragment.
* Remplace dynamiquement les fragments dans `MainActivity`.
*/
private void replaceFragment(Fragment fragment, boolean addToBackStack) {
if (isFinishing() || isDestroyed()) return;
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragmentContainer, fragment);
......@@ -98,7 +102,6 @@ public class MainActivity extends AppCompatActivity implements EditUserFragment.
.commit();
}
/**
* Sauvegarde les informations utilisateur dans `SharedPreferences`.
*/
......@@ -135,8 +138,13 @@ public class MainActivity extends AppCompatActivity implements EditUserFragment.
@Override
public void onDateCancelledListener(String firstName, String lastName, String birthCity, int birthDept, ArrayList<String> phoneNumbers, String previousBirthDate) {
// cancel the date picking, go back to the previous fragment
showEditFragment(firstName, lastName, birthCity, birthDept, phoneNumbers, previousBirthDate);
if (!isFinishing() && !isDestroyed()) {
new Handler(Looper.getMainLooper()).post(() -> {
if (!isFinishing() && !isDestroyed()) {
showEditFragment(firstName, lastName, birthCity, birthDept, phoneNumbers, previousBirthDate);
}
});
}
}
private void showEditFragment(String firstName, String lastName, String birthCity, int birthDept, ArrayList<String> phoneNumbers, String previousBirthDate) {
......
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_phones"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="8dp">
......@@ -44,7 +43,7 @@
android:id="@+id/removePhoneNumberButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="16dp"
android:background="@drawable/rounded_red_button"
android:drawableTop="@android:drawable/ic_menu_delete"
android:gravity="center"
......@@ -55,6 +54,5 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/phoneNumberEditText"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
......@@ -34,4 +34,5 @@
<string name="map_type_normal">Version normale</string>
<string name="map_type_hybrid">Version hybride</string>
<string name="map_type_satellite">Version satellite</string>
<string name="date_in_future">The date can not be in the future.</string>
</resources>
\ No newline at end of file
......@@ -31,8 +31,8 @@
<string name="open_wikipedia">Open Wikipedia</string>
<string name="share">Share</string>
<string name="error_first_name_empty">The first name can not be empty.</string>
<string name="map_type_normal">Normal version</string>
<string name="map_type_hybrid">Hydrid version</string>
<string name="map_type_satellite">Satellite version</string>
<string name="date_in_future">The date can not be in the future.</string>
</resources>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment