Skip to content

Commit

Permalink
Get calendarId instead of eventId on Instances
Browse files Browse the repository at this point in the history
  • Loading branch information
mvmike committed Oct 25, 2024
1 parent a4b7a8b commit fd6ca10
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.time.temporal.ChronoUnit

sealed class Instance(
open val id: Int,
open val eventId: Int,
open val calendarId: Int,
open val isDeclined: Boolean
) {
abstract fun isInDay(
Expand All @@ -22,13 +22,13 @@ sealed class Instance(

data class TimedInstance(
override val id: Int,
override val eventId: Int,
override val calendarId: Int,
override val isDeclined: Boolean,
val start: ZonedDateTime,
val end: ZonedDateTime
) : Instance(
id = id,
eventId = eventId,
calendarId = calendarId,
isDeclined = isDeclined
) {
override fun isInDay(day: LocalDate, dayZoneId: ZoneId): Boolean {
Expand All @@ -43,13 +43,13 @@ sealed class Instance(

data class AllDayInstance(
override val id: Int,
override val eventId: Int,
override val calendarId: Int,
override val isDeclined: Boolean,
val start: LocalDate,
val end: LocalDate
) : Instance(
id = id,
eventId = eventId,
calendarId = calendarId,
isDeclined = isDeclined
) {
override fun isInDay(day: LocalDate, dayZoneId: ZoneId): Boolean =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// See LICENSE for licensing information
package cat.mvmike.minimalcalendarwidget.infrastructure.resolver

import android.Manifest
import android.Manifest.permission.READ_CALENDAR
import android.content.Context
import android.content.pm.PackageManager
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.database.Cursor
import android.provider.CalendarContract
import android.provider.CalendarContract.Events
import android.provider.CalendarContract.Instances
import androidx.core.content.ContextCompat.checkSelfPermission
import cat.mvmike.minimalcalendarwidget.domain.Instance
import cat.mvmike.minimalcalendarwidget.domain.Instance.AllDayInstance
Expand All @@ -17,17 +18,20 @@ import java.time.ZoneId
import java.time.ZonedDateTime

internal val instanceQueryFields = arrayOf(
CalendarContract.Instances._ID,
CalendarContract.Instances.EVENT_ID,
CalendarContract.Instances.BEGIN,
CalendarContract.Instances.END,
CalendarContract.Instances.EVENT_TIMEZONE,
CalendarContract.Instances.SELF_ATTENDEE_STATUS,
CalendarContract.Events.ALL_DAY
Instances._ID,
Events.CALENDAR_ID,
Instances.BEGIN,
Instances.END,
Instances.EVENT_TIMEZONE,
Instances.SELF_ATTENDEE_STATUS,
Events.ALL_DAY
)

object CalendarResolver {

fun isReadCalendarPermitted(context: Context) =
checkSelfPermission(context, READ_CALENDAR) == PERMISSION_GRANTED

fun getInstances(
context: Context,
begin: Long,
Expand All @@ -36,45 +40,36 @@ object CalendarResolver {
val instances: MutableSet<Instance> = HashSet()
var instanceCursor: Cursor? = null
runCatching {
instanceCursor = queryInstances(context, begin, end)
instanceCursor = Instances.query(context.contentResolver, instanceQueryFields, begin, end)
while (instanceCursor!!.moveToNext()) {
instanceCursor!!.toInstance()?.let { instances.add(it) }
instanceCursor.toInstance()?.let { instances.add(it) }
}
}
instanceCursor?.close()
return instances.toSet()
}

fun isReadCalendarPermitted(context: Context) =
checkSelfPermission(context, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED

private fun queryInstances(
context: Context,
begin: Long,
end: Long
): Cursor = CalendarContract.Instances.query(context.contentResolver, instanceQueryFields, begin, end)

private fun Cursor.toInstance(): Instance? = runCatching {
val id = getInt(0)
val eventId = getInt(1)
val calendarId = getInt(1)
val start = Instant.ofEpochMilli(getLong(2))
// end of instances are exclusive (e.g. an hour event is from 10 to 11 and not from 10 to 10:59:59.999)
val end = Instant.ofEpochMilli(getLong(3) - 1)
val zoneId = toZoneIdOrDefault(getString(4))
val isDeclined = getInt(5) == CalendarContract.Instances.STATUS_CANCELED
val isDeclined = getInt(5) == Instances.STATUS_CANCELED
val isAllDay = getInt(6) == 1

when {
isAllDay -> AllDayInstance(
id = id,
eventId = eventId,
calendarId = calendarId,
isDeclined = isDeclined,
start = LocalDateTime.ofInstant(start, zoneId).toLocalDate(),
end = LocalDateTime.ofInstant(end, zoneId).toLocalDate()
)
else -> TimedInstance(
id = id,
eventId = eventId,
calendarId = calendarId,
isDeclined = isDeclined,
start = ZonedDateTime.ofInstant(start, zoneId),
end = ZonedDateTime.ofInstant(end, zoneId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,21 @@ internal class InstanceTest : BaseTest() {
private fun getInstancesStartingAndEndingBeforeSystemLocalDate() = listOf(
TimedInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = ZonedDateTime.parse("2018-12-02T02:15:00+03:00"),
end = ZonedDateTime.parse("2018-12-03T23:15:00+03:00")
),
TimedInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = ZonedDateTime.parse("2018-12-02T02:15:00-08:00"),
end = ZonedDateTime.parse("2018-12-03T11:30:00-08:00")
),
AllDayInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = LocalDate.parse("2018-12-02"),
end = LocalDate.parse("2018-12-03")
Expand All @@ -97,21 +97,21 @@ internal class InstanceTest : BaseTest() {
private fun getInstancesStartingBeforeAndEndingInSystemLocalDate() = listOf(
TimedInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = ZonedDateTime.parse("2018-12-01T00:00:00+03:00"),
end = ZonedDateTime.parse("2018-12-04T23:59:00+03:00")
),
TimedInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = ZonedDateTime.parse("2018-12-02T02:15:00-08:00"),
end = ZonedDateTime.parse("2018-12-03T13:30:00-08:00")
),
AllDayInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = LocalDate.parse("2018-12-01"),
end = LocalDate.parse("2018-12-05")
Expand All @@ -121,14 +121,14 @@ internal class InstanceTest : BaseTest() {
private fun getInstancesStartingBeforeAndEndingAfterSystemLocalDate() = listOf(
TimedInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = ZonedDateTime.parse("2018-12-01T10:55:00+03:00"),
end = ZonedDateTime.parse("2018-12-07T23:00:00+03:00")
),
AllDayInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = LocalDate.parse("2018-12-01"),
end = LocalDate.parse("2018-12-07")
Expand All @@ -138,21 +138,21 @@ internal class InstanceTest : BaseTest() {
private fun getInstancesStartingInAndEndingInSystemLocalDate() = listOf(
TimedInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = ZonedDateTime.parse("2018-12-04T23:00:00+03:00"),
end = ZonedDateTime.parse("2018-12-04T23:50:00+03:00")
),
TimedInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = ZonedDateTime.parse("2018-12-03T13:30:00-08:00"),
end = ZonedDateTime.parse("2018-12-04T10:30:00-08:00")
),
AllDayInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = LocalDate.parse("2018-12-04"),
end = LocalDate.parse("2018-12-05")
Expand All @@ -162,21 +162,21 @@ internal class InstanceTest : BaseTest() {
private fun getInstancesStartingInAndEndingAfterSystemLocalDate() = listOf(
TimedInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = ZonedDateTime.parse("2018-12-04T23:00:00+03:00"),
end = ZonedDateTime.parse("2018-12-05T01:00:00+03:00")
),
TimedInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = ZonedDateTime.parse("2018-12-04T12:30:00-08:00"),
end = ZonedDateTime.parse("2018-12-04T16:30:00-08:00")
),
AllDayInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = LocalDate.parse("2018-12-04"),
end = LocalDate.parse("2018-12-06")
Expand All @@ -186,21 +186,21 @@ internal class InstanceTest : BaseTest() {
private fun getInstancesStartingAfterAndEndingAfterSystemLocalDate() = listOf(
TimedInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = ZonedDateTime.parse("2018-12-05T00:00:00+03:00"),
end = ZonedDateTime.parse("2018-12-05T02:00:00+03:00")
),
TimedInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = ZonedDateTime.parse("2018-12-04T13:30:00-08:00"),
end = ZonedDateTime.parse("2018-12-04T16:30:00-08:00")
),
AllDayInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = LocalDate.parse("2018-12-05"),
end = LocalDate.parse("2018-12-06")
Expand All @@ -212,28 +212,28 @@ internal class InstanceTest : BaseTest() {
setOf(
TimedInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = ZonedDateTime.parse("2018-11-29T23:00:00+03:00"),
end = ZonedDateTime.parse("2018-12-01T23:50:00+03:00")
),
AllDayInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = LocalDate.parse("2018-12-01"),
end = LocalDate.parse("2018-12-02")
),
TimedInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = true,
start = ZonedDateTime.parse("2018-12-02T23:00:00+03:00"),
end = ZonedDateTime.parse("2018-12-09T01:00:00+03:00")
),
AllDayInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = false,
start = LocalDate.parse("2018-12-02"),
end = LocalDate.parse("2018-12-06")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ internal class DaysServiceTest : BaseTest() {
readTestResourceCsvFile("/system_all_day_instances.csv").map {
AllDayInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = it[0].toBoolean(),
start = LocalDate.parse(it[1]),
end = LocalDate.parse(it[2])
Expand All @@ -309,7 +309,7 @@ internal class DaysServiceTest : BaseTest() {
readTestResourceCsvFile("/system_timed_instances.csv").map {
TimedInstance(
id = random.nextInt(),
eventId = random.nextInt(),
calendarId = random.nextInt(),
isDeclined = it[0].toBoolean(),
start = ZonedDateTime.parse(it[1]),
end = ZonedDateTime.parse(it[2])
Expand Down
Loading

0 comments on commit fd6ca10

Please sign in to comment.