129 lines
5.1 KiB
Swift
129 lines
5.1 KiB
Swift
//
|
|
// NotificationModel.swift
|
|
// MVMCoreUI
|
|
//
|
|
// Created by Scott Pfeil on 9/11/20.
|
|
// Copyright © 2020 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import MVMCore
|
|
|
|
open class NotificationModel: Codable, Identifiable, Equatable {
|
|
public var type: String
|
|
public var priority = Operation.QueuePriority.normal
|
|
public var molecule: MoleculeModelProtocol
|
|
public var persistent = false
|
|
public var dismissTime = 5
|
|
public var pages: [String]?
|
|
public var analyticsData: JSONValueDictionary?
|
|
public var id: String
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case type
|
|
case priority
|
|
case molecule
|
|
case persistent
|
|
case dismissTime
|
|
case pages
|
|
case analyticsData
|
|
case id
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Convenience Functions
|
|
//--------------------------------------------------
|
|
|
|
/// Set the priority using percent 0-100
|
|
open func setPriority(with percent: Float) {
|
|
// The new scale
|
|
let scale = Operation.QueuePriority.veryHigh.rawValue - Operation.QueuePriority.veryLow.rawValue
|
|
|
|
// Adjust the percent to the new scale
|
|
let scaledPercent = (percent / 100.0) * Float(scale)
|
|
|
|
// Finish shifting.
|
|
priority = Operation.QueuePriority(rawValue: Operation.QueuePriority.veryLow.rawValue + Int(scaledPercent)) ?? .normal
|
|
}
|
|
|
|
/// Gets the priority as a percent (0-100)
|
|
open func getPriorityPercent() -> Float {
|
|
// Shift the value
|
|
let shifted = Float(priority.rawValue - Operation.QueuePriority.veryLow.rawValue)
|
|
|
|
// The current scale
|
|
let scale = Operation.QueuePriority.veryHigh.rawValue - Operation.QueuePriority.veryLow.rawValue
|
|
|
|
// Adjust to percent
|
|
return (shifted / Float(scale)) * 100.0
|
|
}
|
|
|
|
/// Decodes the top alert json to a model.
|
|
public static func decode(json: [AnyHashable: Any], delegateObject: MVMCoreUIDelegateObject?) throws -> Self {
|
|
let data = try JSONSerialization.data(withJSONObject: json)
|
|
let decoder = JSONDecoder.create(with: delegateObject)
|
|
return try decoder.decode(self, from: data)
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Initializer
|
|
//--------------------------------------------------
|
|
|
|
required public init(with type: String, molecule: MoleculeModelProtocol, priority: Operation.QueuePriority = .normal, persistent: Bool = false, dismissTime: Int? = nil, pages: [String]? = nil, analyticsData: JSONValueDictionary? = nil, id: String = UUID().uuidString) {
|
|
self.type = type
|
|
self.molecule = molecule
|
|
self.priority = priority
|
|
self.persistent = persistent
|
|
if let dismissTime = dismissTime {
|
|
self.dismissTime = dismissTime
|
|
}
|
|
self.pages = pages
|
|
self.analyticsData = analyticsData
|
|
self.id = id
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Codec
|
|
//--------------------------------------------------
|
|
|
|
required public init(from decoder: Decoder) throws {
|
|
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
|
|
type = try typeContainer.decode(String.self, forKey: .type)
|
|
molecule = try typeContainer.decodeModel(codingKey: .molecule)
|
|
id = try typeContainer.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
|
|
if let priorityPercent = try typeContainer.decodeIfPresent(Float.self, forKey: .priority) {
|
|
setPriority(with: priorityPercent)
|
|
}
|
|
if let persistent = try typeContainer.decodeIfPresent(Bool.self, forKey: .persistent) {
|
|
self.persistent = persistent
|
|
}
|
|
if let dismissTime = try typeContainer.decodeIfPresent(Int.self, forKey: .dismissTime) {
|
|
self.dismissTime = dismissTime
|
|
}
|
|
pages = try typeContainer.decodeIfPresent([String].self, forKey: .pages)
|
|
analyticsData = try typeContainer.decodeIfPresent(JSONValueDictionary.self, forKey: .analyticsData)
|
|
}
|
|
|
|
public func encode(to encoder: Encoder) throws {
|
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
try container.encode(type, forKey: .type)
|
|
try container.encodeModel(molecule, forKey: .molecule)
|
|
try container.encode(getPriorityPercent(), forKey: .priority)
|
|
try container.encode(persistent, forKey: .persistent)
|
|
try container.encode(dismissTime, forKey: .dismissTime)
|
|
try container.encodeIfPresent(pages, forKey: .pages)
|
|
try container.encodeIfPresent(analyticsData, forKey: .analyticsData)
|
|
try container.encode(id, forKey: .id)
|
|
}
|
|
|
|
public static func == (lhs: NotificationModel, rhs: NotificationModel) -> Bool {
|
|
lhs.persistent == rhs.persistent
|
|
&& lhs.priority == rhs.priority
|
|
&& lhs.type == rhs.type
|
|
&& lhs.persistent == rhs.persistent
|
|
&& lhs.dismissTime == rhs.dismissTime
|
|
&& lhs.pages == rhs.pages
|
|
&& lhs.analyticsData == rhs.analyticsData
|
|
}
|
|
}
|