mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-27 11:46:58 +02:00
Fix avatar loading in SwiftUI.
This commit is contained in:
@@ -26,9 +26,11 @@ struct AvatarImage: View {
|
||||
var displayName: String?
|
||||
var size: AvatarSize
|
||||
|
||||
@State private var avatar: AvatarViewState = .empty
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
switch viewModel.viewState {
|
||||
switch avatar {
|
||||
case .empty:
|
||||
ProgressView()
|
||||
case .placeholder(let firstCharacter, let colorIndex):
|
||||
@@ -42,13 +44,16 @@ struct AvatarImage: View {
|
||||
.frame(maxWidth: CGFloat(size.rawValue), maxHeight: CGFloat(size.rawValue))
|
||||
.clipShape(Circle())
|
||||
.onAppear {
|
||||
viewModel.loadAvatar(
|
||||
mxContentUri: mxContentUri,
|
||||
matrixItemId: matrixItemId,
|
||||
displayName: displayName,
|
||||
colorCount: theme.colors.namesAndAvatars.count,
|
||||
avatarSize: size
|
||||
)
|
||||
avatar = viewModel.placeholderAvatar(matrixItemId: matrixItemId,
|
||||
displayName: displayName,
|
||||
colorCount: theme.colors.namesAndAvatars.count)
|
||||
viewModel.loadAvatar(mxContentUri: mxContentUri,
|
||||
matrixItemId: matrixItemId,
|
||||
displayName: displayName,
|
||||
colorCount: theme.colors.namesAndAvatars.count,
|
||||
avatarSize: size ) { newState in
|
||||
avatar = newState
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,9 +26,11 @@ struct SpaceAvatarImage: View {
|
||||
var displayName: String?
|
||||
var size: AvatarSize
|
||||
|
||||
@State private var avatar: AvatarViewState = .empty
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
switch viewModel.viewState {
|
||||
switch avatar {
|
||||
case .empty:
|
||||
ProgressView()
|
||||
case .placeholder(let firstCharacter, let colorIndex):
|
||||
@@ -48,23 +50,27 @@ struct SpaceAvatarImage: View {
|
||||
.clipShape(RoundedRectangle(cornerRadius: 8))
|
||||
}
|
||||
}
|
||||
.onChange(of: displayName, perform: { value in
|
||||
viewModel.loadAvatar(
|
||||
mxContentUri: mxContentUri,
|
||||
matrixItemId: matrixItemId,
|
||||
displayName: value,
|
||||
colorCount: theme.colors.namesAndAvatars.count,
|
||||
avatarSize: size
|
||||
)
|
||||
})
|
||||
.onChange(of: displayName) { value in
|
||||
guard case .placeholder = avatar else { return }
|
||||
viewModel.loadAvatar(mxContentUri: mxContentUri,
|
||||
matrixItemId: matrixItemId,
|
||||
displayName: value,
|
||||
colorCount: theme.colors.namesAndAvatars.count,
|
||||
avatarSize: size) { newState in
|
||||
avatar = newState
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
viewModel.loadAvatar(
|
||||
mxContentUri: mxContentUri,
|
||||
matrixItemId: matrixItemId,
|
||||
displayName: displayName,
|
||||
colorCount: theme.colors.namesAndAvatars.count,
|
||||
avatarSize: size
|
||||
)
|
||||
avatar = viewModel.placeholderAvatar(matrixItemId: matrixItemId,
|
||||
displayName: displayName,
|
||||
colorCount: theme.colors.namesAndAvatars.count)
|
||||
viewModel.loadAvatar(mxContentUri: mxContentUri,
|
||||
matrixItemId: matrixItemId,
|
||||
displayName: displayName,
|
||||
colorCount: theme.colors.namesAndAvatars.count,
|
||||
avatarSize: size) { newState in
|
||||
avatar = newState
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,14 +22,22 @@ import Foundation
|
||||
final class AvatarViewModel: ObservableObject {
|
||||
private let avatarService: AvatarServiceProtocol
|
||||
|
||||
@Published private(set) var viewState = AvatarViewState.empty
|
||||
|
||||
init(avatarService: AvatarServiceProtocol) {
|
||||
self.avatarService = avatarService
|
||||
}
|
||||
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
func placeholderAvatar(matrixItemId: String,
|
||||
displayName: String?,
|
||||
colorCount: Int) -> AvatarViewState {
|
||||
let placeholderViewModel = PlaceholderAvatarViewModel(displayName: displayName,
|
||||
matrixItemId: matrixItemId,
|
||||
colorCount: colorCount)
|
||||
|
||||
return .placeholder(placeholderViewModel.firstCharacterCapitalized, placeholderViewModel.stableColorIndex)
|
||||
}
|
||||
|
||||
/// Load an avatar
|
||||
/// - Parameters:
|
||||
/// - mxContentUri: The matrix content URI of the avatar.
|
||||
@@ -41,14 +49,10 @@ final class AvatarViewModel: ObservableObject {
|
||||
matrixItemId: String,
|
||||
displayName: String?,
|
||||
colorCount: Int,
|
||||
avatarSize: AvatarSize) {
|
||||
let placeholderViewModel = PlaceholderAvatarViewModel(displayName: displayName,
|
||||
matrixItemId: matrixItemId,
|
||||
colorCount: colorCount)
|
||||
|
||||
viewState = .placeholder(placeholderViewModel.firstCharacterCapitalized, placeholderViewModel.stableColorIndex)
|
||||
|
||||
avatarSize: AvatarSize,
|
||||
avatarCompletion: @escaping (AvatarViewState) -> Void) {
|
||||
guard let mxContentUri = mxContentUri, mxContentUri.count > 0 else {
|
||||
avatarCompletion(placeholderAvatar(matrixItemId: matrixItemId, displayName: displayName, colorCount: colorCount))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -56,8 +60,9 @@ final class AvatarViewModel: ObservableObject {
|
||||
.sink { completion in
|
||||
guard case let .failure(error) = completion else { return }
|
||||
UILog.error("[AvatarService] Failed to retrieve avatar", context: error)
|
||||
// No need to call the completion, there's nothing we can do and the error is logged.
|
||||
} receiveValue: { image in
|
||||
self.viewState = .avatar(image)
|
||||
avatarCompletion(.avatar(image))
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user