From ff171d402252bef0d40e78c6f9db39f2bfd5f57c Mon Sep 17 00:00:00 2001 From: Tommaso Berlose Date: Wed, 13 May 2020 18:57:44 +0200 Subject: [PATCH] Update the events selector. Fix #95 --- .../anotherwidget/db/EventRepository.kt | 11 +++--- .../anotherwidget/global/Preferences.kt | 2 +- .../anotherwidget/helpers/CalendarHelper.kt | 14 +++++-- .../anotherwidget/helpers/IntentHelper.kt | 37 +++++++++++++++---- 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/db/EventRepository.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/db/EventRepository.kt index 282a04b..a7a86a1 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/db/EventRepository.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/db/EventRepository.kt @@ -16,17 +16,13 @@ class EventRepository(val context: Context) { private val realm by lazy { Realm.getDefaultInstance() } fun saveEvents(eventList: ArrayList) { - realm.executeTransactionAsync { realm -> + realm.executeTransaction { realm -> realm.where(Event::class.java).findAll().deleteAllFromRealm() realm.copyToRealm(eventList) } } fun resetNextEventData() { - realm.executeTransactionAsync { - it.where(Event::class.java).findAll().deleteAllFromRealm() - } - Preferences.bulk { remove(Preferences::nextEventId) remove(Preferences::nextEventName) @@ -95,8 +91,13 @@ class EventRepository(val context: Context) { fun getEvents(): RealmResults { val now = Calendar.getInstance().timeInMillis + realm.refresh() return realm.where(Event::class.java).greaterThan("endDate", now).findAll() } fun getEventsCount(): Int = getEvents().size + + fun close() { + realm.close() + } } \ No newline at end of file diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/global/Preferences.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/global/Preferences.kt index 59befaf..e2a8182 100755 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/global/Preferences.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/global/Preferences.kt @@ -30,7 +30,7 @@ object Preferences : KotprefModel() { var customLocationLon by stringPref(key = "PREF_CUSTOM_LOCATION_LON", default = "") var customLocationAdd by stringPref(key = "PREF_CUSTOM_LOCATION_ADD", default = "") var dateFormat by stringPref(default = "") - var isDateCapitalize by booleanPref(default = false) + var isDateCapitalize by booleanPref(default = true) var weatherRefreshPeriod by intPref(key = "PREF_WEATHER_REFRESH_PERIOD", default = 1) var showUntil by intPref(key = "PREF_SHOW_UNTIL", default = 1) var calendarAppName by stringPref(key = "PREF_CALENDAR_APP_NAME", default = "") diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/CalendarHelper.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/CalendarHelper.kt index ddc7e0a..a140935 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/CalendarHelper.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/CalendarHelper.kt @@ -31,6 +31,12 @@ object CalendarHelper { val eventList = ArrayList() val now = Calendar.getInstance() + val begin = Calendar.getInstance().apply { + set(Calendar.MILLISECOND, 0) + set(Calendar.SECOND, 0) + set(Calendar.MINUTE, 0) + set(Calendar.HOUR_OF_DAY, 0) + } val limit = Calendar.getInstance() when (Preferences.showUntil) { 0 -> limit.add(Calendar.HOUR, 3) @@ -45,7 +51,7 @@ object CalendarHelper { } val builder = CalendarContract.Instances.CONTENT_URI.buildUpon() - ContentUris.appendId(builder, now.timeInMillis) + ContentUris.appendId(builder, begin.timeInMillis) ContentUris.appendId(builder, limit.timeInMillis) if (!context.checkGrantedPermission( @@ -56,14 +62,14 @@ object CalendarHelper { } else { try { val provider = CalendarProvider(context) - val data = provider.getInstances(now.timeInMillis, limit.timeInMillis) + val data = provider.getInstances(begin.timeInMillis, limit.timeInMillis) if (data != null) { val instances = data.list for (instance in instances) { try { val e = provider.getEvent(instance.eventId) - Log.d("ciao", "evento: $e") - if (e != null && !e.deleted && instance.begin <= limit.timeInMillis && (Preferences.calendarAllDay || !e.allDay) && !getFilteredCalendarIdList().contains( + Log.d("ciao", "evento: $instance") + if (e != null && !e.deleted && instance.begin <= limit.timeInMillis && now.timeInMillis < instance.end && (Preferences.calendarAllDay || !e.allDay) && !getFilteredCalendarIdList().contains( e.calendarId ) && (Preferences.showDeclinedEvents || e.selfAttendeeStatus.toInt() != CalendarContract.Attendees.ATTENDEE_STATUS_DECLINED) ) { diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/IntentHelper.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/IntentHelper.kt index 3fa68e7..d122242 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/IntentHelper.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/IntentHelper.kt @@ -114,19 +114,40 @@ object IntentHelper { if (Preferences.calendarAppPackage == "") { Intent(Intent.ACTION_VIEW).apply { data = uri - putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, e.startDate) - putExtra(CalendarContract.EXTRA_EVENT_END_TIME, e.endDate) -// putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, if (e.allDay) 1 else 0) -// type = "vnd.android.cursor.item/event" + if (!e.allDay) { + putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, e.startDate) + putExtra(CalendarContract.EXTRA_EVENT_END_TIME, e.endDate) + } else { + val start = Calendar.getInstance().apply { + timeInMillis = e.startDate + } + val end = Calendar.getInstance().apply { + timeInMillis = e.endDate + } + + putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, e.startDate + start.timeZone.getOffset(start.timeInMillis)) + putExtra(CalendarContract.EXTRA_EVENT_END_TIME, e.endDate + end.timeZone.getOffset(end.timeInMillis)) + putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, 1) + } } } else { getCalendarIntent(context).apply { action = Intent.ACTION_VIEW data = uri - putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, e.startDate) - putExtra(CalendarContract.EXTRA_EVENT_END_TIME, e.endDate) -// putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, if (e.allDay) 1 else 0) -// type = "vnd.android.cursor.item/event" + if (!e.allDay) { + putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, e.startDate) + putExtra(CalendarContract.EXTRA_EVENT_END_TIME, e.endDate) + } else { + val start = Calendar.getInstance().apply { + timeInMillis = e.startDate + } + val end = Calendar.getInstance().apply { + timeInMillis = e.endDate + } + putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, start.timeInMillis + start.timeZone.getOffset(start.timeInMillis)) + putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end.timeInMillis + end.timeZone.getOffset(end.timeInMillis)) + putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, 1) + } } } }