Slide 14
Slide 14 text
検索カテゴリー用のChipのバグ対応
TCAのReducer内部で選択肢の取得はできていたため表示要素処理を個別に作成
searchCategoryFilterChip(
allCategories: store.timetable?.categories ?? [],
selection: store.selectedCategory,
defaultTitle: String(localized: "カテゴリ", bundle: .module),
onSelect: {
store.send(.view(.selectedCategoryChanged($0)))
}
)
private func searchCategoryFilterChip(
allCategories: [TimetableCategory],
selection: TimetableCategory?,
defaultTitle: String,
onSelect: @escaping (TimetableCategory) -> Void
) -> some View {
Menu {
ForEach(allCategories, id: \.id) { category in
Button {
onSelect(category)
} label: {
HStack {
if category == selection {
Image(.icCheck)
}
Text(category.title.currentLangTitle)
}
}
}
} label: {
SelectionChip(
title: selection?.title.currentLangTitle ?? defaultTitle,
isMultiSelect: true,
isSelected: selection != nil
) {}
}
}
public var body: some ReducerOf {
Reduce { state, action in
switch action {
case let .view(viewAction):
switch viewAction {
// … 省略 …
case let .selectedCategoryChanged(category):
state.filters = state.filters.copyWith(
categories: category.map { [$0] } ?? []
)
return .none
// … 省略 …
}
}
}
// MEMO: All Category can get from timetable TimetableCategories get timetable model.
// (TimetableCategory don't have to conform to Selectable protocol.)
Make Categories in TCA Reducer.
表示内容をfilter
@ObservableState
public struct State: Equatable
// 👉 stateの値を作成してView要素で利用
var selectedCategory: TimetableCategory? {
filters.categories.first
}
状態変化
View構築