Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit dffa3aca authored by Jonas Häusler's avatar Jonas Häusler Committed by Michael Bestas
Browse files

remove duplicated check for allowed reminders and availability

these checks seem to be unnecessary as `prepareReminders()` and `prepareAvailability()`
already remove unavailable reminder and availabilities, and while doing so checked for
bad strings delivered by the calendar provider.

by relying only on these functions, we also fix #1292, which occurred because EteSyncs
offline calendar provided said bad string for availability.

Change-Id: Ic7af69fc29f9c62ea6f3da85069db19b67134380
parent 362b9bd6
Loading
Loading
Loading
Loading
+3 −41
Original line number Diff line number Diff line
@@ -1465,9 +1465,6 @@ public class EditEventView implements View.OnClickListener, DialogInterface.OnCa
            return;
        }

        fillModelFromUI();

        // Do nothing if the selection didn't change so that reminders will not get lost
        int idColumn = c.getColumnIndexOrThrow(Calendars._ID);
        long calendarId = c.getLong(idColumn);
        int colorColumn = c.getColumnIndexOrThrow(Calendars.CALENDAR_COLOR);
@@ -1480,6 +1477,9 @@ public class EditEventView implements View.OnClickListener, DialogInterface.OnCa
            return;
        }

        // ensure model is up to date so that reminders don't get lost on calendar change
        fillModelFromUI();

        mModel.mCalendarId = calendarId;
        mModel.setCalendarColor(displayCalendarColor);
        mModel.mCalendarAccountName = c.getString(EditEventHelper.CALENDARS_INDEX_ACCOUNT_NAME);
@@ -1506,9 +1506,6 @@ public class EditEventView implements View.OnClickListener, DialogInterface.OnCa
        int allowedAvailabilityColumn = c.getColumnIndexOrThrow(Calendars.ALLOWED_AVAILABILITY);
        mModel.mCalendarAllowedAvailability = c.getString(allowedAvailabilityColumn);

        removeInvalidReminders();
        resetAvailabilityIfInvalid();

        // Update the UI elements.
        mReminderItems.clear();
        LinearLayout reminderLayout =
@@ -1520,41 +1517,6 @@ public class EditEventView implements View.OnClickListener, DialogInterface.OnCa
        prepareAccess();
    }

    /**
     * Removes reminders from {@link #mModel} that have a reminder method that does not
     * match any of {@link CalendarEventModel#mCalendarAllowedReminders}.
     *
     * Updates {@link CalendarEventModel#mHasAlarm} of {@link #mModel} accordingly.
     */
    private void removeInvalidReminders() {
        final List<Integer> allowedReminderMethods =
                Arrays.stream(mModel.mCalendarAllowedReminders.split(","))
                        .map(Integer::valueOf)
                        .collect(Collectors.toList());

        mModel.mReminders = mModel.mReminders.stream()
                .filter(reminder -> allowedReminderMethods.contains(reminder.getMethod()))
                .limit(mModel.mCalendarMaxReminders)
                .collect(Collectors.toCollection(ArrayList::new));

        mModel.mHasAlarm = mModel.mReminders.size() != 0;
    }

    /**
     * Resets {@link CalendarEventModel#mAvailability} of {@link #mModel} to {@link Events#AVAILABILITY_BUSY}
     * if the current availability does not match any of {@link CalendarEventModel#mCalendarAllowedAvailability}.
     */
    private void resetAvailabilityIfInvalid() {
        final List<Integer> allowedAvailabilities =
                Arrays.stream(mModel.mCalendarAllowedAvailability.split(","))
                        .map(Integer::valueOf)
                        .collect(Collectors.toList());

        if (!allowedAvailabilities.contains(mModel.mAvailability)) {
            mModel.mAvailability = Events.AVAILABILITY_BUSY;
        }
    }

    /**
     * Checks if the start and end times for this event should be displayed in
     * the Calendar app's time zone as well and formats and displays them.