From 1c30f0e6c694770086f3903c7b1023dbde85301e Mon Sep 17 00:00:00 2001 From: Paul Akhamiogu Date: Tue, 10 Aug 2021 14:15:46 +0100 Subject: [PATCH] Add flag to account for missing year for imported contact - this avoids wrongly calculating the age/anniversary years when there was no year specified in the contact data --- .../calendar/pro/activities/MainActivity.kt | 9 ++++++--- .../calendar/pro/helpers/Constants.kt | 11 +++++++++++ .../calendar/pro/helpers/EventsHelper.kt | 9 +++++---- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt index 3a64d018a..c8f1aa809 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt @@ -606,6 +606,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { val selectionArgs = arrayOf(CommonDataKinds.Event.CONTENT_ITEM_TYPE, type.toString()) val dateFormats = getDateFormats() + val yearDateFormats = getDateFormatsWithYear() val existingEvents = if (birthdays) eventsDB.getBirthdays() else eventsDB.getAnniversaries() val importIDs = HashMap() existingEvents.forEach { @@ -624,15 +625,17 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { try { val formatter = SimpleDateFormat(format, Locale.getDefault()) val date = formatter.parse(startDate) - if (date.year < 70) { - date.year = 70 + val flags = if(format in yearDateFormats){ + FLAG_ALL_DAY + }else { + FLAG_ALL_DAY or FLAG_MISSING_YEAR_EVENT } val timestamp = date.time / 1000L val lastUpdated = cursor.getLongValue(CommonDataKinds.Event.CONTACT_LAST_UPDATED_TIMESTAMP) val event = Event( null, timestamp, timestamp, name, reminder1Minutes = reminders[0], reminder2Minutes = reminders[1], - reminder3Minutes = reminders[2], importId = contactId, timeZone = DateTimeZone.getDefault().id, flags = FLAG_ALL_DAY, + reminder3Minutes = reminders[2], importId = contactId, timeZone = DateTimeZone.getDefault().id, flags = flags, repeatInterval = YEAR, repeatRule = REPEAT_SAME_DAY, eventType = eventTypeId, source = source, lastUpdated = lastUpdated ) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt index 2a40446d7..c9d576098 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt @@ -93,6 +93,7 @@ const val REPEAT_ORDER_WEEKDAY = 4 // i.e. every 4th sunday // special event flags const val FLAG_ALL_DAY = 1 const val FLAG_IS_PAST_EVENT = 2 +const val FLAG_MISSING_YEAR_EVENT = 4 // constants related to ICS file exporting / importing const val BEGIN_CALENDAR = "BEGIN:VCALENDAR" @@ -174,3 +175,13 @@ fun isWeekend(i: Int, isSundayFirst: Boolean): Boolean { i == 5 || i == 6 || i == 12 || i == 13 } } + +fun getDateFormatsWithYear() = arrayListOf( + "yyyy-MM-dd", + "yyyyMMdd", + "yyyy.MM.dd", + "yy-MM-dd", + "yyMMdd", + "yy.MM.dd", + "yy/MM/dd", +) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt index c958e58c1..f32101a5e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt @@ -307,11 +307,12 @@ class EventsHelper(val context: Context) { (anniversaryEventId != -1L && it.eventType == anniversaryEventId)){ val eventStartDate = Formatter.getDateFromTS(it.startTS) val originalEventStartDate = Formatter.getDateFromTS(originalEvent.startTS) - val years = (eventStartDate.year - originalEventStartDate.year).coerceAtLeast(0) - if(years > 0){ - it.title = "${it.title} ($years)" + if(it.flags and FLAG_MISSING_YEAR_EVENT == 0){ + val years = (eventStartDate.year - originalEventStartDate.year).coerceAtLeast(0) + if(years > 0){ + it.title = "${it.title} ($years)" + } } - } it.color = eventTypeColors.get(it.eventType) ?: config.primaryColor }