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 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 @@ -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 @@ -88,12 +91,12 @@

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

> Important: To keep things simple for the rest of this tutorial we will _not_ assume that
you have implemented the `Tagged` type into your domain. If you choose to use `Tagged` then
you will need to do some extra work to construct tagged values that is not covered in
the tutorial. For example, once we get to using dependencies to control UUIDs, you will need
to write `Attendee.ID(uuid())` instead of just `uuid()`.
-->
}
}

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 @@ -4,7 +4,7 @@ import SwiftUI
@Reducer
struct SyncUpDetail {
@ObservableState
struct State {
struct State: Equatable {
Copy link
Member Author

Choose a reason for hiding this comment

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

at the beginning of the tutorial we already set the state that we will be marking all States as equatable for testing, and so we should just be doing that for all new features.

@Shared var syncUp: SyncUp
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftUI
@Reducer
struct SyncUpDetail {
@ObservableState
struct State {
struct State: Equatable {
@Presents var editSyncUp: SyncUpForm.State?
@Shared var syncUp: SyncUp
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftUI
@Reducer
struct SyncUpDetail {
@ObservableState
struct State {
struct State: Equatable {
@Presents var editSyncUp: SyncUpForm.State?
@Shared var syncUp: SyncUp
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftUI
@Reducer
struct SyncUpDetail {
@ObservableState
struct State {
struct State: Equatable {
@Presents var editSyncUp: SyncUpForm.State?
@Shared var syncUp: SyncUp
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftUI
@Reducer
struct SyncUpDetail {
@ObservableState
struct State {
struct State: Equatable {
@Presents var editSyncUp: SyncUpForm.State?
@Shared var syncUp: SyncUp
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ struct SyncUpDetailView: View {
.frame(maxWidth: .infinity)
}
}
.navigationTitle(Text(store.syncUp.title))
Copy link
Member Author

Choose a reason for hiding this comment

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

forgot to add a title to the detail screen, so doing that in a bunch of places.

.toolbar {
Button("Edit") {
store.send(.editButtonTapped)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ struct SyncUpDetailView: View {
.frame(maxWidth: .infinity)
}
}
.navigationTitle(Text(store.syncUp.title))
.toolbar {
Button("Edit") {
store.send(.editButtonTapped)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ struct SyncUpDetailView: View {
.frame(maxWidth: .infinity)
}
}
.navigationTitle(Text(store.syncUp.title))
.toolbar {
Button("Edit") {
store.send(.editButtonTapped)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ struct SyncUpDetailView: View {
.frame(maxWidth: .infinity)
}
}
.navigationTitle(Text(store.syncUp.title))
.toolbar {
Button("Edit") {
store.send(.editButtonTapped)
Expand Down
Loading