Skip to content

Commit 7f16357

Browse files
authored
Remove sheet(unwrapping:), etc., helpers for sheet(item:) overloads (pointfreeco#157)
* Remove `sheet(unwrapping:)`, etc., helpers for `sheet(item:)` overloads * wip * wip
1 parent a0ede33 commit 7f16357

27 files changed

+729
-678
lines changed

Examples/CaseStudies/01-Alerts.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@ struct OptionalAlerts: View {
77

88
var body: some View {
99
List {
10-
Stepper("Number: \(self.model.count)", value: self.$model.count)
10+
Stepper("Number: \(model.count)", value: $model.count)
1111
Button {
12-
Task { await self.model.numberFactButtonTapped() }
12+
Task { await model.numberFactButtonTapped() }
1313
} label: {
1414
HStack {
1515
Text("Get number fact")
16-
if self.model.isLoading {
16+
if model.isLoading {
1717
Spacer()
1818
ProgressView()
1919
}
2020
}
2121
}
22-
.disabled(self.model.isLoading)
22+
.disabled(model.isLoading)
2323
}
24-
.alert(item: self.$model.fact) {
24+
.alert(item: $model.fact) {
2525
Text("Fact about \($0.number)")
2626
} actions: {
2727
Button("Get another fact about \($0.number)") {
28-
Task { await self.model.numberFactButtonTapped() }
28+
Task { await model.numberFactButtonTapped() }
2929
}
3030
Button("Close", role: .cancel) {
31-
self.model.fact = nil
31+
model.fact = nil
3232
}
3333
} message: {
3434
Text($0.description)
@@ -45,9 +45,9 @@ private class FeatureModel {
4545

4646
@MainActor
4747
func numberFactButtonTapped() async {
48-
self.isLoading = true
49-
self.fact = await getNumberFact(self.count)
50-
self.isLoading = false
48+
isLoading = true
49+
defer { isLoading = false }
50+
fact = await getNumberFact(count)
5151
}
5252
}
5353

Examples/CaseStudies/02-ConfirmationDialogs.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ struct OptionalConfirmationDialogs: View {
77

88
var body: some View {
99
List {
10-
Stepper("Number: \(self.model.count)", value: self.$model.count)
10+
Stepper("Number: \(model.count)", value: $model.count)
1111
Button {
12-
Task { await self.model.numberFactButtonTapped() }
12+
Task { await model.numberFactButtonTapped() }
1313
} label: {
1414
HStack {
1515
Text("Get number fact")
16-
if self.model.isLoading {
16+
if model.isLoading {
1717
Spacer()
1818
ProgressView()
1919
}
2020
}
2121
}
22-
.disabled(self.model.isLoading)
23-
.confirmationDialog(item: self.$model.fact, titleVisibility: .visible) {
22+
.disabled(model.isLoading)
23+
.confirmationDialog(item: $model.fact, titleVisibility: .visible) {
2424
Text("Fact about \($0.number)")
2525
} actions: {
2626
Button("Get another fact about \($0.number)") {
27-
Task { await self.model.numberFactButtonTapped() }
27+
Task { await model.numberFactButtonTapped() }
2828
}
2929
} message: {
3030
Text($0.description)
@@ -42,9 +42,9 @@ private class FeatureModel {
4242

4343
@MainActor
4444
func numberFactButtonTapped() async {
45-
self.isLoading = true
46-
self.fact = await getNumberFact(self.count)
47-
self.isLoading = false
45+
isLoading = true
46+
defer { isLoading = false }
47+
fact = await getNumberFact(count)
4848
}
4949
}
5050

Examples/CaseStudies/03-Sheets.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ struct OptionalSheets: View {
77
var body: some View {
88
List {
99
Section {
10-
Stepper("Number: \(self.model.count)", value: self.$model.count)
10+
Stepper("Number: \(model.count)", value: $model.count)
1111

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

17-
if self.model.isLoading {
17+
if model.isLoading {
1818
Spacer()
1919
ProgressView()
2020
}
@@ -24,28 +24,28 @@ struct OptionalSheets: View {
2424
}
2525

2626
Section {
27-
ForEach(self.model.savedFacts) { fact in
27+
ForEach(model.savedFacts) { fact in
2828
Text(fact.description)
2929
}
30-
.onDelete { self.model.removeSavedFacts(atOffsets: $0) }
30+
.onDelete { model.removeSavedFacts(atOffsets: $0) }
3131
} header: {
3232
Text("Saved Facts")
3333
}
3434
}
35-
.sheet(unwrapping: self.$model.fact) { $fact in
35+
.sheet(item: $model.fact) { $fact in
3636
NavigationStack {
3737
FactEditor(fact: $fact.description)
38-
.disabled(self.model.isLoading)
39-
.foregroundColor(self.model.isLoading ? .gray : nil)
38+
.disabled(model.isLoading)
39+
.foregroundColor(model.isLoading ? .gray : nil)
4040
.toolbar {
4141
ToolbarItem(placement: .cancellationAction) {
4242
Button("Cancel") {
43-
self.model.cancelButtonTapped()
43+
model.cancelButtonTapped()
4444
}
4545
}
4646
ToolbarItem(placement: .confirmationAction) {
4747
Button("Save") {
48-
self.model.saveButtonTapped(fact: fact)
48+
model.saveButtonTapped(fact: fact)
4949
}
5050
}
5151
}
@@ -60,7 +60,7 @@ private struct FactEditor: View {
6060

6161
var body: some View {
6262
VStack {
63-
TextEditor(text: self.$fact)
63+
TextEditor(text: $fact)
6464
}
6565
.padding()
6666
.navigationTitle("Fact editor")
@@ -76,41 +76,41 @@ private class FeatureModel {
7676
private var task: Task<Void, Never>?
7777

7878
deinit {
79-
self.task?.cancel()
79+
task?.cancel()
8080
}
8181

8282
@MainActor
8383
func numberFactButtonTapped() async {
84-
self.isLoading = true
85-
self.fact = Fact(description: "\(self.count) is still loading...", number: self.count)
86-
self.task = Task {
84+
isLoading = true
85+
fact = Fact(description: "\(count) is still loading...", number: count)
86+
task = Task {
8787
let fact = await getNumberFact(self.count)
88-
self.isLoading = false
88+
isLoading = false
8989
guard !Task.isCancelled
9090
else { return }
9191
self.fact = fact
9292
}
93-
await self.task?.value
93+
await task?.value
9494
}
9595

9696
@MainActor
9797
func cancelButtonTapped() {
98-
self.task?.cancel()
99-
self.task = nil
100-
self.fact = nil
98+
task?.cancel()
99+
task = nil
100+
fact = nil
101101
}
102102

103103
@MainActor
104104
func saveButtonTapped(fact: Fact) {
105-
self.task?.cancel()
106-
self.task = nil
107-
self.savedFacts.append(fact)
105+
task?.cancel()
106+
task = nil
107+
savedFacts.append(fact)
108108
self.fact = nil
109109
}
110110

111111
@MainActor
112112
func removeSavedFacts(atOffsets offsets: IndexSet) {
113-
self.savedFacts.remove(atOffsets: offsets)
113+
savedFacts.remove(atOffsets: offsets)
114114
}
115115
}
116116

Examples/CaseStudies/04-Popovers.swift

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@ struct OptionalPopovers: View {
77
var body: some View {
88
List {
99
Section {
10-
Stepper("Number: \(self.model.count)", value: self.$model.count)
10+
Stepper("Number: \(model.count)", value: $model.count)
1111

1212
HStack {
1313
Button("Get number fact") {
14-
Task { await self.model.numberFactButtonTapped() }
14+
Task { await model.numberFactButtonTapped() }
1515
}
16-
.popover(unwrapping: self.$model.fact, arrowEdge: .bottom) { $fact in
16+
.popover(item: $model.fact, arrowEdge: .bottom) { $fact in
1717
NavigationStack {
1818
FactEditor(fact: $fact.description)
19-
.disabled(self.model.isLoading)
20-
.foregroundColor(self.model.isLoading ? .gray : nil)
19+
.disabled(model.isLoading)
20+
.foregroundColor(model.isLoading ? .gray : nil)
2121
.navigationBarItems(
2222
leading: Button("Cancel") {
23-
self.model.cancelButtonTapped()
23+
model.cancelButtonTapped()
2424
},
2525
trailing: Button("Save") {
26-
self.model.saveButtonTapped(fact: fact)
26+
model.saveButtonTapped(fact: fact)
2727
}
2828
)
2929
}
3030
}
3131

32-
if self.model.isLoading {
32+
if model.isLoading {
3333
Spacer()
3434
ProgressView()
3535
}
@@ -39,10 +39,10 @@ struct OptionalPopovers: View {
3939
}
4040

4141
Section {
42-
ForEach(self.model.savedFacts) { fact in
42+
ForEach(model.savedFacts) { fact in
4343
Text(fact.description)
4444
}
45-
.onDelete { self.model.removeSavedFacts(atOffsets: $0) }
45+
.onDelete { model.removeSavedFacts(atOffsets: $0) }
4646
} header: {
4747
Text("Saved Facts")
4848
}
@@ -56,7 +56,7 @@ private struct FactEditor: View {
5656

5757
var body: some View {
5858
VStack {
59-
TextEditor(text: self.$fact)
59+
TextEditor(text: $fact)
6060
}
6161
.padding()
6262
.navigationTitle("Fact editor")
@@ -77,36 +77,36 @@ private class FeatureModel {
7777

7878
@MainActor
7979
func numberFactButtonTapped() async {
80-
self.isLoading = true
81-
self.fact = Fact(description: "\(self.count) is still loading...", number: self.count)
82-
self.task = Task {
80+
isLoading = true
81+
fact = Fact(description: "\(count) is still loading...", number: count)
82+
task = Task {
8383
let fact = await getNumberFact(self.count)
84-
self.isLoading = false
84+
isLoading = false
8585
guard !Task.isCancelled
8686
else { return }
8787
self.fact = fact
8888
}
89-
await self.task?.value
89+
await task?.value
9090
}
9191

9292
@MainActor
9393
func cancelButtonTapped() {
94-
self.task?.cancel()
95-
self.task = nil
96-
self.fact = nil
94+
task?.cancel()
95+
task = nil
96+
fact = nil
9797
}
9898

9999
@MainActor
100100
func saveButtonTapped(fact: Fact) {
101-
self.task?.cancel()
102-
self.task = nil
103-
self.savedFacts.append(fact)
101+
task?.cancel()
102+
task = nil
103+
savedFacts.append(fact)
104104
self.fact = nil
105105
}
106106

107107
@MainActor
108108
func removeSavedFacts(atOffsets offsets: IndexSet) {
109-
self.savedFacts.remove(atOffsets: offsets)
109+
savedFacts.remove(atOffsets: offsets)
110110
}
111111
}
112112

0 commit comments

Comments
 (0)