diff GeoQuiz/Components/GameAlertsModifier.swift @ 10:a793f33f05fb

refactor code and fix layout
author Dennis C. M. <dennis@denniscm.com>
date Sat, 08 Oct 2022 21:36:40 +0200
parents GeoQuiz/Helpers/GameAlertsModifier.swift@e09959b4e4a8
children f1967f8cc67b
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GeoQuiz/Components/GameAlertsModifier.swift	Sat Oct 08 21:36:40 2022 +0200
@@ -0,0 +1,55 @@
+//
+//  GameAlertsModifier.swift
+//  GeoQuiz
+//
+//  Created by Dennis Concepción Martín on 22/9/22.
+//
+
+import SwiftUI
+
+struct GameAlertsModifier<T: Game>: ViewModifier {
+    @ObservedObject var game: T
+    @Environment(\.dismiss) var dismiss
+    
+    func body(content: Content) -> some View {
+        content
+            .alert(game.alertTitle, isPresented: $game.showingWrongAnswerAlert) {
+                Button("Continue", role: .cancel) {
+                    game.askQuestion {
+                        game.selector()
+                    }
+                }
+            } message: {
+                Text(game.alertMessage)
+            }
+        
+            .alert(game.alertTitle, isPresented: $game.showingGameOverAlert) {
+                Button("Try again") {
+                    game.reset {
+                        game.selector()
+                    }
+                }
+                Button("Exit", role: .cancel) { dismiss()}
+            } message: {
+                Text(game.alertMessage)
+            }
+            
+            .alert(game.alertTitle, isPresented: $game.showingEndGameAlert) {
+                Button("Play again") {
+                    game.reset() {
+                        game.selector()
+                    }
+                }
+                Button("Exit", role: .cancel) { dismiss() }
+            } message: {
+                Text(game.alertMessage)
+            }
+        
+            .alert("Are you sure?", isPresented: $game.showingExitGameAlert) {
+                Button("Exit", role: .destructive) { dismiss() }
+                Button("Cancel", role: .cancel) { }
+            } message: {
+                Text("Progress won't be saved.")
+            }
+    }
+}