Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove sheet(unwrapping:), etc., helpers for sheet(item:) overloads #157

Merged
merged 3 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Examples/CaseStudies/01-Alerts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ struct OptionalAlerts: View {

var body: some View {
List {
Stepper("Number: \(self.model.count)", value: self.$model.count)
Stepper("Number: \(model.count)", value: $model.count)
Button {
Task { await self.model.numberFactButtonTapped() }
Task { await model.numberFactButtonTapped() }
} label: {
HStack {
Text("Get number fact")
if self.model.isLoading {
if model.isLoading {
Spacer()
ProgressView()
}
}
}
.disabled(self.model.isLoading)
.disabled(model.isLoading)
}
.alert(item: self.$model.fact) {
.alert(item: $model.fact) {
Text("Fact about \($0.number)")
} actions: {
Button("Get another fact about \($0.number)") {
Task { await self.model.numberFactButtonTapped() }
Task { await model.numberFactButtonTapped() }
}
Button("Close", role: .cancel) {
self.model.fact = nil
model.fact = nil
}
} message: {
Text($0.description)
Expand All @@ -45,9 +45,9 @@ private class FeatureModel {

@MainActor
func numberFactButtonTapped() async {
self.isLoading = true
self.fact = await getNumberFact(self.count)
self.isLoading = false
isLoading = true
defer { isLoading = false }
fact = await getNumberFact(count)
}
}

Expand Down
18 changes: 9 additions & 9 deletions Examples/CaseStudies/02-ConfirmationDialogs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ struct OptionalConfirmationDialogs: View {

var body: some View {
List {
Stepper("Number: \(self.model.count)", value: self.$model.count)
Stepper("Number: \(model.count)", value: $model.count)
Button {
Task { await self.model.numberFactButtonTapped() }
Task { await model.numberFactButtonTapped() }
} label: {
HStack {
Text("Get number fact")
if self.model.isLoading {
if model.isLoading {
Spacer()
ProgressView()
}
}
}
.disabled(self.model.isLoading)
.confirmationDialog(item: self.$model.fact, titleVisibility: .visible) {
.disabled(model.isLoading)
.confirmationDialog(item: $model.fact, titleVisibility: .visible) {
Text("Fact about \($0.number)")
} actions: {
Button("Get another fact about \($0.number)") {
Task { await self.model.numberFactButtonTapped() }
Task { await model.numberFactButtonTapped() }
}
} message: {
Text($0.description)
Expand All @@ -42,9 +42,9 @@ private class FeatureModel {

@MainActor
func numberFactButtonTapped() async {
self.isLoading = true
self.fact = await getNumberFact(self.count)
self.isLoading = false
isLoading = true
defer { isLoading = false }
fact = await getNumberFact(count)
}
}

Expand Down
48 changes: 24 additions & 24 deletions Examples/CaseStudies/03-Sheets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ struct OptionalSheets: View {
var body: some View {
List {
Section {
Stepper("Number: \(self.model.count)", value: self.$model.count)
Stepper("Number: \(model.count)", value: $model.count)

HStack {
Button("Get number fact") {
Task { await self.model.numberFactButtonTapped() }
Task { await model.numberFactButtonTapped() }
}

if self.model.isLoading {
if model.isLoading {
Spacer()
ProgressView()
}
Expand All @@ -24,28 +24,28 @@ struct OptionalSheets: View {
}

Section {
ForEach(self.model.savedFacts) { fact in
ForEach(model.savedFacts) { fact in
Text(fact.description)
}
.onDelete { self.model.removeSavedFacts(atOffsets: $0) }
.onDelete { model.removeSavedFacts(atOffsets: $0) }
} header: {
Text("Saved Facts")
}
}
.sheet(unwrapping: self.$model.fact) { $fact in
.sheet(item: $model.fact) { $fact in
NavigationStack {
FactEditor(fact: $fact.description)
.disabled(self.model.isLoading)
.foregroundColor(self.model.isLoading ? .gray : nil)
.disabled(model.isLoading)
.foregroundColor(model.isLoading ? .gray : nil)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
self.model.cancelButtonTapped()
model.cancelButtonTapped()
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Save") {
self.model.saveButtonTapped(fact: fact)
model.saveButtonTapped(fact: fact)
}
}
}
Expand All @@ -60,7 +60,7 @@ private struct FactEditor: View {

var body: some View {
VStack {
TextEditor(text: self.$fact)
TextEditor(text: $fact)
}
.padding()
.navigationTitle("Fact editor")
Expand All @@ -76,41 +76,41 @@ private class FeatureModel {
private var task: Task<Void, Never>?

deinit {
self.task?.cancel()
task?.cancel()
}

@MainActor
func numberFactButtonTapped() async {
self.isLoading = true
self.fact = Fact(description: "\(self.count) is still loading...", number: self.count)
self.task = Task {
isLoading = true
fact = Fact(description: "\(count) is still loading...", number: count)
task = Task {
let fact = await getNumberFact(self.count)
self.isLoading = false
isLoading = false
guard !Task.isCancelled
else { return }
self.fact = fact
}
await self.task?.value
await task?.value
}

@MainActor
func cancelButtonTapped() {
self.task?.cancel()
self.task = nil
self.fact = nil
task?.cancel()
task = nil
fact = nil
}

@MainActor
func saveButtonTapped(fact: Fact) {
self.task?.cancel()
self.task = nil
self.savedFacts.append(fact)
task?.cancel()
task = nil
savedFacts.append(fact)
self.fact = nil
}

@MainActor
func removeSavedFacts(atOffsets offsets: IndexSet) {
self.savedFacts.remove(atOffsets: offsets)
savedFacts.remove(atOffsets: offsets)
}
}

Expand Down
46 changes: 23 additions & 23 deletions Examples/CaseStudies/04-Popovers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ struct OptionalPopovers: View {
var body: some View {
List {
Section {
Stepper("Number: \(self.model.count)", value: self.$model.count)
Stepper("Number: \(model.count)", value: $model.count)

HStack {
Button("Get number fact") {
Task { await self.model.numberFactButtonTapped() }
Task { await model.numberFactButtonTapped() }
}
.popover(unwrapping: self.$model.fact, arrowEdge: .bottom) { $fact in
.popover(item: $model.fact, arrowEdge: .bottom) { $fact in
NavigationStack {
FactEditor(fact: $fact.description)
.disabled(self.model.isLoading)
.foregroundColor(self.model.isLoading ? .gray : nil)
.disabled(model.isLoading)
.foregroundColor(model.isLoading ? .gray : nil)
.navigationBarItems(
leading: Button("Cancel") {
self.model.cancelButtonTapped()
model.cancelButtonTapped()
},
trailing: Button("Save") {
self.model.saveButtonTapped(fact: fact)
model.saveButtonTapped(fact: fact)
}
)
}
}

if self.model.isLoading {
if model.isLoading {
Spacer()
ProgressView()
}
Expand All @@ -39,10 +39,10 @@ struct OptionalPopovers: View {
}

Section {
ForEach(self.model.savedFacts) { fact in
ForEach(model.savedFacts) { fact in
Text(fact.description)
}
.onDelete { self.model.removeSavedFacts(atOffsets: $0) }
.onDelete { model.removeSavedFacts(atOffsets: $0) }
} header: {
Text("Saved Facts")
}
Expand All @@ -56,7 +56,7 @@ private struct FactEditor: View {

var body: some View {
VStack {
TextEditor(text: self.$fact)
TextEditor(text: $fact)
}
.padding()
.navigationTitle("Fact editor")
Expand All @@ -77,36 +77,36 @@ private class FeatureModel {

@MainActor
func numberFactButtonTapped() async {
self.isLoading = true
self.fact = Fact(description: "\(self.count) is still loading...", number: self.count)
self.task = Task {
isLoading = true
fact = Fact(description: "\(count) is still loading...", number: count)
task = Task {
let fact = await getNumberFact(self.count)
self.isLoading = false
isLoading = false
guard !Task.isCancelled
else { return }
self.fact = fact
}
await self.task?.value
await task?.value
}

@MainActor
func cancelButtonTapped() {
self.task?.cancel()
self.task = nil
self.fact = nil
task?.cancel()
task = nil
fact = nil
}

@MainActor
func saveButtonTapped(fact: Fact) {
self.task?.cancel()
self.task = nil
self.savedFacts.append(fact)
task?.cancel()
task = nil
savedFacts.append(fact)
self.fact = nil
}

@MainActor
func removeSavedFacts(atOffsets offsets: IndexSet) {
self.savedFacts.remove(atOffsets: offsets)
savedFacts.remove(atOffsets: offsets)
}
}

Expand Down
Loading