Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ public struct ExampleCounter: View {
}
}

struct CounterScreen_Preview: PreviewProvider {
static var previews: some View {
AtomRoot {
CounterScreen()
}
#Preview {
AtomRoot {
CounterScreen()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ struct TodoListScreen: View {
}
}

struct TodoListScreen_Preview: PreviewProvider {
static var previews: some View {
AtomRoot {
TodoListScreen()
}
#Preview {
AtomRoot {
TodoListScreen()
}
}
8 changes: 3 additions & 5 deletions Examples/Packages/iOS/Sources/ExampleMap/Screens.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ struct MapScreen: View {
}
}

struct ExampleScreen_Preview: PreviewProvider {
static var previews: some View {
AtomRoot {
MapScreen()
}
#Preview {
AtomRoot {
MapScreen()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ struct DetailScreen: View {
}
}

struct DetailScreen_Preview: PreviewProvider {
static let movie = Movie(
#Preview {
let movie = Movie(
id: 680,
title: "Pulp Fiction",
overview: """
Expand All @@ -118,9 +118,7 @@ struct DetailScreen_Preview: PreviewProvider {
releaseDate: Date(timeIntervalSinceReferenceDate: -199184400.0)
)

static var previews: some View {
AtomRoot {
DetailScreen(movie: movie)
}
AtomRoot {
DetailScreen(movie: movie)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,10 @@ struct MoviesScreen: View {
}
}

struct MoviesScreen_Preview: PreviewProvider {
static var previews: some View {
AtomRoot {
NavigationStack {
MoviesScreen()
}
#Preview {
AtomRoot {
NavigationStack {
MoviesScreen()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ struct SearchScreen: View {
}
}

struct SearchScreen_Preview: PreviewProvider {
static var previews: some View {
AtomRoot {
NavigationStack {
SearchScreen()
}
}
.override(SearchQueryAtom()) { _ in
"Léon"
#Preview {
AtomRoot {
NavigationStack {
SearchScreen()
}
}
.override(SearchQueryAtom()) { _ in
"Léon"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,10 @@ public struct ExampleTimeTravel: View {
}
}

struct TimeTravelScreen_Preview: PreviewProvider {
static var previews: some View {
AtomRoot {
TimeTravelDebug {
NumberInputScreen()
}
#Preview {
AtomRoot {
TimeTravelDebug {
NumberInputScreen()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,17 @@ private struct ElapsedTimeDisplay: View {
}
}

struct VoiceMemoListScreen_Preview: PreviewProvider {
static var previews: some View {
AtomRoot {
VoiceMemoListScreen()
}
.override(AudioSessionAtom()) { _ in
MockAudioSession()
}
.override(AudioRecorderAtom()) { _ in
MockAudioRecorder()
}
.override(AudioPlayerAtom.self) { _ in
MockAudioPlayer()
}
#Preview {
AtomRoot {
VoiceMemoListScreen()
}
.override(AudioSessionAtom()) { _ in
MockAudioSession()
}
.override(AudioRecorderAtom()) { _ in
MockAudioRecorder()
}
.override(AudioPlayerAtom.self) { _ in
MockAudioPlayer()
}
}
26 changes: 13 additions & 13 deletions Sources/Atoms/AtomRoot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ import SwiftUI
///
public struct AtomRoot<Content: View>: View {
private var storage: Storage
private var overrides = [OverrideKey: any OverrideProtocol]()
private var observers = [Observer]()
private var overrideContainer = OverrideContainer()
private let content: Content

/// Creates an atom root with the specified content that will be allowed to use atoms.
Expand Down Expand Up @@ -93,16 +93,16 @@ public struct AtomRoot<Content: View>: View {
switch storage {
case .managed:
Managed(
overrides: overrides,
observers: observers,
overrideContainer: overrideContainer,
content: content
)

case .unmanaged(let store):
Scope(
store: store,
overrides: overrides,
observers: observers,
overrideContainer: overrideContainer,
content: content
)
}
Expand All @@ -114,7 +114,7 @@ public struct AtomRoot<Content: View>: View {
/// - Parameter onUpdate: A closure to handle a snapshot of recent updates.
///
/// - Returns: The self instance.
public func observe(_ onUpdate: @escaping @MainActor @Sendable (Snapshot) -> Void) -> Self {
public func observe(_ onUpdate: @MainActor @escaping (Snapshot) -> Void) -> Self {
mutating(self) { $0.observers.append(Observer(onUpdate: onUpdate)) }
}

Expand All @@ -128,8 +128,8 @@ public struct AtomRoot<Content: View>: View {
/// - value: A value to be used instead of the atom's value.
///
/// - Returns: The self instance.
public func override<Node: Atom>(_ atom: Node, with value: @escaping @MainActor @Sendable (Node) -> Node.Produced) -> Self {
mutating(self) { $0.overrides[OverrideKey(atom)] = Override(getValue: value) }
public func override<Node: Atom>(_ atom: Node, with value: @MainActor @escaping (Node) -> Node.Produced) -> Self {
mutating(self) { $0.overrideContainer.addOverride(for: atom, with: value) }
}

/// Overrides the atoms with the given value.
Expand All @@ -144,8 +144,8 @@ public struct AtomRoot<Content: View>: View {
/// - value: A value to be used instead of the atom's value.
///
/// - Returns: The self instance.
public func override<Node: Atom>(_ atomType: Node.Type, with value: @escaping @MainActor @Sendable (Node) -> Node.Produced) -> Self {
mutating(self) { $0.overrides[OverrideKey(atomType)] = Override(getValue: value) }
public func override<Node: Atom>(_ atomType: Node.Type, with value: @MainActor @escaping (Node) -> Node.Produced) -> Self {
mutating(self) { $0.overrideContainer.addOverride(for: atomType, with: value) }
}
}

Expand All @@ -156,8 +156,8 @@ private extension AtomRoot {
}

struct Managed: View {
let overrides: [OverrideKey: any OverrideProtocol]
let observers: [Observer]
let overrideContainer: OverrideContainer
let content: Content

@State
Expand All @@ -168,17 +168,17 @@ private extension AtomRoot {
var body: some View {
Scope(
store: store,
overrides: overrides,
observers: observers,
overrideContainer: overrideContainer,
content: content
)
}
}

struct Scope: View {
let store: AtomStore
let overrides: [OverrideKey: any OverrideProtocol]
let observers: [Observer]
let overrideContainer: OverrideContainer
let content: Content

@State
Expand All @@ -189,8 +189,8 @@ private extension AtomRoot {
let store = StoreContext.registerRoot(
in: store,
scopeKey: scopeKey,
overrides: overrides,
observers: observers
observers: observers,
overrideContainer: overrideContainer
)

state.unregister = {
Expand Down
20 changes: 10 additions & 10 deletions Sources/Atoms/AtomScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ import SwiftUI
///
public struct AtomScope<Content: View>: View {
private let inheritance: Inheritance
private var overrides = [OverrideKey: any OverrideProtocol]()
private var observers = [Observer]()
private var overrideContainer = OverrideContainer()
private let content: Content

/// Creates a new scope with the specified content.
Expand Down Expand Up @@ -85,8 +85,8 @@ public struct AtomScope<Content: View>: View {
case .environment(let id):
WithEnvironment(
id: id,
overrides: overrides,
observers: observers,
overrideContainer: overrideContainer,
content: content
)

Expand All @@ -109,7 +109,7 @@ public struct AtomScope<Content: View>: View {
/// - Parameter onUpdate: A closure to handle a snapshot of recent updates.
///
/// - Returns: The self instance.
public func scopedObserve(_ onUpdate: @escaping @MainActor @Sendable (Snapshot) -> Void) -> Self {
public func scopedObserve(_ onUpdate: @MainActor @escaping (Snapshot) -> Void) -> Self {
mutating(self) { $0.observers.append(Observer(onUpdate: onUpdate)) }
}

Expand All @@ -127,8 +127,8 @@ public struct AtomScope<Content: View>: View {
/// - value: A value to be used instead of the atom's value.
///
/// - Returns: The self instance.
public func scopedOverride<Node: Atom>(_ atom: Node, with value: @escaping @MainActor @Sendable (Node) -> Node.Produced) -> Self {
mutating(self) { $0.overrides[OverrideKey(atom)] = Override(getValue: value) }
public func scopedOverride<Node: Atom>(_ atom: Node, with value: @MainActor @escaping (Node) -> Node.Produced) -> Self {
mutating(self) { $0.overrideContainer.addOverride(for: atom, with: value) }
}

/// Override the atoms used in this scope with the given value.
Expand All @@ -147,8 +147,8 @@ public struct AtomScope<Content: View>: View {
/// - value: A value to be used instead of the atom's value.
///
/// - Returns: The self instance.
public func scopedOverride<Node: Atom>(_ atomType: Node.Type, with value: @escaping @MainActor @Sendable (Node) -> Node.Produced) -> Self {
mutating(self) { $0.overrides[OverrideKey(atomType)] = Override(getValue: value) }
public func scopedOverride<Node: Atom>(_ atomType: Node.Type, with value: @MainActor @escaping (Node) -> Node.Produced) -> Self {
mutating(self) { $0.overrideContainer.addOverride(for: atomType, with: value) }
}
}

Expand All @@ -160,8 +160,8 @@ private extension AtomScope {

struct WithEnvironment: View {
let id: ScopeID
let overrides: [OverrideKey: any OverrideProtocol]
let observers: [Observer]
let overrideContainer: OverrideContainer
let content: Content

@State
Expand All @@ -174,8 +174,8 @@ private extension AtomScope {
let store = environmentStore?.registerScope(
scopeID: id,
scopeKey: scopeKey,
overrides: overrides,
observers: observers
observers: observers,
overrideContainer: overrideContainer
)

state.unregister = {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Atoms/Context/AtomContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public protocol AtomContext {
///
/// - Parameter atom: An atom to reset.
@_disfavoredOverload
func reset<Node: Atom>(_ atom: Node)
func reset(_ atom: some Atom)

/// Calls arbitrary reset function of the given atom.
///
Expand All @@ -136,7 +136,7 @@ public protocol AtomContext {
/// ```
///
/// - Parameter atom: An atom to reset.
func reset<Node: Resettable>(_ atom: Node)
func reset(_ atom: some Resettable)
}

public extension AtomContext {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Atoms/Context/AtomCurrentContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public struct AtomCurrentContext: AtomContext {
/// - Parameter atom: An atom to reset.
@inlinable
@_disfavoredOverload
public func reset<Node: Atom>(_ atom: Node) {
public func reset(_ atom: some Atom) {
_store.reset(atom)
}

Expand All @@ -156,7 +156,7 @@ public struct AtomCurrentContext: AtomContext {
///
/// - Parameter atom: An atom to reset.
@inlinable
public func reset<Node: Resettable>(_ atom: Node) {
public func reset(_ atom: some Resettable) {
_store.reset(atom)
}
}
Loading