Skip to content

Commit 4f1c745

Browse files
committed
Add a way to clear the cache for a specific room.
1 parent 0d2efe5 commit 4f1c745

File tree

8 files changed

+45
-1
lines changed

8 files changed

+45
-1
lines changed

features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/RoomListContextMenu.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ fun RoomListContextMenu(
6161
onFavoriteChange = { isFavorite ->
6262
eventSink(RoomListEvents.SetRoomIsFavorite(contextMenu.roomId, isFavorite))
6363
},
64+
onClearCacheRoomClick = {
65+
eventSink(RoomListEvents.HideContextMenu)
66+
eventSink(RoomListEvents.ClearCacheOfRoom(contextMenu.roomId))
67+
},
6468
)
6569
}
6670
}
@@ -73,6 +77,7 @@ private fun RoomListModalBottomSheetContent(
7377
onFavoriteChange: (isFavorite: Boolean) -> Unit,
7478
onRoomMarkReadClick: () -> Unit,
7579
onRoomMarkUnreadClick: () -> Unit,
80+
onClearCacheRoomClick: () -> Unit,
7681
) {
7782
Column(
7883
modifier = Modifier.fillMaxWidth()
@@ -177,6 +182,18 @@ private fun RoomListModalBottomSheetContent(
177182
),
178183
style = ListItemStyle.Destructive,
179184
)
185+
if (contextMenu.eventCacheFeatureFlagEnabled) {
186+
ListItem(
187+
headlineContent = {
188+
Text(text = "Clear cache for this room")
189+
},
190+
modifier = Modifier.clickable { onClearCacheRoomClick() },
191+
leadingContent = ListItemContent.Icon(
192+
iconSource = IconSource.Vector(CompoundIcons.Delete())
193+
),
194+
style = ListItemStyle.Primary,
195+
)
196+
}
180197
}
181198
}
182199

@@ -195,5 +212,6 @@ internal fun RoomListModalBottomSheetContentPreview(
195212
onRoomSettingsClick = {},
196213
onLeaveRoomClick = {},
197214
onFavoriteChange = {},
215+
onClearCacheRoomClick = {},
198216
)
199217
}

features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/RoomListEvents.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ sealed interface RoomListEvents {
2525
data class MarkAsRead(val roomId: RoomId) : ContextMenuEvents
2626
data class MarkAsUnread(val roomId: RoomId) : ContextMenuEvents
2727
data class SetRoomIsFavorite(val roomId: RoomId, val isFavorite: Boolean) : ContextMenuEvents
28+
data class ClearCacheOfRoom(val roomId: RoomId) : ContextMenuEvents
2829
}

features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/RoomListPresenter.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class RoomListPresenter @Inject constructor(
146146
AcceptDeclineInviteEvents.DeclineInvite(event.roomListRoomSummary.toInviteData())
147147
)
148148
}
149+
is RoomListEvents.ClearCacheOfRoom -> coroutineScope.clearCacheOfRoom(event.roomId)
149150
}
150151
}
151152

@@ -255,7 +256,8 @@ class RoomListPresenter @Inject constructor(
255256
isDm = event.roomListRoomSummary.isDm,
256257
isFavorite = event.roomListRoomSummary.isFavorite,
257258
markAsUnreadFeatureFlagEnabled = featureFlagService.isFeatureEnabled(FeatureFlags.MarkAsUnread),
258-
hasNewContent = event.roomListRoomSummary.hasNewContent
259+
hasNewContent = event.roomListRoomSummary.hasNewContent,
260+
eventCacheFeatureFlagEnabled = featureFlagService.isFeatureEnabled(FeatureFlags.EventCache),
259261
)
260262
contextMenuState.value = initialState
261263

@@ -312,6 +314,12 @@ class RoomListPresenter @Inject constructor(
312314
}
313315
}
314316

317+
private fun CoroutineScope.clearCacheOfRoom(roomId: RoomId) = launch {
318+
client.getRoom(roomId)?.use { room ->
319+
room.clearEventCacheStorage()
320+
}
321+
}
322+
315323
/**
316324
* Checks if the user needs to migrate to a native sliding sync version.
317325
*/

features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/RoomListState.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ data class RoomListState(
4646
val isDm: Boolean,
4747
val isFavorite: Boolean,
4848
val markAsUnreadFeatureFlagEnabled: Boolean,
49+
val eventCacheFeatureFlagEnabled: Boolean,
4950
val hasNewContent: Boolean,
5051
) : ContextMenu
5152
}

features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/RoomListStateContextMenuShownProvider.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ internal fun aContextMenuShown(
3131
markAsUnreadFeatureFlagEnabled = true,
3232
hasNewContent = hasNewContent,
3333
isFavorite = isFavorite,
34+
eventCacheFeatureFlagEnabled = false,
3435
)

libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/MatrixRoom.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ interface MatrixRoom : Closeable {
232232
*/
233233
suspend fun setUnreadFlag(isUnread: Boolean): Result<Unit>
234234

235+
/**
236+
* Clear the event cache storage for the current room.
237+
*/
238+
suspend fun clearEventCacheStorage(): Result<Unit>
239+
235240
/**
236241
* Share a location message in the room.
237242
*

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,12 @@ class RustMatrixRoom(
569569
}
570570
}
571571

572+
override suspend fun clearEventCacheStorage(): Result<Unit> = withContext(roomDispatcher) {
573+
runCatching {
574+
innerRoom.clearEventCacheStorage()
575+
}
576+
}
577+
572578
override suspend fun kickUser(userId: UserId, reason: String?): Result<Unit> = withContext(roomDispatcher) {
573579
runCatching {
574580
innerRoom.kickUser(userId.value, reason)

libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/room/FakeMatrixRoom.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,10 @@ class FakeMatrixRoom(
574574
fun givenRoomMembersState(state: MatrixRoomMembersState) {
575575
membersStateFlow.value = state
576576
}
577+
578+
override suspend fun clearEventCacheStorage(): Result<Unit> {
579+
return Result.success(Unit)
580+
}
577581
}
578582

579583
fun defaultRoomPowerLevels() = MatrixRoomPowerLevels(

0 commit comments

Comments
 (0)