-
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.
Added MutableValue property wrapper that creates a property that can be mutated in a SwiftUI view without kicking off a state change.
- Loading branch information
1 parent
1de9b27
commit a6e86ed
Showing
1 changed file
with
59 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// Mutable.swift | ||
// Escape from Mystic Manor | ||
// | ||
// Created by Kevin Mullins on 11/16/21. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
/// Provides storage for my wrapped value | ||
class ValueStorage<T> { | ||
|
||
/// The value being stored | ||
var storedValue:T? = nil | ||
|
||
} | ||
|
||
/// Creates a property that can be mutated in a SwiftUI view without kicking off a state change. | ||
@propertyWrapper struct MutableValue<T> { | ||
/// Provides storage for the mutating value | ||
private let storage:ValueStorage = ValueStorage<T>() | ||
private let name:String | ||
|
||
/// The value being wrapped. | ||
var wrappedValue:T { | ||
get { | ||
if name != "" { | ||
print("Fetching \(name): \(storage.storedValue!)") | ||
} | ||
return storage.storedValue! | ||
} | ||
nonmutating set { | ||
storage.storedValue = newValue | ||
if name != "" { | ||
print("Setting \(name): \(storage.storedValue!)") | ||
} | ||
} | ||
} | ||
|
||
// The value can be bound to SwiftUI | ||
var projectedValue: Binding<T> { | ||
Binding( | ||
get: { wrappedValue }, | ||
set: { wrappedValue = $0 } | ||
) | ||
} | ||
|
||
// Initializers | ||
/// Initializes the mutating value. | ||
/// - Parameter wrappedValue: The initial value for the property. | ||
init(wrappedValue:T, name:String = "") { | ||
self.storage.storedValue = wrappedValue | ||
self.name = name | ||
if name != "" { | ||
print("Initializing \(name): \(wrappedValue)") | ||
} | ||
} | ||
} |