diff LazyBear/Views/Search/CompanyList.swift @ 392:13f3578def61

Implement create watchlist
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sat, 01 May 2021 13:11:32 +0200
parents 8ec37b2baafd
children 0a4c399170c4
line wrap: on
line diff
--- a/LazyBear/Views/Search/CompanyList.swift	Fri Apr 30 20:25:52 2021 +0200
+++ b/LazyBear/Views/Search/CompanyList.swift	Sat May 01 13:11:32 2021 +0200
@@ -13,17 +13,23 @@
     // Only unseful when it's called from Profile View
     var calledFromProfileView: Bool?
     var newWatchlistClass: NewWatchlistClass?
+    @Binding var presentationMode: PresentationMode
     
     var body: some View {
         List(searchResult, id: \.self) { company in
             SearchedCompanyItem(company: company, calledFromProfileView: calledFromProfileView)
                 .if(calledFromProfileView ?? false ) { content in
-                    Button(action: { newWatchlistClass!.companies.append(company) }) {
+                    Button(action: { save(company) }) {
                         content
                     }
                 }
         }
     }
+    
+    private func save(_ company: SearchResponse) {
+        newWatchlistClass!.companies.append(company)
+        $presentationMode.wrappedValue.dismiss()  // Dismiss view
+    }
 }
 
 /*
@@ -43,7 +49,9 @@
 }
 
 struct CompanyList_Previews: PreviewProvider {
+    @Environment(\.presentationMode) static var presentationMode
+    
     static var previews: some View {
-        CompanyList(searchResult: [SearchResponse(currency: "USD", region: "US", securityName: "aaple inc", symbol: "aapl"), SearchResponse(currency: "USD", region: "US", securityName: "aaple inc", symbol: "aapl"), SearchResponse(currency: "USD", region: "US", securityName: "aaple inc", symbol: "aapl"), SearchResponse(currency: "USD", region: "US", securityName: "aaple inc", symbol: "aapl"), SearchResponse(currency: "USD", region: "US", securityName: "aaple inc", symbol: "aapl")])
+        CompanyList(searchResult: [SearchResponse(currency: "USD", region: "US", securityName: "aaple inc", symbol: "aapl"), SearchResponse(currency: "USD", region: "US", securityName: "aaple inc", symbol: "aapl"), SearchResponse(currency: "USD", region: "US", securityName: "aaple inc", symbol: "aapl"), SearchResponse(currency: "USD", region: "US", securityName: "aaple inc", symbol: "aapl"), SearchResponse(currency: "USD", region: "US", securityName: "aaple inc", symbol: "aapl")], presentationMode: presentationMode)
     }
 }