Skip to content

Commit e2f0b4f

Browse files
Johennesbnjbvr
authored andcommitted
feat(ffi): expose media upload progress through EventSendState::NotSentYet
Signed-off-by: Johannes Marbach <[email protected]>
1 parent 0a796cb commit e2f0b4f

File tree

1 file changed

+54
-3
lines changed
  • bindings/matrix-sdk-ffi/src/timeline

1 file changed

+54
-3
lines changed

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

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ use matrix_sdk_common::{
3030
stream::StreamExt,
3131
};
3232
use matrix_sdk_ui::timeline::{
33-
self, AttachmentConfig, AttachmentSource, EventItemOrigin, Profile, TimelineDetails,
33+
self, AttachmentConfig, AttachmentSource, EventItemOrigin,
34+
MediaUploadProgress as SdkMediaUploadProgress, Profile, TimelineDetails,
3435
TimelineUniqueId as SdkTimelineUniqueId,
3536
};
3637
use mime::Mime;
@@ -239,6 +240,50 @@ impl From<UploadSource> for AttachmentSource {
239240
}
240241
}
241242

243+
/// This type represents the progress of a media (consisting of a file and
244+
/// possibly a thumbnail) being uploaded.
245+
#[derive(Clone, Copy, uniffi::Record)]
246+
pub struct MediaUploadProgress {
247+
/// The index of the media within the transaction. A file and its
248+
/// thumbnail share the same index. Will always be 0 for non-gallery
249+
/// media uploads.
250+
pub index: u64,
251+
252+
/// The current combined upload progress for both the file and,
253+
/// if it exists, its thumbnail.
254+
pub progress: AbstractProgress,
255+
}
256+
257+
impl From<SdkMediaUploadProgress> for MediaUploadProgress {
258+
fn from(value: SdkMediaUploadProgress) -> Self {
259+
Self { index: value.index, progress: value.progress.into() }
260+
}
261+
}
262+
263+
/// Progress of an operation in abstract units.
264+
///
265+
/// Contrary to [`TransmissionProgress`], this allows tracking the progress
266+
/// of sending or receiving a payload in estimated pseudo units representing a
267+
/// percentage. This is helpful in cases where the exact progress in bytes isn't
268+
/// known, for instance, because encryption (which changes the size) happens on
269+
/// the fly.
270+
#[derive(Clone, Copy, uniffi::Record)]
271+
pub struct AbstractProgress {
272+
/// How many units were already transferred.
273+
pub current: u64,
274+
/// How many units there are in total.
275+
pub total: u64,
276+
}
277+
278+
impl From<matrix_sdk::send_queue::AbstractProgress> for AbstractProgress {
279+
fn from(value: matrix_sdk::send_queue::AbstractProgress) -> Self {
280+
Self {
281+
current: value.current.try_into().unwrap_or(u64::MAX),
282+
total: value.total.try_into().unwrap_or(u64::MAX),
283+
}
284+
}
285+
}
286+
242287
#[matrix_sdk_ffi_macros::export]
243288
impl Timeline {
244289
pub async fn add_listener(&self, listener: Box<dyn TimelineListener>) -> Arc<TaskHandle> {
@@ -928,7 +973,11 @@ impl TimelineItem {
928973
#[derive(Clone, uniffi::Enum)]
929974
pub enum EventSendState {
930975
/// The local event has not been sent yet.
931-
NotSentYet,
976+
NotSentYet {
977+
/// The progress of the sending operation, if the event involves a media
978+
/// upload.
979+
progress: Option<MediaUploadProgress>,
980+
},
932981

933982
/// The local event has been sent to the server, but unsuccessfully: The
934983
/// sending has failed.
@@ -953,7 +1002,9 @@ impl From<&matrix_sdk_ui::timeline::EventSendState> for EventSendState {
9531002
use matrix_sdk_ui::timeline::EventSendState::*;
9541003

9551004
match value {
956-
NotSentYet => Self::NotSentYet,
1005+
NotSentYet { progress } => {
1006+
Self::NotSentYet { progress: progress.clone().map(|p| p.into()) }
1007+
}
9571008
SendingFailed { error, is_recoverable } => {
9581009
let as_queue_wedge_error: matrix_sdk::QueueWedgeError = (&**error).into();
9591010
Self::SendingFailed {

0 commit comments

Comments
 (0)