[fractal/fractal-next] AuthDialog: pass Client direclty to closure



commit d3286c6257083088b75491314798eb7c8ac63ffe
Author: Julian Sparber <julian sparber net>
Date:   Thu Oct 14 17:56:55 2021 +0200

    AuthDialog: pass Client direclty to closure

 src/components/auth_dialog.rs                      | 16 +++++-----
 .../account_settings/devices_page/device.rs        | 35 ++++++++++------------
 2 files changed, 25 insertions(+), 26 deletions(-)
---
diff --git a/src/components/auth_dialog.rs b/src/components/auth_dialog.rs
index cbd4bad4..e81e5f87 100644
--- a/src/components/auth_dialog.rs
+++ b/src/components/auth_dialog.rs
@@ -215,17 +215,19 @@ impl AuthDialog {
     pub async fn authenticate<
         Response: Send + 'static,
         F1: Future<Output = Result<Response, Error>> + Send + 'static,
-        FN: Fn(Option<AuthData>) -> F1 + Send + 'static + Sync + Clone,
+        FN: Fn(matrix_sdk::Client, Option<AuthData>) -> F1 + Send + 'static + Sync + Clone,
     >(
         &self,
         callback: FN,
     ) -> Option<Result<Response, Error>> {
         let priv_ = imp::AuthDialog::from_instance(self);
+        let client = self.session().client();
         let mut auth_data = None;
 
         loop {
             let callback_clone = callback.clone();
-            let handle = spawn_tokio!(async move { callback_clone(auth_data).await });
+            let client_clone = client.clone();
+            let handle = spawn_tokio!(async move { callback_clone(client_clone, auth_data).await });
             let response = handle.await.unwrap();
 
             let uiaa_info: UiaaInfo = match response {
@@ -266,11 +268,11 @@ impl AuthDialog {
                 _ => {
                     if let Some(session) = uiaa_info.session {
                         priv_.stack.set_visible_child_name("fallback");
-
-                        let client = self.session().client();
-                        let homeserver = spawn_tokio!(async move { client.homeserver().await })
-                            .await
-                            .unwrap();
+                        let client_clone = client.clone();
+                        let homeserver =
+                            spawn_tokio!(async move { client_clone.homeserver().await })
+                                .await
+                                .unwrap();
                         self.setup_fallback_page(
                             homeserver.as_str(),
                             flow.stages.first()?.as_ref(),
diff --git a/src/session/account_settings/devices_page/device.rs 
b/src/session/account_settings/devices_page/device.rs
index bddd20bb..8d585584 100644
--- a/src/session/account_settings/devices_page/device.rs
+++ b/src/session/account_settings/devices_page/device.rs
@@ -1,6 +1,6 @@
 use gtk::{glib, prelude::*, subclass::prelude::*};
 
-use crate::components::{AuthData, AuthDialog};
+use crate::components::AuthDialog;
 use crate::session::Session;
 use matrix_sdk::{
     encryption::identities::Device as CryptoDevice,
@@ -184,28 +184,25 @@ impl Device {
     /// Returns `true` for success
     pub async fn delete(&self, transient_for: Option<&impl IsA<gtk::Window>>) -> bool {
         let session = self.session();
-        let client = session.client();
         let device_id = self.device_id().to_owned();
 
-        let delete_fn = move |auth_data: Option<AuthData>| {
-            let device_id = device_id.clone();
-            let client = client.clone();
-
-            async move {
-                if let Some(auth) = auth_data {
-                    let auth = Some(auth.as_matrix_auth_data());
-                    let request = assign!(delete_device::Request::new(&device_id), { auth });
-                    client.send(request, None).await.map_err(Into::into)
-                } else {
-                    let request = delete_device::Request::new(&device_id);
-                    client.send(request, None).await.map_err(Into::into)
-                }
-            }
-        };
-
         let dialog = AuthDialog::new(transient_for, &session);
 
-        let result = dialog.authenticate(delete_fn).await;
+        let result = dialog
+            .authenticate(move |client, auth_data| {
+                let device_id = device_id.clone();
+                async move {
+                    if let Some(auth) = auth_data {
+                        let auth = Some(auth.as_matrix_auth_data());
+                        let request = assign!(delete_device::Request::new(&device_id), { auth });
+                        client.send(request, None).await.map_err(Into::into)
+                    } else {
+                        let request = delete_device::Request::new(&device_id);
+                        client.send(request, None).await.map_err(Into::into)
+                    }
+                }
+            })
+            .await;
         match result {
             Some(Ok(_)) => true,
             Some(Err(err)) => {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]