Skip to content

Commit

Permalink
MutableValue
Browse files Browse the repository at this point in the history
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
Appracatappra committed Jan 18, 2024
1 parent 1de9b27 commit a6e86ed
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Sources/SwiftUIKit/Properties/MutableValue.swift
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)")
}
}
}

0 comments on commit a6e86ed

Please sign in to comment.