-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix compiler error in tutorial. (#2331)
* Fix compiler error in tutorial. * wip
- Loading branch information
Showing
10 changed files
with
144 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 45 additions & 40 deletions
85
...tTheComposableArchitecture/02-Navigation/02-MultipleDestinations/02-02-02-code-0014.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,53 @@ | ||
struct ContentView: View { | ||
let store: StoreOf<ContactsFeature> | ||
struct ContactsFeature: Reducer { | ||
struct State: Equatable { | ||
var contacts: IdentifiedArrayOf<Contact> = [] | ||
@PresentationState var destination: Destination.State? | ||
} | ||
enum Action: Equatable { | ||
case addButtonTapped | ||
case deleteButtonTapped(id: Contact.ID) | ||
case destination(PresentationAction<Destination.Action>) | ||
enum Alert: Equatable { | ||
case confirmDeletion(id: Contact.ID) | ||
} | ||
} | ||
var body: some ReducerOf<Self> { | ||
Reduce { state, action in | ||
switch action { | ||
case .addButtonTapped: | ||
state.destination = .addContact( | ||
AddContactFeature.State( | ||
contact: Contact(id: UUID(), name: "") | ||
) | ||
) | ||
return .none | ||
|
||
var body: some View { | ||
NavigationStack { | ||
WithViewStore(self.store, observe: \.contacts) { viewStore in | ||
List { | ||
ForEach(viewStore.state) { contact in | ||
HStack { | ||
Text(contact.name) | ||
Spacer() | ||
Button { | ||
viewStore.send(.deleteButtonTapped(id: contact.id)) | ||
} label: { | ||
Image(systemName: "trash") | ||
.foregroundColor(.red) | ||
} | ||
} | ||
} | ||
} | ||
.navigationTitle("Contacts") | ||
.toolbar { | ||
ToolbarItem { | ||
Button { | ||
viewStore.send(.addButtonTapped) | ||
} label: { | ||
Image(systemName: "plus") | ||
case let .destination(.presented(.addContact(.delegate(.saveContact(contact))))): | ||
state.contacts.append(contact) | ||
return .none | ||
|
||
case let .destination(.presented(.alert(.confirmDeletion(id: id)))): | ||
state.contacts.remove(id: id) | ||
return .none | ||
|
||
case .destination: | ||
return .none | ||
|
||
case let .deleteButtonTapped(id: id): | ||
state.destination = .alert( | ||
AlertState { | ||
TextState("Are you sure?") | ||
} actions: { | ||
ButtonState(role: .destructive, action: .confirmDeletion(id: id)) { | ||
TextState("Delete") | ||
} | ||
} | ||
} | ||
) | ||
return .none | ||
} | ||
} | ||
.sheet( | ||
store: self.store.scope(state: \.$destination, action: { .destination($0) }), | ||
state: , | ||
action: | ||
) { addContactStore in | ||
NavigationStack { | ||
AddContactView(store: addContactStore) | ||
} | ||
.ifLet(\.$destination, action: /Action.destination) { | ||
Destination() | ||
} | ||
.alert( | ||
store: self.store.scope(state: \.$destination, action: { .destination($0) }), | ||
state: , | ||
action: | ||
) | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...tTheComposableArchitecture/02-Navigation/02-MultipleDestinations/02-02-02-code-0018.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
struct ContentView: View { | ||
let store: StoreOf<ContactsFeature> | ||
|
||
var body: some View { | ||
NavigationStack { | ||
WithViewStore(self.store, observe: \.contacts) { viewStore in | ||
List { | ||
ForEach(viewStore.state) { contact in | ||
HStack { | ||
Text(contact.name) | ||
Spacer() | ||
Button { | ||
viewStore.send(.deleteButtonTapped(id: contact.id)) | ||
} label: { | ||
Image(systemName: "trash") | ||
.foregroundColor(.red) | ||
} | ||
} | ||
} | ||
} | ||
.navigationTitle("Contacts") | ||
.toolbar { | ||
ToolbarItem { | ||
Button { | ||
viewStore.send(.addButtonTapped) | ||
} label: { | ||
Image(systemName: "plus") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
.sheet( | ||
store: self.store.scope(state: \.$destination, action: { .destination($0) }), | ||
state: /ContactsFeature.Destination.State.addContact, | ||
action: ContactsFeature.Destination.Action.addContact | ||
) { addContactStore in | ||
NavigationStack { | ||
AddContactView(store: addContactStore) | ||
} | ||
} | ||
.alert( | ||
store: self.store.scope(state: \.$destination, action: { .destination($0) }), | ||
state: /ContactsFeature.Destination.State.alert, | ||
action: ContactsFeature.Destination.Action.alert | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters