-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created DebugToggle property wrapper (#10)
* [1] Defined basic structure for Debug menu - Created DebugMenuStore as global data source - Defined Toggle, Button, Submenu actions * - Made shared instance of DebugMenuStore public - Public init for DebugToggleAction * - DebugMenuView init made public * - Added support for submenu action - Navigation titles - Clean up protocol - Move structs to their own files * - Created BaseDebugDataSource for convenience * - Updated example project * - Addressed PR comments * - Fix example app * - Introduced DebugAction protocol - Removed DebugActionType enum - Removed Binding to datasource for submenu action * - Fixed example project * - Removed debug menu store so app side can implement single global store * - Fix example app * - UserDefault property wrapper implemented - DebugToggleAction now just takes a Binding * WIP reset to default * - Added common options flag to data source - Added protocol for DebugResettable and button to reset values to default * - Added defaultValue to DebugResettable * - New init for DebugToggleRow that takes in UserDefault property wrapper * - Debug Toggle Property Wrapper * - Updated example app * - Reset to defaults moved to data source method
- Loading branch information
1 parent
9501e2c
commit 33cf23f
Showing
8 changed files
with
184 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// | ||
// DebugToggle.swift | ||
// | ||
// | ||
// Created by Alejandro Zielinsky on 2021-12-02. | ||
// | ||
|
||
import SwiftUI | ||
import Combine | ||
|
||
@propertyWrapper | ||
public struct DebugToggle<Value> { | ||
|
||
public let displayTitle: String | ||
public let defaultValue: Value | ||
public let storage: UserDefaults | ||
public let key: String? | ||
|
||
private var value: Value | ||
|
||
@available(*, unavailable) | ||
public var wrappedValue: Value { | ||
get { fatalError("only works on instance properties of classes") } | ||
set { fatalError("only works on instance properties of classes") } | ||
} | ||
|
||
public var projectedValue: Self { | ||
self | ||
} | ||
|
||
public static subscript<EnclosingSelf: ObservableObject>( | ||
_enclosingInstance object: EnclosingSelf, | ||
wrapped wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Value>, | ||
storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Self> | ||
) -> Value { | ||
get { | ||
let propertyWrapper = object[keyPath: storageKeyPath] | ||
if let key = propertyWrapper.key { | ||
return propertyWrapper.storage.object(forKey: key) as? Value ?? propertyWrapper.defaultValue | ||
} else { | ||
return propertyWrapper.value | ||
} | ||
} | ||
set { | ||
(object.objectWillChange as? ObservableObjectPublisher)?.send() | ||
let propertyWrapper = object[keyPath: storageKeyPath] | ||
if let key = propertyWrapper.key { | ||
if let optional = newValue as? AnyOptional, optional.isNil { | ||
propertyWrapper.storage.removeObject(forKey: key) | ||
} else { | ||
propertyWrapper.storage.set(newValue, forKey: key) | ||
} | ||
} else { | ||
object[keyPath: storageKeyPath].value = newValue | ||
} | ||
} | ||
} | ||
|
||
public init(wrappedValue: Value, | ||
key: String, | ||
storage: UserDefaults = .standard) { | ||
self.displayTitle = key.camelCaseToWords().replacingOccurrences(of: "Key", with: "") | ||
self.defaultValue = wrappedValue | ||
self.key = key | ||
self.storage = storage | ||
self.value = wrappedValue | ||
} | ||
|
||
public init(wrappedValue: Value, | ||
title: String, | ||
key: String? = nil, | ||
storage: UserDefaults = .standard) { | ||
self.displayTitle = title | ||
self.defaultValue = wrappedValue | ||
self.storage = storage | ||
self.key = key | ||
self.value = wrappedValue | ||
} | ||
} | ||
|
||
public extension DebugToggle where Value: ExpressibleByNilLiteral { | ||
init(title: String) { | ||
self.init(wrappedValue: nil, title: title) | ||
} | ||
} | ||
|
||
private protocol AnyOptional { | ||
var isNil: Bool { get } | ||
} | ||
|
||
private extension String { | ||
func camelCaseToWords() -> String { | ||
return unicodeScalars.reduce("") { | ||
if CharacterSet.uppercaseLetters.contains($1) { | ||
return ($0.capitalized + " " + String($1)) | ||
} | ||
else { | ||
return $0.capitalized + String($1) | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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