Skip to content

Commit

Permalink
Fix focus in tutorial (#3072)
Browse files Browse the repository at this point in the history
* Fix focus logic in tutorial.

* wip

* wip

* wip

* wip

* remove extra alert enum

* wip
  • Loading branch information
mbrandonw authored May 13, 2024
1 parent 48b57d4 commit dd145a1
Show file tree
Hide file tree
Showing 90 changed files with 214 additions and 266 deletions.
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.

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)
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):
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 {
@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))
.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

0 comments on commit dd145a1

Please sign in to comment.