From 32919405d6d87f7255c4f5aff6243183a30f65bd Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Thu, 10 Oct 2024 11:34:50 +0200 Subject: [PATCH] refactor(ffi): use a single provider for lazily computed info --- bindings/matrix-sdk-ffi/src/timeline/mod.rs | 71 +++++++++------------ 1 file changed, 31 insertions(+), 40 deletions(-) diff --git a/bindings/matrix-sdk-ffi/src/timeline/mod.rs b/bindings/matrix-sdk-ffi/src/timeline/mod.rs index 82ba36c82ef..a9dd3597698 100644 --- a/bindings/matrix-sdk-ffi/src/timeline/mod.rs +++ b/bindings/matrix-sdk-ffi/src/timeline/mod.rs @@ -1042,17 +1042,16 @@ pub struct EventTimelineItem { content: TimelineItemContent, timestamp: u64, reactions: Vec, - debug_info_provider: Arc, local_send_state: Option, read_receipts: HashMap, origin: Option, can_be_replied_to: bool, - shields_provider: Arc, + lazy_provider: Arc, } impl From for EventTimelineItem { - fn from(value: matrix_sdk_ui::timeline::EventTimelineItem) -> Self { - let reactions = value + fn from(item: matrix_sdk_ui::timeline::EventTimelineItem) -> Self { + let reactions = item .reactions() .iter() .map(|(k, v)| Reaction { @@ -1066,27 +1065,25 @@ impl From for EventTimelineItem { .collect(), }) .collect(); - let value = Arc::new(value); - let debug_info_provider = Arc::new(EventTimelineItemDebugInfoProvider(value.clone())); - let shields_provider = Arc::new(EventShieldsProvider(value.clone())); + let item = Arc::new(item); + let lazy_provider = Arc::new(LazyTimelineItemProvider(item.clone())); let read_receipts = - value.read_receipts().iter().map(|(k, v)| (k.to_string(), v.clone().into())).collect(); + item.read_receipts().iter().map(|(k, v)| (k.to_string(), v.clone().into())).collect(); Self { - is_remote: !value.is_local_echo(), - event_or_transaction_id: value.identifier().into(), - sender: value.sender().to_string(), - sender_profile: value.sender_profile().into(), - is_own: value.is_own(), - is_editable: value.is_editable(), - content: value.content().clone().into(), - timestamp: value.timestamp().0.into(), + is_remote: !item.is_local_echo(), + event_or_transaction_id: item.identifier().into(), + sender: item.sender().to_string(), + sender_profile: item.sender_profile().into(), + is_own: item.is_own(), + is_editable: item.is_editable(), + content: item.content().clone().into(), + timestamp: item.timestamp().0.into(), reactions, - debug_info_provider, - local_send_state: value.send_state().map(|s| s.into()), + local_send_state: item.send_state().map(|s| s.into()), read_receipts, - origin: value.origin(), - can_be_replied_to: value.can_be_replied_to(), - shields_provider, + origin: item.origin(), + can_be_replied_to: item.can_be_replied_to(), + lazy_provider, } } } @@ -1102,22 +1099,6 @@ impl From for Receipt { } } -/// Wrapper to retrieve the debug info lazily instead of immediately -/// transforming it for each timeline event. -#[derive(uniffi::Object)] -pub struct EventTimelineItemDebugInfoProvider(Arc); - -#[matrix_sdk_ffi_macros::export] -impl EventTimelineItemDebugInfoProvider { - fn get(&self) -> EventTimelineItemDebugInfo { - EventTimelineItemDebugInfo { - model: format!("{:#?}", self.0), - original_json: self.0.original_json().map(|raw| raw.json().get().to_owned()), - latest_edit_json: self.0.latest_edit_json().map(|raw| raw.json().get().to_owned()), - } - } -} - #[derive(Clone, uniffi::Record)] pub struct EventTimelineItemDebugInfo { model: String, @@ -1269,13 +1250,23 @@ impl TryFrom for SdkEditedContent { } } -/// Wrapper to retrieve the shields info lazily. +/// Wrapper to retrieve some timeline item info lazily. #[derive(Clone, uniffi::Object)] -pub struct EventShieldsProvider(Arc); +pub struct LazyTimelineItemProvider(Arc); #[matrix_sdk_ffi_macros::export] -impl EventShieldsProvider { +impl LazyTimelineItemProvider { + /// Returns the shields for this event timeline item. fn get_shields(&self, strict: bool) -> Option { self.0.get_shield(strict).map(Into::into) } + + /// Returns some debug information for this event timeline item. + fn debug_info(&self) -> EventTimelineItemDebugInfo { + EventTimelineItemDebugInfo { + model: format!("{:#?}", self.0), + original_json: self.0.original_json().map(|raw| raw.json().get().to_owned()), + latest_edit_json: self.0.latest_edit_json().map(|raw| raw.json().get().to_owned()), + } + } }