Skip to content

Commit afd13ab

Browse files
committed
Draft : introduce VolatileComposerDraftStore
1 parent bc13c24 commit afd13ab

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2024 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.element.android.features.messages.impl.draft
18+
19+
import io.element.android.libraries.matrix.api.core.RoomId
20+
import io.element.android.libraries.matrix.api.room.draft.ComposerDraft
21+
22+
interface ComposerDraftStore {
23+
suspend fun loadDraft(roomId: RoomId): ComposerDraft?
24+
suspend fun updateDraft(roomId: RoomId, draft: ComposerDraft?)
25+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2024 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.element.android.features.messages.impl.draft
18+
19+
import io.element.android.libraries.matrix.api.MatrixClient
20+
import io.element.android.libraries.matrix.api.core.RoomId
21+
import io.element.android.libraries.matrix.api.room.draft.ComposerDraft
22+
import timber.log.Timber
23+
import javax.inject.Inject
24+
25+
class MatrixComposerDraftStore @Inject constructor(
26+
private val client: MatrixClient,
27+
) : ComposerDraftStore {
28+
29+
override suspend fun loadDraft(roomId: RoomId): ComposerDraft? {
30+
return client.getRoom(roomId)?.use { room ->
31+
room.loadComposerDraft()
32+
.onFailure {
33+
Timber.e(it, "Failed to load composer draft for room $roomId")
34+
}
35+
.onSuccess { draft ->
36+
room.clearComposerDraft()
37+
Timber.d("Loaded composer draft for room $roomId : $draft")
38+
}
39+
.getOrNull()
40+
}
41+
}
42+
43+
override suspend fun updateDraft(roomId: RoomId, draft: ComposerDraft?) {
44+
client.getRoom(roomId)?.use { room ->
45+
val updateDraftResult = if (draft == null) {
46+
room.clearComposerDraft()
47+
} else {
48+
room.saveComposerDraft(draft)
49+
}
50+
updateDraftResult
51+
.onFailure {
52+
Timber.e(it, "Failed to update composer draft for room $roomId")
53+
}
54+
.onSuccess {
55+
Timber.d("Updated composer draft for room $roomId")
56+
}
57+
}
58+
}
59+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2024 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.element.android.features.messages.impl.draft
18+
19+
import io.element.android.libraries.matrix.api.core.RoomId
20+
import io.element.android.libraries.matrix.api.room.draft.ComposerDraft
21+
import javax.inject.Inject
22+
23+
class VolatileComposerDraftStore @Inject constructor() : ComposerDraftStore {
24+
25+
private val drafts: MutableMap<RoomId, ComposerDraft> = mutableMapOf()
26+
27+
override suspend fun loadDraft(roomId: RoomId): ComposerDraft? {
28+
// Remove the draft from the map when it is loaded
29+
return drafts.remove(roomId)
30+
}
31+
32+
override suspend fun updateDraft(roomId: RoomId, draft: ComposerDraft?) {
33+
if (draft == null) {
34+
drafts.remove(roomId)
35+
} else {
36+
drafts[roomId] = draft
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)