|
| 1 | +// |
| 2 | +// CodeViewConfiguration.swift |
| 3 | +// PasscodeKit |
| 4 | +// |
| 5 | +// Created by David Walter on 02.09.23. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | +import PasscodeCore |
| 10 | + |
| 11 | +/// Customize the code view |
| 12 | +public struct CodeViewConfiguration { |
| 13 | + var foregroundColor: Color = .primary |
| 14 | + var emptyImage = Image(systemName: "circle") |
| 15 | + var filledImage = Image(systemName: "circle.fill") |
| 16 | + var spacing: CGFloat = 20 |
| 17 | + |
| 18 | + /// Configures the foreground color. |
| 19 | + /// - Parameter color: The color to use. |
| 20 | + /// - Returns: The configuration with the foreground color configured. |
| 21 | + func foregroundColor(_ color: Color) -> Self { |
| 22 | + var configuration = self |
| 23 | + configuration.foregroundColor = color |
| 24 | + return configuration |
| 25 | + } |
| 26 | + |
| 27 | + /// Configures the empty image code point. |
| 28 | + /// - Parameter image: The name of the system symbol image to use as empty code point. |
| 29 | + /// - Returns: The configuration with the empty image configured. |
| 30 | + func empty(systemName: String) -> Self { |
| 31 | + var configuration = self |
| 32 | + configuration.emptyImage = Image(systemName: systemName) |
| 33 | + return configuration |
| 34 | + } |
| 35 | + |
| 36 | + /// Configures the empty image code point. |
| 37 | + /// - Parameter image: The name of the system symbol image to use as empty code point. |
| 38 | + /// - Returns: The configuration with the empty image configured. |
| 39 | + func empty(image: Image) -> Self { |
| 40 | + var configuration = self |
| 41 | + configuration.emptyImage = image |
| 42 | + return configuration |
| 43 | + } |
| 44 | + |
| 45 | + /// Configures the filled image code point. |
| 46 | + /// - Parameter image: The name of the system symbol image to use as filled code point. |
| 47 | + /// - Returns: The configuration with the filled image configured. |
| 48 | + func filled(systemName: String) -> Self { |
| 49 | + var configuration = self |
| 50 | + configuration.filledImage = Image(systemName: systemName) |
| 51 | + return configuration |
| 52 | + } |
| 53 | + |
| 54 | + /// Configures the filled image code point. |
| 55 | + /// - Parameter image: The image to use as filled code point. |
| 56 | + /// - Returns: The configuration with the filled image configured. |
| 57 | + func filled(image: Image) -> Self { |
| 58 | + var configuration = self |
| 59 | + configuration.filledImage = image |
| 60 | + return configuration |
| 61 | + } |
| 62 | + |
| 63 | + /// Configures the spacing between the code points. |
| 64 | + /// - Parameter value: The spacing to use. |
| 65 | + /// - Returns: The configuration with the spacing configured. |
| 66 | + func spacing(_ value: CGFloat) -> Self { |
| 67 | + var configuration = self |
| 68 | + configuration.spacing = spacing |
| 69 | + return configuration |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +public extension PasscodeEnvironmentValues { |
| 74 | + var codeViewConfiguration: CodeViewConfiguration { |
| 75 | + get { self[CodeViewConfigurationKey.self] } |
| 76 | + set { self[CodeViewConfigurationKey.self] = newValue } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +private struct CodeViewConfigurationKey: EnvironmentKey { |
| 81 | + static var defaultValue: CodeViewConfiguration { |
| 82 | + CodeViewConfiguration() |
| 83 | + } |
| 84 | +} |
0 commit comments