Skip to content
Snippets Groups Projects
Commit 74605c7e authored by COUCHET Thibaud's avatar COUCHET Thibaud
Browse files

Intent implicite pour DatePicker fix

parent 8c5d96bb
No related branches found
No related tags found
No related merge requests found
......@@ -29,7 +29,7 @@
<activity android:name=".DisplayUserActivity" android:exported="true" />
<activity android:name=".DatePickerActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PICK" />
<action android:name="fr.imt_atlantique.myfirstapplication.ACTION_PICK_DATE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
......
......@@ -41,14 +41,12 @@ public class DatePickerActivity extends AppCompatActivity {
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1;
int year = datePicker.getYear();
LocalDate date = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
date = LocalDate.of(year, month, day);
}
fr.imt_atlantique.myfirstapplication.DatePicker dateObject = new fr.imt_atlantique.myfirstapplication.DatePicker(date);
Intent resultIntent = new Intent();
resultIntent.putExtra("date", dateObject);
resultIntent.putExtra("day", day);
resultIntent.putExtra("month", month);
resultIntent.putExtra("year", year);
setResult(RESULT_OK, resultIntent);
finish();
});
......@@ -58,17 +56,21 @@ public class DatePickerActivity extends AppCompatActivity {
setResult(RESULT_CANCELED);
finish();
});
}
private void getValues() {
Intent intent = getIntent();
if (intent != null) {
fr.imt_atlantique.myfirstapplication.DatePicker date = intent.getParcelableExtra("date");
if (date != null) {
this.date = date.getDate();
int day = intent.getIntExtra("day", -1);
int month = intent.getIntExtra("month", -1);
int year = intent.getIntExtra("year", -1);
if (day != -1 && month != -1 && year != -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
datePicker.updateDate(this.date.getYear(), this.date.getMonthValue() - 1, this.date.getDayOfMonth());
date = LocalDate.of(year, month, day);
datePicker.updateDate(year, month - 1, day);
}
}
}
......
......@@ -38,7 +38,7 @@ import java.util.Set;
public class MainActivity extends AppCompatActivity {
String firstName, lastName, birthCity;
String birthDate = getString(R.string.pick_date_hint);
String birthDate;
int birthDept = 0;
ArrayList<String> phoneNumbersList = new ArrayList<>();
......@@ -59,7 +59,7 @@ public class MainActivity extends AppCompatActivity {
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
birthDate = getString(R.string.pick_date_hint);
findAllViewById();
birthDateButton.setOnClickListener(view -> showDatePicker());
restorePreferences();
......@@ -347,15 +347,22 @@ public class MainActivity extends AppCompatActivity {
}
public void showDatePicker() {
Intent intent = new Intent(MainActivity.this, DatePickerActivity.class);
Intent intent = new Intent("fr.imt_atlantique.myfirstapplication.ACTION_PICK_DATE");
if (!birthDate.equals(getString(R.string.pick_date_hint))) {
LocalDate date = null;
int day = 0, month = 0, year = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
date = LocalDate.parse(birthDate, DateTimeFormatter.ofPattern("d/M/yyyy"));
LocalDate date = LocalDate.parse(birthDate, DateTimeFormatter.ofPattern("d/M/yyyy"));
day = date.getDayOfMonth();
month = date.getMonthValue();
year = date.getYear();
}
fr.imt_atlantique.myfirstapplication.DatePicker dateObject = new fr.imt_atlantique.myfirstapplication.DatePicker(date);
intent.putExtra("date", dateObject);
// Instead of sending Parcelable, send date as primitives
intent.putExtra("day", day);
intent.putExtra("month", month);
intent.putExtra("year", year);
}
datePickerLauncher.launch(intent);
......@@ -365,10 +372,13 @@ public class MainActivity extends AppCompatActivity {
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == RESULT_OK && result.getData() != null) {
fr.imt_atlantique.myfirstapplication.DatePicker date = result.getData().getParcelableExtra("date");
if (date != null) {
int day = result.getData().getIntExtra("day", -1);
int month = result.getData().getIntExtra("month", -1);
int year = result.getData().getIntExtra("year", -1);
if (day != -1 && month != -1 && year != -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
birthDate = date.getDate().format(DateTimeFormatter.ofPattern("d/M/yyyy"));
birthDate = LocalDate.of(year, month, day).format(DateTimeFormatter.ofPattern("d/M/yyyy"));
}
birthDateButton.setText(birthDate);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment