Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leif/dependency improvements #48

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion Sources/AppState/Application/Application+public.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public extension Application {
- Returns: The requested dependency of type `Dependency<Value>`.
*/
func dependency<Value>(
_ object: @autoclosure () -> Value,
_ object: () -> Value,
feature: String = "App",
id: String
) -> Dependency<Value> {
Expand All @@ -203,6 +203,42 @@ public extension Application {
return dependency
}

/// Overloaded version of `dependency(_:feature:id:)` function where id is generated from the code context.
func dependency<Value>(
setup: () -> Value,
_ fileID: StaticString = #fileID,
_ function: StaticString = #function,
_ line: Int = #line,
_ column: Int = #column
) -> Dependency<Value> {
dependency(
setup,
id: Application.codeID(
fileID: fileID,
function: function,
line: line,
column: column
)
)
}

/**
Retrieves a dependency for the provided `id`. If dependency is not present, it is created once using the provided closure.

- Parameters:
- object: The closure returning the dependency.
- feature: The name of the feature to which the dependency belongs, default is "App".
- id: The specific identifier for this dependency.
- Returns: The requested dependency of type `Dependency<Value>`.
*/
func dependency<Value>(
_ object: @autoclosure () -> Value,
feature: String = "App",
id: String
) -> Dependency<Value> {
dependency(object, feature: feature, id: id)
}


/// Overloaded version of `dependency(_:feature:id:)` function where id is generated from the code context.
func dependency<Value>(
Expand Down
9 changes: 0 additions & 9 deletions Sources/AppState/Application/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,6 @@ open class Application: NSObject, ObservableObject {
loadDefaultDependencies()

consume(object: cache)

if #available(iOS 15.0, watchOS 9.0, macOS 11.0, tvOS 15.0, *) {
NotificationCenter.default.addObserver(
self,
selector: #selector(didChangeExternally),
name: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
object: NSUbiquitousKeyValueStore.default
)
}
}

@objc @available(iOS 15.0, watchOS 9.0, macOS 11.0, tvOS 15.0, *)
Expand Down
11 changes: 10 additions & 1 deletion Sources/AppState/Application/Types/Application+SyncState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ import Foundation
extension Application {
/// The default `NSUbiquitousKeyValueStore` instance.
public var icloudStore: Dependency<NSUbiquitousKeyValueStore> {
dependency(NSUbiquitousKeyValueStore.default)
dependency {
NotificationCenter.default.addObserver(
self,
selector: #selector(didChangeExternally),
name: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
object: NSUbiquitousKeyValueStore.default
)

return NSUbiquitousKeyValueStore.default
}
}

/**
Expand Down