From 54651b0a657f9859ea8df8bff0704baba20bd0ca Mon Sep 17 00:00:00 2001 From: "Christiano, Kevin" Date: Tue, 3 Aug 2021 17:38:30 +0000 Subject: [PATCH] extension of utility --- MVMCoreUI.xcodeproj/project.pbxproj | 4 ++ .../Utility/MVMCoreUIUtility+Extension.swift | 61 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 MVMCoreUI/Utility/MVMCoreUIUtility+Extension.swift diff --git a/MVMCoreUI.xcodeproj/project.pbxproj b/MVMCoreUI.xcodeproj/project.pbxproj index 6c64b7af..ab5f9887 100644 --- a/MVMCoreUI.xcodeproj/project.pbxproj +++ b/MVMCoreUI.xcodeproj/project.pbxproj @@ -121,6 +121,7 @@ 0AE98BB323FF0934004C5109 /* ExternalLinkModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE98BB223FF0934004C5109 /* ExternalLinkModel.swift */; }; 0AE98BB523FF18D2004C5109 /* Arrow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE98BB423FF18D2004C5109 /* Arrow.swift */; }; 0AE98BB723FF18E9004C5109 /* ArrowModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE98BB623FF18E9004C5109 /* ArrowModel.swift */; }; + 0AF60F0926B3316E00AC3DB4 /* MVMCoreUIUtility+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AF60F0826B3316E00AC3DB4 /* MVMCoreUIUtility+Extension.swift */; }; 1D6D258826899B0C00DEBB08 /* ImageButtonModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D6D258626899B0B00DEBB08 /* ImageButtonModel.swift */; }; 1D6D258926899B0C00DEBB08 /* ImageButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D6D258726899B0B00DEBB08 /* ImageButton.swift */; }; 279B1569242BBC2F00921D6C /* ActionModelAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 279B1568242BBC2F00921D6C /* ActionModelAdapter.swift */; }; @@ -689,6 +690,7 @@ 0AE98BB223FF0934004C5109 /* ExternalLinkModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExternalLinkModel.swift; sourceTree = ""; }; 0AE98BB423FF18D2004C5109 /* Arrow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Arrow.swift; sourceTree = ""; }; 0AE98BB623FF18E9004C5109 /* ArrowModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrowModel.swift; sourceTree = ""; }; + 0AF60F0826B3316E00AC3DB4 /* MVMCoreUIUtility+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MVMCoreUIUtility+Extension.swift"; sourceTree = ""; }; 1D6D258626899B0B00DEBB08 /* ImageButtonModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ImageButtonModel.swift; path = MVMCoreUI/Atomic/Atoms/Buttons/ImageButtonModel.swift; sourceTree = SOURCE_ROOT; }; 1D6D258726899B0B00DEBB08 /* ImageButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ImageButton.swift; path = MVMCoreUI/Atomic/Atoms/Buttons/ImageButton.swift; sourceTree = SOURCE_ROOT; }; 279B1568242BBC2F00921D6C /* ActionModelAdapter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionModelAdapter.swift; sourceTree = ""; }; @@ -2073,6 +2075,7 @@ D29DF2A721E7B2F9003B2FB9 /* MVMCoreUIConstants.h */, D29DF2A821E7B2F9003B2FB9 /* MVMCoreUIConstants.m */, 0A41BA6D2344FCD400D4C0BC /* CATransaction+Extension.swift */, + 0AF60F0826B3316E00AC3DB4 /* MVMCoreUIUtility+Extension.swift */, ); path = Utility; sourceTree = ""; @@ -2536,6 +2539,7 @@ 5248BFED23F12E350059236A /* ListThreeColumnPlanDataDividerModel.swift in Sources */, AA0A257824766C8A00862F64 /* ListLeftVariableIconWithRightCaretBodyTextModel.swift in Sources */, 0A5D59C223AD2F5700EFD9E9 /* AppleGuidelinesProtocol.swift in Sources */, + 0AF60F0926B3316E00AC3DB4 /* MVMCoreUIUtility+Extension.swift in Sources */, 8D070BB0241B56530099AC56 /* ListRightVariableTotalDataModel.swift in Sources */, 943784F5236B77BB006A1E82 /* Wheel.swift in Sources */, D23A90682614B0B4007E14CE /* CoreUIModelMapping.swift in Sources */, diff --git a/MVMCoreUI/Utility/MVMCoreUIUtility+Extension.swift b/MVMCoreUI/Utility/MVMCoreUIUtility+Extension.swift new file mode 100644 index 00000000..465f266f --- /dev/null +++ b/MVMCoreUI/Utility/MVMCoreUIUtility+Extension.swift @@ -0,0 +1,61 @@ +// +// CoureUIUtility.swift +// MVMCoreUI +// +// Created by Kevin Christiano on 7/29/21. +// Copyright © 2021 Verizon Wireless. All rights reserved. +// + +import UIKit + + +public extension MVMCoreUIUtility { + + /// Finds a view by a given type. + /// - Parameters: + /// - type: The type you are looking for. + /// - views: The starting array of subviews. + /// - Returns: If found, the first view matching the type will be casted to it and returned; otherwise nil will be returned. + static func findView(by type: T.Type, views: [UIView]) -> T? { + + guard !views.isEmpty else { return nil } + + var queue = [UIView]() + + // Reversed the array to first check views at the front. + for view in views { + if view is T { + return view as? T + } + + queue.append(contentsOf: view.subviews) + } + + return findView(by: type, views: queue) + } + + + /// Finds an array of views assocaited with a given type. + /// - Parameters: + /// - type: The type you are looking for. + /// - views: The starting array of subviews. + /// - Returns: Will return an array of any view associated with the given type. Will return an empty array of none were found. + static func findViews(by type: T.Type, views: [UIView]) -> [T] { + + guard !views.isEmpty else { return [] } + + var queue = [UIView]() + var matching = [T]() + + // Reversed the array to first check views at the front. + for view in views { + if view is T { + matching.append(view as! T) + } + + queue.append(contentsOf: view.subviews) + } + + return findViews(by: type, views: queue) + matching + } +}