2021-08-12 10:52:06 +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 MatrixSDK
|
|
|
|
import Combine
|
|
|
|
import DesignKit
|
|
|
|
|
|
|
|
/**
|
|
|
|
Provides a simple api to retrieve and cache avatar images
|
|
|
|
*/
|
|
|
|
protocol AvatarServiceType {
|
|
|
|
@available(iOS 14.0, *)
|
2021-08-14 23:36:15 +00:00
|
|
|
func avatarImage(mxContentUri: String, avatarSize: AvatarSize) -> Future<UIImage, Error>
|
2021-08-12 10:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum AvatarServiceError: Error {
|
|
|
|
case pathNotfound
|
|
|
|
case loadingImageFailed(Error?)
|
|
|
|
}
|
|
|
|
|
|
|
|
class AvatarService: AvatarServiceType {
|
|
|
|
|
|
|
|
private enum Constants {
|
|
|
|
static let mimeType = "image/jpeg"
|
|
|
|
static let thumbnailMethod = MXThumbnailingMethodCrop
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:57:19 +00:00
|
|
|
private let mediaManager: MXMediaManager
|
2021-08-12 10:52:06 +00:00
|
|
|
|
2021-08-14 23:36:15 +00:00
|
|
|
init(mediaManager: MXMediaManager) {
|
2021-08-12 10:52:06 +00:00
|
|
|
self.mediaManager = mediaManager
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:57:19 +00:00
|
|
|
/**
|
2021-08-14 23:36:15 +00:00
|
|
|
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.
|
2021-08-12 15:57:19 +00:00
|
|
|
|
2021-08-14 23:36:15 +00:00
|
|
|
- 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
|
2021-08-12 15:57:19 +00:00
|
|
|
*/
|
2021-08-12 10:52:06 +00:00
|
|
|
@available(iOS 14.0, *)
|
2021-08-14 23:36:15 +00:00
|
|
|
func avatarImage(mxContentUri: String, avatarSize: AvatarSize) -> Future<UIImage, Error> {
|
2021-08-12 10:52:06 +00:00
|
|
|
|
|
|
|
let cachePath = MXMediaManager.thumbnailCachePath(
|
|
|
|
forMatrixContentURI: mxContentUri,
|
|
|
|
andType: Constants.mimeType,
|
|
|
|
inFolder: nil,
|
2021-08-14 23:36:15 +00:00
|
|
|
toFitViewSize: avatarSize.size,
|
2021-08-12 10:52:06 +00:00
|
|
|
with: Constants.thumbnailMethod)
|
|
|
|
|
2021-08-14 23:36:15 +00:00
|
|
|
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))
|
|
|
|
}
|
2021-08-12 10:52:06 +00:00
|
|
|
|
|
|
|
self.mediaManager.downloadThumbnail(
|
|
|
|
fromMatrixContentURI: mxContentUri,
|
|
|
|
withType: Constants.mimeType,
|
|
|
|
inFolder: nil,
|
2021-08-14 23:36:15 +00:00
|
|
|
toFitViewSize: avatarSize.size,
|
2021-08-12 10:52:06 +00:00
|
|
|
with: Constants.thumbnailMethod) { path in
|
|
|
|
guard let path = path else {
|
|
|
|
promise(.failure(AvatarServiceError.pathNotfound))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-12 12:49:56 +00:00
|
|
|
guard let image = MXMediaManager.loadThroughCache(withFilePath: path),
|
|
|
|
let imageUp = Self.orientImageUp(image: image) else {
|
|
|
|
promise(.failure(AvatarServiceError.loadingImageFailed(nil)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
promise(.success(imageUp))
|
2021-08-12 10:52:06 +00:00
|
|
|
} failure: { error in
|
|
|
|
promise(.failure(AvatarServiceError.loadingImageFailed(error)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 12:49:56 +00:00
|
|
|
private static func orientImageUp(image: UIImage) -> UIImage? {
|
|
|
|
guard let image = image.cgImage else { return nil }
|
2021-08-12 10:52:06 +00:00
|
|
|
return UIImage(cgImage: image, scale: 1.0, orientation: .up)
|
|
|
|
}
|
|
|
|
}
|