81 lines
3.0 KiB
Swift
81 lines
3.0 KiB
Swift
//
|
|
// ButtonWithImage.swift
|
|
// JSONCreator
|
|
//
|
|
// Created by Matt Bruce on 9/15/23.
|
|
// Copyright © 2023 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import MVMCoreUI
|
|
import VDS
|
|
|
|
public class ButtonWithImage: PillButtonBase<ButtonWithImageModel> {
|
|
|
|
public override func viewModelDidUpdate() {
|
|
super.viewModelDidUpdate()
|
|
|
|
guard var buttonImage = MVMCoreCache.shared()?.getImageFromRegisteredBundles(viewModel.image) else {
|
|
return
|
|
}
|
|
|
|
setImage(buttonImage, for: .normal)
|
|
semanticContentAttribute = viewModel.imagePosition == .left ? .forceLeftToRight : .forceRightToLeft
|
|
let insetAmount = Padding.OneHalf
|
|
imageEdgeInsets = UIEdgeInsets(top: 0, left: viewModel.imagePosition == .left ? -insetAmount: insetAmount, bottom: 0, right: viewModel.imagePosition == .left ? insetAmount : -insetAmount)
|
|
titleEdgeInsets = UIEdgeInsets(top: 0, left: viewModel.imagePosition == .left ? insetAmount : -insetAmount, bottom: 0, right: viewModel.imagePosition == .left ? -insetAmount : insetAmount)
|
|
contentEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: insetAmount)
|
|
}
|
|
}
|
|
|
|
public class ButtonWithImageModel: ButtonModel {
|
|
|
|
public enum ImagePosition: String, Codable {
|
|
case left, right
|
|
}
|
|
|
|
public override class var identifier: String {
|
|
"buttonWithImage"
|
|
}
|
|
|
|
let image: String
|
|
var imagePosition: ImagePosition
|
|
|
|
public init(with title: String, image: String, imagePosition: ImagePosition = .left, action: ActionModelProtocol) {
|
|
self.image = image
|
|
self.imagePosition = imagePosition
|
|
super.init(with: title, action: action)
|
|
}
|
|
|
|
public init(secondaryButtonWith title: String, image: String, imagePosition: ImagePosition = .left, action: ActionModelProtocol) {
|
|
self.image = image
|
|
self.imagePosition = imagePosition
|
|
super.init(secondaryButtonWith: title, action: action)
|
|
}
|
|
|
|
public init(primaryButtonWith title: String, image: String, imagePosition: ImagePosition = .left, action: ActionModelProtocol) {
|
|
self.image = image
|
|
self.imagePosition = imagePosition
|
|
super.init(primaryButtonWith: title, action: action)
|
|
}
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case image
|
|
case imagePosition
|
|
}
|
|
|
|
required public init(from decoder: Decoder) throws {
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
image = try container.decode(String.self, forKey: .image)
|
|
imagePosition = try container.decodeIfPresent(ImagePosition.self, forKey: .imagePosition) ?? .left
|
|
try super.init(from: decoder)
|
|
}
|
|
|
|
public override func encode(to encoder: Encoder) throws {
|
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
try container.encode(image, forKey: .image)
|
|
try container.encode(imagePosition, forKey: .imagePosition)
|
|
try super.encode(to: encoder)
|
|
}
|
|
}
|