Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Space Switcher avatars #7306

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions RiotSwiftUI/Modules/Common/Avatar/View/AvatarImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
}
}
}
}
Expand Down
40 changes: 23 additions & 17 deletions RiotSwiftUI/Modules/Common/Avatar/View/SpaceAvatarImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
}
}
}
}
Expand Down
25 changes: 15 additions & 10 deletions RiotSwiftUI/Modules/Common/Avatar/ViewModel/AvatarViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@ import Foundation
final class AvatarViewModel: ObservableObject {
Copy link
Member Author

@pixlwave pixlwave Jan 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe at this stage we should rename this to AvatarLoader/AvatarProvider?

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.
Expand All @@ -41,23 +49,20 @@ 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
}

avatarService.avatarImage(mxContentUri: mxContentUri, avatarSize: avatarSize)
.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)
}
Expand Down
1 change: 1 addition & 0 deletions changelog.d/7305.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Space Switcher: Fix a bug where the avatars would all be the same.