mastodon-ios/Mastodon/Protocol/AvatarConfigurableView.swift

114 lines
4.3 KiB
Swift
Raw Normal View History

2021-02-04 12:28:16 +01:00
//
// AvatarConfigurableView.swift
// Mastodon
//
// Created by Cirno MainasuK on 2021-2-4.
//
2021-07-21 13:45:24 +02:00
import Foundation
2021-02-04 12:28:16 +01:00
import UIKit
2021-07-21 13:45:24 +02:00
import Combine
2021-02-04 12:28:16 +01:00
import AlamofireImage
import FLAnimatedImage
2021-02-04 12:28:16 +01:00
protocol AvatarConfigurableView {
2021-02-23 08:16:55 +01:00
static var configurableAvatarImageSize: CGSize { get }
static var configurableAvatarImageCornerRadius: CGFloat { get }
2021-07-21 13:45:24 +02:00
var configurableAvatarImageView: FLAnimatedImageView? { get }
2021-02-23 08:16:55 +01:00
func configure(with configuration: AvatarConfigurableViewConfiguration)
2021-02-04 12:28:16 +01:00
func avatarConfigurableView(_ avatarConfigurableView: AvatarConfigurableView, didFinishConfiguration configuration: AvatarConfigurableViewConfiguration)
}
extension AvatarConfigurableView {
2021-02-23 08:16:55 +01:00
public func configure(with configuration: AvatarConfigurableViewConfiguration) {
2021-02-04 12:28:16 +01:00
let placeholderImage: UIImage = {
guard let placeholderImage = configuration.placeholderImage else {
2021-07-19 11:12:45 +02:00
#if APP_EXTENSION
let placeholderImage = configuration.placeholderImage ?? UIImage.placeholder(size: Self.configurableAvatarImageSize, color: .systemFill)
if Self.configurableAvatarImageCornerRadius < Self.configurableAvatarImageSize.width * 0.5 {
return placeholderImage
.af.imageAspectScaled(toFill: Self.configurableAvatarImageSize)
.af.imageRounded(withCornerRadius: Self.configurableAvatarImageCornerRadius, divideRadiusByImageScale: false)
} else {
return placeholderImage.af.imageRoundedIntoCircle()
}
#else
return AppContext.shared.placeholderImageCacheService.image(
color: .systemFill,
size: Self.configurableAvatarImageSize,
cornerRadius: Self.configurableAvatarImageCornerRadius
)
2021-07-19 11:12:45 +02:00
#endif
}
return placeholderImage
2021-02-04 12:28:16 +01:00
}()
2021-05-10 10:06:00 +02:00
// accessibility
configurableAvatarImageView?.accessibilityIgnoresInvertColors = true
2021-07-21 13:45:24 +02:00
2021-02-04 12:28:16 +01:00
defer {
avatarConfigurableView(self, didFinishConfiguration: configuration)
}
2021-04-09 11:31:43 +02:00
2021-07-21 13:45:24 +02:00
guard let configurableAvatarImageView = configurableAvatarImageView else {
2021-02-04 12:28:16 +01:00
return
}
// set corner radius (due to GIF won't crop)
2021-07-21 13:45:24 +02:00
configurableAvatarImageView.layer.masksToBounds = true
configurableAvatarImageView.layer.cornerRadius = Self.configurableAvatarImageCornerRadius
configurableAvatarImageView.layer.cornerCurve = Self.configurableAvatarImageCornerRadius < Self.configurableAvatarImageSize.width * 0.5 ? .continuous :.circular
// set border
2021-07-21 13:45:24 +02:00
configureLayerBorder(view: configurableAvatarImageView, configuration: configuration)
2021-07-21 13:45:24 +02:00
configurableAvatarImageView.setImage(
url: configuration.avatarImageURL,
placeholder: placeholderImage,
2021-07-21 13:45:24 +02:00
scaleToSize: Self.configurableAvatarImageSize
)
2021-02-04 12:28:16 +01:00
}
func configureLayerBorder(view: UIView, configuration: AvatarConfigurableViewConfiguration) {
guard let borderWidth = configuration.borderWidth, borderWidth > 0,
let borderColor = configuration.borderColor else {
return
}
view.layer.masksToBounds = true
view.layer.cornerRadius = Self.configurableAvatarImageCornerRadius
view.layer.cornerCurve = .continuous
view.layer.borderColor = borderColor.cgColor
view.layer.borderWidth = borderWidth
}
2021-02-04 12:28:16 +01:00
func avatarConfigurableView(_ avatarConfigurableView: AvatarConfigurableView, didFinishConfiguration configuration: AvatarConfigurableViewConfiguration) { }
}
struct AvatarConfigurableViewConfiguration {
2021-07-19 11:12:45 +02:00
2021-02-23 08:16:55 +01:00
let avatarImageURL: URL?
let placeholderImage: UIImage?
let borderColor: UIColor?
let borderWidth: CGFloat?
2021-02-04 12:28:16 +01:00
2021-04-28 14:10:17 +02:00
let keepImageCorner: Bool
init(
avatarImageURL: URL?,
placeholderImage: UIImage? = nil,
borderColor: UIColor? = nil,
2021-04-28 14:10:17 +02:00
borderWidth: CGFloat? = nil,
2021-04-28 14:36:10 +02:00
keepImageCorner: Bool = false // default clip corner on image
) {
2021-02-23 08:16:55 +01:00
self.avatarImageURL = avatarImageURL
self.placeholderImage = placeholderImage
self.borderColor = borderColor
self.borderWidth = borderWidth
2021-04-28 14:10:17 +02:00
self.keepImageCorner = keepImageCorner
2021-02-04 12:28:16 +01:00
}
}