Skip to content

Commit 934038e

Browse files
committed
feat(ffi): expose media upload progress through EventSendState::NotSentYet
Signed-off-by: Johannes Marbach <n0-0ne+github@mailbox.org>
1 parent a4c6866 commit 934038e

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

bindings/matrix-sdk-ffi/src/client.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,30 @@ impl From<matrix_sdk::TransmissionProgress> for TransmissionProgress {
224224
}
225225
}
226226

227+
/// Progress of an operation in abstract units.
228+
///
229+
/// Contrary to [`TransmissionProgress`], this allows tracking the progress
230+
/// of sending or receiving a payload in estimated pseudo units representing a
231+
/// percentage. This is helpful in cases where the exact progress in bytes isn't
232+
/// known, for instance, because encryption (which changes the size) happens on
233+
/// the fly.
234+
#[derive(Clone, Copy, uniffi::Record)]
235+
pub struct AbstractProgress {
236+
/// How many units were already transferred.
237+
pub current: u64,
238+
/// How many units there are in total.
239+
pub total: u64,
240+
}
241+
242+
impl From<matrix_sdk::AbstractProgress> for AbstractProgress {
243+
fn from(value: matrix_sdk::AbstractProgress) -> Self {
244+
Self {
245+
current: value.current.try_into().unwrap_or(u64::MAX),
246+
total: value.total.try_into().unwrap_or(u64::MAX),
247+
}
248+
}
249+
}
250+
227251
#[derive(uniffi::Object)]
228252
pub struct Client {
229253
pub(crate) inner: AsyncRuntimeDropped<MatrixClient>,

bindings/matrix-sdk-ffi/src/timeline/mod.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ use matrix_sdk_common::{
3535
stream::StreamExt,
3636
};
3737
use matrix_sdk_ui::timeline::{
38-
self, AttachmentSource, EventItemOrigin, Profile, TimelineDetails,
39-
TimelineUniqueId as SdkTimelineUniqueId,
38+
self, AttachmentSource, EventItemOrigin, EventSendProgress as SdkEventSendProgress, Profile,
39+
TimelineDetails, TimelineUniqueId as SdkTimelineUniqueId,
4040
};
4141
use mime::Mime;
4242
use reply::{EmbeddedEventDetails, InReplyToDetails};
@@ -66,7 +66,7 @@ use uuid::Uuid;
6666
use self::content::TimelineItemContent;
6767
pub use self::msg_like::MessageContent;
6868
use crate::{
69-
client::ProgressWatcher,
69+
client::{AbstractProgress, ProgressWatcher},
7070
error::{ClientError, RoomError},
7171
event::EventOrTransactionId,
7272
helpers::unwrap_or_clone_arc,
@@ -270,6 +270,31 @@ impl TryInto<Reply> for ReplyParameters {
270270
}
271271
}
272272

273+
/// This type represents the "send progress" of a local event timeline item.
274+
#[derive(Clone, Copy, uniffi::Enum)]
275+
pub enum EventSendProgress {
276+
/// A media is being uploaded.
277+
MediaUpload {
278+
/// The index of the media within the transaction. A file and its
279+
/// thumbnail share the same index.
280+
index: u64,
281+
282+
/// The current combined upload progress for both the file and,
283+
/// if it exists, its thumbnail.
284+
progress: AbstractProgress,
285+
},
286+
}
287+
288+
impl From<SdkEventSendProgress> for EventSendProgress {
289+
fn from(value: SdkEventSendProgress) -> Self {
290+
match value {
291+
SdkEventSendProgress::MediaUpload { index, progress } => {
292+
Self::MediaUpload { index, progress: progress.into() }
293+
}
294+
}
295+
}
296+
}
297+
273298
#[matrix_sdk_ffi_macros::export]
274299
impl Timeline {
275300
pub async fn add_listener(&self, listener: Box<dyn TimelineListener>) -> Arc<TaskHandle> {
@@ -1018,7 +1043,10 @@ impl TimelineItem {
10181043
#[derive(Clone, uniffi::Enum)]
10191044
pub enum EventSendState {
10201045
/// The local event has not been sent yet.
1021-
NotSentYet,
1046+
NotSentYet {
1047+
/// The progress of the sending operation, if any is available.
1048+
progress: Option<EventSendProgress>,
1049+
},
10221050

10231051
/// The local event has been sent to the server, but unsuccessfully: The
10241052
/// sending has failed.
@@ -1043,7 +1071,9 @@ impl From<&matrix_sdk_ui::timeline::EventSendState> for EventSendState {
10431071
use matrix_sdk_ui::timeline::EventSendState::*;
10441072

10451073
match value {
1046-
NotSentYet => Self::NotSentYet,
1074+
NotSentYet { progress } => {
1075+
Self::NotSentYet { progress: progress.clone().map(|p| p.into()) }
1076+
}
10471077
SendingFailed { error, is_recoverable } => {
10481078
let as_queue_wedge_error: matrix_sdk::QueueWedgeError = (&**error).into();
10491079
Self::SendingFailed {

0 commit comments

Comments
 (0)