image view name change, allow view override of width and height

This commit is contained in:
Pfeil, Scott Robert 2019-06-21 09:28:51 -04:00
parent 086378608d
commit dc62e16c07

View File

@ -18,13 +18,17 @@ import UIKit
var heightConstraint: NSLayoutConstraint? var heightConstraint: NSLayoutConstraint?
var loadingSpinnerHeightConstraint: 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. // For keeping track of current state.
var edges: UIRectEdge? private var edges: UIRectEdge?
var spinnerHeight: CGFloat? private var spinnerHeight: CGFloat?
var width: CGFloat? private var currentImageWidth: CGFloat?
var height: CGFloat? private var currentImageHeight: CGFloat?
var loadingImageName: String? private var currentImageName: String?
var isFallbackImage: Bool = false private var isFallbackImage: Bool = false
public init(pinnedEdges edge: UIRectEdge) { public init(pinnedEdges edge: UIRectEdge) {
edges = edge edges = edge
@ -123,27 +127,27 @@ import UIKit
open func shouldLoadImage(withName imageName: String?, width: CGFloat) -> Bool { 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. // 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 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 { 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. // 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 return true
} }
// load new image if the width is different // 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 return true
} else if (self.width == nil) != (width == nil) { } else if (self.currentImageWidth == nil) != (width == nil) {
return true return true
} }
// load new image if the height is different // 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 return true
} else if (self.height == nil) || (height == nil) { } else if (self.currentImageHeight == nil) || (height == nil) {
return true return true
} }
return false return false
@ -190,8 +194,8 @@ import UIKit
imageView.accessibilityTraits = .staticText imageView.accessibilityTraits = .staticText
imageView.isAccessibilityElement = true imageView.isAccessibilityElement = true
} }
let width = json?.optionalCGFloatForKey("width") let width = json?.optionalCGFloatForKey("width") ?? imageWidth
let height = json?.optionalCGFloatForKey("height") let height = json?.optionalCGFloatForKey("height") ?? imageHeight
if let imageName = json?.optionalStringForKey("image"), shouldLoadImage(withName: imageName, width: width, height: height) { if let imageName = json?.optionalStringForKey("image"), shouldLoadImage(withName: imageName, width: width, height: height) {
imageView.image = nil imageView.image = nil
imageView.animatedImage = nil imageView.animatedImage = nil
@ -202,20 +206,16 @@ import UIKit
// MARK: - load functions // MARK: - load functions
public func loadImage(withName imageName: String?, format: String?, width: NSNumber?, height: NSNumber?, customFallbackImage: String?, completionHandler: @escaping MVMCoreGetImageBlock) { public func loadImage(withName imageName: String?, format: String?, width: NSNumber?, height: NSNumber?, customFallbackImage: String?, completionHandler: @escaping MVMCoreGetImageBlock) {
MVMCoreDispatchUtility.performBlock(onMainThread: { [unowned self] in MVMCoreDispatchUtility.performBlock(onMainThread: { [unowned self] in
self.loadingImageName = imageName self.currentImageName = imageName
if let width = width { self.currentImageWidth = width?.cgfloat()
self.width = width.cgfloat() self.currentImageHeight = height?.cgfloat()
}
if let height = height {
self.height = height.cgfloat()
}
self.loadingSpinner.resumeSpinnerAfterDelay() self.loadingSpinner.resumeSpinnerAfterDelay()
if let height = self.spinnerHeight { if let height = self.spinnerHeight {
self.loadingSpinnerHeightConstraint?.constant = height self.loadingSpinnerHeightConstraint?.constant = height
} }
let finishedLoadingBlock: MVMCoreGetImageBlock = {[weak self] (image, data, isFallbackImage) in MVMCoreDispatchUtility.performBlock(onMainThread: { [weak self] in 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 return
} }
self?.isFallbackImage = isFallbackImage self?.isFallbackImage = isFallbackImage
@ -238,14 +238,14 @@ import UIKit
public func loadCroppedImage(withName imageName: public func loadCroppedImage(withName imageName:
String?, width: NSNumber?, height: NSNumber?, cropRect: CGRect, flipImage: Bool, customFallbackImage: String?) { String?, width: NSNumber?, height: NSNumber?, cropRect: CGRect, flipImage: Bool, customFallbackImage: String?) {
MVMCoreDispatchUtility.performBlock(onMainThread: { [unowned self] in MVMCoreDispatchUtility.performBlock(onMainThread: { [unowned self] in
self.loadingImageName = imageName self.currentImageName = imageName
self.loadingSpinner.resumeSpinnerAfterDelay() self.loadingSpinner.resumeSpinnerAfterDelay()
if let height = self.spinnerHeight { if let height = self.spinnerHeight {
self.loadingSpinnerHeightConstraint?.constant = height 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 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: { 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 return
} }
self?.loadingSpinnerHeightConstraint?.constant = 0 self?.loadingSpinnerHeightConstraint?.constant = 0