-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c6aa00
commit 40534cc
Showing
117 changed files
with
12,841 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## [Point-Free](https://www.pointfree.co) | ||
|
||
> #### This directory contains code from Point-Free Episode: [Modern UIKit: Stack Navigation, Part 2](https://www.pointfree.co/episodes/ep288-modern-uikit-stack-navigation-part-2) | ||
> | ||
> We round out our stack navigation tools with support for an `@Environment`-like feature for holding onto the stack’s path, a `NavigationLink`-like feature for pushing features onto the stack from anywhere, and we’ll handle every corner case from deep-linking to user dismissal. |
433 changes: 433 additions & 0 deletions
433
0289-modern-uikit-pt9/ModernUIKit/ModernUIKit.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
...-uikit-pt9/ModernUIKit/ModernUIKit.xcodeproj/project.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
...dernUIKit/ModernUIKit.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
114 changes: 114 additions & 0 deletions
114
0289-modern-uikit-pt9/ModernUIKit/ModernUIKit/AppFeature.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import Perception | ||
import SwiftUI | ||
|
||
@Perceptible | ||
class AppModel { | ||
var path: [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), | ||
]) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...dern-uikit-pt9/ModernUIKit/ModernUIKit/Assets.xcassets/AccentColor.colorset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...modern-uikit-pt9/ModernUIKit/ModernUIKit/Assets.xcassets/AppIcon.appiconset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
0289-modern-uikit-pt9/ModernUIKit/ModernUIKit/Assets.xcassets/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Oops, something went wrong.