diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt index 0a10696a0..63aca978a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt @@ -246,18 +246,65 @@ class EventActivity : SimpleActivity() { return true } + private fun getStartEndTimes(): Pair { + val offset = if (!config.allowChangingTimeZones || mEvent.getTimeZoneString().equals(mOriginalTimeZone, true)) { + 0 + } else { + val original = if (mOriginalTimeZone.isEmpty()) DateTimeZone.getDefault().id else mOriginalTimeZone + (DateTimeZone.forID(mEvent.getTimeZoneString()).getOffset(System.currentTimeMillis()) - DateTimeZone.forID(original).getOffset(System.currentTimeMillis())) / 1000L + } + + val newStartTS = mEventStartDateTime.withSecondOfMinute(0).withMillisOfSecond(0).seconds() - offset + val newEndTS = mEventEndDateTime.withSecondOfMinute(0).withMillisOfSecond(0).seconds() - offset + return Pair(newStartTS, newEndTS) + } + + private fun getReminders(): ArrayList { + var reminders = arrayListOf( + Reminder(mReminder1Minutes, mReminder1Type), + Reminder(mReminder2Minutes, mReminder2Type), + Reminder(mReminder3Minutes, mReminder3Type) + ) + reminders = reminders.filter { it.minutes != REMINDER_OFF }.sortedBy { it.minutes }.toMutableList() as ArrayList + return reminders + } + + private fun isEventChanged(): Boolean { + var newStartTS = 0L + var newEndTS = 0L + getStartEndTimes().apply { + newStartTS = first + newEndTS = second + } + + val reminders = getReminders() + if (event_title.value != mEvent.title || + event_location.value != mEvent.location || + event_description.value != mEvent.location || + newStartTS != mEvent.startTS || + newEndTS != mEvent.endTS || + event_time_zone.text != mEvent.getTimeZoneString() || + reminders != mEvent.getReminders() || + mRepeatInterval != mEvent.repeatInterval || + mRepeatRule != mEvent.repeatRule || + mEventTypeId != mEvent.eventType) { + return true + } + return false + } + override fun onBackPressed() { - CloseEditingDialog(this) { - ensureBackgroundThread { + if (isEventChanged()) { + CloseEditingDialog(this) { when (it) { CLOSE_WITHOUT_SAVING -> { - runOnUiThread { - finish() - } + finish() } - SAVE_AND_CLOSE -> saveEvent() + SAVE_AND_CLOSE -> saveCurrentEvent() } } + } else { + super.onBackPressed() } } @@ -318,7 +365,7 @@ class EventActivity : SimpleActivity() { mRepeatLimit = getLong(REPEAT_LIMIT) mAttendees = Gson().fromJson>(getString(ATTENDEES), object : TypeToken>() {}.type) - ?: ArrayList() + ?: ArrayList() mEventTypeId = getLong(EVENT_TYPE_ID) mEventCalendarId = getInt(EVENT_CALENDAR_ID) @@ -386,7 +433,8 @@ class EventActivity : SimpleActivity() { mRepeatRule = mEvent.repeatRule mEventTypeId = mEvent.eventType mEventCalendarId = mEvent.getCalDAVCalendarId() - mAttendees = Gson().fromJson>(mEvent.attendees, object : TypeToken>() {}.type) ?: ArrayList() + mAttendees = Gson().fromJson>(mEvent.attendees, object : TypeToken>() {}.type) + ?: ArrayList() checkRepeatTexts(mRepeatInterval) checkAttendees() } @@ -396,7 +444,8 @@ class EventActivity : SimpleActivity() { event_title.requestFocus() updateActionBarTitle(getString(R.string.new_event)) if (config.defaultEventTypeId != -1L) { - config.lastUsedCaldavCalendarId = mStoredEventTypes.firstOrNull { it.id == config.defaultEventTypeId }?.caldavCalendarId ?: 0 + config.lastUsedCaldavCalendarId = mStoredEventTypes.firstOrNull { it.id == config.defaultEventTypeId }?.caldavCalendarId + ?: 0 } val isLastCaldavCalendarOK = config.caldavSync && config.getSyncedCalendarIdsAsList().contains(config.lastUsedCaldavCalendarId) @@ -749,8 +798,8 @@ class EventActivity : SimpleActivity() { private fun showReminderTypePicker(currentValue: Int, callback: (Int) -> Unit) { val items = arrayListOf( - RadioItem(REMINDER_NOTIFICATION, getString(R.string.notification)), - RadioItem(REMINDER_EMAIL, getString(R.string.email)) + RadioItem(REMINDER_NOTIFICATION, getString(R.string.notification)), + RadioItem(REMINDER_EMAIL, getString(R.string.email)) ) RadioGroupDialog(this, items, currentValue) { callback(it as Int) @@ -826,7 +875,7 @@ class EventActivity : SimpleActivity() { private fun getCalendarId() = if (mEvent.source == SOURCE_SIMPLE_CALENDAR) config.lastUsedCaldavCalendarId else mEvent.getCalDAVCalendarId() private fun getCalendarWithId(calendars: List, calendarId: Int): CalDAVCalendar? = - calendars.firstOrNull { it.id == calendarId } + calendars.firstOrNull { it.id == calendarId } private fun updateCurrentCalendarInfo(currentCalendar: CalDAVCalendar?) { event_type_image.beVisibleIf(currentCalendar == null) @@ -850,7 +899,8 @@ class EventActivity : SimpleActivity() { event_caldav_calendar_email.text = currentCalendar.accountName ensureBackgroundThread { - val calendarColor = eventsHelper.getEventTypeWithCalDAVCalendarId(currentCalendar.id)?.color ?: currentCalendar.color + val calendarColor = eventsHelper.getEventTypeWithCalDAVCalendarId(currentCalendar.id)?.color + ?: currentCalendar.color runOnUiThread { event_caldav_calendar_color.setFillWithStroke(calendarColor, config.backgroundColor) @@ -869,8 +919,8 @@ class EventActivity : SimpleActivity() { private fun resetTime() { if (mEventEndDateTime.isBefore(mEventStartDateTime) && - mEventStartDateTime.dayOfMonth() == mEventEndDateTime.dayOfMonth() && - mEventStartDateTime.monthOfYear() == mEventEndDateTime.monthOfYear()) { + mEventStartDateTime.dayOfMonth() == mEventEndDateTime.dayOfMonth() && + mEventStartDateTime.monthOfYear() == mEventEndDateTime.monthOfYear()) { mEventEndDateTime = mEventEndDateTime.withTime(mEventStartDateTime.hourOfDay, mEventStartDateTime.minuteOfHour, mEventStartDateTime.secondOfMinute, 0) updateEndTimeText() @@ -935,16 +985,13 @@ class EventActivity : SimpleActivity() { return } - val offset = if (!config.allowChangingTimeZones || mEvent.getTimeZoneString().equals(mOriginalTimeZone, true)) { - 0 - } else { - val original = if (mOriginalTimeZone.isEmpty()) DateTimeZone.getDefault().id else mOriginalTimeZone - (DateTimeZone.forID(mEvent.getTimeZoneString()).getOffset(System.currentTimeMillis()) - DateTimeZone.forID(original).getOffset(System.currentTimeMillis())) / 1000L + var newStartTS = 0L + var newEndTS = 0L + getStartEndTimes().apply { + newStartTS = first + newEndTS = second } - val newStartTS = mEventStartDateTime.withSecondOfMinute(0).withMillisOfSecond(0).seconds() - offset - val newEndTS = mEventEndDateTime.withSecondOfMinute(0).withMillisOfSecond(0).seconds() - offset - if (newStartTS > newEndTS) { toast(R.string.end_before_start) return @@ -966,7 +1013,8 @@ class EventActivity : SimpleActivity() { } } - eventsHelper.getEventTypeWithCalDAVCalendarId(mEventCalendarId)?.id ?: config.lastUsedLocalEventTypeId + eventsHelper.getEventTypeWithCalDAVCalendarId(mEventCalendarId)?.id + ?: config.lastUsedLocalEventTypeId } val newSource = if (!config.caldavSync || mEventCalendarId == STORED_LOCALLY_ONLY) { @@ -976,13 +1024,7 @@ class EventActivity : SimpleActivity() { "$CALDAV-$mEventCalendarId" } - var reminders = arrayListOf( - Reminder(mReminder1Minutes, mReminder1Type), - Reminder(mReminder2Minutes, mReminder2Type), - Reminder(mReminder3Minutes, mReminder3Type) - ) - reminders = reminders.filter { it.minutes != REMINDER_OFF }.sortedBy { it.minutes }.toMutableList() as ArrayList - + val reminders = getReminders() val reminder1 = reminders.getOrNull(0) ?: Reminder(REMINDER_OFF, REMINDER_NOTIFICATION) val reminder2 = reminders.getOrNull(1) ?: Reminder(REMINDER_OFF, REMINDER_NOTIFICATION) val reminder3 = reminders.getOrNull(2) ?: Reminder(REMINDER_OFF, REMINDER_NOTIFICATION) @@ -1153,7 +1195,7 @@ class EventActivity : SimpleActivity() { hideKeyboard() config.backgroundColor.getContrastColor() val datepicker = DatePickerDialog(this, mDialogTheme, startDateSetListener, mEventStartDateTime.year, mEventStartDateTime.monthOfYear - 1, - mEventStartDateTime.dayOfMonth) + mEventStartDateTime.dayOfMonth) datepicker.datePicker.firstDayOfWeek = if (config.isSundayFirst) Calendar.SUNDAY else Calendar.MONDAY datepicker.show() @@ -1167,7 +1209,7 @@ class EventActivity : SimpleActivity() { private fun setupEndDate() { hideKeyboard() val datepicker = DatePickerDialog(this, mDialogTheme, endDateSetListener, mEventEndDateTime.year, mEventEndDateTime.monthOfYear - 1, - mEventEndDateTime.dayOfMonth) + mEventEndDateTime.dayOfMonth) datepicker.datePicker.firstDayOfWeek = if (config.isSundayFirst) Calendar.SUNDAY else Calendar.MONDAY datepicker.show() @@ -1389,9 +1431,9 @@ class EventActivity : SimpleActivity() { if (attendee.isMe) { event_contact_attendee.setOnClickListener { val items = arrayListOf( - RadioItem(Attendees.ATTENDEE_STATUS_ACCEPTED, getString(R.string.going)), - RadioItem(Attendees.ATTENDEE_STATUS_DECLINED, getString(R.string.not_going)), - RadioItem(Attendees.ATTENDEE_STATUS_TENTATIVE, getString(R.string.maybe_going)) + RadioItem(Attendees.ATTENDEE_STATUS_ACCEPTED, getString(R.string.going)), + RadioItem(Attendees.ATTENDEE_STATUS_DECLINED, getString(R.string.not_going)), + RadioItem(Attendees.ATTENDEE_STATUS_TENTATIVE, getString(R.string.maybe_going)) ) RadioGroupDialog(this@EventActivity, items, attendee.status) { @@ -1464,13 +1506,13 @@ class EventActivity : SimpleActivity() { val contacts = ArrayList() val uri = Data.CONTENT_URI val projection = arrayOf( - Data.CONTACT_ID, - StructuredName.PREFIX, - StructuredName.GIVEN_NAME, - StructuredName.MIDDLE_NAME, - StructuredName.FAMILY_NAME, - StructuredName.SUFFIX, - StructuredName.PHOTO_THUMBNAIL_URI) + Data.CONTACT_ID, + StructuredName.PREFIX, + StructuredName.GIVEN_NAME, + StructuredName.MIDDLE_NAME, + StructuredName.FAMILY_NAME, + StructuredName.SUFFIX, + StructuredName.PHOTO_THUMBNAIL_URI) val selection = "${Data.MIMETYPE} = ?" val selectionArgs = arrayOf(StructuredName.CONTENT_ITEM_TYPE) @@ -1498,8 +1540,8 @@ class EventActivity : SimpleActivity() { val contacts = ArrayList() val uri = CommonDataKinds.Email.CONTENT_URI val projection = arrayOf( - Data.CONTACT_ID, - CommonDataKinds.Email.DATA + Data.CONTACT_ID, + CommonDataKinds.Email.DATA ) queryCursor(uri, projection) { cursor -> @@ -1516,7 +1558,7 @@ class EventActivity : SimpleActivity() { event_show_on_map.applyColorFilter(getAdjustedPrimaryColor()) val textColor = config.textColor arrayOf(event_time_image, event_time_zone_image, event_repetition_image, event_reminder_image, event_type_image, event_caldav_calendar_image, - event_reminder_1_type, event_reminder_2_type, event_reminder_3_type, event_attendees_image).forEach { + event_reminder_1_type, event_reminder_2_type, event_reminder_3_type, event_attendees_image).forEach { it.applyColorFilter(textColor) } }