button size

This commit is contained in:
Pfeil, Scott Robert 2020-01-14 16:32:42 -05:00
parent 7255c0c354
commit fc2326e9a8
8 changed files with 52 additions and 20 deletions

View File

@ -13,12 +13,18 @@ public enum ButtonStyle: String, Codable {
case secondary
}
public enum ButtonSize: String, Codable {
case standard
case tiny
}
public class ButtonModel: MoleculeProtocol {
public static var identifier: String = "button"
public var backgroundColor: Color?
public var title: String
public var action: ActionProtocol
public var style: ButtonStyle?
public var style: ButtonStyle? = .primary
public var size: ButtonSize? = .standard
init(with title: String, action: ActionProtocol) {
self.title = title
@ -30,6 +36,7 @@ public class ButtonModel: MoleculeProtocol {
case title
case action
case style
case size
}
required public init(from decoder: Decoder) throws {
@ -37,7 +44,12 @@ public class ButtonModel: MoleculeProtocol {
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
title = try typeContainer.decode(String.self, forKey: .title)
action = try typeContainer.decodeModel(codingKey: .action, typeCodingKey: ActionCodingKey.actionType)
style = try typeContainer.decodeIfPresent(ButtonStyle.self, forKey: .style)
if let style = try typeContainer.decodeIfPresent(ButtonStyle.self, forKey: .style) {
self.style = style
}
if let size = try typeContainer.decodeIfPresent(ButtonSize.self, forKey: .size) {
self.size = size
}
}
public func encode(to encoder: Encoder) throws {
@ -46,5 +58,6 @@ public class ButtonModel: MoleculeProtocol {
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
try container.encodeModel(action, forKey: .action)
try container.encodeIfPresent(style, forKey: .style)
try container.encodeIfPresent(size, forKey: .size)
}
}

View File

@ -22,6 +22,14 @@ extension PrimaryButton: ModelMoleculeViewProtocol {
setAsSecondaryCustom()
}
}
if let size = model.size {
switch size {
case .standard:
setAsTiny(false)
case .tiny:
setAsTiny(true)
}
}
set(with: model.action, delegateObject: delegateObject, additionalData: additionalData)
}
}

View File

@ -46,6 +46,18 @@ import UIKit
self.backgroundColor = backgroundColor.uiColor
}
}
public class func nameForReuse(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?) -> String? {
return model?.moleculeName
}
public class func estimatedHeight(forRow molecule: MoleculeProtocol?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
return nil
}
public class func requiredModules(_ molecule: MoleculeProtocol?, delegateObject: MVMCoreUIDelegateObject?, error: AutoreleasingUnsafeMutablePointer<MVMCoreErrorObject?>?) -> [String]? {
return nil
}
}
// MARK:- MVMCoreViewProtocol

View File

@ -27,7 +27,6 @@ import MVMCore
/// Defaults to set
func setDefaults() {
style = "standard"
if useHorizontalMargins == nil {
useHorizontalMargins = true
}

View File

@ -41,7 +41,7 @@ import UIKit
super.setWithModel(model, delegateObject, additionalData)
}
public static func estimatedHeight(forRow molecule: MoleculeProtocol?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
public class override func estimatedHeight(forRow molecule: MoleculeProtocol?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
return 30
}

View File

@ -46,7 +46,7 @@ open class ModuleMolecule: Container {
}
}
public static func estimatedHeight(forRow molecule: MoleculeProtocol?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
public override class func estimatedHeight(forRow molecule: MoleculeProtocol?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
guard let moduleMolecule = molecule as? ModuleMoleculeModel,
let moduleModel = delegateObject?.moleculeDelegate?.getModuleWithName(moduleMolecule.moduleName),
@ -58,7 +58,7 @@ open class ModuleMolecule: Container {
return height
}
public class func nameForReuse(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?) -> String? {
public override class func nameForReuse(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?) -> String? {
guard let moduleMolecule = model as? ModuleMoleculeModel,
let moduleModel = delegateObject?.moleculeDelegate?.getModuleWithName(moduleMolecule.moduleName),
let classType = MVMCoreUIMoleculeMappingObject.shared()?.getMoleculeClass(moduleModel) as? ModelMoleculeViewProtocol.Type,
@ -69,7 +69,7 @@ open class ModuleMolecule: Container {
return name
}
public static func requiredModules(_ molecule: MoleculeProtocol?, delegateObject: MVMCoreUIDelegateObject?, error: AutoreleasingUnsafeMutablePointer<MVMCoreErrorObject?>?) -> [String]? {
public override class func requiredModules(_ molecule: MoleculeProtocol?, delegateObject: MVMCoreUIDelegateObject?, error: AutoreleasingUnsafeMutablePointer<MVMCoreErrorObject?>?) -> [String]? {
guard let moduleName = (molecule as? ModuleMoleculeModel)?.moduleName,
let _ = delegateObject?.moleculeDelegate?.getModuleWithName(moduleName) else {

View File

@ -85,7 +85,7 @@ struct EyebrowHeadlineBodyLinkModel: MoleculeProtocol {
body.styleB2(true)
}
public static func estimatedHeight(forRow molecule: MoleculeProtocol?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
public override class func estimatedHeight(forRow molecule: MoleculeProtocol?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
return 65
}
}

View File

@ -111,7 +111,18 @@ open class MoleculeStackView: Container {
stackModel?.useVerticalMargins = moleculesShouldSetVerticalMargins
}
public class func nameForReuse(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?) -> String? {
open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
if model == nil {
let data = try! JSONSerialization.data(withJSONObject: json!)
let decoder = JSONDecoder()
let model = try! decoder.decode(MoleculeStackModel.self, from: data)
setWithModel(model, delegateObject, additionalData)
} else {
setWithModel(model, delegateObject, additionalData)
}
}
public override class func nameForReuse(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?) -> String? {
// This will aggregate names of molecules to make an id.
guard let model = model as? MoleculeStackModel else {
return "stack<>"
@ -131,17 +142,6 @@ open class MoleculeStackView: Container {
return name
}
open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
if model == nil {
let data = try! JSONSerialization.data(withJSONObject: json!)
let decoder = JSONDecoder()
let model = try! decoder.decode(MoleculeStackModel.self, from: data)
setWithModel(model, delegateObject, additionalData)
} else {
setWithModel(model, delegateObject, additionalData)
}
}
public class func name(forReuse molecule: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?) -> String? {
// This will aggregate names of molecules to make an id.
guard let molecules = molecule?.optionalArrayForKey(KeyMolecules) else {