Skip to content

Commit a01918f

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

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

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

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

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

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

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ use matrix_sdk_common::{
3131
stream::StreamExt,
3232
};
3333
use matrix_sdk_ui::timeline::{
34-
self, AttachmentConfig, AttachmentSource, EventItemOrigin, Profile, TimelineDetails,
34+
self, AttachmentConfig, AttachmentSource, EventItemOrigin,
35+
EventSendProgress as SdkEventSendProgress, Profile, TimelineDetails,
3536
TimelineUniqueId as SdkTimelineUniqueId,
3637
};
3738
use mime::Mime;
@@ -61,7 +62,7 @@ use uuid::Uuid;
6162
use self::content::TimelineItemContent;
6263
pub use self::msg_like::MessageContent;
6364
use crate::{
64-
client::ProgressWatcher,
65+
client::{AbstractProgress, ProgressWatcher},
6566
error::{ClientError, RoomError},
6667
event::EventOrTransactionId,
6768
helpers::unwrap_or_clone_arc,
@@ -241,6 +242,32 @@ impl From<UploadSource> for AttachmentSource {
241242
}
242243
}
243244

245+
/// This type represents the "send progress" of a local event timeline item.
246+
#[derive(Clone, Copy, uniffi::Enum)]
247+
pub enum EventSendProgress {
248+
/// A media is being uploaded.
249+
MediaUpload {
250+
/// The index of the media within the transaction. A file and its
251+
/// thumbnail share the same index. Will always be 0 for non-gallery
252+
/// media uploads.
253+
index: u64,
254+
255+
/// The current combined upload progress for both the file and,
256+
/// if it exists, its thumbnail.
257+
progress: AbstractProgress,
258+
},
259+
}
260+
261+
impl From<SdkEventSendProgress> for EventSendProgress {
262+
fn from(value: SdkEventSendProgress) -> Self {
263+
match value {
264+
SdkEventSendProgress::MediaUpload { index, progress } => {
265+
Self::MediaUpload { index, progress: progress.into() }
266+
}
267+
}
268+
}
269+
}
270+
244271
#[matrix_sdk_ffi_macros::export]
245272
impl Timeline {
246273
pub async fn add_listener(&self, listener: Box<dyn TimelineListener>) -> Arc<TaskHandle> {
@@ -990,7 +1017,10 @@ impl TimelineItem {
9901017
#[derive(Clone, uniffi::Enum)]
9911018
pub enum EventSendState {
9921019
/// The local event has not been sent yet.
993-
NotSentYet,
1020+
NotSentYet {
1021+
/// The progress of the sending operation, if any is available.
1022+
progress: Option<EventSendProgress>,
1023+
},
9941024

9951025
/// The local event has been sent to the server, but unsuccessfully: The
9961026
/// sending has failed.
@@ -1015,7 +1045,9 @@ impl From<&matrix_sdk_ui::timeline::EventSendState> for EventSendState {
10151045
use matrix_sdk_ui::timeline::EventSendState::*;
10161046

10171047
match value {
1018-
NotSentYet => Self::NotSentYet,
1048+
NotSentYet { progress } => {
1049+
Self::NotSentYet { progress: progress.clone().map(|p| p.into()) }
1050+
}
10191051
SendingFailed { error, is_recoverable } => {
10201052
let as_queue_wedge_error: matrix_sdk::QueueWedgeError = (&**error).into();
10211053
Self::SendingFailed {

0 commit comments

Comments
 (0)