Draft : expose new apis from sdk

This commit is contained in:
ganfra 2024-06-19 15:28:41 +02:00
parent 112cd36ce2
commit dc331640f9
6 changed files with 152 additions and 0 deletions

View file

@ -29,6 +29,7 @@ import io.element.android.libraries.matrix.api.media.ImageInfo
import io.element.android.libraries.matrix.api.media.MediaUploadHandler
import io.element.android.libraries.matrix.api.media.VideoInfo
import io.element.android.libraries.matrix.api.poll.PollKind
import io.element.android.libraries.matrix.api.room.draft.ComposerDraft
import io.element.android.libraries.matrix.api.room.location.AssetType
import io.element.android.libraries.matrix.api.room.powerlevels.MatrixRoomPowerLevels
import io.element.android.libraries.matrix.api.room.powerlevels.UserRoleChange
@ -337,5 +338,20 @@ interface MatrixRoom : Closeable {
suspend fun setSendQueueEnabled(enabled: Boolean)
/**
* Store the given `ComposerDraft` in the state store of this room.
*/
suspend fun saveComposerDraft(composerDraft: ComposerDraft): Result<Unit>
/**
* Retrieve the `ComposerDraft` stored in the state store for this room.
*/
suspend fun loadComposerDraft(): Result<ComposerDraft?>
/**
* Clear the `ComposerDraft` stored in the state store for this room.
*/
suspend fun clearComposerDraft(): Result<Unit>
override fun close() = destroy()
}

View file

@ -0,0 +1,29 @@
/*
* Copyright (c) 2024 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.matrix.api.room.draft
/**
* A draft of a message composed by the user.
* @param plainText The draft content in plain text.
* @param htmlText If the message is formatted in HTML, the HTML representation of the message.
* @param draftType The type of draft.
*/
data class ComposerDraft(
val plainText: String,
val htmlText: String?,
val draftType: ComposerDraftType
)

View file

@ -0,0 +1,25 @@
/*
* Copyright (c) 2024 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.matrix.api.room.draft
import io.element.android.libraries.matrix.api.core.EventId
sealed class ComposerDraftType {
data object NewMessage : ComposerDraftType()
data class Reply(val eventId: EventId) : ComposerDraftType()
data class Edit(val eventId: EventId) : ComposerDraftType()
}

View file

@ -41,6 +41,7 @@ import io.element.android.libraries.matrix.api.room.Mention
import io.element.android.libraries.matrix.api.room.MessageEventType
import io.element.android.libraries.matrix.api.room.RoomMember
import io.element.android.libraries.matrix.api.room.StateEventType
import io.element.android.libraries.matrix.api.room.draft.ComposerDraft
import io.element.android.libraries.matrix.api.room.location.AssetType
import io.element.android.libraries.matrix.api.room.powerlevels.MatrixRoomPowerLevels
import io.element.android.libraries.matrix.api.room.powerlevels.UserRoleChange
@ -49,6 +50,7 @@ import io.element.android.libraries.matrix.api.timeline.ReceiptType
import io.element.android.libraries.matrix.api.timeline.Timeline
import io.element.android.libraries.matrix.api.widget.MatrixWidgetDriver
import io.element.android.libraries.matrix.api.widget.MatrixWidgetSettings
import io.element.android.libraries.matrix.impl.room.draft.into
import io.element.android.libraries.matrix.impl.room.member.RoomMemberListFetcher
import io.element.android.libraries.matrix.impl.room.member.RoomMemberMapper
import io.element.android.libraries.matrix.impl.room.powerlevels.RoomPowerLevelsMapper
@ -605,6 +607,21 @@ class RustMatrixRoom(
innerRoom.enableSendQueue(enabled)
}
override suspend fun saveComposerDraft(composerDraft: ComposerDraft): Result<Unit> = runCatching {
Timber.d("saveComposerDraft: $composerDraft into $roomId")
innerRoom.saveComposerDraft(composerDraft.into())
}
override suspend fun loadComposerDraft(): Result<ComposerDraft?> = runCatching {
Timber.d("loadComposerDraft for $roomId")
innerRoom.loadComposerDraft()?.into()
}
override suspend fun clearComposerDraft(): Result<Unit> = runCatching {
Timber.d("clearComposerDraft: for $roomId")
innerRoom.clearComposerDraft()
}
private fun createTimeline(
timeline: InnerTimeline,
isLive: Boolean,

View file

@ -0,0 +1,55 @@
/*
* Copyright (c) 2024 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.matrix.impl.room.draft
import io.element.android.libraries.matrix.api.core.EventId
import io.element.android.libraries.matrix.api.room.draft.ComposerDraft
import io.element.android.libraries.matrix.api.room.draft.ComposerDraftType
import uniffi.matrix_sdk_base.ComposerDraft as RustComposerDraft
import uniffi.matrix_sdk_base.ComposerDraftType as RustComposerDraftType
internal fun ComposerDraft.into(): RustComposerDraft {
return RustComposerDraft(
plainText = plainText,
htmlText = htmlText,
draftType = draftType.into()
)
}
internal fun RustComposerDraft.into(): ComposerDraft {
return ComposerDraft(
plainText = plainText,
htmlText = htmlText,
draftType = draftType.into()
)
}
private fun RustComposerDraftType.into(): ComposerDraftType {
return when (this) {
RustComposerDraftType.NewMessage -> ComposerDraftType.NewMessage
is RustComposerDraftType.Reply -> ComposerDraftType.Reply(EventId(eventId))
is RustComposerDraftType.Edit -> ComposerDraftType.Edit(EventId(eventId))
}
}
private fun ComposerDraftType.into(): RustComposerDraftType {
return when (this) {
ComposerDraftType.NewMessage -> RustComposerDraftType.NewMessage
is ComposerDraftType.Reply -> RustComposerDraftType.Reply(eventId.value)
is ComposerDraftType.Edit -> RustComposerDraftType.Edit(eventId.value)
}
}

View file

@ -40,6 +40,7 @@ import io.element.android.libraries.matrix.api.room.MessageEventType
import io.element.android.libraries.matrix.api.room.RoomMember
import io.element.android.libraries.matrix.api.room.RoomNotificationMode
import io.element.android.libraries.matrix.api.room.StateEventType
import io.element.android.libraries.matrix.api.room.draft.ComposerDraft
import io.element.android.libraries.matrix.api.room.location.AssetType
import io.element.android.libraries.matrix.api.room.powerlevels.MatrixRoomPowerLevels
import io.element.android.libraries.matrix.api.room.powerlevels.UserRoleChange
@ -527,6 +528,15 @@ class FakeMatrixRoom(
var setSendQueueEnabledLambda = { _: Boolean -> }
override suspend fun setSendQueueEnabled(enabled: Boolean) = setSendQueueEnabledLambda(enabled)
var saveComposerDraftLambda = { _: ComposerDraft -> Result.success(Unit) }
override suspend fun saveComposerDraft(composerDraft: ComposerDraft) = saveComposerDraftLambda(composerDraft)
var loadComposerDraftLambda = { Result.success<ComposerDraft?>(null) }
override suspend fun loadComposerDraft() = loadComposerDraftLambda()
var clearComposerDraftLambda = { Result.success(Unit) }
override suspend fun clearComposerDraft() = clearComposerDraftLambda()
override fun getWidgetDriver(widgetSettings: MatrixWidgetSettings): Result<MatrixWidgetDriver> = getWidgetDriverResult
fun givenRoomMembersState(state: MatrixRoomMembersState) {