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

Fix focus in tutorial #3072

Merged
merged 8 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -54,6 +54,9 @@
@Code(name: "Models.swift", file: ListsOfSyncUps-01-code-0003.swift)
}

<!--
NB: Removing the Tagged discussion for now because it was too confusing for readers.

Comment on lines +57 to +59
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's just drop the tagged digression. it's causing too much confusion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It helped me to re-discover tagged again. I agree - it can be confusing for junior devs, but it's a pity.

There's another improvement that can be made to our domain models, but this one is completely
optional.

Expand Down Expand Up @@ -87,6 +90,7 @@

@Code(name: "Models.swift", file: ListsOfSyncUps-01-code-0004.swift)
}
-->
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ struct SyncUpForm {
case .binding:
return .none

case let .onDeleteAttendees(indexSet):
state.syncUp.attendees.remove(atOffsets: indexSet)
case let .onDeleteAttendees(indices):
state.syncUp.attendees.remove(atOffsets: indices)
Comment on lines -24 to +25
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

settling on indices over indexSet.

if state.syncUp.attendees.isEmpty {
state.syncUp.attendees.append(
Attendee(id: Attendee.ID())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ struct SyncUpForm {
case .binding:
return .none

case let .onDeleteAttendees(indexSet):
state.syncUp.attendees.remove(atOffsets: indexSet)
case let .onDeleteAttendees(indices):
state.syncUp.attendees.remove(atOffsets: indices)
if state.syncUp.attendees.isEmpty {
state.syncUp.attendees.append(
Attendee(id: Attendee.ID())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,18 @@ struct SyncUpFormView: View {
.focused($focus, equals: .attendee(attendee.id))
}
.onDelete { indices in
guard let firstDeletedIndex = indices.first
else { return }
let firstDeletedAttendee = store.syncUp.attendees[firstDeletedIndex]
store.send(.onDeleteAttendees(indices))
guard focus == .attendee(firstDeletedAttendee.id)
guard
!store.syncUp.attendees.isEmpty,
let firstIndex = indices.first
else { return }
let index = min(firstDeletedIndex, store.syncUp.attendees.count - 1)
let index = min(firstIndex, store.syncUp.attendees.count - 1)
focus = .attendee(store.syncUp.attendees[index].id)
}

Button("New attendee") {
store.send(.addAttendeeButtonTapped)
focus = .attendee(store.attendees.last!.id)
focus = .attendee(store.syncUp.attendees.last!.id)
}
} header: {
Text("Attendees")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,18 @@ struct SyncUpFormView: View {
.focused($focus, equals: .attendee(attendee.id))
}
.onDelete { indices in
guard let firstDeletedIndex = indices.first
else { return }
let firstDeletedAttendee = store.syncUp.attendees[firstDeletedIndex]
store.send(.onDeleteAttendees(indices))
guard focus == .attendee(firstDeletedAttendee.id)
guard
!store.syncUp.attendees.isEmpty,
let firstIndex = indices.first
else { return }
let index = min(firstDeletedIndex, store.syncUp.attendees.count - 1)
let index = min(firstIndex, store.syncUp.attendees.count - 1)
focus = .attendee(store.syncUp.attendees[index].id)
}

Button("New attendee") {
store.send(.addAttendeeButtonTapped)
focus = .attendee(store.attendees.last!.id)
focus = .attendee(store.syncUp.attendees.last!.id)
}
} header: {
Text("Attendees")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ struct SyncUpForm {
case .binding:
return .none

case let .onDeleteAttendees(indexSet):
state.syncUp.attendees.remove(atOffsets: indexSet)
case let .onDeleteAttendees(indices):
state.syncUp.attendees.remove(atOffsets: indices)
if state.syncUp.attendees.isEmpty {
state.syncUp.attendees.append(
Attendee(id: Attendee.ID())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ struct SyncUpForm {
case .binding:
return .none

case let .onDeleteAttendees(indexSet):
state.syncUp.attendees.remove(atOffsets: indexSet)
case let .onDeleteAttendees(indices):
state.syncUp.attendees.remove(atOffsets: indices)
if state.syncUp.attendees.isEmpty {
state.syncUp.attendees.append(
Attendee(id: Attendee.ID())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ struct SyncUpForm {
case .binding:
return .none

case let .onDeleteAttendees(indexSet):
state.syncUp.attendees.remove(atOffsets: indexSet)
case let .onDeleteAttendees(indices):
state.syncUp.attendees.remove(atOffsets: indices)
if state.syncUp.attendees.isEmpty {
state.syncUp.attendees.append(
Attendee(id: Attendee.ID())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,14 @@ struct SyncUpForm {
case .binding:
return .none

case let .onDeleteAttendees(indexSet):
guard let firstDeletedIndex = indexSet.first
case let .onDeleteAttendees(indices):
mbrandonw marked this conversation as resolved.
Show resolved Hide resolved
state.syncUp.attendees.remove(atOffsets: indices)
guard
!state.syncUp.attendees.isEmpty,
let firstIndex = indices.first
else { return .none }
let firstDeletedAttendee = state.syncUp.attendees[firstDeletedIndex]

state.syncUp.attendees.remove(atOffsets: indexSet)
if state.syncUp.attendees.isEmpty {
state.syncUp.attendees.append(
Attendee(id: Attendee.ID())
)
}

guard state.focus == .attendee(firstDeletedAttendee.id)
else { return .none }
let index = min(firstDeletedIndex, state.syncUp.attendees.count - 1)
let index = min(firstIndex, state.syncUp.attendees.count - 1)
state.focus = .attendee(state.syncUp.attendees[index].id)

return .none
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,18 @@ struct SyncUpFormView: View {
.focused($focus, equals: .attendee(attendee.id))
}
.onDelete { indices in
guard let firstDeletedIndex = indices.first
else { return }
let firstDeletedAttendee = store.syncUp.attendees[firstDeletedIndex]
store.send(.onDeleteAttendees(indices))
guard focus == .attendee(firstDeletedAttendee.id)
guard
!store.syncUp.attendees.isEmpty,
let firstIndex = indices.first
else { return }
let index = min(firstDeletedIndex, store.syncUp.attendees.count - 1)
let index = min(firstIndex, store.syncUp.attendees.count - 1)
focus = .attendee(store.syncUp.attendees[index].id)
}

Button("New attendee") {
store.send(.addAttendeeButtonTapped)
focus = .attendee(store.attendees.last!.id)
focus = .attendee(store.syncUp.attendees.last!.id)
}
} header: {
Text("Attendees")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ struct SyncUpFormView: View {
.focused($focus, equals: .attendee(attendee.id))
}
.onDelete { indices in
// guard let firstDeletedIndex = indices.first
// else { return }
// let firstDeletedAttendee = store.syncUp.attendees[firstDeletedIndex]
store.send(.onDeleteAttendees(indices))
// guard focus == .attendee(firstDeletedAttendee.id)
// else { return }
// let index = min(firstDeletedIndex, store.syncUp.attendees.count - 1)
// store.send(.onDeleteAttendees(indices))
// guard
// !store.syncUp.attendees.isEmpty,
// let firstIndex = indices.first
// else { return .none }
// let index = min(firstIndex, store.syncUp.attendees.count - 1)
// focus = .attendee(store.syncUp.attendees[index].id)
}

Button("New attendee") {
store.send(.addAttendeeButtonTapped)
// focus = .attendee(store.attendees.last!.id)
// focus = .attendee(store.syncUp.attendees.last!.id)
}
} header: {
Text("Attendees")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,14 @@ struct SyncUpForm {
case .binding:
return .none

case let .onDeleteAttendees(indexSet):
guard let firstDeletedIndex = indexSet.first
case let .onDeleteAttendees(indices):
state.syncUp.attendees.remove(atOffsets: indices)
guard
!state.syncUp.attendees.isEmpty,
let firstIndex = indices.first
else { return .none }
let firstDeletedAttendee = state.syncUp.attendees[firstDeletedIndex]

state.syncUp.attendees.remove(atOffsets: indexSet)
if state.syncUp.attendees.isEmpty {
state.syncUp.attendees.append(
Attendee(id: Attendee.ID())
)
}

guard state.focus == .attendee(firstDeletedAttendee.id)
else { return .none }
let index = min(firstDeletedIndex, state.syncUp.attendees.count - 1)
let index = min(firstIndex, state.syncUp.attendees.count - 1)
state.focus = .attendee(state.syncUp.attendees[index].id)

return .none
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,14 @@ struct SyncUpForm {
case .binding:
return .none

case let .onDeleteAttendees(indexSet):
guard let firstDeletedIndex = indexSet.first
case let .onDeleteAttendees(indices):
state.syncUp.attendees.remove(atOffsets: indices)
guard
!state.syncUp.attendees.isEmpty,
let firstIndex = indices.first
else { return .none }
let firstDeletedAttendee = state.syncUp.attendees[firstDeletedIndex]

state.syncUp.attendees.remove(atOffsets: indexSet)
if state.syncUp.attendees.isEmpty {
state.syncUp.attendees.append(
Attendee(id: Attendee.ID())
)
}

guard state.focus == .attendee(firstDeletedAttendee.id)
else { return .none }
let index = min(firstDeletedIndex, state.syncUp.attendees.count - 1)
let index = min(firstIndex, state.syncUp.attendees.count - 1)
state.focus = .attendee(state.syncUp.attendees[index].id)

return .none
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,14 @@ struct SyncUpForm {
case .binding:
return .none

case let .onDeleteAttendees(indexSet):
guard let firstDeletedIndex = indexSet.first
case let .onDeleteAttendees(indices):
state.syncUp.attendees.remove(atOffsets: indices)
guard
!state.syncUp.attendees.isEmpty,
let firstIndex = indices.first
else { return .none }
let firstDeletedAttendee = state.syncUp.attendees[firstDeletedIndex]

state.syncUp.attendees.remove(atOffsets: indexSet)
if state.syncUp.attendees.isEmpty {
state.syncUp.attendees.append(
Attendee(id: uuid())
)
}

guard state.focus == .attendee(firstDeletedAttendee.id)
else { return .none }
let index = min(firstDeletedIndex, state.syncUp.attendees.count - 1)
let index = min(firstIndex, state.syncUp.attendees.count - 1)
state.focus = .attendee(state.syncUp.attendees[index].id)

return .none
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ struct SyncUpDetail {
Reduce { state, action in
switch action {
case .alert(.presented(.confirmButtonTapped)):
return .run { send in
await send(.delegate(.deleteSyncUp(id: state.syncUp.id)))
await dismiss()
}
@Shared(.fileStorage(.syncUps)) var syncUps: IdentifiedArrayOf<SyncUp> = []
syncUps.remove(id: state.syncUp.id)
return .run { _ in await dismiss() }
Comment on lines -42 to +44
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was brought up in an email, but we had some old delegate code in the tutorial still.


case .alert(.dismiss):
return .none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ struct SyncUpDetail {
Reduce { state, action in
switch action {
case .alert(.presented(.confirmButtonTapped)):
return .run { send in
await send(.delegate(.deleteSyncUp(id: state.syncUp.id)))
await dismiss()
}
@Shared(.fileStorage(.syncUps)) var syncUps: IdentifiedArrayOf<SyncUp> = []
syncUps.remove(id: state.syncUp.id)
return .run { _ in await dismiss() }

case .alert(.dismiss):
return .none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ struct SyncUpDetail {
Reduce { state, action in
switch action {
case .alert(.presented(.confirmButtonTapped)):
return .run { send in
await send(.delegate(.deleteSyncUp(id: state.syncUp.id)))
await dismiss()
}
@Shared(.fileStorage(.syncUps)) var syncUps: IdentifiedArrayOf<SyncUp> = []
syncUps.remove(id: state.syncUp.id)
return .run { _ in await dismiss() }

case .alert(.dismiss):
return .none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ struct SyncUpDetail {
case destination(PresentationAction<Destination.Action>)
case doneEditingButtonTapped
case editButtonTapped
@CasePathable
enum Alert {
Comment on lines -28 to -29
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was also brought up in an email. we have some extra Alert actions hanging around that should have been deleted after introducing the Destination reducer.

case confirmButtonTapped
}
}

@Dependency(\.dismiss) var dismiss
Expand All @@ -37,10 +33,9 @@ struct SyncUpDetail {
Reduce { state, action in
switch action {
case .alert(.presented(.confirmButtonTapped)):
return .run { send in
await send(.delegate(.deleteSyncUp(id: state.syncUp.id)))
await dismiss()
}
@Shared(.fileStorage(.syncUps)) var syncUps: IdentifiedArrayOf<SyncUp> = []
syncUps.remove(id: state.syncUp.id)
return .run { _ in await dismiss() }

case .alert(.dismiss):
return .none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ struct SyncUpDetail {
case destination(PresentationAction<Destination.Action>)
case doneEditingButtonTapped
case editButtonTapped
@CasePathable
enum Alert {
case confirmButtonTapped
}
}

@Dependency(\.dismiss) var dismiss
Expand All @@ -38,10 +34,9 @@ struct SyncUpDetail {
switch action {
// case .alert(.presented(.confirmButtonTapped)):
case .destination(.presented(.alert(.confirmButtonTapped))):
return .run { send in
await send(.delegate(.deleteSyncUp(id: state.syncUp.id)))
await dismiss()
}
@Shared(.fileStorage(.syncUps)) var syncUps: IdentifiedArrayOf<SyncUp> = []
syncUps.remove(id: state.syncUp.id)
return .run { _ in await dismiss() }

case .alert(.dismiss):
return .none
Expand Down
Loading