mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Use dismiss from SwiftUI environment
This commit is contained in:
1 parent
54bef0bba6
commit
0bed585fd0
2 files changed
+7
-13
No files matched your search
@@ -111,13 +111,9 @@ struct AppView: View {
|
||||
},
|
||||
label: { Image(systemName: "plus") })
|
||||
.accessibility(label: Text("New Player"))
|
||||
.sheet(
|
||||
isPresented: $newPlayerIsPresented,
|
||||
content: {
|
||||
PlayerCreationView(dismissAction: {
|
||||
newPlayerIsPresented = false
|
||||
})
|
||||
})
|
||||
.sheet(isPresented: $newPlayerIsPresented) {
|
||||
PlayerCreationView()
|
||||
}
|
||||
}
|
||||
|
||||
private func stopEditing() {
|
||||
|
||||
@@ -2,10 +2,8 @@ import SwiftUI
|
||||
|
||||
/// The view that creates a new player.
|
||||
struct PlayerCreationView: View {
|
||||
/// Executed when user cancels or saves the new user.
|
||||
let dismissAction: () -> Void
|
||||
|
||||
@Environment(\.appDatabase) private var appDatabase
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@State private var name = ""
|
||||
@State private var score = ""
|
||||
@State private var errorAlertIsPresented = false
|
||||
@@ -20,7 +18,7 @@ struct PlayerCreationView: View {
|
||||
.navigationBarTitle("New Player")
|
||||
.navigationBarItems(
|
||||
leading: Button(
|
||||
action: dismissAction,
|
||||
action: { dismiss() },
|
||||
label: { Text("Cancel") }),
|
||||
trailing: Button(
|
||||
action: {
|
||||
@@ -34,7 +32,7 @@ struct PlayerCreationView: View {
|
||||
do {
|
||||
var player = Player(id: nil, name: name, score: Int(score) ?? 0)
|
||||
try await appDatabase?.savePlayer(&player)
|
||||
dismissAction()
|
||||
dismiss()
|
||||
} catch {
|
||||
errorAlertTitle = (error as? LocalizedError)?.errorDescription ?? "An error occurred"
|
||||
errorAlertIsPresented = true
|
||||
@@ -44,7 +42,7 @@ struct PlayerCreationView: View {
|
||||
|
||||
struct PlayerCreationSheet_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
PlayerCreationView(dismissAction: { })
|
||||
PlayerCreationView()
|
||||
.environment(\.appDatabase, .empty())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user