|
| 1 | +// |
| 2 | +// UserSelection.swift |
| 3 | +// DevFoundation |
| 4 | +// |
| 5 | +// Created by Prachi Gauriar on 10/22/25. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +/// A generic structure that manages a user’s selection with a fallback to a default value. |
| 11 | +/// |
| 12 | +/// `UserSelection` prioritizes explicit user choices over programmatic defaults while maintaining separate tracking |
| 13 | +/// of both values. When a user has made an explicit selection, that choice is always preserved and used, regardless |
| 14 | +/// of changes to the default value. This ensures user preferences are never accidentally overwritten by programmatic |
| 15 | +/// updates to defaults. |
| 16 | +/// |
| 17 | +/// For example, |
| 18 | +/// |
| 19 | +/// // Programmatically set an initial default theme |
| 20 | +/// var themeSelection = UserSelection(defaultValue: "light") |
| 21 | +/// print(themeSelection.value) // "light" |
| 22 | +/// |
| 23 | +/// // User explicitly selects a theme |
| 24 | +/// themeSelection.selectedValue = "dark" |
| 25 | +/// print(themeSelection.value) // "dark" |
| 26 | +/// |
| 27 | +/// // Programmatically update the default theme preference |
| 28 | +/// themeSelection.defaultValue = "auto" |
| 29 | +/// print(themeSelection.value) // Still "dark" - user’s choice is preserved |
| 30 | +/// |
| 31 | +/// // User clears their selection to use programmatic default |
| 32 | +/// themeSelection.selectedValue = nil |
| 33 | +/// print(themeSelection.value) // "auto" - now uses updated programmatic default |
| 34 | +/// |
| 35 | +/// This pattern is particularly useful when you need to distinguish between user-specified values and |
| 36 | +/// programmatically-determined defaults that may change over time, ensuring explicit user choices are never overwritten |
| 37 | +/// by updated defaults. |
| 38 | +/// |
| 39 | +/// ## Protocol Conformance |
| 40 | +/// |
| 41 | +/// `UserSelection` conditionally conforms to `Codable`, `Equatable`, `Hashable`, and `Sendable` when the wrapped |
| 42 | +/// `Value` type also conforms to these protocols, making it suitable for persistence, comparison, hashing, and |
| 43 | +/// concurrent programming scenarios. |
| 44 | +public struct UserSelection<Value> { |
| 45 | + /// The default value to use when no selection has been made. |
| 46 | + /// |
| 47 | + /// This value is always available and serves as the fallback when `selectedValue` is `nil`. |
| 48 | + public var defaultValue: Value |
| 49 | + |
| 50 | + /// The user’s optional selection. |
| 51 | + /// |
| 52 | + /// When `nil`, the `value` property will return `defaultValue`. When set to a value, the `value` property will |
| 53 | + /// return this selection. |
| 54 | + public var selectedValue: Value? |
| 55 | + |
| 56 | + |
| 57 | + /// Creates a new user selection with the specified default value. |
| 58 | + /// |
| 59 | + /// - Parameter defaultValue: The value to use when no selection has been made. |
| 60 | + public init(defaultValue: Value) { |
| 61 | + self.defaultValue = defaultValue |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + /// The effective value, either the user’s selection or the default value. |
| 66 | + /// |
| 67 | + /// This computed property returns `selectedValue` if it’s not `nil`, otherwise it returns `defaultValue`. |
| 68 | + public var value: Value { |
| 69 | + selectedValue ?? defaultValue |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +extension UserSelection: Decodable where Value: Decodable {} |
| 75 | +extension UserSelection: Encodable where Value: Encodable {} |
| 76 | +extension UserSelection: Equatable where Value: Equatable {} |
| 77 | +extension UserSelection: Hashable where Value: Hashable {} |
| 78 | +extension UserSelection: Sendable where Value: Sendable {} |
0 commit comments