Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Switch to state management using reference types
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Aug 31, 2024
1 parent 2e11525 commit 54292ed
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 144 deletions.
12 changes: 6 additions & 6 deletions Sources/Model/Data Flow/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public protocol Model {
public struct ModelData {

/// The state value's identifier.
var id: String
var storage: StateContent.Storage
/// Whether to force update the views when this value changes.
var force: Bool

Expand All @@ -78,8 +78,8 @@ extension Model {
guard let data = model else {
return
}
StateManager.setState(id: data.id, value: newValue)
StateManager.updateState(id: data.id)
data.storage.value = newValue
data.storage.update = true
StateManager.updateViews(force: data.force)
}
}
Expand All @@ -98,8 +98,8 @@ extension Model {
}
var model = getModel()
setModel(&model)
StateManager.setState(id: data.id, value: model)
StateManager.updateState(id: data.id)
data.storage.value = model
data.storage.update = true
StateManager.updateViews(force: data.force)
}

Expand All @@ -112,7 +112,7 @@ extension Model {
guard let data = model else {
return self
}
return StateManager.getState(id: data.id) as? Self ?? self
return data.storage.value as? Self ?? self
}

}
24 changes: 12 additions & 12 deletions Sources/Model/Data Flow/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public struct State<Value>: StateProtocol {
}
nonmutating set {
rawValue = newValue
StateManager.updateState(id: id)
content.update = true
StateManager.updateViews(force: forceUpdates)
}
}
Expand All @@ -35,42 +35,42 @@ public struct State<Value>: StateProtocol {
/// Get and set the value without updating the views.
public var rawValue: Value {
get {
guard let value = StateManager.getState(id: id) as? Value else {
guard let value = content.value as? Value else {
let initialValue = getInitialValue()
let storage = StateContent.Storage(value: initialValue)
if var model = initialValue as? Model {
model.model = .init(id: id, force: forceUpdates)
model.model = .init(storage: storage, force: forceUpdates)
model.setup()
StateManager.setState(id: id, value: model)
StateManager.addConstantID(id)
content.storage = storage
content.value = model
} else {
StateManager.setState(id: id, value: initialValue)
content.storage = storage
}
return initialValue
}
return value
}
nonmutating set {
StateManager.setState(id: id, value: newValue)
content.value = newValue
}
}

/// The state's identifier for the stored value.
var id: String

/// Whether to force update the views when the value changes.
var forceUpdates: Bool

/// The closure for initializing the state property's value.
var getInitialValue: () -> Value

/// The content.
let content: StateContent = .init()

/// Initialize a property representing a state in the view with an autoclosure.
/// - Parameters:
/// - wrappedValue: The wrapped value.
/// - id: An explicit identifier.
/// - forceUpdates: Whether to force update all available views when the property gets modified.
public init(wrappedValue: @autoclosure @escaping () -> Value, id: String? = nil, forceUpdates: Bool = false) {
public init(wrappedValue: @autoclosure @escaping () -> Value, forceUpdates: Bool = false) {
getInitialValue = wrappedValue
self.id = id ?? UUID().uuidString
self.forceUpdates = forceUpdates
}

Expand Down
36 changes: 20 additions & 16 deletions Sources/Model/Data Flow/StateContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,34 @@
class StateContent {

/// The storage.
var storage: Storage {
var storage: Storage?

/// The value.
var value: Any? {
get {
if let internalStorage {
return internalStorage
storage?.value
}
set {
if let storage {
storage.value = newValue as Any
} else {
storage = .init(value: newValue as Any)
}
let value = getInitialValue()
let storage = Storage(value: value)
internalStorage = storage
return storage
}
}

/// Whether to update the views.
var update: Bool {
get {
storage?.update ?? false
}
set {
internalStorage = newValue
storage?.update = newValue
}
}
/// The internal storage.
var internalStorage: Storage?
/// The initial value.
private var getInitialValue: () -> Any

/// Initialize the content without already initializing the storage or initializing the value.
/// - Parameter initialValue: The initial value.
init(getInitialValue: @escaping () -> Any) {
self.getInitialValue = getInitialValue
}
init() { }

/// A class storing the value.
class Storage {
Expand Down
94 changes: 0 additions & 94 deletions Sources/Model/Data Flow/StateManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,10 @@ public enum StateManager {

/// Whether to block updates in general.
public static var blockUpdates = false
/// Whether to save state.
public static var saveState = true
/// The application identifier.
static var appID: String?
/// The functions handling view updates.
static var updateHandlers: [(Bool) -> Void] = []
/// The state.
static var state: [State] = []

/// Information about a piece of state.
struct State {

/// The state's identifier.
var id: String
/// Old identifiers of the state which need to be saved.
var oldIDs: [String] = []
/// The state value.
var value: Any?
/// Whether to update in the next iteration.
var update = false

/// Whether the state's identifiers contain a certain identifier.
/// - Parameter id: The identifier.
/// - Returns: Whether the id is contained.
func contains(id: String) -> Bool {
id == self.id || oldIDs.contains(id)
}

/// Change the identifier to a new one.
/// - Parameter newID: The new identifier.
mutating func changeID(new newID: String) {
id = newID
}

}

/// Update all of the views.
/// - Parameter force: Whether to force all views to update.
Expand All @@ -57,9 +26,6 @@ public enum StateManager {
for handler in updateHandlers {
handler(force)
}
for state in state where state.update {
updatedState(id: state.id)
}
}
}

Expand All @@ -69,64 +35,4 @@ public enum StateManager {
updateHandlers.append(handler)
}

/// Set the state value for a certain ID.
/// - Parameters:
/// - id: The identifier.
/// - value: The new value.
static func setState(id: String, value: Any?) {
if saveState {
guard let index = state.firstIndex(where: { $0.contains(id: id) }) else {
state.append(.init(id: id, value: value))
return
}
state[safe: index]?.value = value
}
}

/// Get the state value for a certain ID.
/// - Parameter id: The identifier.
/// - Returns: The value.
static func getState(id: String) -> Any? {
state[safe: state.firstIndex { $0.contains(id: id) }]?.value
}

/// Mark the state of a certain id as updated.
/// - Parameter id: The identifier.
static func updateState(id: String) {
if saveState {
state[safe: state.firstIndex { $0.contains(id: id) }]?.update = true
}
}

/// Mark the state of a certain id as not updated.
/// - Parameter id: The identifier.
static func updatedState(id: String) {
if saveState {
state[safe: state.firstIndex { $0.contains(id: id) }]?.update = false
}
}

/// Get whether to update the state of a certain id.
/// - Parameter id: The identifier.
/// - Returns: Whether to update the state.
static func getUpdateState(id: String) -> Bool {
state[safe: state.firstIndex { $0.contains(id: id) }]?.update ?? false
}

/// Change the identifier for a certain state value.
/// - Parameters:
/// - oldID: The old identifier.
/// - newID: The new identifier.
static func changeID(old oldID: String, new newID: String) {
if saveState {
state[safe: state.firstIndex { $0.contains(id: oldID) }]?.changeID(new: newID)
}
}

/// Save a state's identifier until the program ends.
/// - Parameter id: The identifier.
static func addConstantID(_ id: String) {
state[safe: state.firstIndex { $0.id == id }]?.oldIDs.append(id)
}

}
4 changes: 2 additions & 2 deletions Sources/Model/Data Flow/StateProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
/// An interface for accessing `State` without specifying the generic type.
protocol StateProtocol {

/// The identifier for the state property's value.
var id: String { get set }
/// The state content.
var content: StateContent { get }

}
11 changes: 1 addition & 10 deletions Sources/Model/User Interface/App/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,7 @@ extension App {
appInstance.app = Storage(id: appInstance.id)
appInstance.app.storage.app = { appInstance }
StateManager.addUpdateHandler { force in
var updateProperties = force
for property in appInstance.getState() {
if let oldID = appInstance.app.storage.stateStorage[property.key]?.id {
StateManager.changeID(old: oldID, new: property.value.id)
appInstance.app.storage.stateStorage[property.key]?.id = property.value.id
}
if StateManager.getUpdateState(id: property.value.id) {
updateProperties = true
}
}
let updateProperties = force || appInstance.getState().contains { $0.value.content.update }
var removeIndices: [Int] = []
for (index, element) in appInstance.app.storage.sceneStorage.enumerated() {
if element.destroy {
Expand Down
8 changes: 4 additions & 4 deletions Sources/View/StateWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ struct StateWrapper: ConvenienceWidget {
) where Data: ViewRenderData {
var updateProperties = updateProperties
for property in state {
if let oldID = storage.state[property.key]?.id {
StateManager.changeID(old: oldID, new: property.value.id)
storage.state[property.key]?.id = property.value.id
if let storage = storage.state[property.key]?.content.storage {
property.value.content.storage = storage
}
if StateManager.getUpdateState(id: property.value.id) {
if property.value.content.update {
updateProperties = true
property.value.content.update = false
}
}
guard let storage = storage.content[.mainContent]?.first else {
Expand Down

0 comments on commit 54292ed

Please sign in to comment.