From bc1c737973a8099b2478cc49a974335229af02c3 Mon Sep 17 00:00:00 2001 From: Benoit Martins Date: Fri, 5 Sep 2025 15:49:24 +0200 Subject: [PATCH] Use saveImage on core queue --- Linphone/Contacts/ContactsManager.swift | 77 ++++++------------- .../ViewModel/AccountProfileViewModel.swift | 12 ++- Linphone/UI/Main/Viewmodel/AccountModel.swift | 12 ++- Linphone/Utils/EditContactController.swift | 62 ++++++++------- 4 files changed, 63 insertions(+), 100 deletions(-) diff --git a/Linphone/Contacts/ContactsManager.swift b/Linphone/Contacts/ContactsManager.swift index c940069df..a087637b6 100644 --- a/Linphone/Contacts/ContactsManager.swift +++ b/Linphone/Contacts/ContactsManager.swift @@ -154,25 +154,22 @@ final class ContactsManager: ObservableObject { let imageThumbnail = UIImage(data: contact.thumbnailImageData ?? Data()) if let image = imageThumbnail { - DispatchQueue.main.async { - self.saveImage( - image: image, - name: contact.givenName + contact.familyName, - prefix: "", - contact: newContact, linphoneFriend: self.nativeAddressBookFriendList, existingFriend: nil) { - dispatchGroup.leave() - } - } + self.saveImage( + image: image, + name: contact.givenName + contact.familyName, + prefix: "", + contact: newContact, linphoneFriend: self.nativeAddressBookFriendList, existingFriend: nil) { + dispatchGroup.leave() + } } else { - self.textToImageInMainThread(firstName: contact.givenName, lastName: contact.familyName) { image in - self.saveImage( - image: image, - name: contact.givenName + contact.familyName, - prefix: "-default", - contact: newContact, linphoneFriend: self.nativeAddressBookFriendList, existingFriend: nil) { - dispatchGroup.leave() - } - } + let image = self.textToImage(firstName: contact.givenName, lastName: contact.familyName) + self.saveImage( + image: image, + name: contact.givenName + contact.familyName, + prefix: "-default", + contact: newContact, linphoneFriend: self.nativeAddressBookFriendList, existingFriend: nil) { + dispatchGroup.leave() + } } }) @@ -197,33 +194,6 @@ final class ContactsManager: ObservableObject { } } - func textToImageInMainThread(firstName: String?, lastName: String?, completion: @escaping (UIImage) -> Void) { - DispatchQueue.main.async { - let lblNameInitialize = UILabel() - lblNameInitialize.frame.size = CGSize(width: 200.0, height: 200.0) - lblNameInitialize.font = UIFont(name: "NotoSans-ExtraBold", size: 80) ?? UIFont.boldSystemFont(ofSize: 80) - lblNameInitialize.textColor = UIColor(Color.grayMain2c600) - - let textToDisplay = (firstName?.first.map { String($0) } ?? "") + (lastName?.first.map { String($0) } ?? "") - lblNameInitialize.text = textToDisplay.uppercased() - lblNameInitialize.textAlignment = .center - lblNameInitialize.backgroundColor = UIColor(Color.grayMain2c200) - lblNameInitialize.layer.cornerRadius = 10.0 - lblNameInitialize.clipsToBounds = true - - UIGraphicsBeginImageContextWithOptions(lblNameInitialize.frame.size, false, 0) - defer { UIGraphicsEndImageContext() } - - if let context = UIGraphicsGetCurrentContext() { - lblNameInitialize.layer.render(in: context) - let image = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage() - completion(image) - } else { - completion(UIImage()) - } - } - } - func textToImage(firstName: String?, lastName: String?) -> UIImage { let firstInitial = firstName?.first.map { String($0) } ?? "" let lastInitial = lastName?.first.map { String($0) } ?? "" @@ -468,15 +438,14 @@ final class ContactsManager: ObservableObject { imageData: "" ) - self.textToImageInMainThread(firstName: friend.name ?? addressTmp, lastName: "") { image in - self.saveImage( - image: image, - name: friend.name ?? addressTmp, - prefix: "-default", - contact: newContact, linphoneFriend: friendList.displayName ?? "No Display Name", existingFriend: nil) { - dispatchGroup.leave() - } - } + let image = self.textToImage(firstName: friend.name ?? addressTmp, lastName: "") + self.saveImage( + image: image, + name: friend.name ?? addressTmp, + prefix: "-default", + contact: newContact, linphoneFriend: friendList.displayName ?? "No Display Name", existingFriend: nil) { + dispatchGroup.leave() + } } dispatchGroup.notify(queue: .main) { diff --git a/Linphone/UI/Main/Settings/ViewModel/AccountProfileViewModel.swift b/Linphone/UI/Main/Settings/ViewModel/AccountProfileViewModel.swift index 93fa29cb0..74dcfaa42 100644 --- a/Linphone/UI/Main/Settings/ViewModel/AccountProfileViewModel.swift +++ b/Linphone/UI/Main/Settings/ViewModel/AccountProfileViewModel.swift @@ -52,13 +52,11 @@ class AccountProfileViewModel: ObservableObject { if self.getImagePath().lastPathComponent.contains("-default") || self.getImagePath().lastPathComponent == "Documents" { let usernameTmp = CoreContext.shared.accounts[self.accountModelIndex!].usernaneAvatar - DispatchQueue.main.async { - self.saveImage( - image: ContactsManager.shared.textToImage( - firstName: displayNameAccountModel.isEmpty ? usernameTmp : displayNameAccountModel, lastName: ""), - name: usernameTmp, - prefix: "-default") - } + self.saveImage( + image: ContactsManager.shared.textToImage( + firstName: displayNameAccountModel.isEmpty ? usernameTmp : displayNameAccountModel, lastName: ""), + name: usernameTmp, + prefix: "-default") } } diff --git a/Linphone/UI/Main/Viewmodel/AccountModel.swift b/Linphone/UI/Main/Viewmodel/AccountModel.swift index ba4ea5c3f..6944cce9c 100644 --- a/Linphone/UI/Main/Viewmodel/AccountModel.swift +++ b/Linphone/UI/Main/Viewmodel/AccountModel.swift @@ -105,13 +105,11 @@ class AccountModel: ObservableObject { if !photoAvatarModelKey.isEmpty { if preferences.object(forKey: photoAvatarModelKey) == nil { - DispatchQueue.main.async { - self.saveImage( - image: ContactsManager.shared.textToImage( - firstName: usernaneAvatarTmp, lastName: ""), - name: usernaneAvatarTmp, - prefix: "-default") - } + self.saveImage( + image: ContactsManager.shared.textToImage( + firstName: usernaneAvatarTmp, lastName: ""), + name: usernaneAvatarTmp, + prefix: "-default") } else { photoAvatarModelTmp = preferences.string(forKey: photoAvatarModelKey)! } diff --git a/Linphone/Utils/EditContactController.swift b/Linphone/Utils/EditContactController.swift index 1e6b970fe..a59b812ae 100644 --- a/Linphone/Utils/EditContactController.swift +++ b/Linphone/Utils/EditContactController.swift @@ -26,38 +26,36 @@ struct EditContactView: UIViewControllerRepresentable { class Coordinator: NSObject, CNContactViewControllerDelegate, UINavigationControllerDelegate { func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) { if let cnc = contact { - DispatchQueue.main.asyncAfter(deadline: .now() + 1) { - self.parent.contact = cnc - - let newContact = Contact( - identifier: cnc.identifier, - firstName: cnc.givenName, - lastName: cnc.familyName, - organizationName: cnc.organizationName, - jobTitle: "", - displayName: cnc.nickname, - sipAddresses: cnc.instantMessageAddresses.map { $0.value.service == "SIP" ? $0.value.username : "" }, - phoneNumbers: cnc.phoneNumbers.map { PhoneNumber(numLabel: $0.label ?? "", num: $0.value.stringValue)}, - imageData: "" - ) - - let imageThumbnail = UIImage(data: contact!.thumbnailImageData ?? Data()) - ContactsManager.shared.saveImage( - image: imageThumbnail - ?? ContactsManager.shared.textToImage( - firstName: cnc.givenName.isEmpty - && cnc.familyName.isEmpty - && cnc.phoneNumbers.first?.value.stringValue != nil - ? cnc.phoneNumbers.first!.value.stringValue - : cnc.givenName, lastName: cnc.familyName), - name: cnc.givenName + cnc.familyName, - prefix: ((imageThumbnail == nil) ? "-default" : ""), - contact: newContact, - linphoneFriend: "Native address-book", - existingFriend: ContactsManager.shared.getFriendWithContact(contact: newContact)) { - MagicSearchSingleton.shared.searchForContacts() - } - } + self.parent.contact = cnc + + let newContact = Contact( + identifier: cnc.identifier, + firstName: cnc.givenName, + lastName: cnc.familyName, + organizationName: cnc.organizationName, + jobTitle: "", + displayName: cnc.nickname, + sipAddresses: cnc.instantMessageAddresses.map { $0.value.service == "SIP" ? $0.value.username : "" }, + phoneNumbers: cnc.phoneNumbers.map { PhoneNumber(numLabel: $0.label ?? "", num: $0.value.stringValue)}, + imageData: "" + ) + + let imageThumbnail = UIImage(data: contact!.thumbnailImageData ?? Data()) + ContactsManager.shared.saveImage( + image: imageThumbnail + ?? ContactsManager.shared.textToImage( + firstName: cnc.givenName.isEmpty + && cnc.familyName.isEmpty + && cnc.phoneNumbers.first?.value.stringValue != nil + ? cnc.phoneNumbers.first!.value.stringValue + : cnc.givenName, lastName: cnc.familyName), + name: cnc.givenName + cnc.familyName, + prefix: ((imageThumbnail == nil) ? "-default" : ""), + contact: newContact, + linphoneFriend: "Native address-book", + existingFriend: ContactsManager.shared.getFriendWithContact(contact: newContact)) { + MagicSearchSingleton.shared.searchForContacts() + } } viewController.dismiss(animated: true, completion: {}) }