Created
May 15, 2023 11:54
-
-
Save davidsteppenbeck/6b76347bf8f26deba6ab6cf6fa01e903 to your computer and use it in GitHub Desktop.
Methods for providing widget family specific values in Swift.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import WidgetKit | |
@available(iOS 14.0, macOS 11.0, *) | |
public extension WidgetFamily { | |
/// Returns the value for the current widget family. | |
/// | |
/// If no value is provided for the current widget family, the method will return the `defaultValue`. | |
/// | |
/// - Parameters: | |
/// - systemSmallValue: The value to use for the small widget familiy. | |
/// - systemMediumValue: The value to use for the medium widget familiy. | |
/// - systemLargeValue: The value to use for the large widget familiy. | |
/// - systemExtraLargeValue: The value to use for the extra large widget familiy. | |
/// - accessoryCircularValue: The value to use for the accessory circular widget familiy. | |
/// - accessoryRectangularValue: The value to use for the accessory rectangular widget familiy. | |
/// - accessoryInlineValue: The value to use for the accessory inline widget familiy. | |
/// - defaultValue: The default value to use when no specific value for a widget family is provided. | |
func valueFor<Value>(systemSmall systemSmallValue: @autoclosure () -> Value? = nil, systemMedium systemMediumValue: @autoclosure () -> Value? = nil, systemLarge systemLargeValue: @autoclosure () -> Value? = nil, systemExtraLarge systemExtraLargeValue: @autoclosure () -> Value? = nil, accessoryCircular accessoryCircularValue: @autoclosure () -> Value? = nil, accessoryRectangular accessoryRectangularValue: @autoclosure () -> Value? = nil, accessoryInline accessoryInlineValue: @autoclosure () -> Value? = nil, defaultValue: @autoclosure () -> Value) -> Value { | |
_valueFor( | |
systemSmall: systemSmallValue, | |
systemMedium: systemMediumValue, | |
systemLarge: systemLargeValue, | |
systemExtraLarge: systemExtraLargeValue, | |
accessoryCircular: accessoryCircularValue, | |
accessoryRectangular: accessoryRectangularValue, | |
accessoryInline: accessoryInlineValue, | |
defaultValue: defaultValue | |
) | |
} | |
/// Returns the value for the current widget family. | |
/// | |
/// If no value is provided for the current widget family, the method will return the `defaultValue`. | |
/// | |
/// - Parameters: | |
/// - systemSmallValue: The value to use for the small widget familiy. | |
/// - systemMediumValue: The value to use for the medium widget familiy. | |
/// - systemLargeValue: The value to use for the large widget familiy. | |
/// - systemExtraLargeValue: The value to use for the extra large widget familiy. | |
/// - accessoryCircularValue: The value to use for the accessory circular widget familiy. | |
/// - accessoryRectangularValue: The value to use for the accessory rectangular widget familiy. | |
/// - accessoryInlineValue: The value to use for the accessory inline widget familiy. | |
/// - defaultValue: The default value to use when no specific value for a widget family is provided. | |
func valueFor<Value: Numeric>(systemSmall systemSmallValue: @autoclosure () -> Value? = nil, systemMedium systemMediumValue: @autoclosure () -> Value? = nil, systemLarge systemLargeValue: @autoclosure () -> Value? = nil, systemExtraLarge systemExtraLargeValue: @autoclosure () -> Value? = nil, accessoryCircular accessoryCircularValue: @autoclosure () -> Value? = nil, accessoryRectangular accessoryRectangularValue: @autoclosure () -> Value? = nil, accessoryInline accessoryInlineValue: @autoclosure () -> Value? = nil, defaultValue: @autoclosure () -> Value = .zero) -> Value { | |
_valueFor( | |
systemSmall: systemSmallValue, | |
systemMedium: systemMediumValue, | |
systemLarge: systemLargeValue, | |
systemExtraLarge: systemExtraLargeValue, | |
accessoryCircular: accessoryCircularValue, | |
accessoryRectangular: accessoryRectangularValue, | |
accessoryInline: accessoryInlineValue, | |
defaultValue: defaultValue | |
) | |
} | |
/// Returns the value for the current widget family. | |
/// | |
/// If no value is provided for the current widget family, the method will return the `defaultValue`. | |
/// | |
/// - Parameters: | |
/// - systemSmallValue: The array to use for the small widget familiy. | |
/// - systemMediumValue: The array to use for the medium widget familiy. | |
/// - systemLargeValue: The array to use for the large widget familiy. | |
/// - systemExtraLargeValue: The array to use for the extra large widget familiy. | |
/// - accessoryCircularValue: The array to use for the accessory circular widget familiy. | |
/// - accessoryRectangularValue: The array to use for the accessory rectangular widget familiy. | |
/// - accessoryInlineValue: The array to use for the accessory inline widget familiy. | |
/// - defaultValue: The default array to use when no specific value for a widget family is provided. | |
func valueFor<Element>(systemSmall systemSmallValue: @autoclosure () -> [Element]? = nil, systemMedium systemMediumValue: @autoclosure () -> [Element]? = nil, systemLarge systemLargeValue: @autoclosure () -> [Element]? = nil, systemExtraLarge systemExtraLargeValue: @autoclosure () -> [Element]? = nil, accessoryCircular accessoryCircularValue: @autoclosure () -> [Element]? = nil, accessoryRectangular accessoryRectangularValue: @autoclosure () -> [Element]? = nil, accessoryInline accessoryInlineValue: @autoclosure () -> [Element]? = nil, defaultValue: @autoclosure () -> [Element] = []) -> [Element] { | |
_valueFor( | |
systemSmall: systemSmallValue, | |
systemMedium: systemMediumValue, | |
systemLarge: systemLargeValue, | |
systemExtraLarge: systemExtraLargeValue, | |
accessoryCircular: accessoryCircularValue, | |
accessoryRectangular: accessoryRectangularValue, | |
accessoryInline: accessoryInlineValue, | |
defaultValue: defaultValue | |
) | |
} | |
/// Returns the font for the current widget family. | |
/// | |
/// If no value is provided for the current widget family, the method will return the `defaultValue`. | |
/// | |
/// - Parameters: | |
/// - systemSmallValue: The string to use for the small widget familiy. | |
/// - systemMediumValue: The string to use for the medium widget familiy. | |
/// - systemLargeValue: The string to use for the large widget familiy. | |
/// - systemExtraLargeValue: The string to use for the extra large widget familiy. | |
/// - accessoryCircularValue: The string to use for the accessory circular widget familiy. | |
/// - accessoryRectangularValue: The string to use for the accessory rectangular widget familiy. | |
/// - accessoryInlineValue: The string to use for the accessory inline widget familiy. | |
/// - defaultValue: The default string to use when no specific value for a widget family is provided. | |
func valueFor(systemSmall systemSmallValue: @autoclosure () -> String? = nil, systemMedium systemMediumValue: @autoclosure () -> String? = nil, systemLarge systemLargeValue: @autoclosure () -> String? = nil, systemExtraLarge systemExtraLargeValue: @autoclosure () -> String? = nil, accessoryCircular accessoryCircularValue: @autoclosure () -> String? = nil, accessoryRectangular accessoryRectangularValue: @autoclosure () -> String? = nil, accessoryInline accessoryInlineValue: @autoclosure () -> String? = nil, defaultValue: @autoclosure () -> String = "") -> String { | |
_valueFor( | |
systemSmall: systemSmallValue, | |
systemMedium: systemMediumValue, | |
systemLarge: systemLargeValue, | |
systemExtraLarge: systemExtraLargeValue, | |
accessoryCircular: accessoryCircularValue, | |
accessoryRectangular: accessoryRectangularValue, | |
accessoryInline: accessoryInlineValue, | |
defaultValue: defaultValue | |
) | |
} | |
/// Returns the font for the current widget family. | |
/// | |
/// If no value is provided for the current widget family, the method will return the `defaultValue`. | |
/// | |
/// - Parameters: | |
/// - systemSmallValue: The font to use for the small widget familiy. | |
/// - systemMediumValue: The font to use for the medium widget familiy. | |
/// - systemLargeValue: The font to use for the large widget familiy. | |
/// - systemExtraLargeValue: The font to use for the extra large widget familiy. | |
/// - accessoryCircularValue: The font to use for the accessory circular widget familiy. | |
/// - accessoryRectangularValue: The font to use for the accessory rectangular widget familiy. | |
/// - accessoryInlineValue: The font to use for the accessory inline widget familiy. | |
/// - defaultValue: The default font to use when no specific value for a widget family is provided. | |
func valueFor(systemSmall systemSmallValue: @autoclosure () -> Font? = nil, systemMedium systemMediumValue: @autoclosure () -> Font? = nil, systemLarge systemLargeValue: @autoclosure () -> Font? = nil, systemExtraLarge systemExtraLargeValue: @autoclosure () -> Font? = nil, accessoryCircular accessoryCircularValue: @autoclosure () -> Font? = nil, accessoryRectangular accessoryRectangularValue: @autoclosure () -> Font? = nil, accessoryInline accessoryInlineValue: @autoclosure () -> Font? = nil, defaultValue: @autoclosure () -> Font = .body) -> Font { | |
_valueFor( | |
systemSmall: systemSmallValue, | |
systemMedium: systemMediumValue, | |
systemLarge: systemLargeValue, | |
systemExtraLarge: systemExtraLargeValue, | |
accessoryCircular: accessoryCircularValue, | |
accessoryRectangular: accessoryRectangularValue, | |
accessoryInline: accessoryInlineValue, | |
defaultValue: defaultValue | |
) | |
} | |
/// Returns the dynamic type size for the current widget family. | |
/// | |
/// If no value is provided for the current widget family, the method will return the `defaultValue`. | |
/// | |
/// - Parameters: | |
/// - systemSmallValue: The dynamic type size to use for the small widget familiy. | |
/// - systemMediumValue: The dynamic type size to use for the medium widget familiy. | |
/// - systemLargeValue: The dynamic type size to use for the large widget familiy. | |
/// - systemExtraLargeValue: The dynamic type size to use for the extra large widget familiy. | |
/// - accessoryCircularValue: The dynamic type size to use for the accessory circular widget familiy. | |
/// - accessoryRectangularValue: The dynamic type size to use for the accessory rectangular widget familiy. | |
/// - accessoryInlineValue: The dynamic type size to use for the accessory inline widget familiy. | |
/// - defaultValue: The default dynamic type size to use when no specific value for a widget family is provided. | |
@available(iOS 15.0, macOS 12.0, *) | |
func valueFor(systemSmall systemSmallValue: @autoclosure () -> DynamicTypeSize? = nil, systemMedium systemMediumValue: @autoclosure () -> DynamicTypeSize? = nil, systemLarge systemLargeValue: @autoclosure () -> DynamicTypeSize? = nil, systemExtraLarge systemExtraLargeValue: @autoclosure () -> DynamicTypeSize? = nil, accessoryCircular accessoryCircularValue: @autoclosure () -> DynamicTypeSize? = nil, accessoryRectangular accessoryRectangularValue: @autoclosure () -> DynamicTypeSize? = nil, accessoryInline accessoryInlineValue: @autoclosure () -> DynamicTypeSize? = nil, defaultValue: @autoclosure () -> DynamicTypeSize = .large) -> DynamicTypeSize { | |
_valueFor( | |
systemSmall: systemSmallValue, | |
systemMedium: systemMediumValue, | |
systemLarge: systemLargeValue, | |
systemExtraLarge: systemExtraLargeValue, | |
accessoryCircular: accessoryCircularValue, | |
accessoryRectangular: accessoryRectangularValue, | |
accessoryInline: accessoryInlineValue, | |
defaultValue: defaultValue | |
) | |
} | |
private func _valueFor<Value>(systemSmall systemSmallValue: () -> Value?, systemMedium systemMediumValue: () -> Value?, systemLarge systemLargeValue: () -> Value?, systemExtraLarge systemExtraLargeValue: () -> Value?, accessoryCircular accessoryCircularValue: () -> Value?, accessoryRectangular accessoryRectangularValue: () -> Value?, accessoryInline accessoryInlineValue: () -> Value?, defaultValue: () -> Value) -> Value { | |
switch self { | |
case .systemSmall: | |
return systemSmallValue() ?? defaultValue() | |
case .systemMedium: | |
return systemMediumValue() ?? defaultValue() | |
case .systemLarge: | |
return systemLargeValue() ?? defaultValue() | |
case .systemExtraLarge: | |
return systemExtraLargeValue() ?? defaultValue() | |
case .accessoryCircular: | |
return accessoryCircularValue() ?? defaultValue() | |
case .accessoryRectangular: | |
return accessoryRectangularValue() ?? defaultValue() | |
case .accessoryInline: | |
return accessoryInlineValue() ?? defaultValue() | |
@unknown default: | |
return defaultValue() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment