Skip to content

Commit

Permalink
Test for deallocation, clear all properties, remove view modifiers te…
Browse files Browse the repository at this point in the history
…st multi loading
  • Loading branch information
Supereg committed Jun 5, 2024
1 parent e198c62 commit 5f05cb5
Show file tree
Hide file tree
Showing 17 changed files with 243 additions and 96 deletions.
5 changes: 5 additions & 0 deletions Sources/Spezi/Capabilities/ApplicationPropertyWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ extension _ApplicationPropertyWrapper: SpeziPropertyWrapper {
self.shadowCopy = spezi[keyPath: keyPath]
}
}

func clear() {
spezi = nil
shadowCopy = nil
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ public class _CollectPropertyWrapper<Value> {
}


extension _CollectPropertyWrapper: StorageValueCollector {
public func retrieve<Repository: SharedRepository<SpeziAnchor>>(from repository: Repository) {
injectedValues = repository[CollectedModuleValues<Value>.self]?.map { $0.value } ?? []
}

func clear() {
injectedValues = nil
}
}


extension Module {
/// The `@Collect` property wrapper can be used to retrieve data communicated by other `Module`s.
///
Expand All @@ -63,10 +74,3 @@ extension Module {
/// ```
public typealias Collect = _CollectPropertyWrapper
}


extension _CollectPropertyWrapper: StorageValueCollector {
public func retrieve<Repository: SharedRepository<SpeziAnchor>>(from repository: Repository) {
injectedValues = repository[CollectedModuleValues<Value>.self]?.map { $0.value } ?? []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ extension _ProvidePropertyWrapper: StorageValueProvider {

collected = true
}

func clear() {
collected = false
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import SpeziFoundation
/// data provided by other ``Module``s.
///
/// Data requested through a Storage Value Collector might be provided through a ``_StorageValueProvider``.
protocol StorageValueCollector {
protocol StorageValueCollector: SpeziPropertyWrapper {
/// This method is called to retrieve all the requested values from the given ``SpeziStorage`` repository.
/// - Parameter repository: Provides access to the ``SpeziStorage`` repository for read access.
func retrieve<Repository: SharedRepository<SpeziAnchor>>(from repository: Repository)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import SpeziFoundation
/// data with other ``Module``s.
///
/// Data provided through a Storage Value Provider can be retrieved through a ``_StorageValueCollector``.
protocol StorageValueProvider {
protocol StorageValueProvider: SpeziPropertyWrapper {
/// This method is called to collect all provided values into the given ``SpeziStorage`` repository.
/// - Parameter repository: Provides access to the ``SpeziStorage`` repository.
func collect<Repository: SharedRepository<SpeziAnchor>>(into repository: inout Repository)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public class _ModelPropertyWrapper<Model: Observable & AnyObject> {
}


extension _ModelPropertyWrapper: SpeziPropertyWrapper {
func clear() {
collected = false
}
}


extension Module {
/// Places an observable object in the global view environment.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ public class _ModifierPropertyWrapper<Modifier: ViewModifier> {
}


extension _ModifierPropertyWrapper: SpeziPropertyWrapper {
func clear() {
collected = false
}
}


extension Module {
/// Provide a SwiftUI `ViewModifier` to modify the global view hierarchy.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protocol ViewModifierProvider {

/// Defines the placement order of this view modifier.
///
/// `ViewModifier`s retrieved from a ``Module` might modify the view hierarchy in a different order than they
/// `ViewModifier`s retrieved from a ``Module`` might modify the view hierarchy in a different order than they
/// are supplied. This is important to, e.g., ensure that modifiers injecting model types are placed at the outermost
/// level to ensure other view modifiers supplied by the module can access those model types.
var placement: ModifierPlacement { get }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public struct DependencyCollection: DependencyDeclaration {
}
}

func uninjectDependencies() {
for entry in entries {
entry.uninjectDependencies()
}
}

private func singleDependencyContext() -> AnyDependencyContext {
guard let dependency = entries.first else {
preconditionFailure("DependencyCollection unexpectedly empty!")
Expand Down
4 changes: 4 additions & 0 deletions Sources/Spezi/Dependencies/Property/DependencyContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class DependencyContext<Dependency: Module>: AnyDependencyContext {
injectedDependency = dependencyManager.retrieve(optional: isOptional)
}

func uninjectDependencies() {
injectedDependency = nil
}

func retrieve<M>(dependency: M.Type) -> M {
guard let injectedDependency else {
preconditionFailure(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ protocol DependencyDeclaration {
/// Inject the dependency instance from the ``DependencyManager``. Use `DependencyManager/retrieve(module:)`.
func inject(from dependencyManager: DependencyManager)

/// Remove all dependency injections.
func uninjectDependencies()

/// Determine the dependency relationship to a given module.
/// - Parameter module: The module to retrieve the dependency relationship for.
/// - Returns: Returns the `DependencyRelation`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ public class _DependencyPropertyWrapper<Value> { // swiftlint:disable:this type_
}


extension _DependencyPropertyWrapper: SpeziPropertyWrapper {
func clear() {
uninjectDependencies()
}
}


extension _DependencyPropertyWrapper: DependencyDeclaration {
var injectedDependencies: [any Module] {
dependencies.injectedDependencies
Expand All @@ -76,6 +83,10 @@ extension _DependencyPropertyWrapper: DependencyDeclaration {
func inject(from dependencyManager: DependencyManager) {
dependencies.inject(from: dependencyManager)
}

func uninjectDependencies() {
dependencies.uninjectDependencies()
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct LaunchOptionsKey: DefaultProvidingKnowledgeSource {
public typealias Value = [UIApplication.LaunchOptionsKey: Any]
#elseif os(macOS)
/// Currently not supported as ``SpeziAppDelegate/applicationWillFinishLaunching(_:)`` on macOS
/// is executed after the initialization of ``Spezi`` via `View/spezi(_:)` is done, breaking our initialization assumption in ``SpeziAppDelegate/applicationWillFinishLaunching(_:)``.
/// is executed after the initialization of ``Spezi/Spezi`` via `View/spezi(_:)` is done, breaking our initialization assumption in ``SpeziAppDelegate/applicationWillFinishLaunching(_:)``.
public typealias Value = [Never: Any]
#else // os(watchOS)
public typealias Value = [Never: Any]
Expand Down
Loading

0 comments on commit 5f05cb5

Please sign in to comment.