|
| 1 | +import Foundation |
| 2 | +import UIKit |
| 3 | + |
| 4 | +#if SWIFT_PACKAGE |
| 5 | +import WordPressUIObjC |
| 6 | +#endif |
| 7 | + |
| 8 | +/// Wrapper class used to ensure removeObserver is called |
| 9 | +private class GravatarNotificationWrapper { |
| 10 | + let observer: NSObjectProtocol |
| 11 | + |
| 12 | + init(observer: NSObjectProtocol) { |
| 13 | + self.observer = observer |
| 14 | + } |
| 15 | + |
| 16 | + deinit { |
| 17 | + NotificationCenter.default.removeObserver(observer) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +/// UIImageView Helper Methods that allow us to download a Gravatar, given the User's Email |
| 22 | +/// |
| 23 | +extension UIImageView { |
| 24 | + |
| 25 | + /// Downloads and sets the User's Gravatar, given his email. |
| 26 | + /// TODO: This is a convenience method. Please, remove once all of the code has been migrated over to Swift. |
| 27 | + /// |
| 28 | + /// - Parameters: |
| 29 | + /// - email: the user's email |
| 30 | + /// - rating: expected image rating |
| 31 | + /// |
| 32 | + /// This method uses deprecated types. Please check the deprecation warning in `GravatarRatings`. Also check out the UIImageView extension from the Gravatar iOS SDK as an alternative to download images. See: https://github.com/Automattic/Gravatar-SDK-iOS. |
| 33 | + @available(*, deprecated, message: "Usage of the deprecated type: GravatarRatings.") |
| 34 | + @objc |
| 35 | + public func downloadGravatarWithEmail(_ email: String, rating: GravatarRatings) { |
| 36 | + downloadGravatarWithEmail(email, rating: rating, placeholderImage: .gravatarPlaceholderImage) |
| 37 | + } |
| 38 | + |
| 39 | + /// Downloads and sets the User's Gravatar, given his email. |
| 40 | + /// |
| 41 | + /// - Parameters: |
| 42 | + /// - email: the user's email |
| 43 | + /// - rating: expected image rating |
| 44 | + /// - placeholderImage: Image to be used as Placeholder |
| 45 | + /// This method uses deprecated types. Please check the deprecation warning in `GravatarRatings`. Also check out the UIImageView extension from the Gravatar iOS SDK as an alternative to download images. See: https://github.com/Automattic/Gravatar-SDK-iOS. |
| 46 | + @available(*, deprecated, message: "Usage of the deprecated type: GravatarRatings.") |
| 47 | + @objc |
| 48 | + public func downloadGravatarWithEmail(_ email: String, rating: GravatarRatings = .default, placeholderImage: UIImage = .gravatarPlaceholderImage) { |
| 49 | + let gravatarURL = Gravatar.gravatarUrl(for: email, size: gravatarDefaultSize(), rating: rating) |
| 50 | + |
| 51 | + listenForGravatarChanges(forEmail: email) |
| 52 | + downloadImage(from: gravatarURL, placeholderImage: placeholderImage) |
| 53 | + } |
| 54 | + |
| 55 | + /// Configures the UIImageView to listen for changes to the gravatar it is displaying |
| 56 | + public func listenForGravatarChanges(forEmail trackedEmail: String) { |
| 57 | + if let currentObersver = gravatarWrapper?.observer { |
| 58 | + NotificationCenter.default.removeObserver(currentObersver) |
| 59 | + gravatarWrapper = nil |
| 60 | + } |
| 61 | + |
| 62 | + let observer = NotificationCenter.default.addObserver(forName: .GravatarImageUpdateNotification, object: nil, queue: nil) { [weak self] (notification) in |
| 63 | + guard let userInfo = notification.userInfo, |
| 64 | + let email = userInfo[Defaults.emailKey] as? String, |
| 65 | + email == trackedEmail, |
| 66 | + let image = userInfo[Defaults.imageKey] as? UIImage else { |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + self?.image = image |
| 71 | + } |
| 72 | + gravatarWrapper = GravatarNotificationWrapper(observer: observer) |
| 73 | + } |
| 74 | + |
| 75 | + /// Stores the gravatar observer |
| 76 | + /// |
| 77 | + fileprivate var gravatarWrapper: GravatarNotificationWrapper? { |
| 78 | + get { |
| 79 | + return objc_getAssociatedObject(self, &Defaults.gravatarWrapperKey) as? GravatarNotificationWrapper |
| 80 | + } |
| 81 | + set { |
| 82 | + objc_setAssociatedObject(self, &Defaults.gravatarWrapperKey, newValue as AnyObject, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /// Downloads the provided Gravatar. |
| 87 | + /// |
| 88 | + /// - Parameters: |
| 89 | + /// - gravatar: the user's Gravatar |
| 90 | + /// - placeholder: Image to be used as Placeholder |
| 91 | + /// - animate: enable/disable fade in animation |
| 92 | + /// - failure: Callback block to be invoked when an error occurs while fetching the Gravatar image |
| 93 | + /// |
| 94 | + /// This method uses deprecated types. Please check the deprecation warning in `GravatarRatings`. Also check out the UIImageView extension from the Gravatar iOS SDK as an alternative to download images. See: https://github.com/Automattic/Gravatar-SDK-iOS. |
| 95 | + @available(*, deprecated, message: "Usage of the deprecated type: Gravatar.") |
| 96 | + public func downloadGravatar(_ gravatar: Gravatar?, placeholder: UIImage, animate: Bool, failure: ((Error?) -> Void)? = nil) { |
| 97 | + guard let gravatar = gravatar else { |
| 98 | + self.image = placeholder |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + // Starting with iOS 10, it seems `initWithCoder` uses a default size |
| 103 | + // of 1000x1000, which was messing with our size calculations for gravatars |
| 104 | + // on newly created table cells. |
| 105 | + // Calling `layoutIfNeeded()` forces UIKit to calculate the actual size. |
| 106 | + layoutIfNeeded() |
| 107 | + |
| 108 | + let size = Int(ceil(frame.width * UIScreen.main.scale)) |
| 109 | + let url = gravatar.urlWithSize(size) |
| 110 | + |
| 111 | + self.downloadImage(from: url, |
| 112 | + placeholderImage: placeholder, |
| 113 | + success: { image in |
| 114 | + guard image != self.image else { |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + self.image = image |
| 119 | + if animate { |
| 120 | + self.fadeInAnimation() |
| 121 | + } |
| 122 | + }, failure: { error in |
| 123 | + failure?(error) |
| 124 | + }) |
| 125 | + } |
| 126 | + |
| 127 | + /// Sets an Image Override in both, AFNetworking's Private Cache + NSURLCache |
| 128 | + /// |
| 129 | + /// - Parameters: |
| 130 | + /// - image: new UIImage |
| 131 | + /// - rating: rating for the new image. |
| 132 | + /// - email: associated email of the new gravatar |
| 133 | + /// - Note: You may want to use `updateGravatar(image:, email:)` instead |
| 134 | + /// |
| 135 | + /// *WHY* is this required?. *WHY* life has to be so complicated?, is the universe against us? |
| 136 | + /// This has been implemented as a workaround. During Upload, we want any async calls made to the |
| 137 | + /// `downloadGravatar` API to return the "Fresh" image. |
| 138 | + /// |
| 139 | + /// Note II: |
| 140 | + /// We cannot just clear NSURLCache, since the helper that's supposed to do that, is broken since iOS 8. |
| 141 | + /// Ref: Ref: http://blog.airsource.co.uk/2014/10/11/nsurlcache-ios8-broken/ |
| 142 | + /// |
| 143 | + /// P.s.: |
| 144 | + /// Hope buddah, and the code reviewer, can forgive me for this hack. |
| 145 | + /// |
| 146 | + @available(*, deprecated, message: "Usage of the deprecated type: GravatarRatings.") |
| 147 | + @objc public func overrideGravatarImageCache(_ image: UIImage, rating: GravatarRatings, email: String) { |
| 148 | + guard let gravatarURL = Gravatar.gravatarUrl(for: email, size: gravatarDefaultSize(), rating: rating) else { |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + listenForGravatarChanges(forEmail: email) |
| 153 | + overrideImageCache(for: gravatarURL, with: image) |
| 154 | + } |
| 155 | + |
| 156 | + /// Updates the gravatar image for the given email, and notifies all gravatar image views |
| 157 | + /// |
| 158 | + /// - Parameters: |
| 159 | + /// - image: the new UIImage |
| 160 | + /// - email: associated email of the new gravatar |
| 161 | + @objc public func updateGravatar(image: UIImage, email: String?) { |
| 162 | + self.image = image |
| 163 | + guard let email = email else { |
| 164 | + return |
| 165 | + } |
| 166 | + NotificationCenter.default.post(name: .GravatarImageUpdateNotification, object: self, userInfo: [Defaults.emailKey: email, Defaults.imageKey: image]) |
| 167 | + } |
| 168 | + |
| 169 | + // MARK: - Private Helpers |
| 170 | + |
| 171 | + /// Returns the required gravatar size. If the current view's size is zero, falls back to the default size. |
| 172 | + /// |
| 173 | + private func gravatarDefaultSize() -> Int { |
| 174 | + guard bounds.size.equalTo(.zero) == false else { |
| 175 | + return Defaults.imageSize |
| 176 | + } |
| 177 | + |
| 178 | + let targetSize = max(bounds.width, bounds.height) * UIScreen.main.scale |
| 179 | + return Int(targetSize) |
| 180 | + } |
| 181 | + |
| 182 | + /// Private helper structure: contains the default Gravatar parameters |
| 183 | + /// |
| 184 | + private struct Defaults { |
| 185 | + static let imageSize = 80 |
| 186 | + static var gravatarWrapperKey = 0x1000 |
| 187 | + static let emailKey = "email" |
| 188 | + static let imageKey = "image" |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +public extension NSNotification.Name { |
| 193 | + static let GravatarImageUpdateNotification = NSNotification.Name(rawValue: "GravatarImageUpdateNotification") |
| 194 | +} |
0 commit comments