Skip to content

Commit

Permalink
288
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Jul 29, 2024
1 parent 628463e commit 0c6aa00
Show file tree
Hide file tree
Showing 16 changed files with 1,347 additions and 0 deletions.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
118 changes: 118 additions & 0 deletions 0288-modern-uikit-pt8/ModernUIKit/ModernUIKit/AppFeature.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import Perception
import SwiftUI

@Perceptible
class AppModel {
var path: [Path] {
didSet {
print(path)
}
}
init(path: [Path] = []) {
self.path = path
}

enum Path: Hashable {
case counter(CounterModel)
case settings(SettingsModel)
}
}

struct AppView: View {
@Perception.Bindable var model: AppModel

var body: some View {
WithPerceptionTracking {
NavigationStack(path: $model.path) {
Form {
Button("Counter") {
model.path.append(.counter(CounterModel()))
}
Button("Settings") {
model.path.append(.settings(SettingsModel()))
}

NavigationLink("Counter w/ value", value: AppModel.Path.counter(CounterModel()))
}
.navigationDestination(for: AppModel.Path.self) { path in
switch path {
case let .counter(model):
CounterView(model: model)
case let .settings(model):
SettingsView(model: model)
}
}
}
}
}
}

extension NavigationStackController where Data == [AppModel.Path] {
convenience init(model: AppModel) {
@UIBindable var model = model
self.init(path: $model.path) {
RootViewController()
} destination: { path in
switch path {
case let .counter(model):
CounterViewController(model: model)
case let .settings(model):
SettingsViewController(model: model)
}
}
if #available(iOS 17.0, *) {
self.traitOverrides.path = $model.path
} else {
// Fallback on earlier versions
}
}
}

private enum PathTrait: UITraitDefinition {
static var defaultValue: UIBinding<[AppModel.Path]> {
@UIBindable var model = AppModel()
return $model.path
}
}

extension UITraitCollection {
@available(iOS 17.0, *)
var path: UIBinding<[AppModel.Path]> {
self[PathTrait.self]
}
}

@available(iOS 17.0, *)
extension UIMutableTraits {
fileprivate(set) var path: UIBinding<[AppModel.Path]> {
get { self[PathTrait.self] }
set { self[PathTrait.self] = newValue }
}
}

final class RootViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

let counterButton = UIButton(type: .system, primaryAction: UIAction { [weak self] _ in
self?.navigationController?.push(value: AppModel.Path.counter(CounterModel()))
})
counterButton.setTitle("Counter", for: .normal)
let settingsButton = UIButton(type: .system, primaryAction: UIAction { [weak self] _ in
self?.navigationController?.push(value: AppModel.Path.settings(SettingsModel()))
})
settingsButton.setTitle("Settings", for: .normal)
let stack = UIStackView(arrangedSubviews: [
counterButton,
settingsButton,
])
stack.axis = .vertical
stack.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stack)

NSLayoutConstraint.activate([
stack.centerXAnchor.constraint(equalTo: view.centerXAnchor),
stack.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"images" : [
{
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading

0 comments on commit 0c6aa00

Please sign in to comment.