changeset 178:c1aa75608c27

Implement HUD
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sun, 21 Feb 2021 17:38:23 +0100
parents 235ee168a9c3
children 46299426d953
files LazyBear/ContentView.swift LazyBear/HUDManager.swift LazyBear/UI/CompanyView.swift LazyBear/UI/HUD.swift LazyBear/UI/Watchlist.swift
diffstat 5 files changed, 57 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear/ContentView.swift	Sun Feb 21 17:38:02 2021 +0100
+++ b/LazyBear/ContentView.swift	Sun Feb 21 17:38:23 2021 +0100
@@ -8,25 +8,33 @@
 import SwiftUI
 
 struct ContentView: View {
+    @ObservedObject var hudManager = HUDManager()
+    
     var body: some View {
-        TabView {
-            // First view
-            Watchlist()
-                .tabItem {
-                    Label("Watchlist", systemImage: "list.dash")
-                }
+        ZStack(alignment: .top) {
+            TabView {
+                // First view
+                Watchlist(hudManager: hudManager)
+                    .tabItem {
+                        Label("Watchlist", systemImage: "list.dash")
+                    }
+                
+                // First view
+                Search(hudManager: hudManager)
+                    .tabItem {
+                        Label("Search", systemImage: "magnifyingglass")
+                    }
+                
+                // First view
+                Settings()
+                    .tabItem {
+                        Label("Settings", systemImage: "gear")
+                    }
+            }
             
-            // First view
-            Search()
-                .tabItem {
-                    Label("Search", systemImage: "magnifyingglass")
-                }
-            
-            // First view
-            Settings()
-                .tabItem {
-                    Label("Settings", systemImage: "gear")
-                }
+            HUD(text: hudManager.text, image: hudManager.image)
+                .offset(y: hudManager.isShowing ? 0 : -100)
+                .animation(.easeInOut)
         }
     }
 }
--- a/LazyBear/HUDManager.swift	Sun Feb 21 17:38:02 2021 +0100
+++ b/LazyBear/HUDManager.swift	Sun Feb 21 17:38:23 2021 +0100
@@ -7,14 +7,19 @@
 
 import SwiftUI
 
-struct HUDManager: View {
-    var body: some View {
-        Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
+class HUDManager: ObservableObject {
+    var text = String()
+    var image = "checkmark.circle"
+    @Published var isShowing = false
+    
+    func show(text: String, image: String) {
+        self.text = text
+        self.image = image
+        self.isShowing = true
+        
+        // Dimiss after time
+        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
+            self.isShowing = false
+        }
     }
 }
-
-struct HUDManager_Previews: PreviewProvider {
-    static var previews: some View {
-        HUDManager()
-    }
-}
--- a/LazyBear/UI/CompanyView.swift	Sun Feb 21 17:38:02 2021 +0100
+++ b/LazyBear/UI/CompanyView.swift	Sun Feb 21 17:38:23 2021 +0100
@@ -8,6 +8,7 @@
 import SwiftUI
 
 struct CompanyView: View {
+    @ObservedObject var hudManager: HUDManager
     @FetchRequest(entity: Company.entity(), sortDescriptors: []) var companies: FetchedResults<Company>
     @Environment(\.managedObjectContext) private var moc
     
@@ -38,11 +39,14 @@
     
     // Add to watchlist
     private func add() {
+        let generator = UINotificationFeedbackGenerator()  // Haptic
         let company = Company(context: moc)
         company.symbol = symbol
         company.name = name
         do {
             try moc.save()
+            hudManager.show(text: "Company saved", image: "checkmark.circle")
+            generator.notificationOccurred(.success)
             print("Company saved.")
         } catch {
             print(error.localizedDescription)
@@ -53,7 +57,7 @@
 struct CompanyView_Previews: PreviewProvider {
     static var previews: some View {
         NavigationView {
-            CompanyView(name: "apple inc", symbol: "aapl")
+            CompanyView(hudManager: HUDManager(), name: "apple inc", symbol: "aapl")
         }
         .navigationViewStyle(StackNavigationViewStyle())
     }
--- a/LazyBear/UI/HUD.swift	Sun Feb 21 17:38:02 2021 +0100
+++ b/LazyBear/UI/HUD.swift	Sun Feb 21 17:38:23 2021 +0100
@@ -8,8 +8,11 @@
 import SwiftUI
 
 struct HUD: View {
-    @ViewBuilder var body: some View {
-        Text("Saved image")
+    var text: String
+    var image: String
+    
+    var body: some View {
+        Label(text, systemImage: image)
             .foregroundColor(.gray)
             .padding(.horizontal, 10)
             .padding(14)
--- a/LazyBear/UI/Watchlist.swift	Sun Feb 21 17:38:02 2021 +0100
+++ b/LazyBear/UI/Watchlist.swift	Sun Feb 21 17:38:23 2021 +0100
@@ -8,6 +8,7 @@
 import SwiftUI
 
 struct Watchlist: View {
+    @ObservedObject var hudManager: HUDManager
     @Environment(\.managedObjectContext) private var moc
     @FetchRequest(entity: Company.entity(), sortDescriptors: []) var companies: FetchedResults<Company>
     
@@ -15,7 +16,7 @@
         NavigationView {
             List {
                 ForEach(companies, id: \.self) { company in
-                    NavigationLink(destination: CompanyView(name: company.name, symbol: company.symbol)) {
+                    NavigationLink(destination: CompanyView(hudManager: hudManager, name: company.name, symbol: company.symbol)) {
                         CompanyRow(symbol: company.symbol, name: company.name)
                     }
                 }
@@ -35,12 +36,16 @@
             let company = companies[index]
             moc.delete(company)
         }
-        try? moc.save()
+        do {
+            try moc.save()
+        } catch {
+            // Error
+        }
     }
 }
 
 struct Watchlist_Previews: PreviewProvider {
     static var previews: some View {
-        Watchlist()
+        Watchlist(hudManager: HUDManager())
     }
 }