Skip to content

Commit 13d9b0f

Browse files
Modernize with Observation (pointfreeco#130)
* Modernize for @observable. * wipo * wip * wip * prune * wip * wip --------- Co-authored-by: Stephen Celis <[email protected]>
1 parent 391abd4 commit 13d9b0f

File tree

69 files changed

+356
-4479
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+356
-4479
lines changed

Examples/CaseStudies/01-Alerts.swift

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import SwiftUINavigation
33

44
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *)
55
struct OptionalAlerts: View {
6-
@ObservedObject private var model = FeatureModel()
6+
@State private var model = FeatureModel()
77

88
var body: some View {
99
List {
1010
Stepper("Number: \(self.model.count)", value: self.$model.count)
11-
Button(action: { self.model.numberFactButtonTapped() }) {
11+
Button {
12+
Task { await self.model.numberFactButtonTapped() }
13+
} label: {
1214
HStack {
1315
Text("Get number fact")
1416
if self.model.isLoading {
@@ -24,9 +26,9 @@ struct OptionalAlerts: View {
2426
unwrapping: self.$model.fact,
2527
actions: {
2628
Button("Get another fact about \($0.number)") {
27-
self.model.numberFactButtonTapped()
29+
Task { await self.model.numberFactButtonTapped() }
2830
}
29-
Button("Cancel", role: .cancel) {
31+
Button("Close", role: .cancel) {
3032
self.model.fact = nil
3133
}
3234
},
@@ -36,17 +38,20 @@ struct OptionalAlerts: View {
3638
}
3739
}
3840

39-
@MainActor
40-
private class FeatureModel: ObservableObject {
41-
@Published var count = 0
42-
@Published var isLoading = false
43-
@Published var fact: Fact?
41+
@Observable
42+
private class FeatureModel {
43+
var count = 0
44+
var isLoading = false
45+
var fact: Fact?
4446

45-
func numberFactButtonTapped() {
46-
Task {
47-
self.isLoading = true
48-
self.fact = await getNumberFact(self.count)
49-
self.isLoading = false
50-
}
47+
@MainActor
48+
func numberFactButtonTapped() async {
49+
self.isLoading = true
50+
self.fact = await getNumberFact(self.count)
51+
self.isLoading = false
5152
}
5253
}
54+
55+
#Preview {
56+
OptionalAlerts()
57+
}

Examples/CaseStudies/02-ConfirmationDialogs.swift

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import SwiftUINavigation
33

44
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *)
55
struct OptionalConfirmationDialogs: View {
6-
@ObservedObject private var model = FeatureModel()
6+
@State private var model = FeatureModel()
77

88
var body: some View {
99
List {
1010
Stepper("Number: \(self.model.count)", value: self.$model.count)
11-
Button(action: { self.model.numberFactButtonTapped() }) {
11+
Button {
12+
Task { await self.model.numberFactButtonTapped() }
13+
} label: {
1214
HStack {
1315
Text("Get number fact")
1416
if self.model.isLoading {
@@ -24,7 +26,7 @@ struct OptionalConfirmationDialogs: View {
2426
unwrapping: self.$model.fact,
2527
actions: {
2628
Button("Get another fact about \($0.number)") {
27-
self.model.numberFactButtonTapped()
29+
Task { await self.model.numberFactButtonTapped() }
2830
}
2931
},
3032
message: { Text($0.description) }
@@ -34,17 +36,20 @@ struct OptionalConfirmationDialogs: View {
3436
}
3537
}
3638

37-
@MainActor
38-
private class FeatureModel: ObservableObject {
39-
@Published var count = 0
40-
@Published var isLoading = false
41-
@Published var fact: Fact?
39+
@Observable
40+
private class FeatureModel {
41+
var count = 0
42+
var isLoading = false
43+
var fact: Fact?
4244

43-
func numberFactButtonTapped() {
44-
Task {
45-
self.isLoading = true
46-
self.fact = await getNumberFact(self.count)
47-
self.isLoading = false
48-
}
45+
@MainActor
46+
func numberFactButtonTapped() async {
47+
self.isLoading = true
48+
self.fact = await getNumberFact(self.count)
49+
self.isLoading = false
4950
}
5051
}
52+
53+
#Preview {
54+
OptionalConfirmationDialogs()
55+
}

Examples/CaseStudies/03-Sheets.swift

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import SwiftUI
22
import SwiftUINavigation
33

44
struct OptionalSheets: View {
5-
@ObservedObject private var model = FeatureModel()
5+
@State private var model = FeatureModel()
66

77
var body: some View {
88
List {
@@ -11,7 +11,7 @@ struct OptionalSheets: View {
1111

1212
HStack {
1313
Button("Get number fact") {
14-
self.model.numberFactButtonTapped()
14+
Task { await self.model.numberFactButtonTapped() }
1515
}
1616

1717
if self.model.isLoading {
@@ -33,7 +33,7 @@ struct OptionalSheets: View {
3333
}
3434
}
3535
.sheet(unwrapping: self.$model.fact) { $fact in
36-
NavigationView {
36+
NavigationStack {
3737
FactEditor(fact: $fact.description)
3838
.disabled(self.model.isLoading)
3939
.foregroundColor(self.model.isLoading ? .gray : nil)
@@ -67,43 +67,53 @@ private struct FactEditor: View {
6767
}
6868
}
6969

70-
@MainActor
71-
private class FeatureModel: ObservableObject {
72-
@Published var count = 0
73-
@Published var fact: Fact?
74-
@Published var isLoading = false
75-
@Published var savedFacts: [Fact] = []
76-
private var task: Task<Void, Error>?
70+
@Observable
71+
private class FeatureModel {
72+
var count = 0
73+
var fact: Fact?
74+
var isLoading = false
75+
var savedFacts: [Fact] = []
76+
private var task: Task<Void, Never>?
7777

7878
deinit {
7979
self.task?.cancel()
8080
}
8181

82-
func numberFactButtonTapped() {
82+
@MainActor
83+
func numberFactButtonTapped() async {
8384
self.isLoading = true
8485
self.fact = Fact(description: "\(self.count) is still loading...", number: self.count)
8586
self.task = Task {
8687
let fact = await getNumberFact(self.count)
8788
self.isLoading = false
88-
try Task.checkCancellation()
89+
guard !Task.isCancelled
90+
else { return }
8991
self.fact = fact
9092
}
93+
await self.task?.value
9194
}
9295

96+
@MainActor
9397
func cancelButtonTapped() {
9498
self.task?.cancel()
9599
self.task = nil
96100
self.fact = nil
97101
}
98102

103+
@MainActor
99104
func saveButtonTapped(fact: Fact) {
100105
self.task?.cancel()
101106
self.task = nil
102107
self.savedFacts.append(fact)
103108
self.fact = nil
104109
}
105110

111+
@MainActor
106112
func removeSavedFacts(atOffsets offsets: IndexSet) {
107113
self.savedFacts.remove(atOffsets: offsets)
108114
}
109115
}
116+
117+
#Preview {
118+
OptionalSheets()
119+
}

Examples/CaseStudies/04-Popovers.swift

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import SwiftUI
22
import SwiftUINavigation
33

44
struct OptionalPopovers: View {
5-
@ObservedObject private var model = FeatureModel()
5+
@State private var model = FeatureModel()
66

77
var body: some View {
88
List {
@@ -11,10 +11,10 @@ struct OptionalPopovers: View {
1111

1212
HStack {
1313
Button("Get number fact") {
14-
self.model.numberFactButtonTapped()
14+
Task { await self.model.numberFactButtonTapped() }
1515
}
1616
.popover(unwrapping: self.$model.fact, arrowEdge: .bottom) { $fact in
17-
NavigationView {
17+
NavigationStack {
1818
FactEditor(fact: $fact.description)
1919
.disabled(self.model.isLoading)
2020
.foregroundColor(self.model.isLoading ? .gray : nil)
@@ -63,43 +63,53 @@ private struct FactEditor: View {
6363
}
6464
}
6565

66-
@MainActor
67-
private class FeatureModel: ObservableObject {
68-
@Published var count = 0
69-
@Published var fact: Fact?
70-
@Published var isLoading = false
71-
@Published var savedFacts: [Fact] = []
72-
private var task: Task<Void, Error>?
66+
@Observable
67+
private class FeatureModel {
68+
var count = 0
69+
var fact: Fact?
70+
var isLoading = false
71+
var savedFacts: [Fact] = []
72+
private var task: Task<Void, Never>?
7373

7474
deinit {
7575
self.task?.cancel()
7676
}
7777

78-
func numberFactButtonTapped() {
78+
@MainActor
79+
func numberFactButtonTapped() async {
7980
self.isLoading = true
8081
self.fact = Fact(description: "\(self.count) is still loading...", number: self.count)
8182
self.task = Task {
8283
let fact = await getNumberFact(self.count)
8384
self.isLoading = false
84-
try Task.checkCancellation()
85+
guard !Task.isCancelled
86+
else { return }
8587
self.fact = fact
8688
}
89+
await self.task?.value
8790
}
8891

92+
@MainActor
8993
func cancelButtonTapped() {
9094
self.task?.cancel()
9195
self.task = nil
9296
self.fact = nil
9397
}
9498

99+
@MainActor
95100
func saveButtonTapped(fact: Fact) {
96101
self.task?.cancel()
97102
self.task = nil
98103
self.savedFacts.append(fact)
99104
self.fact = nil
100105
}
101106

107+
@MainActor
102108
func removeSavedFacts(atOffsets offsets: IndexSet) {
103109
self.savedFacts.remove(atOffsets: offsets)
104110
}
105111
}
112+
113+
#Preview {
114+
OptionalPopovers()
115+
}

Examples/CaseStudies/05-FullScreenCovers.swift

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import SwiftUI
22
import SwiftUINavigation
33

44
struct OptionalFullScreenCovers: View {
5-
@ObservedObject private var model = FeatureModel()
5+
@State private var model = FeatureModel()
66

77
var body: some View {
88
List {
@@ -11,7 +11,7 @@ struct OptionalFullScreenCovers: View {
1111

1212
HStack {
1313
Button("Get number fact") {
14-
self.model.numberFactButtonTapped()
14+
Task { await self.model.numberFactButtonTapped() }
1515
}
1616

1717
if self.model.isLoading {
@@ -33,7 +33,7 @@ struct OptionalFullScreenCovers: View {
3333
}
3434
}
3535
.fullScreenCover(unwrapping: self.$model.fact) { $fact in
36-
NavigationView {
36+
NavigationStack {
3737
FactEditor(fact: $fact.description)
3838
.disabled(self.model.isLoading)
3939
.foregroundColor(self.model.isLoading ? .gray : nil)
@@ -67,39 +67,49 @@ private struct FactEditor: View {
6767
}
6868
}
6969

70-
@MainActor
71-
private class FeatureModel: ObservableObject {
72-
@Published var count = 0
73-
@Published var fact: Fact?
74-
@Published var isLoading = false
75-
@Published var savedFacts: [Fact] = []
76-
private var task: Task<Void, Error>?
70+
@Observable
71+
private class FeatureModel {
72+
var count = 0
73+
var fact: Fact?
74+
var isLoading = false
75+
var savedFacts: [Fact] = []
76+
private var task: Task<Void, Never>?
7777

78-
func numberFactButtonTapped() {
78+
@MainActor
79+
func numberFactButtonTapped() async {
7980
self.isLoading = true
8081
self.fact = Fact(description: "\(self.count) is still loading...", number: self.count)
8182
self.task = Task {
8283
let fact = await getNumberFact(self.count)
8384
self.isLoading = false
84-
try Task.checkCancellation()
85+
guard !Task.isCancelled
86+
else { return }
8587
self.fact = fact
8688
}
89+
await self.task?.value
8790
}
8891

92+
@MainActor
8993
func cancelButtonTapped() {
9094
self.task?.cancel()
9195
self.task = nil
9296
self.fact = nil
9397
}
9498

99+
@MainActor
95100
func saveButtonTapped(fact: Fact) {
96101
self.task?.cancel()
97102
self.task = nil
98103
self.savedFacts.append(fact)
99104
self.fact = nil
100105
}
101106

107+
@MainActor
102108
func removeSavedFacts(atOffsets offsets: IndexSet) {
103109
self.savedFacts.remove(atOffsets: offsets)
104110
}
105111
}
112+
113+
#Preview {
114+
OptionalFullScreenCovers()
115+
}

0 commit comments

Comments
 (0)