Store and use default account avatar to improve display speed

This commit is contained in:
benoit.martins 2025-02-06 23:53:46 +01:00
parent 56f732144d
commit 413e1b4082
2 changed files with 34 additions and 11 deletions

View file

@ -81,7 +81,6 @@ struct ContentView: View {
@State private var isShowLoginFragment: Bool = false @State private var isShowLoginFragment: Bool = false
private let avatarSize = 45.0 private let avatarSize = 45.0
@State private var imagePath: URL?
var body: some View { var body: some View {
let pub = NotificationCenter.default let pub = NotificationCenter.default
@ -316,8 +315,8 @@ struct ContentView: View {
VStack(spacing: 0) { VStack(spacing: 0) {
if searchIsActive == false { if searchIsActive == false {
HStack { HStack {
if (accountProfileViewModel.accountModelIndex ?? 0) < CoreContext.shared.accounts.count { if sharedMainViewModel.defaultAvatar != nil {
AsyncImage(url: imagePath) { image in AsyncImage(url: sharedMainViewModel.defaultAvatar) { image in
switch image { switch image {
case .empty: case .empty:
ProgressView() ProgressView()
@ -342,22 +341,29 @@ struct ContentView: View {
.onTapGesture { .onTapGesture {
openMenu() openMenu()
} }
.onAppear {
if let accountModelIndex = accountProfileViewModel.accountModelIndex,
accountModelIndex < CoreContext.shared.accounts.count {
imagePath = CoreContext.shared.accounts[accountModelIndex].getImagePath()
}
}
.onChange(of: CoreContext.shared.accounts[accountProfileViewModel.accountModelIndex ?? 0].usernaneAvatar) { _ in .onChange(of: CoreContext.shared.accounts[accountProfileViewModel.accountModelIndex ?? 0].usernaneAvatar) { _ in
if let accountModelIndex = accountProfileViewModel.accountModelIndex, if let accountModelIndex = accountProfileViewModel.accountModelIndex,
accountModelIndex < CoreContext.shared.accounts.count { accountModelIndex < CoreContext.shared.accounts.count {
imagePath = CoreContext.shared.accounts[accountModelIndex].getImagePath() sharedMainViewModel.changeDefaultAvatar(defaultAvatarURL: CoreContext.shared.accounts[accountModelIndex].getImagePath())
} }
} }
.onReceive(imageChanged) { _ in .onReceive(imageChanged) { _ in
if let accountModelIndex = accountProfileViewModel.accountModelIndex, if let accountModelIndex = accountProfileViewModel.accountModelIndex,
accountModelIndex < CoreContext.shared.accounts.count { accountModelIndex < CoreContext.shared.accounts.count {
imagePath = CoreContext.shared.accounts[accountModelIndex].getImagePath() sharedMainViewModel.changeDefaultAvatar(defaultAvatarURL: CoreContext.shared.accounts[accountModelIndex].getImagePath())
}
}
} else {
Image(uiImage: contactsManager.textToImage(
firstName: CoreContext.shared.accounts[accountProfileViewModel.accountModelIndex ?? 0].avatarModel?.name ?? "",
lastName: ""))
.resizable()
.frame(width: avatarSize, height: avatarSize)
.clipShape(Circle())
.onAppear {
if let accountModelIndex = accountProfileViewModel.accountModelIndex,
accountModelIndex < CoreContext.shared.accounts.count {
sharedMainViewModel.changeDefaultAvatar(defaultAvatarURL: CoreContext.shared.accounts[accountModelIndex].getImagePath())
} }
} }
} }

View file

@ -26,10 +26,12 @@ class SharedMainViewModel: ObservableObject {
@Published var welcomeViewDisplayed = false @Published var welcomeViewDisplayed = false
@Published var generalTermsAccepted = false @Published var generalTermsAccepted = false
@Published var displayProfileMode = false @Published var displayProfileMode = false
@Published var defaultAvatar: URL?
let welcomeViewKey = "welcome_view" let welcomeViewKey = "welcome_view"
let generalTermsKey = "general_terms" let generalTermsKey = "general_terms"
let displayProfileModeKey = "display_profile_mode" let displayProfileModeKey = "display_profile_mode"
let defaultAvatarKey = "default_avatar"
var maxWidth = 400.0 var maxWidth = 400.0
@ -53,6 +55,14 @@ class SharedMainViewModel: ObservableObject {
} else { } else {
displayProfileMode = preferences.bool(forKey: displayProfileModeKey) displayProfileMode = preferences.bool(forKey: displayProfileModeKey)
} }
if preferences.object(forKey: defaultAvatarKey) == nil {
preferences.set(defaultAvatar, forKey: defaultAvatarKey)
} else {
if let defaultAvatarTmp = preferences.url(forKey: defaultAvatarKey) {
defaultAvatar = defaultAvatarTmp
}
}
} }
func changeWelcomeView() { func changeWelcomeView() {
@ -82,4 +92,11 @@ class SharedMainViewModel: ObservableObject {
displayProfileMode = false displayProfileMode = false
preferences.set(displayProfileMode, forKey: displayProfileModeKey) preferences.set(displayProfileMode, forKey: displayProfileModeKey)
} }
func changeDefaultAvatar(defaultAvatarURL: URL) {
let preferences = UserDefaults.standard
defaultAvatar = defaultAvatarURL
preferences.set(defaultAvatar, forKey: defaultAvatarKey)
}
} }