Do avatar placeholder in SwiftUI, Add AvatarViewModel, Add dependency injection for MxMediaManager dependency.

This commit is contained in:
David Langley
2021-08-15 00:36:15 +01:00
parent 1c1122da98
commit c68e901de6
28 changed files with 490 additions and 190 deletions
@@ -0,0 +1,99 @@
//
// 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 MatrixSDK
import Combine
import DesignKit
/**
Provides a simple api to retrieve and cache avatar images
*/
protocol AvatarServiceType {
@available(iOS 14.0, *)
func avatarImage(mxContentUri: String, avatarSize: AvatarSize) -> Future<UIImage, Error>
}
enum AvatarServiceError: Error {
case pathNotfound
case loadingImageFailed(Error?)
}
class AvatarService: AvatarServiceType {
private enum Constants {
static let mimeType = "image/jpeg"
static let thumbnailMethod = MXThumbnailingMethodCrop
}
private let mediaManager: MXMediaManager
init(mediaManager: MXMediaManager) {
self.mediaManager = mediaManager
}
/**
Given an mxContentUri, this function returns a future of UIImage.
If possible to retrieve the actual image it will from network or cache, otherwise it will error.
- Parameter mxContentUri: matrix uri of the avatar to fetch
- Returns: A Future of UIImage that returns an error if it fails to fetch the image
*/
@available(iOS 14.0, *)
func avatarImage(mxContentUri: String, avatarSize: AvatarSize) -> Future<UIImage, Error> {
let cachePath = MXMediaManager.thumbnailCachePath(
forMatrixContentURI: mxContentUri,
andType: Constants.mimeType,
inFolder: nil,
toFitViewSize: avatarSize.size,
with: Constants.thumbnailMethod)
return Future<UIImage, Error> { promise in
if let image = MXMediaManager.loadThroughCache(withFilePath: cachePath),
let imageUp = Self.orientImageUp(image: image) {
// Already cached return avatar
promise(.success(imageUp))
}
self.mediaManager.downloadThumbnail(
fromMatrixContentURI: mxContentUri,
withType: Constants.mimeType,
inFolder: nil,
toFitViewSize: avatarSize.size,
with: Constants.thumbnailMethod) { path in
guard let path = path else {
promise(.failure(AvatarServiceError.pathNotfound))
return
}
guard let image = MXMediaManager.loadThroughCache(withFilePath: path),
let imageUp = Self.orientImageUp(image: image) else {
promise(.failure(AvatarServiceError.loadingImageFailed(nil)))
return
}
promise(.success(imageUp))
} failure: { error in
promise(.failure(AvatarServiceError.loadingImageFailed(error)))
}
}
}
private static func orientImageUp(image: UIImage) -> UIImage? {
guard let image = image.cgImage else { return nil }
return UIImage(cgImage: image, scale: 1.0, orientation: .up)
}
}
@@ -0,0 +1,44 @@
//
// 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, *)
class AvatarViewModel: ObservableObject, Injectable {
var dependencies: DependencyContainer!
@Inject var avatarService: AvatarServiceType
@Published private(set) var viewState = AvatarViewState()
private var cancellables = Set<AnyCancellable>()
func loadAvatar(mxContentUri: String?, avatarSize: AvatarSize) {
guard let mxContentUri = mxContentUri else { return }
avatarService.avatarImage(mxContentUri: mxContentUri, avatarSize: avatarSize)
.sink { error in
MXLog.error("[AvatarService] Failed to retrieve avatar.")
// TODO: Report non-fatal error when we have Sentry or similar.
} receiveValue: { image in
self.viewState.avatarImage = image
}
.store(in: &cancellables)
}
}
@@ -0,0 +1,21 @@
//
// 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
struct AvatarViewState {
var avatarImage: UIImage?
}