2021-08-14 23:36:15 +00:00
|
|
|
//
|
|
|
|
// Copyright 2021 New Vector Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
|
|
|
import DesignKit
|
|
|
|
|
|
|
|
@available(iOS 14.0, *)
|
2021-09-13 10:36:33 +00:00
|
|
|
/// Simple ViewModel that supports loading an avatar image
|
2021-08-17 09:19:41 +00:00
|
|
|
class AvatarViewModel: InjectableObject, ObservableObject {
|
2021-08-14 23:36:15 +00:00
|
|
|
|
2021-09-08 14:47:24 +00:00
|
|
|
@Inject var avatarService: AvatarServiceProtocol
|
2021-08-14 23:36:15 +00:00
|
|
|
|
2021-08-18 11:45:13 +00:00
|
|
|
@Published private(set) var viewState = AvatarViewState.empty
|
2021-08-14 23:36:15 +00:00
|
|
|
|
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
|
2021-09-13 10:36:33 +00:00
|
|
|
/// Load an avatar
|
|
|
|
/// - Parameters:
|
|
|
|
/// - mxContentUri: The matrix content URI of the avatar.
|
|
|
|
/// - matrixItemId: The id of the matrix item represented by the avatar.
|
|
|
|
/// - displayName: Display name of the avatar.
|
|
|
|
/// - colorCount: The count of total avatar colors used to generate the stable color index.
|
|
|
|
/// - avatarSize: The size of the avatar to fetch (as defined within DesignKit).
|
2021-08-18 11:45:13 +00:00
|
|
|
func loadAvatar(
|
|
|
|
mxContentUri: String?,
|
|
|
|
matrixItemId: String,
|
|
|
|
displayName: String?,
|
|
|
|
colorCount: Int,
|
2022-03-24 10:00:29 +00:00
|
|
|
avatarSize: AvatarSize) {
|
2021-08-18 11:45:13 +00:00
|
|
|
|
2022-03-15 11:51:17 +00:00
|
|
|
let placeholderViewModel = PlaceholderAvatarViewModel(displayName: displayName,
|
|
|
|
matrixItemId: matrixItemId,
|
|
|
|
colorCount: colorCount)
|
|
|
|
|
|
|
|
self.viewState = .placeholder(placeholderViewModel.firstCharacterCapitalized, placeholderViewModel.stableColorIndex)
|
2021-08-18 11:45:13 +00:00
|
|
|
|
2021-10-07 06:23:32 +00:00
|
|
|
guard let mxContentUri = mxContentUri, mxContentUri.count > 0 else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-24 10:00:29 +00:00
|
|
|
avatarService.avatarImage(mxContentUri: mxContentUri, avatarSize: avatarSize)
|
2021-08-18 14:12:12 +00:00
|
|
|
.sink { completion in
|
|
|
|
guard case let .failure(error) = completion else { return }
|
2021-09-09 21:42:00 +00:00
|
|
|
UILog.error("[AvatarService] Failed to retrieve avatar: \(error)")
|
2021-08-14 23:36:15 +00:00
|
|
|
} receiveValue: { image in
|
2021-08-18 11:45:13 +00:00
|
|
|
self.viewState = .avatar(image)
|
2021-08-14 23:36:15 +00:00
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
|
|
|
}
|
|
|
|
}
|