style for notification

This commit is contained in:
Pfeil, Scott Robert 2021-04-30 13:28:49 -04:00
parent e5c5a593eb
commit 0673c5b3a8
3 changed files with 73 additions and 9 deletions

View File

@ -8,6 +8,21 @@
open class NotificationModel: MoleculeModelProtocol {
/**
The style of the notification:
- success, green background, white content
- error, orange background, black content
- \warning, yellow background, black content
- information, blue background, white content
*/
public enum Style: String, Codable {
case success
case error
case warning
case information
}
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
@ -19,6 +34,7 @@ open class NotificationModel: MoleculeModelProtocol {
public var body: LabelModel?
public var button: ButtonModel?
public var closeButton: NotificationXButtonModel?
public var style: NotificationModel.Style = .success
//--------------------------------------------------
// MARK: - Initializer
@ -34,20 +50,63 @@ open class NotificationModel: MoleculeModelProtocol {
open func setDefault() {
if backgroundColor == nil {
backgroundColor = Color(uiColor: .mvmGreen)
switch style {
case .error:
backgroundColor = Color(uiColor: .mvmOrange)
case .warning:
backgroundColor = Color(uiColor: .mvmYellow)
case .information:
backgroundColor = Color(uiColor: .mvmBlue)
default:
backgroundColor = Color(uiColor: .mvmGreen)
}
}
if headline.textColor == nil {
headline.textColor = Color(uiColor: .mvmWhite)
switch style {
case .error, .warning:
headline.textColor = Color(uiColor: .mvmBlack)
default:
headline.textColor = Color(uiColor: .mvmWhite)
}
}
if body?.textColor == nil {
body?.textColor = Color(uiColor: .mvmWhite)
switch style {
case .error, .warning:
body?.textColor = Color(uiColor: .mvmBlack)
default:
body?.textColor = Color(uiColor: .mvmWhite)
}
}
button?.size = .tiny
if button?.enabledTextColor == nil {
switch style {
case .error, .warning:
button?.enabledTextColor = Color(uiColor: .mvmBlack)
default:
button?.enabledTextColor = Color(uiColor: .mvmWhite)
}
}
if button?.enabledBorderColor == nil {
switch style {
case .error, .warning:
button?.enabledBorderColor = Color(uiColor: .mvmBlack)
default:
button?.enabledBorderColor = Color(uiColor: .mvmWhite)
}
}
if button?.style == nil {
button?.style = .secondary
}
button?.size = .tiny
button?.enabledTextColor = Color(uiColor: .mvmWhite)
button?.enabledBorderColor = Color(uiColor: .mvmWhite)
if closeButton?.color == nil {
switch style {
case .error, .warning:
closeButton?.color = Color(uiColor: .mvmBlack)
default:
closeButton?.color = Color(uiColor: .mvmWhite)
}
}
}
//--------------------------------------------------
@ -62,6 +121,7 @@ open class NotificationModel: MoleculeModelProtocol {
case body
case button
case closeButton
case style
}
//--------------------------------------------------
@ -76,6 +136,9 @@ open class NotificationModel: MoleculeModelProtocol {
body = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .body)
button = try typeContainer.decodeIfPresent(ButtonModel.self, forKey: .button)
closeButton = try typeContainer.decodeIfPresent(NotificationXButtonModel.self, forKey: .closeButton)
if let style = try typeContainer.decodeIfPresent(NotificationModel.Style.self, forKey: .style) {
self.style = style
}
setDefault()
}
@ -88,5 +151,6 @@ open class NotificationModel: MoleculeModelProtocol {
try container.encodeIfPresent(body, forKey: .body)
try container.encodeIfPresent(button, forKey: .button)
try container.encodeIfPresent(closeButton, forKey: .closeButton)
try container.encode(style, forKey: .style)
}
}

View File

@ -33,7 +33,7 @@ import Foundation
open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) {
super.set(with: model, delegateObject, additionalData)
guard let model = model as? NotificationXButtonModel else { return }
tintColor = model.color.uiColor
tintColor = model.color?.uiColor ?? .white
// TODO: Temporary, consider action for dismissing top alert
if model.action.actionType == ActionNoopModel.identifier {

View File

@ -11,7 +11,7 @@ import Foundation
public class NotificationXButtonModel: ButtonModelProtocol, MoleculeModelProtocol {
public static var identifier: String = "notificationXButton"
public var backgroundColor: Color?
public var color = Color(uiColor: .white)
public var color: Color?
public var action: ActionModelProtocol = ActionNoopModel()
private enum CodingKeys: String, CodingKey {
@ -33,7 +33,7 @@ public class NotificationXButtonModel: ButtonModelProtocol, MoleculeModelProtoco
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(moleculeName, forKey: .moleculeName)
try container.encode(color, forKey: .color)
try container.encodeIfPresent(color, forKey: .color)
try container.encodeModel(action, forKey: .action)
}
}