From bad4c67e9e8aa5a3edc2199e3cac8cee95fc05fa Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Mon, 15 Mar 2021 16:39:46 -0500 Subject: [PATCH 1/2] added ModelHandlerProtocol Signed-off-by: Matt Bruce --- MVMCore/MVMCore.xcodeproj/project.pbxproj | 4 ++++ .../MVMCore/Models/Model/ModelHandlerProtocol.swift | 11 +++++++++++ 2 files changed, 15 insertions(+) create mode 100644 MVMCore/MVMCore/Models/Model/ModelHandlerProtocol.swift diff --git a/MVMCore/MVMCore.xcodeproj/project.pbxproj b/MVMCore/MVMCore.xcodeproj/project.pbxproj index 449ae7c..afc5dae 100644 --- a/MVMCore/MVMCore.xcodeproj/project.pbxproj +++ b/MVMCore/MVMCore.xcodeproj/project.pbxproj @@ -149,6 +149,7 @@ D2DEDCB923C6400600C44CC4 /* UnitInterval.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2DEDCB823C6400600C44CC4 /* UnitInterval.swift */; }; D2DEDCBB23C65BC300C44CC4 /* Percent.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2DEDCBA23C65BC300C44CC4 /* Percent.swift */; }; D2E1FAD92260C3E400AEFD8C /* DelegateObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E1FAD82260C3E400AEFD8C /* DelegateObject.swift */; }; + EA3B264C25FC0B7600008074 /* ModelHandlerProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA3B264B25FC0B7600008074 /* ModelHandlerProtocol.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -288,6 +289,7 @@ D2DEDCB823C6400600C44CC4 /* UnitInterval.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnitInterval.swift; sourceTree = ""; }; D2DEDCBA23C65BC300C44CC4 /* Percent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Percent.swift; sourceTree = ""; }; D2E1FAD82260C3E400AEFD8C /* DelegateObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DelegateObject.swift; sourceTree = ""; }; + EA3B264B25FC0B7600008074 /* ModelHandlerProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModelHandlerProtocol.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -404,6 +406,7 @@ 946EE1A8237B5C650036751F /* Model */ = { isa = PBXGroup; children = ( + EA3B264B25FC0B7600008074 /* ModelHandlerProtocol.swift */, 946EE1A2237B59C30036751F /* ModelProtocol.swift */, 946EE1A6237B5B1C0036751F /* ModelRegistry.swift */, ); @@ -869,6 +872,7 @@ 94C014D924212360005811A9 /* ActionSettingModel.swift in Sources */, D2DEDCB723C63F3B00C44CC4 /* Clamping.swift in Sources */, 01DF561421F90ADC00CC099B /* Dictionary+MFConvenience.swift in Sources */, + EA3B264C25FC0B7600008074 /* ModelHandlerProtocol.swift in Sources */, AFBB96B11FBA3B590008D868 /* MVMCoreDispatchUtility.m in Sources */, 946EE1A3237B59C30036751F /* ModelProtocol.swift in Sources */, AFEA17A9209B6A1C00BC6740 /* MVMCoreBlockOperation.m in Sources */, diff --git a/MVMCore/MVMCore/Models/Model/ModelHandlerProtocol.swift b/MVMCore/MVMCore/Models/Model/ModelHandlerProtocol.swift new file mode 100644 index 0000000..522147b --- /dev/null +++ b/MVMCore/MVMCore/Models/Model/ModelHandlerProtocol.swift @@ -0,0 +1,11 @@ +// +// ModelViewProtocol.swift +// MVMCore +// +// Created by Matt Bruce on 3/12/21. +// Copyright © 2021 myverizon. All rights reserved. +// + +import Foundation + +public protocol ModelHandlerProtocol {} From 473da43e6b4a84a2d9746927f05a38869cceda08 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Mon, 15 Mar 2021 16:40:22 -0500 Subject: [PATCH 2/2] refactored Category to include the mapping handler Dictionary added methods to register and get handlers --- .../MVMCore/Models/Model/ModelRegistry.swift | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift index 93407d1..deeb6ed 100644 --- a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift +++ b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift @@ -25,24 +25,59 @@ public struct ModelRegistry { var name: String var codingKey: String var instanceTypes: [String: ModelProtocol.Type] = [:] + var handlerTypes: [String: ModelHandlerProtocol.Type] = [:] } private static var categories: [String: Category] = [:] + + /// Registers models for Atomic use. + public static func register(handler: H.Type, for model: M.Type) throws { + //register the type + try self.register(model) + + //get the key for the handler + let key = model.identifier + + //get the category for the ModelProtocol + var category = getCategory(for: model) + + // Check to ensure the Category/Container combination doesn't exist. + if category.handlerTypes[key] != nil { + throw ModelRegistry.Error.other(message: "ModelHandlerProtocol: \(String(describing: handler)) already exists in Category: \(category.name)") + } else { + category.handlerTypes[key] = handler + } + categories[category.name] = category + } /// Registers models for Atomic use. public static func register(_ type: M.Type) throws { - - var category = categories[M.categoryName] ?? Category(name: M.categoryName, codingKey: M.categoryCodingKey) + //get the category for the ModelProtocol + var category = getCategory(for: type) // Check to ensure the Category/Type combination doesn't exist. if category.instanceTypes[M.identifier] != nil { throw ModelRegistry.Error.other(message: "ModelProtocol: \(M.identifier) already exists in Category: \(M.categoryName)") } - category.instanceTypes[M.identifier] = type categories[M.categoryName] = category } + public static func getHandler(_ model: ModelProtocol) -> ModelHandlerProtocol.Type? { + //get the modelType + let modelType = type(of: model) + + //get the category for the ModelProtocol + guard let category = categories[modelType.categoryName] else { return nil } + + //get the containerProtocol for this ModelProtocol you had registered + return category.handlerTypes[modelType.identifier] + } + + private static func getCategory(for type: M.Type) -> Category { + return categories[type.categoryName] ?? Category(name: type.categoryName, codingKey: type.categoryCodingKey) + } + private static func getCategory(for type: T.Type) -> Category? { // Temporary code till we find a better solution. //if this is a protocol composotion, loop through each protocol for a category lookup