|
|
|
@ -5,6 +5,7 @@ import android.content.Context;
|
|
|
|
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.widget.ArrayAdapter;
|
|
|
|
import android.widget.Toast;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.common.base.Function;
|
|
|
|
import com.google.common.base.Function;
|
|
|
|
@ -17,70 +18,64 @@ import org.tasks.dialogs.DialogBuilder;
|
|
|
|
import org.tasks.injection.DialogFragmentComponent;
|
|
|
|
import org.tasks.injection.DialogFragmentComponent;
|
|
|
|
import org.tasks.injection.ForActivity;
|
|
|
|
import org.tasks.injection.ForActivity;
|
|
|
|
import org.tasks.injection.InjectingDialogFragment;
|
|
|
|
import org.tasks.injection.InjectingDialogFragment;
|
|
|
|
|
|
|
|
import org.tasks.preferences.FragmentPermissionRequestor;
|
|
|
|
|
|
|
|
import org.tasks.preferences.PermissionChecker;
|
|
|
|
|
|
|
|
import org.tasks.preferences.PermissionRequestor;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
|
|
|
|
import static com.google.common.collect.Lists.transform;
|
|
|
|
import static com.google.common.collect.Lists.transform;
|
|
|
|
|
|
|
|
import static org.tasks.PermissionUtil.verifyPermissions;
|
|
|
|
|
|
|
|
|
|
|
|
public class CalendarSelectionDialog extends InjectingDialogFragment {
|
|
|
|
public class CalendarSelectionDialog extends InjectingDialogFragment {
|
|
|
|
|
|
|
|
|
|
|
|
public void enableNone() {
|
|
|
|
|
|
|
|
enableNone = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
protected void inject(DialogFragmentComponent component) {
|
|
|
|
|
|
|
|
component.inject(this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public interface CalendarSelectionHandler {
|
|
|
|
public interface CalendarSelectionHandler {
|
|
|
|
void selectedCalendar(AndroidCalendar calendar);
|
|
|
|
void selectedCalendar(AndroidCalendar calendar);
|
|
|
|
void dismiss();
|
|
|
|
|
|
|
|
|
|
|
|
void cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Inject DialogBuilder dialogBuilder;
|
|
|
|
@Inject DialogBuilder dialogBuilder;
|
|
|
|
@Inject CalendarProvider calendarProvider;
|
|
|
|
@Inject CalendarProvider calendarProvider;
|
|
|
|
@Inject @ForActivity Context context;
|
|
|
|
@Inject @ForActivity Context context;
|
|
|
|
|
|
|
|
@Inject FragmentPermissionRequestor fragmentPermissionRequestor;
|
|
|
|
|
|
|
|
@Inject PermissionChecker permissionChecker;
|
|
|
|
private CalendarSelectionHandler handler;
|
|
|
|
private CalendarSelectionHandler handler;
|
|
|
|
private boolean enableNone;
|
|
|
|
private boolean enableNone;
|
|
|
|
|
|
|
|
private ArrayAdapter<String> adapter;
|
|
|
|
|
|
|
|
private final List<AndroidCalendar> calendars = new ArrayList<>();
|
|
|
|
|
|
|
|
private final List<String> calendarNames = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
|
|
final List<AndroidCalendar> calendars = calendarProvider.getCalendars();
|
|
|
|
if (savedInstanceState == null) {
|
|
|
|
List<String> calendarNames = transform(calendars, new Function<AndroidCalendar, String>() {
|
|
|
|
fragmentPermissionRequestor.requestCalendarPermissions();
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public String apply(AndroidCalendar androidCalendar) {
|
|
|
|
|
|
|
|
return androidCalendar.getName();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (calendarNames.isEmpty()) {
|
|
|
|
adapter = new ArrayAdapter<>(context, R.layout.simple_list_item_single_choice_themed, calendarNames);
|
|
|
|
Toast.makeText(context, R.string.no_calendars_found, Toast.LENGTH_LONG).show();
|
|
|
|
|
|
|
|
handler.dismiss();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AlertDialogBuilder builder = dialogBuilder.newDialog()
|
|
|
|
AlertDialogBuilder builder = dialogBuilder.newDialog()
|
|
|
|
.setItems(calendarNames, new DialogInterface.OnClickListener() {
|
|
|
|
.setAdapter(adapter, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
handler.selectedCalendar(calendars.get(which));
|
|
|
|
handler.selectedCalendar(calendars.get(which));
|
|
|
|
dialog.dismiss();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
if (enableNone) {
|
|
|
|
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
|
|
|
|
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
public void onClick(DialogInterface dialogInterface, int i) {
|
|
|
|
dialog.dismiss();
|
|
|
|
handler.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).setNeutralButton(R.string.none, new DialogInterface.OnClickListener() {
|
|
|
|
});
|
|
|
|
|
|
|
|
if (enableNone) {
|
|
|
|
|
|
|
|
builder.setNeutralButton(R.string.none, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
handler.selectedCalendar(AndroidCalendar.NONE);
|
|
|
|
handler.selectedCalendar(AndroidCalendar.NONE);
|
|
|
|
dialog.dismiss();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -89,10 +84,53 @@ public class CalendarSelectionDialog extends InjectingDialogFragment {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onDismiss(DialogInterface dialog) {
|
|
|
|
public void onResume() {
|
|
|
|
super.onDismiss(dialog);
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (permissionChecker.canAccessCalendars()) {
|
|
|
|
|
|
|
|
calendars.clear();
|
|
|
|
|
|
|
|
calendars.addAll(calendarProvider.getCalendars());
|
|
|
|
|
|
|
|
calendarNames.clear();
|
|
|
|
|
|
|
|
calendarNames.addAll(transform(calendars, new Function<AndroidCalendar, String>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public String apply(AndroidCalendar androidCalendar) {
|
|
|
|
|
|
|
|
return androidCalendar.getName();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (calendarNames.isEmpty()) {
|
|
|
|
|
|
|
|
Toast.makeText(context, R.string.no_calendars_found, Toast.LENGTH_LONG).show();
|
|
|
|
|
|
|
|
handler.cancel();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
adapter.notifyDataSetChanged();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void onCancel(DialogInterface dialog) {
|
|
|
|
|
|
|
|
super.onCancel(dialog);
|
|
|
|
|
|
|
|
|
|
|
|
handler.dismiss();
|
|
|
|
handler.cancel();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
|
|
|
|
|
if (requestCode == PermissionRequestor.REQUEST_CALENDAR) {
|
|
|
|
|
|
|
|
if (grantResults.length > 0 && !verifyPermissions(grantResults)) {
|
|
|
|
|
|
|
|
handler.cancel();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
protected void inject(DialogFragmentComponent component) {
|
|
|
|
|
|
|
|
component.inject(this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void enableNone() {
|
|
|
|
|
|
|
|
enableNone = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setCalendarSelectionHandler(CalendarSelectionHandler handler) {
|
|
|
|
public void setCalendarSelectionHandler(CalendarSelectionHandler handler) {
|
|
|
|
|