From abff7091cb522780f24515c54d4d8b1590724370 Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Thu, 9 Feb 2023 07:36:23 +0100 Subject: [PATCH] Add avatar in account selector menu --- .../AppAccount/AppAccountViewModel.swift | 21 +++++++++++++++---- .../AppAccount/AppAccountsSelectorView.swift | 4 ++-- .../Sources/DesignSystem/UIImage.swift | 16 ++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 Packages/DesignSystem/Sources/DesignSystem/UIImage.swift diff --git a/Packages/AppAccount/Sources/AppAccount/AppAccountViewModel.swift b/Packages/AppAccount/Sources/AppAccount/AppAccountViewModel.swift index 5464afbb..43f5cae9 100644 --- a/Packages/AppAccount/Sources/AppAccount/AppAccountViewModel.swift +++ b/Packages/AppAccount/Sources/AppAccount/AppAccountViewModel.swift @@ -1,15 +1,19 @@ import Models import Network import SwiftUI +import DesignSystem @MainActor public class AppAccountViewModel: ObservableObject { + private static var avatarsCache: [String: UIImage] = [:] + var appAccount: AppAccount let client: Client let isCompact: Bool @Published var account: Account? - + @Published var roundedAvatar: UIImage? + var acct: String { if let acct = appAccount.accountName { return acct @@ -31,8 +35,17 @@ public class AppAccountViewModel: ObservableObject { appAccount.accountName = "\(account.acct)@\(appAccount.server)" try appAccount.save() } - } catch { - print(error) - } + + if let account { + if let image = Self.avatarsCache[account.id] { + self.roundedAvatar = image + } else if let (data, _) = try? await URLSession.shared.data(from: account.avatar), + let image = UIImage(data: data)?.roundedImage { + self.roundedAvatar = image + Self.avatarsCache[account.id] = image + } + } + + } catch { } } } diff --git a/Packages/AppAccount/Sources/AppAccount/AppAccountsSelectorView.swift b/Packages/AppAccount/Sources/AppAccount/AppAccountsSelectorView.swift index 28af48bd..69562576 100644 --- a/Packages/AppAccount/Sources/AppAccount/AppAccountsSelectorView.swift +++ b/Packages/AppAccount/Sources/AppAccount/AppAccountsSelectorView.swift @@ -81,8 +81,8 @@ public struct AppAccountsSelectorView: View { HapticManager.shared.fireHaptic(of: .buttonPress) } label: { HStack { - if viewModel.account?.id == currentAccount.account?.id { - Image(systemName: "checkmark.circle.fill") + if let image = viewModel.roundedAvatar { + Image(uiImage: image) } Text("\(viewModel.account?.displayName ?? "")") } diff --git a/Packages/DesignSystem/Sources/DesignSystem/UIImage.swift b/Packages/DesignSystem/Sources/DesignSystem/UIImage.swift new file mode 100644 index 00000000..3eb8d996 --- /dev/null +++ b/Packages/DesignSystem/Sources/DesignSystem/UIImage.swift @@ -0,0 +1,16 @@ +import UIKit + +extension UIImage{ + public var roundedImage: UIImage? { + let rect = CGRect(origin:CGPoint(x: 0, y: 0), size: self.size) + UIGraphicsBeginImageContextWithOptions(self.size, false, 1) + defer { UIGraphicsEndImageContext() } + UIBezierPath( + roundedRect: rect, + cornerRadius: self.size.height + ).addClip() + self.draw(in: rect) + return UIGraphicsGetImageFromCurrentImageContext() + } +} +