Files
swift-composable-architectu…/Examples/CaseStudies/UIKitCaseStudies/RootViewController.swift
Stephen Celis 767231d179 Add Store.init that takes reducer builder (#2087)
* Add `Store.init` that takes reducer builder

* wip

* wip

* added some tests

* wip

* wip

* wip

---------

Co-authored-by: Brandon Williams <mbrandonw@hey.com>
2023-05-11 12:30:08 -07:00

93 lines
2.3 KiB
Swift

import ComposableArchitecture
import SwiftUI
import UIKit
struct CaseStudy {
let title: String
let viewController: () -> UIViewController
init(title: String, viewController: @autoclosure @escaping () -> UIViewController) {
self.title = title
self.viewController = viewController
}
}
@MainActor
let dataSource: [CaseStudy] = [
CaseStudy(
title: "Basics",
viewController: CounterViewController(
store: Store(initialState: Counter.State()) {
Counter()
}
)
),
CaseStudy(
title: "Lists",
viewController: CountersTableViewController(
store: Store(
initialState: CounterList.State(
counters: [
Counter.State(),
Counter.State(),
Counter.State(),
]
)
) {
CounterList()
}
)
),
CaseStudy(
title: "Navigate and load",
viewController: EagerNavigationViewController(
store: Store(initialState: EagerNavigation.State()) {
EagerNavigation()
}
)
),
CaseStudy(
title: "Load then navigate",
viewController: LazyNavigationViewController(
store: Store(initialState: LazyNavigation.State()) {
LazyNavigation()
}
)
),
]
final class RootViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Case Studies"
self.navigationController?.navigationBar.prefersLargeTitles = true
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dataSource.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
-> UITableViewCell
{
let caseStudy = dataSource[indexPath.row]
let cell = UITableViewCell()
cell.accessoryType = .disclosureIndicator
cell.textLabel?.text = caseStudy.title
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let caseStudy = dataSource[indexPath.row]
self.navigationController?.pushViewController(caseStudy.viewController(), animated: true)
}
}
struct RootViewController_Previews: PreviewProvider {
static var previews: some View {
let vc = UINavigationController(rootViewController: RootViewController())
return UIViewRepresented(makeUIView: { _ in vc.view })
}
}