Browse Source

Improved code

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 2 years ago
parent
commit
21daea5177
1 changed files with 40 additions and 18 deletions
  1. 40 18
      iOSClient/Utility/HUD.swift

+ 40 - 18
iOSClient/Utility/HUD.swift

@@ -9,20 +9,32 @@
 import SwiftUI
 
 struct HUD: View {
-     var body: some View {
-        Text("Saved image")
-            .foregroundColor(.gray)
-            .padding(.horizontal, 10)
-            .padding(14)
-            .background(
-                Blur(style: .systemMaterial)
-                    .clipShape(Capsule())
-                    .shadow(color: Color(.black).opacity(0.22), radius: 12, x: 0, y: 5)
-                )
+
+    @Binding var showHUD: Bool
+    @State var textLabel: String
+    @State var image: String
+
+    var body: some View {
+        Button(action: {
+            withAnimation {
+                self.showHUD = false
+            }
+        }) {
+            Label(textLabel, systemImage: image)
+                .foregroundColor(.gray)
+                .padding(.horizontal, 10)
+                .padding(14)
+                .background(
+                    Blur(style: .systemMaterial)
+                        .clipShape(Capsule())
+                        .shadow(color: Color(.black).opacity(0.22), radius: 12, x: 0, y: 5)
+                    )
+        }.buttonStyle(PlainButtonStyle())
     }
 }
 
 struct Blur: UIViewRepresentable {
+
     var style: UIBlurEffect.Style
 
     func makeUIView(context: Context) -> UIVisualEffectView {
@@ -35,19 +47,29 @@ struct Blur: UIViewRepresentable {
 }
 
 struct ContentView: View {
+
     @State private var showHUD = false
+    @Namespace var hudAnimation
+
+    func dismissHUDAfterTime() {
+        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
+            self.showHUD = false
+        }
+    }
 
     var body: some View {
-        ZStack(alignment: .top) {
-            NavigationView {
-                Button("Save image") {
-                    self.showHUD.toggle()
+        GeometryReader { geo in
+            ZStack(alignment: .top) {
+                NavigationView {
+                    Button("Save image") {
+                        self.showHUD.toggle()
+                    }
+                    .navigationTitle("Content View")
                 }
-                .navigationTitle("Content View")
+                HUD(showHUD: $showHUD, textLabel: "xxx", image: "photo")
+                    .offset(y: showHUD ? (geo.size.height / 2) : -200)
+                    .animation(.easeOut)
             }
-            HUD()
-                .offset(y: showHUD ? 0 : -150)
-                .animation(.easeOut)
         }
     }
 }