Skip to content

Commit

Permalink
Leif/improvements (#29)
Browse files Browse the repository at this point in the history
* Add more logging and values to logs

* Add log to StoredState set

* Seperate guard conditions

* Add function for storedState nil initial value
  • Loading branch information
0xLeif authored Nov 26, 2023
1 parent bcc7856 commit b492ae1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
35 changes: 29 additions & 6 deletions Sources/AppState/Application/Application+public.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public extension Application {
let dependency = shared.value(keyPath: keyPath)

log(
debug: "🟢 Starting Dependency Override \(String(describing: keyPath))",
debug: "🟢 Starting Dependency Override \(String(describing: keyPath)) with \(value)",
fileID: fileID,
function: function,
line: line,
Expand All @@ -99,7 +99,7 @@ public extension Application {

return DependencyOverride {
log(
debug: "🟢 Cancelling Dependency Override \(String(describing: keyPath))",
debug: "🟢 Cancelling Dependency Override \(String(describing: keyPath)) ",
fileID: fileID,
function: function,
line: line,
Expand Down Expand Up @@ -146,15 +146,17 @@ public extension Application {
_ line: Int = #line,
_ column: Int = #column
) -> State<Value> {
let appState = shared.value(keyPath: keyPath)

log(
debug: "🔵 Getting State \(String(describing: keyPath))",
debug: "🔵 Getting State \(String(describing: keyPath)) -> \(appState.value)",
fileID: fileID,
function: function,
line: line,
column: column
)

return shared.value(keyPath: keyPath)
return appState
}

/**
Expand All @@ -170,15 +172,17 @@ public extension Application {
_ line: Int = #line,
_ column: Int = #column
) -> StoredState<Value> {
let storedState = shared.value(keyPath: keyPath)

log(
debug: "🟣 Getting StoredState \(String(describing: keyPath))",
debug: "🟣 Getting StoredState \(String(describing: keyPath)) -> \(storedState.value)",
fileID: fileID,
function: function,
line: line,
column: column
)

return shared.value(keyPath: keyPath)
return storedState
}

// MARK: - Instance Methods
Expand Down Expand Up @@ -298,4 +302,23 @@ public extension Application {
scope: Scope(name: feature, id: id)
)
}

/**
Retrieves a `UserDefaults` backed state for the provided `id` with a default value of `nil`.

- Parameters:
- feature: The name of the feature to which the state belongs, default is "App".
- id: The specific identifier for this state.
- Returns: The state of type `Value`.
*/
func storedState<Value>(
feature: String = "App",
id: String
) -> StoredState<Value?> {
storedState(
initial: nil,
feature: feature,
id: id
)
}
}
7 changes: 3 additions & 4 deletions Sources/AppState/Application/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ public class Application: ObservableObject {
line: Int,
column: Int
) {
guard isLoggingEnabled else { return }

let excludedFileIDs: [String] = [
"AppState/Application+StoredState.swift"
]
let isFileIDValue: Bool = excludedFileIDs.contains(fileID.description) == false

guard
isLoggingEnabled,
isFileIDValue
else { return }
guard isFileIDValue else { return }

let codeID = codeID(fileID: fileID, function: function, line: line, column: column)

Expand Down
8 changes: 8 additions & 0 deletions Sources/AppState/PropertyWrappers/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ import SwiftUI
).value
}
nonmutating set {
Application.log(
debug: "🔵 Setting State \(String(describing: keyPath)) = \(newValue)",
fileID: fileID,
function: function,
line: line,
column: column
)

var state = app.value(keyPath: keyPath)
state.value = newValue
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/AppState/PropertyWrappers/StoredState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ import SwiftUI
).value
}
nonmutating set {
Application.log(
debug: "🟣 Setting StoredState \(String(describing: keyPath)) = \(newValue)",
fileID: fileID,
function: function,
line: line,
column: column
)

var state = app.value(keyPath: keyPath)
state.value = newValue
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/AppStateTests/StoredStateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import XCTest

fileprivate extension Application {
var storedValue: StoredState<Int?> {
storedState(initial: nil, id: "storedValue")
storedState(id: "storedValue")
}
}

Expand Down

0 comments on commit b492ae1

Please sign in to comment.