Repository navigation
Expand file tree
/
Copy pathMainMenuViewController.swift
More file actions
105 lines (85 loc) · 3.91 KB
/
Copy pathMainMenuViewController.swift
File metadata and controls
105 lines (85 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
See LICENSE folder for this sample’s licensing information.
Abstract:
The main menu for selecting different examples.
*/
import UIKit
import SwiftUI
private struct MenuItem {
var title: String
var subtitle: String
var viewControllerProvider: () -> UIViewController
static let allExamples = [
MenuItem(title: "SwiftUI View Controllers",
subtitle: "Using UIHostingController",
viewControllerProvider: { HostingControllerViewController() }),
MenuItem(title: "SwiftUI in Cells",
subtitle: "Using UIHostingConfiguration",
viewControllerProvider: { HostingConfigurationViewController() }),
MenuItem(title: "Data Flow with SwiftUI Cells",
subtitle: "Using ObservableObject",
viewControllerProvider: { MedicalConditionsViewController() })
]
}
class MainMenuViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
private var collectionView: UICollectionView!
override func loadView() {
setUpCollectionView()
view = collectionView
}
override func viewDidLoad() {
super.viewDidLoad()
title = "Examples"
navigationController?.navigationBar.prefersLargeTitles = true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
deselectSelectedItem(animated)
}
private func deselectSelectedItem(_ animated: Bool) {
guard let indexPath = collectionView.indexPathsForSelectedItems?.first else { return }
if let coordinator = transitionCoordinator {
coordinator.animate(alongsideTransition: { _ in
self.collectionView.deselectItem(at: indexPath, animated: true)
}, completion: { context in
if context.isCancelled {
self.collectionView.selectItem(at: indexPath, animated: false, scrollPosition: [])
}
})
} else {
collectionView.deselectItem(at: indexPath, animated: animated)
}
}
private func setUpCollectionView() {
let layout = UICollectionViewCompositionalLayout { sectionIndex, layoutEnvironment in
let listConfig = UICollectionLayoutListConfiguration(appearance: .plain)
let layoutSection = NSCollectionLayoutSection.list(using: listConfig, layoutEnvironment: layoutEnvironment)
return layoutSection
}
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
}
private var cellRegistration: UICollectionView.CellRegistration<UICollectionViewListCell, MenuItem> = {
.init { cell, indexPath, item in
cell.accessories = [.disclosureIndicator()]
var content = cell.defaultContentConfiguration()
content.text = item.title
content.secondaryText = item.subtitle
content.secondaryTextProperties.color = .secondaryLabel
cell.contentConfiguration = content
}
}()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
MenuItem.allExamples.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let item = MenuItem.allExamples[indexPath.item]
return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: item)
}
func collectionView(_ collectionView: UICollectionView, performPrimaryActionForItemAt indexPath: IndexPath) {
let item = MenuItem.allExamples[indexPath.item]
let viewController = item.viewControllerProvider()
navigationController?.pushViewController(viewController, animated: true)
}
}