Skip to content

Commit

Permalink
Fixing an issue with retrieving events by id
Browse files Browse the repository at this point in the history
  • Loading branch information
mkieres authored and nickrandolph committed Jun 14, 2018
1 parent 10ecb30 commit 7a46942
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import com.builttoroam.devicecalendar.common.Constants.Companion.EVENT_PROJECTIO
import com.builttoroam.devicecalendar.common.ErrorCodes.Companion.GENERIC_ERROR
import com.builttoroam.devicecalendar.common.ErrorCodes.Companion.NOT_ALLOWED
import com.builttoroam.devicecalendar.common.ErrorCodes.Companion.NOT_AUTHORIZED
import com.builttoroam.devicecalendar.common.ErrorMessages
import com.builttoroam.devicecalendar.common.ErrorMessages.Companion.CALENDAR_ID_INVALID_ARGUMENT_NOT_A_NUMBER_MESSAGE
import com.builttoroam.devicecalendar.common.ErrorMessages.Companion.CREATE_EVENT_ARGUMENTS_NOT_VALID_MESSAGE
import com.builttoroam.devicecalendar.common.ErrorMessages.Companion.DELETING_RECURRING_EVENT_NOT_SUPPORTED_MESSAGE
Expand Down Expand Up @@ -229,7 +230,7 @@ public class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener
}
} else {
if (!isInternalCall) {
finishWithError(NOT_FOUND, "The calendar with the ID ${calendarId} could not be found", pendingChannelResult)
finishWithError(NOT_FOUND, "The calendar with the ID $calendarId could not be found", pendingChannelResult)
}
}
} catch (e: Exception) {
Expand All @@ -246,24 +247,29 @@ public class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener
}

@SuppressLint("MissingPermission")
public fun retrieveEvents(calendarId: String, startDate: Long, endDate: Long, eventIds: List<String>, pendingChannelResult: MethodChannel.Result) {
public fun retrieveEvents(calendarId: String, startDate: Long?, endDate: Long?, eventIds: List<String>, pendingChannelResult: MethodChannel.Result) {
if (startDate == null && endDate == null && eventIds.isEmpty()) {
finishWithError(INVALID_ARGUMENT, ErrorMessages.RETRIEVE_EVENTS_ARGUMENTS_NOT_VALID_MESSAGE, pendingChannelResult)
return
}

if (arePermissionsGranted()) {
val calendar = retrieveCalendar(calendarId, pendingChannelResult, true)
if (calendar == null) {
finishWithError(NOT_FOUND, "Couldn't retrieve the Calendar with ID ${calendarId}", pendingChannelResult)
finishWithError(NOT_FOUND, "Couldn't retrieve the Calendar with ID $calendarId", pendingChannelResult)
return
}

val contentResolver: ContentResolver? = _context?.getContentResolver()

val eventsUriBuilder = CalendarContract.Instances.CONTENT_URI.buildUpon()
ContentUris.appendId(eventsUriBuilder, startDate)
ContentUris.appendId(eventsUriBuilder, endDate)
ContentUris.appendId(eventsUriBuilder, startDate ?: Date(0).time)
ContentUris.appendId(eventsUriBuilder, endDate ?: Date(Long.MAX_VALUE).time)

val eventsUri = eventsUriBuilder.build()
val eventsCalendarQuery = "(${CalendarContract.Events.CALENDAR_ID} = ${calendarId})"
val eventsCalendarQuery = "(${CalendarContract.Events.CALENDAR_ID} = $calendarId)"
val eventsNotDeletedQuery = "(${CalendarContract.Events.DELETED} != 1)"
val eventsIdsQueryElements = eventIds.map { "(${CalendarContract.Instances.EVENT_ID} = ${it})" }
val eventsIdsQueryElements = eventIds.map { "(${CalendarContract.Instances.EVENT_ID} = $it)" }
val eventsIdsQuery = eventsIdsQueryElements.joinToString(" OR ")

var eventsSelectionQuery = "$eventsCalendarQuery AND $eventsNotDeletedQuery"
Expand Down Expand Up @@ -353,12 +359,12 @@ public class CalendarDelegate : PluginRegistry.RequestPermissionsResultListener
if (arePermissionsGranted()) {
var existingCal = retrieveCalendar(calendarId, pendingChannelResult, true)
if (existingCal == null) {
finishWithError(NOT_FOUND, "The calendar with the ID ${calendarId} could not be found", pendingChannelResult)
finishWithError(NOT_FOUND, "The calendar with the ID $calendarId could not be found", pendingChannelResult)
return
}

if (existingCal.isReadOnly) {
finishWithError(NOT_ALLOWED, "Calendar with ID ${calendarId} is read-only", pendingChannelResult)
finishWithError(NOT_ALLOWED, "Calendar with ID $calendarId is read-only", pendingChannelResult)
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.builttoroam.devicecalendar.common
class ErrorMessages {
companion object {
const val CALENDAR_ID_INVALID_ARGUMENT_NOT_A_NUMBER_MESSAGE: String = "Calendar ID is not a number"
const val RETRIEVE_EVENTS_ARGUMENTS_NOT_VALID_MESSAGE: String = "Provided arguments (i.e. start, end and event ids) are null or emtpy"
const val CREATE_EVENT_ARGUMENTS_NOT_VALID_MESSAGE: String = "Some of the event arguments are not valid"
const val DELETING_RECURRING_EVENT_NOT_SUPPORTED_MESSAGE: String = "Currently, deleting of recurring events is not supported"
const val NOT_AUTHORIZED_MESSAGE: String = "The user has not allowed this application to modify their calendar(s)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import io.flutter.plugin.common.MethodChannel
class CalendarMethodsParametersCacheModel(val pendingChannelResult: MethodChannel.Result,
val calendarDelegateMethodCode: Int,
var calendarId: String = "",
var calendarEventsStartDate: Long = -1,
var calendarEventsEndDate: Long = -1,
var calendarEventsStartDate: Long? = null,
var calendarEventsEndDate: Long? = null,
var calendarEventsIds: List<String> = listOf(),
var eventId: String = "",
var event: Event? = null) {
Expand Down
4 changes: 2 additions & 2 deletions device_calendar/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class MyHomePageState extends State<MyHomePage> {
floatingActionButton: !(_selectedCalendar?.isReadOnly ?? true)
? new FloatingActionButton(
onPressed: () async {
final now = DateTime.now();
final now = new DateTime.now();
final eventToCreate = new Event(_selectedCalendar.id);
eventToCreate.title =
"Event created with Device Calendar Plugin";
Expand Down Expand Up @@ -141,7 +141,7 @@ class MyHomePageState extends State<MyHomePage> {

Future _retrieveCalendarEvents(String calendarId) async {
try {
final startDate = DateTime.now().add(new Duration(days: -30));
final startDate = new DateTime.now().add(new Duration(days: -30));
final endDate = new DateTime.now().add(new Duration(days: 30));
final retrieveEventsParams =
new RetrieveEventsParams(startDate: startDate, endDate: endDate);
Expand Down

0 comments on commit 7a46942

Please sign in to comment.