RawRepresentableCodable

This commit is contained in:
Scott Pfeil 2023-09-14 16:18:10 -04:00
parent c82a0cd4d6
commit a925560ecb
4 changed files with 52 additions and 23 deletions

View File

@ -289,6 +289,7 @@
AF766D262A3CD4C600749099 /* UIAccessibilityTraits+Codable.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF766D252A3CD4C600749099 /* UIAccessibilityTraits+Codable.swift */; };
AF7E509829E477C1009DC2AD /* AlertHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF7E509629E477C0009DC2AD /* AlertHandler.swift */; };
AF7E509929E477C1009DC2AD /* AlertController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF7E509729E477C0009DC2AD /* AlertController.swift */; };
AF8118302AB39B0900FAD1BA /* RawRepresentableCodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF81182F2AB39B0900FAD1BA /* RawRepresentableCodable.swift */; };
AFA4932029E5CA73001A9663 /* AlertOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = AFA4931F29E5CA73001A9663 /* AlertOperation.swift */; };
AFA4932229E5EF2E001A9663 /* NotificationHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = AFA4932129E5EF2E001A9663 /* NotificationHandler.swift */; };
AFA4933F29E874F0001A9663 /* MVMCoreUILoggingDelegateProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = AFA4933E29E874F0001A9663 /* MVMCoreUILoggingDelegateProtocol.swift */; };
@ -875,6 +876,7 @@
AF766D252A3CD4C600749099 /* UIAccessibilityTraits+Codable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIAccessibilityTraits+Codable.swift"; sourceTree = "<group>"; };
AF7E509629E477C0009DC2AD /* AlertHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlertHandler.swift; sourceTree = "<group>"; };
AF7E509729E477C0009DC2AD /* AlertController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlertController.swift; sourceTree = "<group>"; };
AF81182F2AB39B0900FAD1BA /* RawRepresentableCodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RawRepresentableCodable.swift; sourceTree = "<group>"; };
AFA4931F29E5CA73001A9663 /* AlertOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertOperation.swift; sourceTree = "<group>"; };
AFA4932129E5EF2E001A9663 /* NotificationHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationHandler.swift; sourceTree = "<group>"; };
AFA4933E29E874F0001A9663 /* MVMCoreUILoggingDelegateProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MVMCoreUILoggingDelegateProtocol.swift; sourceTree = "<group>"; };
@ -2152,6 +2154,7 @@
D29DF2A821E7B2F9003B2FB9 /* MVMCoreUIConstants.m */,
0A41BA6D2344FCD400D4C0BC /* CATransaction+Extension.swift */,
0AF60F0826B3316E00AC3DB4 /* MVMCoreUIUtility+Extension.swift */,
AF81182F2AB39B0900FAD1BA /* RawRepresentableCodable.swift */,
);
path = Utility;
sourceTree = "<group>";
@ -2746,6 +2749,7 @@
0AE98BAF23FEF956004C5109 /* ExternalLink.swift in Sources */,
012A88C4238D86E600FE3DA1 /* CarouselItemModelProtocol.swift in Sources */,
D2E2A9A123E095AB000B42E6 /* ButtonModelProtocol.swift in Sources */,
AF8118302AB39B0900FAD1BA /* RawRepresentableCodable.swift in Sources */,
94C2D9AB23872EB50006CF46 /* LabelAttributeActionModel.swift in Sources */,
D22D8395241FB41200D3DF69 /* UIStackView+Extension.swift in Sources */,
52B201D324081CFB00D2011E /* ListLeftVariableRadioButtonAndPaymentMethodModel.swift in Sources */,

View File

@ -68,16 +68,17 @@ open class PillButton: VDS.Button, MVMCoreUIViewConstrainingProtocol, MoleculeVi
if let accessibilityText = viewModel.accessibilityText {
accessibilityLabel = accessibilityText
}
FormValidator.setupValidation(for: viewModel, delegate: delegateObject?.formHolderDelegate)
set(with: viewModel.action, delegateObject: delegateObject, additionalData: additionalData)
viewModel.updateUI = { [weak self] in
MVMCoreDispatchUtility.performBlock(onMainThread: {
self?.viewModelDidUpdate()
guard let self = self else { return }
self.isEnabled = self.viewModel.enabled
})
}
FormValidator.setupValidation(for: viewModel, delegate: delegateObject?.formHolderDelegate)
}
//--------------------------------------------------

View File

@ -17,23 +17,7 @@ extension TileContainer.BackgroundColor: Codable {}
extension TileContainer.Padding: Codable {}
extension TileContainer.AspectRatio: Codable {}
extension Use: Codable {}
extension VDS.Button.Size: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let string = try container.decode(String.self)
switch string {
case VDS.Button.Size.large.rawValue, "standard":
self = .large
case VDS.Button.Size.small.rawValue, "tiny":
self = .small
default:
self = .large
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self)
}
extension VDS.Button.Size: RawRepresentableCodable {
public static var mapping: [String : VDS.Button.Size] { ["standard": .large, "tiny": .small] }
public static var defaultValue: VDS.Button.Size? { nil }
}

View File

@ -0,0 +1,40 @@
//
// RawRepresentableCodable.swift
// MVMCoreUI
//
// Created by Scott Pfeil on 9/14/23.
// Copyright © 2023 Verizon Wireless. All rights reserved.
//
import Foundation
public protocol RawRepresentableCodable: RawRepresentable, Codable where RawValue: Hashable & Decodable {
static var mapping: [RawValue: Self] { get }
static var defaultValue: Self? { get }
}
public enum RawRepresentableCodableError: Swift.Error {
case invalid(value: String)
}
extension RawRepresentableCodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let rawValue = try container.decode(RawValue.self)
if let found = Self(rawValue: rawValue) {
self = found
} else if let found = Self.mapping[rawValue] {
self = found
} else if let defaultValue = Self.defaultValue {
self = defaultValue
} else {
throw RawRepresentableCodableError.invalid(value: "\(rawValue)")
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self)
}
}