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

TCA v0.19.0からのSwitchStore/CaseLetが良い

yimajo
June 23, 2021

TCA v0.19.0からのSwitchStore/CaseLetが良い

#potatotips 74の資料です

https://potatotips.connpass.com/event/214754/

yimajo

June 23, 2021
Tweet

More Decks by yimajo

Other Decks in Technology

Transcript

  1. Effect View Store DI Environment TCA ( The Composable Architecture

    )ͱ͸ w J04ΞϓϦ։ൃͷͨΊͷ3FEVY෩ ຯͳ044 w "QQMFͷެࣜϦΞΫςΟϒϓϩά ϥϛϯάϑϨʔϜϫʔΫͰ͋Δ $PNCJOFΛར༻͍ͯ͠Δ Reducerʢؔ਺ʣ State Action Combine @Published Combine Publisher
  2. v0.19.0 w FOVNͳ4UBUFʹରԠ͢ΔDBTFQBUIͷ 3FEVDFSQVMMCBDL͕௥Ճ w FOVNͳ4UBUFΛݸʑͷ4UBUFʹม׵͢Δ 4XJUDI4UPSFͷ௥Ճ w & ff

    FDUUJNFS͕DBODFM*O'MJHIUUSVFΛݺͼग़ͤΔΑ ͏ʹͳΓɺࣗಈతʹDBODFMMBCMFJE͕ಉ͡ͳΒ DBODFMͰ͖Δɻ w 'PS&BDI4UPSF͕վྑ
  3. public enum AppState: Equatable { case login(LoginState) case newGame(NewGameState) public

    init() { self = .login(.init()) } } public struct AppState: Equatable { public var login: LoginState? = LoginState() public var newGame: NewGameState? public init() {} } public var body: some View { SwitchStore(self.store) { CaseLet( state: /AppState.login, action: AppAction.login ) { store in NavigationView { LoginView(store: store) } .navigationViewStyle(StackNavigationViewStyle()) } CaseLet( state: /AppState.newGame, action: AppAction.newGame ) { store in NavigationView { NewGameView(store: store) } .navigationViewStyle(StackNavigationViewStyle()) } } @ViewBuilder public var body: some View { IfLetStore( self.store.scope(state: \.login, action: AppAction.login) ) { store in NavigationView { LoginView(store: store) } .navigationViewStyle(StackNavigationViewStyle()) } IfLetStore( self.store.scope(state: \.newGame, action: AppAction.newGame) ) { store in NavigationView { NewGameView(store: store) } .navigationViewStyle(StackNavigationViewStyle()) } } Ҏલͷ΍Γํ 4XJUDI4UPSF$BTF-FUͰͷ΍Γํ 4XJGU6*7JFX 4XJGU6*7JFX 4UBUF 4UBUF
  4. public var body: some View { SwitchStore(self.store) { CaseLet( state:

    /AppState.login, action: AppAction.login ) { store in NavigationView { LoginView(store: store) } .navigationViewStyle(StackNavigationViewStyle()) } CaseLet( state: /AppState.newGame, action: AppAction.newGame ) { store in NavigationView { NewGameView(store: store) } .navigationViewStyle(StackNavigationViewStyle()) } Default { Text(“ະ࣮૷: ͋ͱͰ͍͍ײ͡ʹCaseLetॻ͘”) } } 4XJGU6*7JFX DefaultΛ࢖ͬͯΈΔྫ %FGBVMU͸։ൃ࣌͘Β͍ʹ࢖ͬͯجຊతʹ͸$BTF-FUͰ໢ཏ͠Α͏ public enum AppState: Equatable { case login(LoginState) case newGame(NewGameState) case foo(FooState) public init() { self = .login(.init()) } } 4UBUF
  5. -PHJO 7JFX /FX (BNF 7JFX ىಈ public enum AppState: Equatable

    { case launch(LaunchState) case login(LoginState) case newGame(NewGameState) public init() { self = .launch(.init()) } } -BVODI 7JFX ϩάΠϯ ͯ͠ͳ͚Ε͹ LoginView ϩάΠϯͯ͠Ε͹ NewGameView Ԡ༻ྫ1 LaunchViewͰΞχϝʔγϣϯͱ͔Ͱ͖Δ
  6. ىಈ public struct AppState: Equatable { enum Scene: Equatable {

    case login(LoginState) case newGame(NewGameState) } var scene: Scene var flag: Bool // ڞ௨ͳԿ͔ public init( scene: Scene = .loginCore(.init()) flag: Bool = false ) { self.scene = scene self.flag = flag } } 4UBUF -PHJO 7JFX /FX (BNF 7JFX ϩάΠϯ൑ఆ ͯ͠ NewGameView Ԡ༻ྫ2 ୹ॴ: CathPathͷ࢓༷Λཧղ͠ͳ͍ͱೲಘͰ͖ͳ͍΍ΓํʹͳΔ
  7. ىಈ public struct RootState: Equatable { var appCore: AppCoreState var

    flag: Bool // ڞ௨ͳԿ͔ public init( appCore: AppCoreState = .loginState(.init()) flag: Bool = false ) { self.appCore = appCore self.flag = flag } } let reducer = Reducer.combine( appCoreReducer( // … ), Reducer { // … } ) 4UBUF -PHJO 7JFX /FX (BNF 7JFX ϩάΠϯ൑ఆ ͯ͠ NewGameView Ԡ༻ྫ2.1 public enum AppCoreState: Equatable { case login(LoginState) case newGame(NewGameState) } public enum AppCoreAction: Equatable { case login(LoginAction) case newGame(NewGameAction) } $PSFͱͯ͠FOVN4UBUFʹ͢Δ͕3PPU͸TUSVDU