Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TCA Practice in 5 min

d_date
March 18, 2024

TCA Practice in 5 min

d_date

March 18, 2024
Tweet

More Decks by d_date

Other Decks in Education

Transcript

  1. ViewAction View͔Βݺͼग़͍ͨ͠ActionΛ੍ݶ͢Δ • ActionΛViewActionʹద߹ • Viewʹ@ViewAction(for:) ϚΫϩΛ͚ͭΔ • store.send(_: Action)

    Λ send(_: ViewAction) ʹมߋ • ʢvar storeΛpublicʹ͢Δ) public enum Action: ViewAction { case view(View) public enum View { case onAppear } } @ViewAction(for: Schedule.self) public struct ScheduleView: View { public var store:ɹStoreOf<Schedule> public var body: some View { ScrollView { … } .onAppear(perform: { send(.onAppear) }) } }
  2. @Reducer struct Schedule { @ObservableState struct State: Equatable { var

    path = StackState<Path.State>() } enum Action { case path(StackActionOf<ScheduleDetail>) } var body: some View { Reduce { state, action in … } .forEach(\.path, action: \.path) { Path() } } } extension Schedule { @Reducer struct Path { @ObservableState enum State: Equatable { case detail(ScheduleDetail.State) } enum Action { case detail(ScheduleDetail.Action) } var body: some ReducerOf<Self> { Scope(state: \.detail, action: \.detail) { ScheduleDetail() } } } } @Reducer struct Schedule { @ObservableState struct State: Equatable { @Presents var destination: Destination.State? } enum Action { case destination(PresentationAction<Destination.Action>) } var body: some ReducerOf<Schedule> { Reduce { state, action in … } .ifLet(\.$destination, action: \.destination) { Destination() } } } extension Schedule { @Reducer struct Destination { @ObservableState enum State: Equatable { case detail(ScheduleDetail.State) } enum Action { case detail(ScheduleDetail.Action) } var body: some ReducerOf<Self> { Reduce { state, action in Scope(state: \.detail, action: \.detail) { ScheduleDetail() } } } …
  3. @Reducer struct Schedule { @ObservableState struct State: Equatable { var

    path = StackState<Path.State>() } enum Action { case path(StackActionOf<ScheduleDetail>) } var body: some View { Reduce { state, action in … } .forEach(\.path, action: \.path) } } extension Schedule { @Reducer(state: .equatable) enum Path { case detail(ScheduleDetail.State) } } @Reducer struct Schedule { @ObservableState struct State: Equatable { @Presents var destination: Destination.State? } enum Action { case destination(PresentationAction<Destination.Action>) } var body: some ReducerOf<Schedule> { Reduce { state, action in … } .ifLet(\.$destination, action: \.destination) } } extension Schedule { @Reducer(state: .equatable) enum Destination { case detail(ScheduleDetail.State) } } …
  4. Implicit @CasePathable to enum Note: Circular reference errors / Swift

    5.10 @Reducer enum Destination { case detail(Detail) } extension Destination.State: Equatable {}
  5. Implicit @CasePathable to enum @Reducer(state: .equatable) enum Destination { case

    detail(Detail) } Note: Circular reference errors / Swift 5.10
  6. @Dependency(\.apiClient) extension DependencyValues { public var apiClient: ApiClient { get

    { self[ApiClient.self] } set { self[ApiClient.self] = newValue } } }
  7. import Dependencies import DependenciesMacros import Foundation @DependencyClient public struct DataClient

    { public var fetchData: @Sendable () throws -> Conference public var fetchSpeaker: @Sendable (_ name: String) throws -> Speaker? } extension DataClient: DependencyKey { static public var liveValue: DataClient = .init( fetchData: { // fetch data return response }, fetchSpeaker: { // fetch Speaker } ) }
  8. New features you missed when migrating • Generics shorthands •

    StackActionOf (Next release) • ViewAction (v1.7) • Implicit @CasePathable to enum (v1.8) • Direct dependency key value (v1.9)