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
|
|
|
|
|
2021-08-17 09:19:41 +00:00
|
|
|
/**
|
2021-08-18 14:12:12 +00:00
|
|
|
Simple ViewModel that supports loading an avatar image of a particular size
|
2021-08-17 09:19:41 +00:00
|
|
|
as specified in DesignKit and delivering the UIImage to the UI if possible.
|
|
|
|
*/
|
2021-08-14 23:36:15 +00:00
|
|
|
@available(iOS 14.0, *)
|
2021-08-17 09:19:41 +00:00
|
|
|
class AvatarViewModel: InjectableObject, ObservableObject {
|
2021-08-14 23:36:15 +00:00
|
|
|
|
|
|
|
@Inject var avatarService: AvatarServiceType
|
|
|
|
|
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-08-18 11:45:13 +00:00
|
|
|
func loadAvatar(
|
|
|
|
mxContentUri: String?,
|
|
|
|
matrixItemId: String,
|
|
|
|
displayName: String?,
|
|
|
|
colorCount: Int,
|
|
|
|
avatarSize: AvatarSize) {
|
|
|
|
|
|
|
|
self.viewState = .placeholder(
|
|
|
|
firstCharacterCapitalized(displayName),
|
|
|
|
stableColorIndex(matrixItemId: matrixItemId, colorCount: colorCount)
|
|
|
|
)
|
|
|
|
|
2021-08-14 23:36:15 +00:00
|
|
|
guard let mxContentUri = mxContentUri else { return }
|
|
|
|
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 }
|
|
|
|
MXLog.error("[AvatarService] Failed to retrieve avatar: \(error)")
|
2021-08-14 23:36:15 +00:00
|
|
|
// TODO: Report non-fatal error when we have Sentry or similar.
|
|
|
|
} 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)
|
|
|
|
}
|
|
|
|
|
2021-08-18 11:45:13 +00:00
|
|
|
/**
|
|
|
|
Get the first character of a string capialized or else an empty string.
|
|
|
|
*/
|
|
|
|
private func firstCharacterCapitalized(_ string: String?) -> String {
|
|
|
|
guard let character = string?.first else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return String(character).capitalized
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Provides the same color each time for a specified matrixId.
|
|
|
|
Same algorithm as in AvatarGenerator.
|
|
|
|
*/
|
|
|
|
private func stableColorIndex(matrixItemId: String, colorCount: Int) -> Int {
|
|
|
|
// Sum all characters
|
|
|
|
let sum = matrixItemId.utf8
|
|
|
|
.map({ UInt($0) })
|
|
|
|
.reduce(0, +)
|
|
|
|
// modulo the color count
|
|
|
|
return Int(sum) % colorCount
|
|
|
|
}
|
|
|
|
|
2021-08-14 23:36:15 +00:00
|
|
|
}
|