diff --git a/MVMCoreUI.xcodeproj/project.pbxproj b/MVMCoreUI.xcodeproj/project.pbxproj index 15f22974..8941d5af 100644 --- a/MVMCoreUI.xcodeproj/project.pbxproj +++ b/MVMCoreUI.xcodeproj/project.pbxproj @@ -207,6 +207,7 @@ 94CA227D24058534002D6750 /* VerizonNHGeDS-Regular.otf in Resources */ = {isa = PBXBuildFile; fileRef = 94CA227924058533002D6750 /* VerizonNHGeDS-Regular.otf */; }; 94CA227E24058534002D6750 /* VerizonNHGeDS-Bold.otf in Resources */ = {isa = PBXBuildFile; fileRef = 94CA227A24058533002D6750 /* VerizonNHGeDS-Bold.otf */; }; 94F6516D2437954100631BF9 /* Tabs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94F6516C2437954100631BF9 /* Tabs.swift */; }; + A5DB67FB26B2BE0D00DC6941 /* RuleDateValidationModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5DB67FA26B2BE0D00DC6941 /* RuleDateValidationModel.swift */; }; AA07EA912510A442009A2AE3 /* StarModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA07EA902510A442009A2AE3 /* StarModel.swift */; }; AA07EA932510A451009A2AE3 /* Star.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA07EA922510A451009A2AE3 /* Star.swift */; }; AA0A257824766C8A00862F64 /* ListLeftVariableIconWithRightCaretBodyTextModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0A257724766C8A00862F64 /* ListLeftVariableIconWithRightCaretBodyTextModel.swift */; }; @@ -772,6 +773,7 @@ 94CA227A24058533002D6750 /* VerizonNHGeDS-Bold.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "VerizonNHGeDS-Bold.otf"; sourceTree = ""; }; 94CA227B24058533002D6750 /* VerizonNHGeTX-Regular.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "VerizonNHGeTX-Regular.otf"; sourceTree = ""; }; 94F6516C2437954100631BF9 /* Tabs.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Tabs.swift; sourceTree = ""; }; + A5DB67FA26B2BE0D00DC6941 /* RuleDateValidationModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RuleDateValidationModel.swift; sourceTree = ""; }; AA07EA902510A442009A2AE3 /* StarModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StarModel.swift; sourceTree = ""; }; AA07EA922510A451009A2AE3 /* Star.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Star.swift; sourceTree = ""; }; AA0A257724766C8A00862F64 /* ListLeftVariableIconWithRightCaretBodyTextModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListLeftVariableIconWithRightCaretBodyTextModel.swift; sourceTree = ""; }; @@ -1180,6 +1182,7 @@ 011D959E240453A1000E3791 /* RuleAllValueChangedModel.swift */, 011D95A0240453D0000E3791 /* RuleEqualsModel.swift */, 011D95A2240453F8000E3791 /* RuleRegexModel.swift */, + A5DB67FA26B2BE0D00DC6941 /* RuleDateValidationModel.swift */, 0A69F610241BDEA700F7231B /* RuleAnyRequiredModel.swift */, 0A849EFD246F1775009F277F /* RuleEqualsIgnoreCaseModel.swift */, ); @@ -2760,6 +2763,7 @@ D29E28DA23D21AFA00ACEA85 /* StringAndMoleculeModel.swift in Sources */, D260105D23D0BCD400764D80 /* Stack.swift in Sources */, 0A7EF85D23D8A95600B2AAD1 /* TextEntryFieldModel.swift in Sources */, + A5DB67FB26B2BE0D00DC6941 /* RuleDateValidationModel.swift in Sources */, BB54C5212434D92F0038326C /* ListRightVariableButtonAllTextAndLinksModel.swift in Sources */, D2092349244A51D40044AD09 /* RadioSwatchModel.swift in Sources */, 0A775F2824893937009EFB58 /* ThreeHeadlineBodyLinkModel.swift in Sources */, diff --git a/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleDateValidationModel.swift b/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleDateValidationModel.swift new file mode 100644 index 00000000..7490c8b8 --- /dev/null +++ b/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleDateValidationModel.swift @@ -0,0 +1,119 @@ +// +// RuleDateValidationModel.swift +// MVMCoreUI +// +// Created by Tondapu, Alekhya on 29/07/21. +// Copyright © 2021 Verizon Wireless. All rights reserved. +// + + +public class RuleDateValidationModel: RulesProtocol { + //-------------------------------------------------- + // MARK: - Properties + //-------------------------------------------------- + + public static var identifier: String = "dateValidation" + public var type: String = RuleDateValidationModel.identifier + public var fields: [String] + public var date: String + public var dateFormat: String + public var errorMessage: [String: String]? + public var dateOperator: String + + //-------------------------------------------------- + // MARK: - Validation + //-------------------------------------------------- + public func isValid(_ formField: FormFieldProtocol) -> Bool { + return false + } + + public func validate(_ fieldMolecules: [String: FormFieldProtocol],_ previousFieldValidity: [String: Bool]) -> (valid: Bool, fieldValidity: [String: Bool]) { + var valid = true + var compareValue: AnyHashable? + var previousValidity: [String: Bool] = [:] + var isValidDate: Bool = false + + for formKey in fields { + guard let formField = fieldMolecules[formKey] else { continue } + + if compareValue == nil { + compareValue = formField.formFieldValue() + } + + /// Fetch date format and apply them to selected date from picker and validationdate for comparision + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = dateFormat + let validDate = dateFormatter.date(from: date) ?? Date() + let enteredDate = dateFormatter.date(from: compareValue as? String ?? "") ?? Date() + + switch dateOperator { + case "greaterThanOrEqual": + isValidDate = enteredDate.isGreaterThenOrEqualToDate(validDate) + break + case "lessThanOrEqual": + isValidDate = enteredDate.isLessThenOrEqualToDate(validDate) + break + case "equal": + isValidDate = enteredDate.isEqualToDate(validDate) + break + case "greaterThan": + isValidDate = enteredDate.isGreaterThenDate(validDate) + break + case "lessThan": + isValidDate = enteredDate.isLessThenDate(validDate) + break + default: break + } + + if (!isValidDate) { + valid = false + previousValidity[formKey] = valid + (formField as? FormRuleWatcherFieldProtocol)?.setValidity(valid, rule: self) + break + } else { + var fieldValidity = valid + // If past rule is invalid for a field, the current rule should not flip the validity of a field + if let validity = previousFieldValidity[formKey], !validity, fieldValidity { + fieldValidity = false + } + (formField as? FormRuleWatcherFieldProtocol)?.setValidity(fieldValidity, rule: self) + } + + } + return (valid: valid, fieldValidity: previousValidity) + } +} + +//-------------------------------------------------- +// MARK: - Date Extension +//-------------------------------------------------- + +extension Date { + + /// Note: This methods compare two dates without considering the time + func compareWithDate(_ date:Date) -> ComparisonResult { + let order = NSCalendar.current.compare(self, to: date, toGranularity: .day) + return order + } + + func isLessThenDate(_ date: Date) -> Bool { + return compareWithDate(date) == .orderedAscending + } + + func isGreaterThenDate(_ date: Date) -> Bool { + return compareWithDate(date) == .orderedDescending + } + + func isEqualToDate(_ date: Date) -> Bool { + return compareWithDate(date) == .orderedSame + } + + func isGreaterThenOrEqualToDate(_ date: Date) -> Bool { + return isEqualToDate(date) || isGreaterThenDate(date) + } + + func isLessThenOrEqualToDate(_ date: Date) -> Bool { + return isEqualToDate(date) || isLessThenDate(date) + } + /// Note: This methods compare two dates without considering the time +} diff --git a/MVMCoreUI/OtherHandlers/CoreUIModelMapping.swift b/MVMCoreUI/OtherHandlers/CoreUIModelMapping.swift index 1edea27b..97a1c5c7 100644 --- a/MVMCoreUI/OtherHandlers/CoreUIModelMapping.swift +++ b/MVMCoreUI/OtherHandlers/CoreUIModelMapping.swift @@ -242,5 +242,6 @@ open class CoreUIModelMapping: ModelMapping { ModelRegistry.register(RuleEqualsModel.self) ModelRegistry.register(RuleEqualsIgnoreCaseModel.self) ModelRegistry.register(RuleRegexModel.self) + ModelRegistry.register(RuleDateValidationModel.self) } }