Add support for explicit identifiers
This commit is contained in:
parent
aabd36d7ac
commit
7b26dce977
@ -61,7 +61,7 @@ public protocol Model {
|
|||||||
public struct ModelData {
|
public struct ModelData {
|
||||||
|
|
||||||
/// The state value's identifier.
|
/// The state value's identifier.
|
||||||
var id: UUID
|
var id: String
|
||||||
/// Whether to force update the views when this value changes.
|
/// Whether to force update the views when this value changes.
|
||||||
var force: Bool
|
var force: Bool
|
||||||
|
|
||||||
|
|||||||
@ -55,7 +55,7 @@ public struct State<Value>: StateProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The state's identifier for the stored value.
|
/// The state's identifier for the stored value.
|
||||||
var id: UUID = .init()
|
var id: String
|
||||||
|
|
||||||
/// Whether to force update the views when the value changes.
|
/// Whether to force update the views when the value changes.
|
||||||
var forceUpdates: Bool
|
var forceUpdates: Bool
|
||||||
@ -66,9 +66,11 @@ public struct State<Value>: StateProtocol {
|
|||||||
/// Initialize a property representing a state in the view with an autoclosure.
|
/// Initialize a property representing a state in the view with an autoclosure.
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - wrappedValue: The wrapped value.
|
/// - wrappedValue: The wrapped value.
|
||||||
|
/// - id: An explicit identifier.
|
||||||
/// - forceUpdates: Whether to force update all available views when the property gets modified.
|
/// - forceUpdates: Whether to force update all available views when the property gets modified.
|
||||||
public init(wrappedValue: @autoclosure @escaping () -> Value, forceUpdates: Bool = false) {
|
public init(wrappedValue: @autoclosure @escaping () -> Value, id: String? = nil, forceUpdates: Bool = false) {
|
||||||
getInitialValue = wrappedValue
|
getInitialValue = wrappedValue
|
||||||
|
self.id = id ?? UUID().uuidString
|
||||||
self.forceUpdates = forceUpdates
|
self.forceUpdates = forceUpdates
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -25,9 +25,9 @@ public enum StateManager {
|
|||||||
struct State {
|
struct State {
|
||||||
|
|
||||||
/// The state's identifier.
|
/// The state's identifier.
|
||||||
var id: UUID
|
var id: String
|
||||||
/// Old identifiers of the state which need to be saved.
|
/// Old identifiers of the state which need to be saved.
|
||||||
var oldIDs: [UUID] = []
|
var oldIDs: [String] = []
|
||||||
/// The state value.
|
/// The state value.
|
||||||
var value: Any?
|
var value: Any?
|
||||||
/// Whether to update in the next iteration.
|
/// Whether to update in the next iteration.
|
||||||
@ -36,13 +36,13 @@ public enum StateManager {
|
|||||||
/// Whether the state's identifiers contain a certain identifier.
|
/// Whether the state's identifiers contain a certain identifier.
|
||||||
/// - Parameter id: The identifier.
|
/// - Parameter id: The identifier.
|
||||||
/// - Returns: Whether the id is contained.
|
/// - Returns: Whether the id is contained.
|
||||||
func contains(id: UUID) -> Bool {
|
func contains(id: String) -> Bool {
|
||||||
id == self.id || oldIDs.contains(id)
|
id == self.id || oldIDs.contains(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Change the identifier to a new one.
|
/// Change the identifier to a new one.
|
||||||
/// - Parameter newID: The new identifier.
|
/// - Parameter newID: The new identifier.
|
||||||
mutating func changeID(new newID: UUID) {
|
mutating func changeID(new newID: String) {
|
||||||
id = newID
|
id = newID
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,6 +57,9 @@ public enum StateManager {
|
|||||||
for handler in updateHandlers {
|
for handler in updateHandlers {
|
||||||
handler(force)
|
handler(force)
|
||||||
}
|
}
|
||||||
|
for state in state where state.update {
|
||||||
|
updatedState(id: state.id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +73,7 @@ public enum StateManager {
|
|||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - id: The identifier.
|
/// - id: The identifier.
|
||||||
/// - value: The new value.
|
/// - value: The new value.
|
||||||
static func setState(id: UUID, value: Any?) {
|
static func setState(id: String, value: Any?) {
|
||||||
if saveState {
|
if saveState {
|
||||||
guard let index = state.firstIndex(where: { $0.contains(id: id) }) else {
|
guard let index = state.firstIndex(where: { $0.contains(id: id) }) else {
|
||||||
state.append(.init(id: id, value: value))
|
state.append(.init(id: id, value: value))
|
||||||
@ -83,13 +86,13 @@ public enum StateManager {
|
|||||||
/// Get the state value for a certain ID.
|
/// Get the state value for a certain ID.
|
||||||
/// - Parameter id: The identifier.
|
/// - Parameter id: The identifier.
|
||||||
/// - Returns: The value.
|
/// - Returns: The value.
|
||||||
static func getState(id: UUID) -> Any? {
|
static func getState(id: String) -> Any? {
|
||||||
state[safe: state.firstIndex { $0.contains(id: id) }]?.value
|
state[safe: state.firstIndex { $0.contains(id: id) }]?.value
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Mark the state of a certain id as updated.
|
/// Mark the state of a certain id as updated.
|
||||||
/// - Parameter id: The identifier.
|
/// - Parameter id: The identifier.
|
||||||
static func updateState(id: UUID) {
|
static func updateState(id: String) {
|
||||||
if saveState {
|
if saveState {
|
||||||
state[safe: state.firstIndex { $0.contains(id: id) }]?.update = true
|
state[safe: state.firstIndex { $0.contains(id: id) }]?.update = true
|
||||||
}
|
}
|
||||||
@ -97,7 +100,7 @@ public enum StateManager {
|
|||||||
|
|
||||||
/// Mark the state of a certain id as not updated.
|
/// Mark the state of a certain id as not updated.
|
||||||
/// - Parameter id: The identifier.
|
/// - Parameter id: The identifier.
|
||||||
static func updatedState(id: UUID) {
|
static func updatedState(id: String) {
|
||||||
if saveState {
|
if saveState {
|
||||||
state[safe: state.firstIndex { $0.contains(id: id) }]?.update = false
|
state[safe: state.firstIndex { $0.contains(id: id) }]?.update = false
|
||||||
}
|
}
|
||||||
@ -106,7 +109,7 @@ public enum StateManager {
|
|||||||
/// Get whether to update the state of a certain id.
|
/// Get whether to update the state of a certain id.
|
||||||
/// - Parameter id: The identifier.
|
/// - Parameter id: The identifier.
|
||||||
/// - Returns: Whether to update the state.
|
/// - Returns: Whether to update the state.
|
||||||
static func getUpdateState(id: UUID) -> Bool {
|
static func getUpdateState(id: String) -> Bool {
|
||||||
state[safe: state.firstIndex { $0.contains(id: id) }]?.update ?? false
|
state[safe: state.firstIndex { $0.contains(id: id) }]?.update ?? false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,7 +117,7 @@ public enum StateManager {
|
|||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - oldID: The old identifier.
|
/// - oldID: The old identifier.
|
||||||
/// - newID: The new identifier.
|
/// - newID: The new identifier.
|
||||||
static func changeID(old oldID: UUID, new newID: UUID) {
|
static func changeID(old oldID: String, new newID: String) {
|
||||||
if saveState {
|
if saveState {
|
||||||
state[safe: state.firstIndex { $0.contains(id: oldID) }]?.changeID(new: newID)
|
state[safe: state.firstIndex { $0.contains(id: oldID) }]?.changeID(new: newID)
|
||||||
}
|
}
|
||||||
@ -122,7 +125,7 @@ public enum StateManager {
|
|||||||
|
|
||||||
/// Save a state's identifier until the program ends.
|
/// Save a state's identifier until the program ends.
|
||||||
/// - Parameter id: The identifier.
|
/// - Parameter id: The identifier.
|
||||||
static func addConstantID(_ id: UUID) {
|
static func addConstantID(_ id: String) {
|
||||||
state[safe: state.firstIndex { $0.id == id }]?.oldIDs.append(id)
|
state[safe: state.firstIndex { $0.id == id }]?.oldIDs.append(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -11,6 +11,6 @@ import Foundation
|
|||||||
protocol StateProtocol {
|
protocol StateProtocol {
|
||||||
|
|
||||||
/// The identifier for the state property's value.
|
/// The identifier for the state property's value.
|
||||||
var id: UUID { get set }
|
var id: String { get set }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -69,7 +69,6 @@ extension App {
|
|||||||
}
|
}
|
||||||
if StateManager.getUpdateState(id: property.value.id) {
|
if StateManager.getUpdateState(id: property.value.id) {
|
||||||
updateProperties = true
|
updateProperties = true
|
||||||
StateManager.updatedState(id: property.value.id)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var removeIndices: [Int] = []
|
var removeIndices: [Int] = []
|
||||||
|
|||||||
@ -49,7 +49,6 @@ struct StateWrapper: ConvenienceWidget {
|
|||||||
}
|
}
|
||||||
if StateManager.getUpdateState(id: property.value.id) {
|
if StateManager.getUpdateState(id: property.value.id) {
|
||||||
updateProperties = true
|
updateProperties = true
|
||||||
StateManager.updatedState(id: property.value.id)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
guard let storage = storage.content[.mainContent]?.first else {
|
guard let storage = storage.content[.mainContent]?.first else {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user