Skip to content

Commit 5012782

Browse files
committed
Add method to find first event row
1 parent 3eab4eb commit 5012782

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

lib/src/androidTest/kotlin/at/bitfire/synctools/storage/calendar/AndroidCalendarTest.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ class AndroidCalendarTest {
110110
assertEquals(setOf("Some Other Event 1", "Some Other Event 2"), result.map { it.title }.toSet())
111111
}
112112

113+
@Test
114+
fun testFindEventRow() {
115+
calendar.addEvent(Entity(contentValuesOf(
116+
Events.CALENDAR_ID to calendar.id,
117+
Events.DTSTART to now,
118+
Events.DTEND to now + 3600000,
119+
Events.TITLE to "Some Event"
120+
)))
121+
val result = calendar.findEventRow(arrayOf(Events.TITLE), "${Events.DTSTART}=?", arrayOf(now.toString()))
122+
assertContentValuesEqual(
123+
contentValuesOf(Events.TITLE to "Some Event"),
124+
result!!
125+
)
126+
}
127+
128+
@Test
129+
fun testFindEventRow_NotExisting() {
130+
assertNull(calendar.findEventRow(arrayOf(Events.TITLE), "${Events.DTSTART}=?", arrayOf(now.toString())))
131+
}
113132

114133
// getEvent and getEventEntity are implicitly tested by testAddEvent_and_GetEvent
115134

lib/src/main/kotlin/at/bitfire/synctools/storage/calendar/AndroidCalendar.kt

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,25 @@ class AndroidCalendar(
131131
return events
132132
}
133133

134+
/**
135+
* Gets the first event row that matches the given query.
136+
*
137+
* @return first event row that matches [where]/[whereArgs] (or `null` if not found)
138+
*
139+
* @throws LocalStorageException when the content provider returns an error
140+
*/
141+
fun findEventRow(projection: Array<String>? = null, where: String? = null, whereArgs: Array<String>? = null): ContentValues? {
142+
try {
143+
client.query(eventsUri, projection, where, whereArgs, null)?.use { cursor ->
144+
if (cursor.moveToNext())
145+
return cursor.toContentValues()
146+
}
147+
} catch (e: RemoteException) {
148+
throw LocalStorageException("Couldn't query event rows", e)
149+
}
150+
return null
151+
}
152+
134153
/**
135154
* Gets a specific event, identified by its ID, from this calendar.
136155
*
@@ -171,7 +190,7 @@ class AndroidCalendar(
171190
return cursor.toContentValues()
172191
}
173192
} catch (e: RemoteException) {
174-
throw LocalStorageException("Couldn't query event", e)
193+
throw LocalStorageException("Couldn't query event row", e)
175194
}
176195
return null
177196
}
@@ -197,7 +216,7 @@ class AndroidCalendar(
197216
}
198217
}
199218
} catch (e: RemoteException) {
200-
throw LocalStorageException("Couldn't iterate events", e)
219+
throw LocalStorageException("Couldn't iterate event rows", e)
201220
}
202221
}
203222

@@ -222,7 +241,7 @@ class AndroidCalendar(
222241
body(entity)
223242
}
224243
} catch (e: RemoteException) {
225-
throw LocalStorageException("Couldn't iterate event entities", e)
244+
throw LocalStorageException("Couldn't iterate events", e)
226245
}
227246
}
228247

@@ -242,7 +261,7 @@ class AndroidCalendar(
242261
try {
243262
client.update(eventUri(id), values, null, null)
244263
} catch (e: RemoteException) {
245-
throw LocalStorageException("Couldn't update event $id", e)
264+
throw LocalStorageException("Couldn't update event row $id", e)
246265
}
247266
}
248267

0 commit comments

Comments
 (0)