diff --git a/Tusker/Views/CachedImageView.swift b/Tusker/Views/CachedImageView.swift index fe23e4ff15..3ca93ce851 100644 --- a/Tusker/Views/CachedImageView.swift +++ b/Tusker/Views/CachedImageView.swift @@ -33,6 +33,11 @@ class CachedImageView: UIImageView { commonInit() } + deinit { + fetchTask?.cancel() + blurHashTask?.cancel() + } + private func commonInit() { NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil) } diff --git a/Tusker/Views/Profile Header/ProfileHeaderView.swift b/Tusker/Views/Profile Header/ProfileHeaderView.swift index 3628a0204d..a30c69daef 100644 --- a/Tusker/Views/Profile Header/ProfileHeaderView.swift +++ b/Tusker/Views/Profile Header/ProfileHeaderView.swift @@ -25,9 +25,9 @@ class ProfileHeaderView: UIView { weak var delegate: ProfileHeaderViewDelegate? var mastodonController: MastodonController! { delegate?.apiController } - @IBOutlet weak var headerImageView: UIImageView! + @IBOutlet weak var headerImageView: CachedImageView! @IBOutlet weak var avatarContainerView: UIView! - @IBOutlet weak var avatarImageView: UIImageView! + @IBOutlet weak var avatarImageView: CachedImageView! @IBOutlet weak var moreButton: ProfileHeaderButton! @IBOutlet weak var followButton: ProfileHeaderButton! @IBOutlet weak var displayNameLabel: AccountDisplayNameLabel! @@ -44,8 +44,6 @@ class ProfileHeaderView: UIView { var accountID: String! - private var imagesTask: Task? - private var isGrayscale = false private var followButtonMode = FollowButtonMode.follow { didSet { @@ -56,10 +54,6 @@ class ProfileHeaderView: UIView { } } - deinit { - imagesTask?.cancel() - } - override func awakeFromNib() { super.awakeFromNib() @@ -69,11 +63,13 @@ class ProfileHeaderView: UIView { avatarContainerView.layer.cornerCurve = .continuous // Set zPositions so the gallery presentation/dismissal animation looks correct. avatarContainerView.layer.zPosition = 2 + avatarImageView.cache = .avatars avatarImageView.layer.masksToBounds = true avatarImageView.layer.cornerCurve = .continuous avatarImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(avatarPressed))) avatarImageView.isUserInteractionEnabled = true + headerImageView.cache = .headers headerImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(headerPressed))) headerImageView.isUserInteractionEnabled = true headerImageView.layer.zPosition = 1 @@ -138,11 +134,11 @@ class ProfileHeaderView: UIView { usernameLabel.text = "@\(account.acct)" lockImageView.isHidden = !account.locked - imagesTask?.cancel() - let avatar = account.avatar - let header = account.header - imagesTask = Task { - await updateImages(avatar: avatar, header: header) + if let avatar = account.avatar { + avatarImageView.update(for: avatar) + } + if let header = account.header { + headerImageView.update(for: header) } moreButton.menu = UIMenu(title: "", image: nil, identifier: nil, options: [], children: delegate?.actionsForProfile(accountID: accountID, source: .view(moreButton), fetchRelationship: false) ?? []) @@ -294,44 +290,6 @@ class ProfileHeaderView: UIView { avatarContainerView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarContainerView) avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) displayNameLabel.updateForAccountDisplayName(account: account) - - if isGrayscale != Preferences.shared.grayscaleImages { - isGrayscale = Preferences.shared.grayscaleImages - imagesTask?.cancel() - let avatar = account.avatar - let header = account.header - imagesTask = Task { - await updateImages(avatar: avatar, header: header) - } - } - } - - private nonisolated func updateImages(avatar: URL?, header: URL?) async { - await withTaskGroup(of: Void.self) { group in - group.addTask { - guard let avatar, - let image = await ImageCache.avatars.get(avatar, loadOriginal: true).1, - let transformedImage = await ImageGrayscalifier.convertIfNecessary(url: avatar, image: image), - !Task.isCancelled else { - return - } - await MainActor.run { - self.avatarImageView.image = transformedImage - } - } - group.addTask { - guard let header, - let image = await ImageCache.avatars.get(header, loadOriginal: true).1, - let transformedImage = await ImageGrayscalifier.convertIfNecessary(url: header, image: image), - !Task.isCancelled else { - return - } - await MainActor.run { - self.headerImageView.image = transformedImage - } - } - await group.waitForAll() - } } private func formatBigNumber(_ value: Int) -> (String, String) { diff --git a/Tusker/Views/Profile Header/ProfileHeaderView.xib b/Tusker/Views/Profile Header/ProfileHeaderView.xib index 086b35dff5..daa54c5450 100644 --- a/Tusker/Views/Profile Header/ProfileHeaderView.xib +++ b/Tusker/Views/Profile Header/ProfileHeaderView.xib @@ -3,7 +3,7 @@ - + @@ -14,7 +14,7 @@ - + @@ -23,7 +23,7 @@ - +