diff GeoQuiz/Helpers/GameAlertsModifier.swift @ 26:425078c01194

refactor code
author Dennis C. M. <dennis@denniscm.com>
date Wed, 09 Nov 2022 10:30:01 +0100
parents GeoQuiz/Components/GameAlertsModifier.swift@f1967f8cc67b
children 6d574bd1644f
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GeoQuiz/Helpers/GameAlertsModifier.swift	Wed Nov 09 10:30:01 2022 +0100
@@ -0,0 +1,47 @@
+//
+//  GameAlertsModifier.swift
+//  GeoQuiz
+//
+//  Created by Dennis Concepción Martín on 22/9/22.
+//
+
+import SwiftUI
+import CoreData
+
+struct GameAlertsModifier<T: Game>: ViewModifier {
+    @ObservedObject var gameController: T
+    
+    var gameType: GameType
+    var moc: NSManagedObjectContext
+    
+    @Environment(\.dismiss) var dismiss
+    
+    func body(content: Content) -> some View {
+        content
+            .alert(gameController.alertTitle, isPresented: $gameController.showingWrongAnswerAlert) {
+                Button("Continue", role: .cancel) {
+                    gameController.askQuestion {
+                        gameController.selector()
+                    }
+                }
+            } message: {
+                Text(gameController.alertMessage)
+            }
+            
+            .alert(gameController.alertTitle, isPresented: $gameController.showingEndGameAlert) {
+                Button("Exit", role: .cancel) {
+                    gameController.save(gameType, with: moc)
+                    dismiss()
+                }
+            } message: {
+                Text(gameController.alertMessage)
+            }
+        
+            .alert("Are you sure?", isPresented: $gameController.showingExitGameAlert) {
+                Button("Exit", role: .destructive) { dismiss() }
+                Button("Cancel", role: .cancel) { }
+            } message: {
+                Text("Progress won't be saved.")
+            }
+    }
+}