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
3 changes: 3 additions & 0 deletions Modules/AppFeature/Sources/AppFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public enum AppFeature {
return .none

case .setSettingsPresented(let isPresented):
guard isPresented != state.isSettingsPresented else {
return .none
}
state.isSettingsPresented = isPresented
if isPresented && state.settings == nil {
state.settings = .init()
Expand Down
31 changes: 28 additions & 3 deletions Modules/AppFeature/Sources/AppView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public struct AppView: View {
public typealias ViewStore = Store<AppFeature.State, AppFeature.Action>

@StateObject private var store: ViewStore
@State private var path: [AppFeature.State.Destination] = []
@State private var isSettingsPresented: Bool = false

@Environment(\.theme) private var theme

Expand All @@ -25,23 +27,46 @@ public struct AppView: View {
}

public var body: some View {
NavigationStack(path: store.binding(\.path, send: { .setPath($0) })) {
NavigationStack(path: $path) {
HomeView {
store.projection(state: \.home, action: { .home($0) })
}
.navigationDestination(for: AppFeature.State.Destination.self) {
viewForDestination($0)
}
}
.sheet(isPresented: store.binding(\.isSettingsPresented, send: { .setSettingsPresented($0) })) {
.sheet(isPresented: $isSettingsPresented) {
SettingsView {
store.projection(state: \.settings, action: { .settings($0) })
}
}
.accentColor(theme.colorAccent)
// Fix Error `Update NavigationAuthority bound path tried to update multiple times per frame`
.onReceive(store.$state) { newValue in
if newValue.path != path {
path = newValue.path
}
if newValue.isSettingsPresented != isSettingsPresented {
isSettingsPresented = newValue.isSettingsPresented
}
}
.onChange(of: path) { _, newValue in
if newValue != store.state.path {
Task {
await store.dispatch(.setPath(newValue))
}
}
}
.onChange(of: isSettingsPresented) { _, newValue in
if newValue != store.state.isSettingsPresented {
Task {
await store.dispatch(.setSettingsPresented(newValue))
}
}
}
.onReceive(store.dispatchedAction) { event in
print(event)
}
.accentColor(theme.colorAccent)
}

@ViewBuilder private func viewForDestination(_ destination: AppFeature.State.Destination) -> some View {
Expand Down
16 changes: 15 additions & 1 deletion Modules/SettingsFeature/Sources/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public struct SettingsView: View {
public typealias ViewStore = Store<SettingsFeature.State, SettingsFeature.Action>

@StateObject private var store: ViewStore
@State private var path: [SettingsFeature.State.Destination] = []

public init(store: @escaping () -> ViewStore) {
// SwiftUI ensures that the following initialization uses the
Expand All @@ -19,7 +20,7 @@ public struct SettingsView: View {
}

public var body: some View {
NavigationStack(path: store.binding(\.path, send: { .setPath($0) })) {
NavigationStack(path: $path) {
SettingsHomeView {
store.projection(state: \.home, action: { .home($0) })
}
Expand All @@ -31,6 +32,19 @@ public struct SettingsView: View {
}
}
.presentationDetents([.large])
// Fix Error `Update NavigationAuthority bound path tried to update multiple times per frame`
.onReceive(store.$state) { newValue in
if newValue.path != path {
path = newValue.path
}
}
.onChange(of: path) { _, newValue in
if newValue != store.state.path {
Task {
await store.dispatch(.setPath(newValue))
}
}
}
}

@ViewBuilder private func viewForDestination(_ destination: SettingsFeature.State.Destination) -> some View {
Expand Down