|
|
|
@ -1,12 +1,15 @@
|
|
|
|
package org.tasks.activities;
|
|
|
|
package org.tasks.activities;
|
|
|
|
|
|
|
|
|
|
|
|
import static com.google.common.collect.Lists.transform;
|
|
|
|
import static com.google.common.collect.Lists.transform;
|
|
|
|
|
|
|
|
import static org.tasks.PermissionUtil.verifyPermissions;
|
|
|
|
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.Dialog;
|
|
|
|
import android.app.Dialog;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
|
|
|
import android.support.v7.app.AlertDialog;
|
|
|
|
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
import android.widget.Toast;
|
|
|
|
import com.google.common.base.Strings;
|
|
|
|
import com.google.common.base.Strings;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
@ -18,20 +21,25 @@ import org.tasks.calendars.CalendarProvider;
|
|
|
|
import org.tasks.dialogs.DialogBuilder;
|
|
|
|
import org.tasks.dialogs.DialogBuilder;
|
|
|
|
import org.tasks.injection.DialogFragmentComponent;
|
|
|
|
import org.tasks.injection.DialogFragmentComponent;
|
|
|
|
import org.tasks.injection.InjectingDialogFragment;
|
|
|
|
import org.tasks.injection.InjectingDialogFragment;
|
|
|
|
|
|
|
|
import org.tasks.preferences.FragmentPermissionRequestor;
|
|
|
|
import org.tasks.preferences.PermissionChecker;
|
|
|
|
import org.tasks.preferences.PermissionChecker;
|
|
|
|
|
|
|
|
import org.tasks.preferences.PermissionRequestor;
|
|
|
|
import org.tasks.themes.Theme;
|
|
|
|
import org.tasks.themes.Theme;
|
|
|
|
import org.tasks.ui.SingleCheckedArrayAdapter;
|
|
|
|
import org.tasks.ui.SingleCheckedArrayAdapter;
|
|
|
|
|
|
|
|
|
|
|
|
public class CalendarSelectionDialog extends InjectingDialogFragment {
|
|
|
|
public class CalendarSelectionDialog extends InjectingDialogFragment {
|
|
|
|
|
|
|
|
|
|
|
|
private static final String EXTRA_SELECTED = "extra_selected";
|
|
|
|
private static final String EXTRA_SELECTED = "extra_selected";
|
|
|
|
|
|
|
|
private final List<String> calendarNames = new ArrayList<>();
|
|
|
|
private final List<AndroidCalendar> calendars = new ArrayList<>();
|
|
|
|
private final List<AndroidCalendar> calendars = new ArrayList<>();
|
|
|
|
@Inject DialogBuilder dialogBuilder;
|
|
|
|
@Inject DialogBuilder dialogBuilder;
|
|
|
|
@Inject CalendarProvider calendarProvider;
|
|
|
|
@Inject CalendarProvider calendarProvider;
|
|
|
|
@Inject PermissionChecker permissionChecker;
|
|
|
|
@Inject PermissionChecker permissionChecker;
|
|
|
|
|
|
|
|
@Inject FragmentPermissionRequestor permissionRequestor;
|
|
|
|
@Inject Theme theme;
|
|
|
|
@Inject Theme theme;
|
|
|
|
private CalendarSelectionHandler handler;
|
|
|
|
private CalendarSelectionHandler handler;
|
|
|
|
private SingleCheckedArrayAdapter adapter;
|
|
|
|
private SingleCheckedArrayAdapter adapter;
|
|
|
|
|
|
|
|
private ListView listView;
|
|
|
|
|
|
|
|
|
|
|
|
public static CalendarSelectionDialog newCalendarSelectionDialog(String selected) {
|
|
|
|
public static CalendarSelectionDialog newCalendarSelectionDialog(String selected) {
|
|
|
|
CalendarSelectionDialog dialog = new CalendarSelectionDialog();
|
|
|
|
CalendarSelectionDialog dialog = new CalendarSelectionDialog();
|
|
|
|
@ -44,20 +52,8 @@ public class CalendarSelectionDialog extends InjectingDialogFragment {
|
|
|
|
@NonNull
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
|
|
Bundle arguments = getArguments();
|
|
|
|
|
|
|
|
String selected = arguments.getString(EXTRA_SELECTED);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
theme.applyToContext(getActivity());
|
|
|
|
theme.applyToContext(getActivity());
|
|
|
|
|
|
|
|
|
|
|
|
int selectedIndex = -1;
|
|
|
|
|
|
|
|
calendars.clear();
|
|
|
|
|
|
|
|
calendars.addAll(calendarProvider.getCalendars());
|
|
|
|
|
|
|
|
if (calendars.isEmpty()) {
|
|
|
|
|
|
|
|
Toast.makeText(getActivity(), R.string.no_calendars_found, Toast.LENGTH_LONG).show();
|
|
|
|
|
|
|
|
handler.cancel();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
calendars.add(0, new AndroidCalendar(null, getString(R.string.dont_add_to_calendar), -1));
|
|
|
|
|
|
|
|
List<String> calendarNames = transform(calendars, AndroidCalendar::getName);
|
|
|
|
|
|
|
|
adapter =
|
|
|
|
adapter =
|
|
|
|
new SingleCheckedArrayAdapter(getActivity(), calendarNames, theme.getThemeAccent()) {
|
|
|
|
new SingleCheckedArrayAdapter(getActivity(), calendarNames, theme.getThemeAccent()) {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
@ -70,17 +66,40 @@ public class CalendarSelectionDialog extends InjectingDialogFragment {
|
|
|
|
return calendars.get(position).getColor();
|
|
|
|
return calendars.get(position).getColor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
selectedIndex = Strings.isNullOrEmpty(selected) ? 0 : calendarNames.indexOf(selected);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return dialogBuilder
|
|
|
|
AlertDialog dialog = dialogBuilder
|
|
|
|
.newDialog()
|
|
|
|
.newDialog()
|
|
|
|
.setSingleChoiceItems(
|
|
|
|
.setSingleChoiceItems(
|
|
|
|
adapter,
|
|
|
|
adapter, -1, (d, which) -> handler.selectedCalendar(calendars.get(which)))
|
|
|
|
selectedIndex,
|
|
|
|
|
|
|
|
(dialog, which) -> handler.selectedCalendar(calendars.get(which)))
|
|
|
|
|
|
|
|
.setOnDismissListener(dialogInterface -> handler.cancel())
|
|
|
|
.setOnDismissListener(dialogInterface -> handler.cancel())
|
|
|
|
.show();
|
|
|
|
.show();
|
|
|
|
|
|
|
|
listView = dialog.getListView();
|
|
|
|
|
|
|
|
if (permissionChecker.canAccessCalendars()) {
|
|
|
|
|
|
|
|
loadCalendars();
|
|
|
|
|
|
|
|
} else if (savedInstanceState == null) {
|
|
|
|
|
|
|
|
permissionRequestor.requestCalendarPermissions();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return dialog;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void loadCalendars() {
|
|
|
|
|
|
|
|
calendars.clear();
|
|
|
|
|
|
|
|
calendarNames.clear();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
calendars.addAll(calendarProvider.getCalendars());
|
|
|
|
|
|
|
|
if (calendars.isEmpty()) {
|
|
|
|
|
|
|
|
Toast.makeText(getActivity(), R.string.no_calendars_found, Toast.LENGTH_LONG).show();
|
|
|
|
|
|
|
|
handler.cancel();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
calendars.add(0, new AndroidCalendar(null, getString(R.string.dont_add_to_calendar), -1));
|
|
|
|
|
|
|
|
calendarNames.addAll(transform(calendars, AndroidCalendar::getName));
|
|
|
|
|
|
|
|
Bundle arguments = getArguments();
|
|
|
|
|
|
|
|
String selected = arguments.getString(EXTRA_SELECTED);
|
|
|
|
|
|
|
|
int selectedIndex = Strings.isNullOrEmpty(selected) ? 0 : calendarNames.indexOf(selected);
|
|
|
|
|
|
|
|
adapter.notifyDataSetChanged();
|
|
|
|
|
|
|
|
listView.setItemChecked(selectedIndex, true);
|
|
|
|
|
|
|
|
listView.setSelection(selectedIndex);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
@ -91,20 +110,25 @@ public class CalendarSelectionDialog extends InjectingDialogFragment {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
public void onCancel(DialogInterface dialog) {
|
|
|
|
super.onResume();
|
|
|
|
super.onCancel(dialog);
|
|
|
|
|
|
|
|
|
|
|
|
if (!permissionChecker.canAccessCalendars()) {
|
|
|
|
|
|
|
|
handler.cancel();
|
|
|
|
handler.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onCancel(DialogInterface dialog) {
|
|
|
|
public void onRequestPermissionsResult(
|
|
|
|
super.onCancel(dialog);
|
|
|
|
int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
|
|
|
|
|
if (requestCode == PermissionRequestor.REQUEST_CALENDAR) {
|
|
|
|
|
|
|
|
if (verifyPermissions(grantResults)) {
|
|
|
|
|
|
|
|
loadCalendars();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
handler.cancel();
|
|
|
|
handler.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void inject(DialogFragmentComponent component) {
|
|
|
|
protected void inject(DialogFragmentComponent component) {
|
|
|
|
|