diff --git a/MVMCoreUI/Atoms/Views/MFLoadImageView.swift b/MVMCoreUI/Atoms/Views/MFLoadImageView.swift index 9d101646..9bd22c19 100644 --- a/MVMCoreUI/Atoms/Views/MFLoadImageView.swift +++ b/MVMCoreUI/Atoms/Views/MFLoadImageView.swift @@ -18,13 +18,17 @@ import UIKit var heightConstraint: NSLayoutConstraint? var loadingSpinnerHeightConstraint: NSLayoutConstraint? + // Allows for a view to hardcode which height to use if there is none in the json. + var imageWidth: CGFloat? + var imageHeight: CGFloat? + // For keeping track of current state. - var edges: UIRectEdge? - var spinnerHeight: CGFloat? - var width: CGFloat? - var height: CGFloat? - var loadingImageName: String? - var isFallbackImage: Bool = false + private var edges: UIRectEdge? + private var spinnerHeight: CGFloat? + private var currentImageWidth: CGFloat? + private var currentImageHeight: CGFloat? + private var currentImageName: String? + private var isFallbackImage: Bool = false public init(pinnedEdges edge: UIRectEdge) { edges = edge @@ -123,27 +127,27 @@ import UIKit open func shouldLoadImage(withName imageName: String?, width: CGFloat) -> Bool { // We should load a new image if there is no current image, the image names are different, the width is different, or we are using a fallback image. - guard let currentWidth = self.width else { + guard let currentWidth = self.currentImageWidth else { return true } - return (imageView.image == nil && imageView.animatedImage == nil) || imageName != loadingImageName || !MVMCoreGetterUtility.cgfequal(width, currentWidth) || isFallbackImage + return (imageView.image == nil && imageView.animatedImage == nil) || imageName != currentImageName || !MVMCoreGetterUtility.cgfequal(width, currentWidth) || isFallbackImage } open func shouldLoadImage(withName imageName: String?, width: CGFloat?, height: CGFloat?) -> Bool { // We should load a new image if there is no current image, the image names are different, or we are using a fallback image. - if ((imageView.image == nil && imageView.animatedImage == nil) || imageName != loadingImageName || isFallbackImage) { + if ((imageView.image == nil && imageView.animatedImage == nil) || imageName != currentImageName || isFallbackImage) { return true } // load new image if the width is different - if let oldWidth = self.width, let newWidth = width, !MVMCoreGetterUtility.cgfequal(oldWidth, newWidth) { + if let oldWidth = self.currentImageWidth, let newWidth = width, !MVMCoreGetterUtility.cgfequal(oldWidth, newWidth) { return true - } else if (self.width == nil) != (width == nil) { + } else if (self.currentImageWidth == nil) != (width == nil) { return true } // load new image if the height is different - if let oldHeight = self.height, let newHeight = height, !MVMCoreGetterUtility.cgfequal(oldHeight, newHeight) { + if let oldHeight = self.currentImageHeight, let newHeight = height, !MVMCoreGetterUtility.cgfequal(oldHeight, newHeight) { return true - } else if (self.height == nil) || (height == nil) { + } else if (self.currentImageHeight == nil) || (height == nil) { return true } return false @@ -190,8 +194,8 @@ import UIKit imageView.accessibilityTraits = .staticText imageView.isAccessibilityElement = true } - let width = json?.optionalCGFloatForKey("width") - let height = json?.optionalCGFloatForKey("height") + let width = json?.optionalCGFloatForKey("width") ?? imageWidth + let height = json?.optionalCGFloatForKey("height") ?? imageHeight if let imageName = json?.optionalStringForKey("image"), shouldLoadImage(withName: imageName, width: width, height: height) { imageView.image = nil imageView.animatedImage = nil @@ -202,20 +206,16 @@ import UIKit // MARK: - load functions public func loadImage(withName imageName: String?, format: String?, width: NSNumber?, height: NSNumber?, customFallbackImage: String?, completionHandler: @escaping MVMCoreGetImageBlock) { MVMCoreDispatchUtility.performBlock(onMainThread: { [unowned self] in - self.loadingImageName = imageName - if let width = width { - self.width = width.cgfloat() - } - if let height = height { - self.height = height.cgfloat() - } + self.currentImageName = imageName + self.currentImageWidth = width?.cgfloat() + self.currentImageHeight = height?.cgfloat() self.loadingSpinner.resumeSpinnerAfterDelay() if let height = self.spinnerHeight { self.loadingSpinnerHeightConstraint?.constant = height } let finishedLoadingBlock: MVMCoreGetImageBlock = {[weak self] (image, data, isFallbackImage) in MVMCoreDispatchUtility.performBlock(onMainThread: { [weak self] in - guard let loadingImageName = self?.loadingImageName, loadingImageName == imageName else { + guard let loadingImageName = self?.currentImageName, loadingImageName == imageName else { return } self?.isFallbackImage = isFallbackImage @@ -238,14 +238,14 @@ import UIKit public func loadCroppedImage(withName imageName: String?, width: NSNumber?, height: NSNumber?, cropRect: CGRect, flipImage: Bool, customFallbackImage: String?) { MVMCoreDispatchUtility.performBlock(onMainThread: { [unowned self] in - self.loadingImageName = imageName + self.currentImageName = imageName self.loadingSpinner.resumeSpinnerAfterDelay() if let height = self.spinnerHeight { self.loadingSpinnerHeightConstraint?.constant = height } MVMCoreCache.shared()?.getCroppedImage(imageName, useWidth: width != nil, widthForS7: width?.intValue ?? 0, useHeight: height != nil, heightForS7: height?.intValue ?? 0, finalRect: cropRect, flipImage: flipImage, localFallbackImageName: customFallbackImage ?? MVMCoreUIUtility.localizedImageName("fallback"), completionHandler: { [weak self] (image, data, isFallBackImage) in MVMCoreDispatchUtility.performBlock(onMainThread: { - guard let image = image, let loadingImageName = self?.loadingImageName, loadingImageName == imageName else { + guard let image = image, let loadingImageName = self?.currentImageName, loadingImageName == imageName else { return } self?.loadingSpinnerHeightConstraint?.constant = 0