Merge branch 'feature/handlerRegistry' into 'develop'

Feature/handler registry

See merge request BPHV_MIPS/mvm_core!150
This commit is contained in:
Pfeil, Scott Robert 2021-03-18 11:09:00 -04:00
commit 34391ce326
3 changed files with 53 additions and 3 deletions

View File

@ -150,6 +150,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 */
@ -290,6 +291,7 @@
D2DEDCB823C6400600C44CC4 /* UnitInterval.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnitInterval.swift; sourceTree = "<group>"; };
D2DEDCBA23C65BC300C44CC4 /* Percent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Percent.swift; sourceTree = "<group>"; };
D2E1FAD82260C3E400AEFD8C /* DelegateObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DelegateObject.swift; sourceTree = "<group>"; };
EA3B264B25FC0B7600008074 /* ModelHandlerProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModelHandlerProtocol.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -407,6 +409,7 @@
946EE1A8237B5C650036751F /* Model */ = {
isa = PBXGroup;
children = (
EA3B264B25FC0B7600008074 /* ModelHandlerProtocol.swift */,
946EE1A2237B59C30036751F /* ModelProtocol.swift */,
946EE1A6237B5B1C0036751F /* ModelRegistry.swift */,
);
@ -873,6 +876,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 */,

View File

@ -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 {}

View File

@ -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<H: ModelHandlerProtocol, M: ModelProtocol>(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<M: ModelProtocol>(_ 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<M: ModelProtocol>(for type: M.Type) -> Category {
return categories[type.categoryName] ?? Category(name: type.categoryName, codingKey: type.categoryCodingKey)
}
private static func getCategory<T>(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