はじめに
最近、個人開発アプリをTCAにリアーキテクチャしています。
これから学習も兼ねて、基本的な形を記事にしていこうと思います。
実装
ContentView
import SwiftUI
import ComposableArchitecture
struct ContentView: View {
let store: StoreOf<Feature>
var body: some View {
WithViewStore(store) { viewStore in
Button {
viewStore.send(.alertPresented)
} label: {
Text("アラート")
}
.alert(store.scope(state: \.alert), dismiss: .alertDismissed)
}
}
}
Feature
import Foundation
import ComposableArchitecture
struct Feature: ReducerProtocol {
struct State: Equatable {
var alert: AlertState<Feature.Action>?
}
enum Action: Equatable {
case alertPresented
case alertDismissed
}
func reduce(into state: inout State, action: Action) -> EffectPublisher<Action, Never> {
switch action {
case .alertPresented:
state.alert = .init(title: .init("テストアラート"))
return .none
case .alertDismissed:
state.alert = nil
return .none
}
}
}
完成
おわり
TCAめっちゃ気に入りました笑