// // TitleLockupStandardStyleConfiguration.swift // VDS // // Created by Matt Bruce on 6/29/23. // import Foundation import UIKit extension TitleLockup { public struct StandardStyleConfigurationProvider { public var styleConfigurations: [StandardStyleConfiguration] public init(styleConfigurations: [StandardStyleConfiguration]) { self.styleConfigurations = styleConfigurations } public func configuration(for titleStandardStyle: TitleStandardStyle) -> StandardStyleConfiguration? { let deviceType: StandardStyleConfiguration.DeviceType = UIDevice.isIPad ? .iPad : .iPhone guard let config: StandardStyleConfiguration = styleConfigurations.first(where: { return titleStandardStyle.isWithin($0.titleStandardStyles) && ($0.deviceType == deviceType || $0.deviceType == .all )}) else { return nil } return config } public func isValid(otherStandardStyle: OtherStandardStyle, for titleStandardStyle: TitleStandardStyle) -> Bool { guard let config = configuration(for: titleStandardStyle) else { return false } return otherStandardStyle.isWithin(config.spacingConfigurations.flatMap {$0.otherStandardStyles}) } public func spacing(for titleStandardStyle: TitleStandardStyle, otherStandardStyle: OtherStandardStyle) -> (otherStandardStyle: OtherStandardStyle, topSpacing: CGFloat, bottomSpacing: CGFloat)? { guard let config = configuration(for: titleStandardStyle) else { return nil } return config.styleSpacing(for: otherStandardStyle) } } public struct StandardStyleConfiguration { public enum DeviceType { case iPhone, iPad, all } public var deviceType: DeviceType public var titleStandardStyles:[TitleStandardStyle] public var spacingConfigurations: [SpacingConfiguration] public init(deviceType: DeviceType, titleStandardStyles: [TitleStandardStyle], spacingConfigurations: [SpacingConfiguration]) { self.deviceType = deviceType self.titleStandardStyles = titleStandardStyles self.spacingConfigurations = spacingConfigurations } public var allOtherStandardStyles: [OtherStandardStyle] { spacingConfigurations.flatMap {$0.otherStandardStyles} } public func styleSpacing(for otherStandardStyle: OtherStandardStyle) -> (otherStandardStyle: OtherStandardStyle, topSpacing: CGFloat, bottomSpacing: CGFloat) { //set default return other style what you pass in var realOtherStyle = otherStandardStyle //flatten all of the other styles registered for the title styles let allOtherStyles = spacingConfigurations.flatMap {$0.otherStandardStyles} //get the default other style incase what is passed isn't within the registered collection if let first = allOtherStyles.first, !realOtherStyle.isWithin(allOtherStyles) { realOtherStyle = first } //get the config against the other style or return defaults guard let styleSpacing = spacingConfigurations.first(where: {realOtherStyle.isWithin($0.otherStandardStyles)}) else { return (realOtherStyle, VDSLayout.Spacing.space2X.value, VDSLayout.Spacing.space2X.value) } return (realOtherStyle, styleSpacing.topSpacing, styleSpacing.bottomSpacing) } } public struct SpacingConfiguration { public var otherStandardStyles: [OtherStandardStyle] public var topSpacing: CGFloat public var bottomSpacing: CGFloat public init(otherStandardStyles: [OtherStandardStyle], topSpacing: CGFloat, bottomSpacing: CGFloat) { self.otherStandardStyles = otherStandardStyles self.topSpacing = topSpacing self.bottomSpacing = bottomSpacing } } }