From bf4354d9b1a834703bb553a2571f7142a6a150e2 Mon Sep 17 00:00:00 2001
From: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
Date: Thu, 14 Nov 2024 11:39:42 -0500
Subject: [PATCH] profile: use new API

Change-Id: I754836e4b73bfdb2181dc21879e54a2a753b688a
---
 .../Bridging/AccountCreation/AccountAdapter.h |  3 +++
 .../AccountCreation/AccountAdapter.mm         |  8 ++++++
 .../Features/Settings/Me/EditProfileVM.swift  | 24 ++++++++---------
 .../Walkthrough/Models/WelcomeVM.swift        | 27 ++++++++++---------
 Ring/Ring/Services/AccountsService.swift      |  4 +++
 5 files changed, 41 insertions(+), 25 deletions(-)

diff --git a/Ring/Ring/Bridging/AccountCreation/AccountAdapter.h b/Ring/Ring/Bridging/AccountCreation/AccountAdapter.h
index aeacf1a96..9ff185b34 100644
--- a/Ring/Ring/Bridging/AccountCreation/AccountAdapter.h
+++ b/Ring/Ring/Bridging/AccountCreation/AccountAdapter.h
@@ -89,5 +89,8 @@
               destinationPath:(NSString *)destinationPath
                        scheme:(NSString *)scheme
                      password:(NSString *)password;
+-(void)updateProfile:(NSString *)accountId
+         displayName:(NSString *)displayName
+              avatar:(NSString *)avatar;
 
 @end
diff --git a/Ring/Ring/Bridging/AccountCreation/AccountAdapter.mm b/Ring/Ring/Bridging/AccountCreation/AccountAdapter.mm
index 2620fcebd..86bf9f7ed 100644
--- a/Ring/Ring/Bridging/AccountCreation/AccountAdapter.mm
+++ b/Ring/Ring/Bridging/AccountCreation/AccountAdapter.mm
@@ -225,6 +225,14 @@ static id <AccountAdapterDelegate> _delegate;
                                  std::string([newPassword UTF8String]));
 }
 
+-(void)updateProfile:(NSString *)accountId
+         displayName:(NSString *)displayName
+              avatar:(NSString *)avatar {
+    updateProfile(std::string([accountId UTF8String]),
+                  std::string([displayName UTF8String]),
+                  std::string([avatar UTF8String]), 1);
+}
+
 -(void)setAccountsActive:(BOOL) active {
     auto accounts = getAccountList();
     for(auto account: accounts) {
diff --git a/Ring/Ring/Features/Settings/Me/EditProfileVM.swift b/Ring/Ring/Features/Settings/Me/EditProfileVM.swift
index 0af4282dd..a103f2e67 100644
--- a/Ring/Ring/Features/Settings/Me/EditProfileVM.swift
+++ b/Ring/Ring/Features/Settings/Me/EditProfileVM.swift
@@ -42,18 +42,18 @@ class EditProfileVM: ObservableObject, AvatarViewDataModel {
     }
 
     func updateProfile() {
-        var photo: String?
-        if let image = self.profileImage?.fixOrientation(),
-           let imageData = image.convertToData(ofMaxSize: 40000) {
-            photo = imageData.base64EncodedString()
+        // Run on a background thread
+        Task {
+            var photo: String?
+
+            if let image = self.profileImage?.fixOrientation(),
+               let imageData = image.convertToData(ofMaxSize: 40000) {
+                photo = imageData.base64EncodedString()
+            }
+
+            let avatar: String = photo ?? ""
+
+            await self.accountService.updateProfile(accountId: self.account.id, displayName: self.profileName, avatar: avatar)
         }
-        guard let details = self.account.details else { return }
-        details.set(withConfigKeyModel: ConfigKeyModel(withKey: ConfigKey.displayName), withValue: self.profileName)
-        account.details = details
-        self.accountService.setAccountDetails(forAccountId: account.id, withDetails: details)
-        let accountUri = AccountModelHelper.init(withAccount: account).uri ?? ""
-        self.profileService.updateAccountProfile(accountId: account.id,
-                                                 alias: self.profileName,
-                                                 photo: photo, accountURI: accountUri)
     }
 }
diff --git a/Ring/Ring/Features/Walkthrough/Models/WelcomeVM.swift b/Ring/Ring/Features/Walkthrough/Models/WelcomeVM.swift
index 825736beb..93f7181d4 100644
--- a/Ring/Ring/Features/Walkthrough/Models/WelcomeVM.swift
+++ b/Ring/Ring/Features/Walkthrough/Models/WelcomeVM.swift
@@ -229,21 +229,22 @@ extension WelcomeVM {
     }
 
     private func saveProfile(accountId: String) {
-        guard let account = self.accountService.getAccount(fromAccountId: accountId) else {
-            return
-        }
-        let photo = convertProfileImageToBase64()
+        // Run on a background thread
+        Task {
+            guard let account = self.accountService.getAccount(fromAccountId: accountId) else {
+                return
+            }
+            let photo = convertProfileImageToBase64()
 
-        if photo == nil && profileName.isEmpty {
-            // No changes for profile
-            return
-        }
+            if photo == nil && profileName.isEmpty {
+                // No changes for profile
+                return
+            }
 
-        let accountURI = AccountModelHelper(withAccount: account).uri ?? ""
-        profileService.updateAccountProfile(accountId: accountId,
-                                            alias: profileName,
-                                            photo: photo,
-                                            accountURI: accountURI)
+            let avatar: String = photo ?? ""
+
+            await self.accountService.updateProfile(accountId: account.id, displayName: self.profileName, avatar: avatar)
+        }
     }
 
     private func convertProfileImageToBase64() -> String? {
diff --git a/Ring/Ring/Services/AccountsService.swift b/Ring/Ring/Services/AccountsService.swift
index d8a3a9a6b..94d3f215f 100644
--- a/Ring/Ring/Services/AccountsService.swift
+++ b/Ring/Ring/Services/AccountsService.swift
@@ -382,6 +382,10 @@ class AccountsService: AccountAdapterDelegate {
         accountAdapter.revokeDevice(account, password: password, deviceId: deviceId)
     }
 
+    func updateProfile(accountId: String, displayName: String, avatar: String) async {
+        accountAdapter.updateProfile(accountId, displayName: displayName, avatar: avatar)
+    }
+
     /**
      Gathers all the initial default details contained by any accounts, Ring or SIP.
 
-- 
GitLab