updated the loadImage

made ViewModelMolecule optional

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-04-16 10:45:47 -05:00
parent 44cbeeb695
commit 300047fdbe
2 changed files with 56 additions and 6 deletions

View File

@ -35,10 +35,18 @@ open class TileContainer: VDS.TileContainer, VDSMoleculeViewProtocol{
//--------------------------------------------------
public func viewModelDidUpdate() {
if molecule != nil {
molecule?.set(with: viewModel.molecule, delegateObject, additionalData)
} else if let moleculeView = ModelRegistry.createMolecule(viewModel.molecule, delegateObject: delegateObject, additionalData: additionalData) {
addContentView(moleculeView)
if let moleculeModel = viewModel.molecule{
if molecule != nil {
molecule?.set(with: moleculeModel, delegateObject, additionalData)
} else if let moleculeView = ModelRegistry.createMolecule(moleculeModel, delegateObject: delegateObject, additionalData: additionalData) {
addContentView(moleculeView)
}
} else {
molecule = nil
}
if let imageName = viewModel.backgroundImage {
loadImage(imageName)
}
padding = viewModel.padding
@ -61,6 +69,21 @@ open class TileContainer: VDS.TileContainer, VDSMoleculeViewProtocol{
}
}
}
private func loadImage(_ imageName: String? = nil) {
guard let imageName else {
if backgroundImage != nil {
backgroundImage = nil
}
return
}
let finishedLoadingBlock: MVMCoreGetImageBlock = {[weak self] (image, data, isFallbackImage) in MVMCoreDispatchUtility.performBlock(onMainThread: { [weak self] in
guard let self = self else { return }
self.backgroundImage = image
})}
MVMCoreCache.shared()?.getImage(imageName, useWidth: false, widthForS7: 0, useHeight: false, heightForS7: 0, format: nil, localFallbackImageName: nil, allowServerQueryParameters: false, localBundle: nil, completionHandler: finishedLoadingBlock)
}
//--------------------------------------------------
// MARK: - MVMCoreViewProtocol
@ -86,4 +109,30 @@ open class TileContainer: VDS.TileContainer, VDSMoleculeViewProtocol{
self.delegateObject?.moleculeDelegate?.moleculeLayoutUpdated(self)
}
}
public func needsToBeConstrained() -> Bool { true }
public func horizontalAlignment() -> UIStackView.Alignment { .leading }
}
extension UIImageView {
public func loadImage(withName imageName: String?, format: String? = nil, width: CGFloat? = nil, height: CGFloat? = nil, customFallbackImage: String? = nil, allowServerParameters: Bool = false, localBundle: Bundle? = nil, completionHandler: MVMCoreGetImageBlock? = nil) {
MVMCoreDispatchUtility.performBlock(onMainThread: { [unowned self] in
let finishedLoadingBlock: MVMCoreGetImageBlock = {[weak self] (image, data, isFallbackImage) in MVMCoreDispatchUtility.performBlock(onMainThread: { [weak self] in
guard let self = self else { return }
self.image = image
completionHandler?(image,data,isFallbackImage)
})}
let fallbackImageName = customFallbackImage ?? MVMCoreUIUtility.localizedImageName("fallback")
if let format = format, format.lowercased().contains("gif") {
// Gifs aren't supported by default and need special handling
MVMCoreCache.shared()?.getGif(imageName, useWidth: width != nil, widthForS7: Int(width ?? 0), useHeight: height != nil, heightForS7: Int(height ?? 0), format: format, localFallbackImageName: fallbackImageName, allowServerQueryParameters: allowServerParameters, localBundle: localBundle, completionHandler: finishedLoadingBlock)
} else {
MVMCoreCache.shared()?.getImage(imageName, useWidth: width != nil, widthForS7: Int(width ?? 0), useHeight: height != nil, heightForS7: Int(height ?? 0), format: format, localFallbackImageName: fallbackImageName, allowServerQueryParameters: allowServerParameters, localBundle: localBundle, completionHandler: finishedLoadingBlock)
}
})
}
}

View File

@ -21,7 +21,7 @@ open class TileContainerModel: TileContainerBaseModel, MoleculeModelProtocol {
public var aspectRatio: TileContainer.AspectRatio = .ratio1x1
public var color: TileContainer.BackgroundColor = .secondary
public var backgroundEffect: TileContainer.BackgroundEffect = .none
public var molecule: MoleculeModelProtocol
public var molecule: MoleculeModelProtocol?
private enum CodingKeys: String, CodingKey {
case id
@ -35,7 +35,7 @@ open class TileContainerModel: TileContainerBaseModel, MoleculeModelProtocol {
required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
molecule = try container.decodeModel(codingKey: .molecule)
molecule = try container.decodeModelIfPresent(codingKey: .molecule)
padding = try container.decodeIfPresent(TileContainer.Padding.self, forKey: .padding) ?? .padding4X
color = try container.decodeIfPresent(TileContainer.BackgroundColor.self, forKey: .color) ?? .secondary
backgroundEffect = try container.decodeIfPresent(TileContainer.BackgroundEffect.self, forKey: .backgroundEffect) ?? .none
@ -47,6 +47,7 @@ open class TileContainerModel: TileContainerBaseModel, MoleculeModelProtocol {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(moleculeName, forKey: .moleculeName)
try container.encodeModelIfPresent(molecule, forKey: .molecule)
try container.encodeIfPresent(padding, forKey: .padding)
try container.encodeIfPresent(color, forKey: .color)
try container.encodeIfPresent(backgroundEffect, forKey: .backgroundEffect)