mirror of https://github.com/tasks/tasks
Remove DatePickerActivity
parent
abc9e7bfd0
commit
32b3f15901
@ -1,66 +0,0 @@
|
|||||||
package org.tasks.activities;
|
|
||||||
|
|
||||||
import static org.tasks.time.DateTimeUtils.currentTimeMillis;
|
|
||||||
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import androidx.fragment.app.FragmentManager;
|
|
||||||
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import org.tasks.dialogs.MyDatePickerDialog;
|
|
||||||
import org.tasks.injection.ActivityComponent;
|
|
||||||
import org.tasks.injection.InjectingAppCompatActivity;
|
|
||||||
import org.tasks.preferences.Preferences;
|
|
||||||
import org.tasks.themes.ThemeAccent;
|
|
||||||
import org.tasks.themes.ThemeBase;
|
|
||||||
import org.tasks.time.DateTime;
|
|
||||||
|
|
||||||
public class DatePickerActivity extends InjectingAppCompatActivity
|
|
||||||
implements DatePickerDialog.OnDateSetListener {
|
|
||||||
|
|
||||||
public static final String EXTRA_TIMESTAMP = "extra_timestamp";
|
|
||||||
private static final String FRAG_TAG_DATE_PICKER = "frag_tag_date_picker";
|
|
||||||
@Inject ThemeBase themeBase;
|
|
||||||
@Inject ThemeAccent themeAccent;
|
|
||||||
@Inject Preferences preferences;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
long timestamp = getIntent().getLongExtra(EXTRA_TIMESTAMP, currentTimeMillis());
|
|
||||||
DateTime initial = (timestamp > 0 ? new DateTime(timestamp) : new DateTime()).startOfDay();
|
|
||||||
|
|
||||||
FragmentManager fragmentManager = getSupportFragmentManager();
|
|
||||||
MyDatePickerDialog dialog =
|
|
||||||
(MyDatePickerDialog) fragmentManager.findFragmentByTag(FRAG_TAG_DATE_PICKER);
|
|
||||||
if (dialog == null) {
|
|
||||||
dialog = new MyDatePickerDialog();
|
|
||||||
dialog.initialize(
|
|
||||||
null, initial.getYear(), initial.getMonthOfYear() - 1, initial.getDayOfMonth());
|
|
||||||
dialog.setVersion(DatePickerDialog.Version.VERSION_2);
|
|
||||||
dialog.setThemeDark(themeBase.isDarkTheme(this));
|
|
||||||
dialog.setAccentColor(themeAccent.getAccentColor());
|
|
||||||
int firstDayOfWeek = preferences.getFirstDayOfWeek();
|
|
||||||
if (firstDayOfWeek >= 1 && firstDayOfWeek <= 7) {
|
|
||||||
dialog.setFirstDayOfWeek(firstDayOfWeek);
|
|
||||||
}
|
|
||||||
dialog.show(fragmentManager, FRAG_TAG_DATE_PICKER);
|
|
||||||
}
|
|
||||||
dialog.setOnCancelListener(dialogInterface -> finish());
|
|
||||||
dialog.setOnDateSetListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void inject(ActivityComponent component) {
|
|
||||||
component.inject(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDateSet(
|
|
||||||
DatePickerDialog view, final int year, final int monthOfYear, final int dayOfMonth) {
|
|
||||||
Intent data = new Intent();
|
|
||||||
data.putExtra(EXTRA_TIMESTAMP, new DateTime(year, monthOfYear + 1, dayOfMonth).getMillis());
|
|
||||||
setResult(RESULT_OK, data);
|
|
||||||
finish();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,23 +1,90 @@
|
|||||||
package org.tasks.dialogs;
|
package org.tasks.dialogs;
|
||||||
|
|
||||||
|
import static android.app.Activity.RESULT_OK;
|
||||||
|
import static org.tasks.time.DateTimeUtils.currentTimeMillis;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.fragment.app.Fragment;
|
||||||
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;
|
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;
|
||||||
|
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog.OnDateSetListener;
|
||||||
|
import org.tasks.R;
|
||||||
|
import org.tasks.preferences.Preferences;
|
||||||
|
import org.tasks.time.DateTime;
|
||||||
|
|
||||||
|
public class MyDatePickerDialog extends DatePickerDialog implements OnDateSetListener {
|
||||||
|
|
||||||
|
public static final String EXTRA_TIMESTAMP = "extra_timestamp";
|
||||||
|
public static final int NO_DATE = -1;
|
||||||
|
|
||||||
|
public static MyDatePickerDialog newDatePicker(Fragment target, int rc, long initial) {
|
||||||
|
Bundle arguments = new Bundle();
|
||||||
|
arguments.putLong(EXTRA_TIMESTAMP, initial);
|
||||||
|
MyDatePickerDialog dialog = new MyDatePickerDialog();
|
||||||
|
dialog.setArguments(arguments);
|
||||||
|
dialog.setTargetFragment(target, rc);
|
||||||
|
return dialog;
|
||||||
|
}
|
||||||
|
|
||||||
public class MyDatePickerDialog extends DatePickerDialog {
|
public interface DatePickerCallback{ // TODO: remove this after removing DateAndTimePickerActivity
|
||||||
|
void onDatePicked(DialogInterface dialog, long timestamp);
|
||||||
|
}
|
||||||
|
|
||||||
private DialogInterface.OnCancelListener listener;
|
private DatePickerCallback callback;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setOnCancelListener(DialogInterface.OnCancelListener listener) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
this.listener = listener;
|
if (savedInstanceState == null) {
|
||||||
|
long timestamp = getArguments().getLong(EXTRA_TIMESTAMP, currentTimeMillis());
|
||||||
|
DateTime initial = (timestamp > 0 ? new DateTime(timestamp) : new DateTime()).startOfDay();
|
||||||
|
initialize(
|
||||||
|
null,
|
||||||
|
initial.getYear(),
|
||||||
|
initial.getMonthOfYear() - 1,
|
||||||
|
initial.getDayOfMonth());
|
||||||
|
setVersion(DatePickerDialog.Version.VERSION_2);
|
||||||
|
int firstDayOfWeek = new Preferences(getContext()).getFirstDayOfWeek();
|
||||||
|
if (firstDayOfWeek >= 1 && firstDayOfWeek <= 7) {
|
||||||
|
setFirstDayOfWeek(firstDayOfWeek);
|
||||||
|
}
|
||||||
|
setThemeDark(getResources().getBoolean(R.bool.is_dark)); // TODO: remove this after removing DateAndTimePickerActivity
|
||||||
|
}
|
||||||
|
|
||||||
|
setOnDateSetListener(this);
|
||||||
|
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAttach(@NonNull Context context) {
|
||||||
|
super.onAttach(context);
|
||||||
|
|
||||||
|
if (context instanceof DatePickerCallback) {
|
||||||
|
callback = (DatePickerCallback) context;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCancel(DialogInterface dialog) {
|
public void onCancel(DialogInterface dialog) {
|
||||||
super.onCancel(dialog);
|
super.onCancel(dialog);
|
||||||
|
|
||||||
if (listener != null) {
|
if (getTargetFragment() == null) {
|
||||||
listener.onCancel(dialog);
|
callback.onDatePicked(dialog, NO_DATE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
|
||||||
|
long result = new DateTime(year, monthOfYear + 1, dayOfMonth).getMillis();
|
||||||
|
if (getTargetFragment() == null) {
|
||||||
|
callback.onDatePicked(getDialog(), result);
|
||||||
|
} else {
|
||||||
|
Intent data = new Intent();
|
||||||
|
data.putExtra(EXTRA_TIMESTAMP, result);
|
||||||
|
getTargetFragment().onActivityResult(getTargetRequestCode(), RESULT_OK, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue