diff --git a/MVMCore/MVMCore/ActionHandling/ActionActionsModel.swift b/MVMCore/MVMCore/ActionHandling/ActionActionsModel.swift index b36ae29..b2c8233 100644 --- a/MVMCore/MVMCore/ActionHandling/ActionActionsModel.swift +++ b/MVMCore/MVMCore/ActionHandling/ActionActionsModel.swift @@ -65,9 +65,9 @@ public struct ActionActionsModel: ActionModelProtocol { try container.encodeIfPresent(analyticsData, forKey: .analyticsData) } - public func isEqual(to model: any ModelProtocol) -> Bool { + public func isEqual(to model: any ModelComparisonProtocol) -> Bool { guard let model = model as? Self else { return false } - return actions.areEqual(to: model.actions) + return actions.isEqual(to: model.actions) && concurrent == model.concurrent && extraParameters == model.extraParameters && analyticsData == model.analyticsData diff --git a/MVMCore/MVMCore/ActionHandling/ActionOpenPageModel.swift b/MVMCore/MVMCore/ActionHandling/ActionOpenPageModel.swift index 2da0e0c..e899d8c 100644 --- a/MVMCore/MVMCore/ActionHandling/ActionOpenPageModel.swift +++ b/MVMCore/MVMCore/ActionHandling/ActionOpenPageModel.swift @@ -116,7 +116,7 @@ public struct ActionOpenPageModel: ActionModelProtocol, ActionOpenPageProtocol, try container.encodeIfPresent(fallbackResponse, forKey: .fallbackResponse) } - public func isEqual(to model: any ModelProtocol) -> Bool { + public func isEqual(to model: any ModelComparisonProtocol) -> Bool { guard let model = model as? Self else { return false } return pageType == model.pageType && baseURL == model.baseURL diff --git a/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterModel.swift b/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterModel.swift index 3fae967..ec8cf40 100644 --- a/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterModel.swift +++ b/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterModel.swift @@ -36,7 +36,7 @@ public class ClientParameterModel: Equatable, Codable { } public static func == (lhs: ClientParameterModel, rhs: ClientParameterModel) -> Bool { - return lhs.list.areEqual(to: rhs.list) + return lhs.list.isEqual(to: rhs.list) && lhs.timeout == rhs.timeout } } diff --git a/MVMCore/MVMCore/Models/Model/ModelProtocol.swift b/MVMCore/MVMCore/Models/Model/ModelProtocol.swift index 302b91d..cdabef2 100644 --- a/MVMCore/MVMCore/Models/Model/ModelProtocol.swift +++ b/MVMCore/MVMCore/Models/Model/ModelProtocol.swift @@ -8,7 +8,7 @@ import Foundation -public protocol ModelProtocol: Codable { +public protocol ModelProtocol: ModelComparisonProtocol, Codable { /// The key name of the molecule static var identifier: String { get } @@ -30,9 +30,6 @@ public protocol ModelProtocol: Codable { /// Convenience function to encode model using an unkeyed container. func encode(unkeyedContainer: inout UnkeyedEncodingContainer) throws - - /// Deep checks if the curent model is equal to another model. Defaults to false unless implemented otherwise. - func isEqual(to model: ModelProtocol) -> Bool } extension ModelProtocol { @@ -52,17 +49,23 @@ extension ModelProtocol { public func encode(unkeyedContainer: inout UnkeyedEncodingContainer) throws { try unkeyedContainer.encode(self) } - - public func isEqual(to model: ModelProtocol) -> Bool { +} + +public protocol ModelComparisonProtocol { + /// Deep checks if the curent model is equal to another model. Defaults to false unless implemented otherwise. + func isEqual(to model: ModelComparisonProtocol) -> Bool +} + +extension ModelComparisonProtocol { + public func isEqual(to model: ModelComparisonProtocol) -> Bool { return false } } public extension Optional { - /// Checks if the curent model is equal to another model. - func isEqual(to model: ModelProtocol?) -> Bool { - guard let self = self as? ModelProtocol else { + func isEqual(to model: ModelComparisonProtocol?) -> Bool { + guard let self = self as? ModelComparisonProtocol else { return model == nil } guard let model = model else { @@ -72,11 +75,10 @@ public extension Optional { } } - -public extension Array /*where Element == ModelProtocol*/ { - /// Checks if all the models in the given array match another given array of models. - func areEqual(to models: [ModelProtocol]) -> Bool { - guard count == models.count, let self = self as? [ModelProtocol] else { return false } +public extension Collection { + /// Checks if all the models in the given collection match another given collection. + func isEqual(to models: [ModelComparisonProtocol]) -> Bool { + guard count == models.count, let self = self as? [ModelComparisonProtocol] else { return false } return models.indices.allSatisfy { index in self[index].isEqual(to: models[index]) } @@ -84,15 +86,14 @@ public extension Array /*where Element == ModelProtocol*/ { } public extension Optional where Wrapped: Collection { - - func areEqual(to models: [ModelProtocol]?) -> Bool { - guard let self = self as? [ModelProtocol] else { + /// Checks if the curent model is equal to another model. + func isEqual(to models: [ModelComparisonProtocol]?) -> Bool { + guard let self = self as? [ModelComparisonProtocol] else { return models == nil } guard let models = models else { return false } - return self.areEqual(to: models) + return self.isEqual(to: models) } - }