changeset 391:8ec37b2baafd

Implementing CreateNewWatchlist
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 30 Apr 2021 20:25:52 +0200
parents 6303385b3629
children 13f3578def61
files LazyBear.xcodeproj/project.pbxproj LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate LazyBear/Views/Profile/Helpers/CreateNewWatchlist.swift LazyBear/Views/Profile/Helpers/NewWatchlistClass.swift LazyBear/Views/Profile/ProfileView.swift LazyBear/Views/Search/CompanyList.swift LazyBear/Views/Search/Helpers/SearchedCompanyItem.swift LazyBear/Views/Search/SearchView.swift
diffstat 8 files changed, 134 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear.xcodeproj/project.pbxproj	Sun Apr 25 19:52:04 2021 +0200
+++ b/LazyBear.xcodeproj/project.pbxproj	Fri Apr 30 20:25:52 2021 +0200
@@ -30,6 +30,8 @@
 		9550444C26111FED000E0BCB /* StockItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9550444B26111FED000E0BCB /* StockItem.swift */; };
 		955E73392623568F005652FF /* Home.swift in Sources */ = {isa = PBXBuildFile; fileRef = 955E73382623568F005652FF /* Home.swift */; };
 		955E733C262356F3005652FF /* HomeResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 955E733B262356F3005652FF /* HomeResponse.swift */; };
+		9562404C263C766D00C6C511 /* CreateNewWatchlist.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9562404B263C766D00C6C511 /* CreateNewWatchlist.swift */; };
+		9562404E263C7D8800C6C511 /* NewWatchlistClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9562404D263C7D8800C6C511 /* NewWatchlistClass.swift */; };
 		95672B8F25DDA54700DCBE4A /* LazyBearApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95672B8E25DDA54700DCBE4A /* LazyBearApp.swift */; };
 		95672B9325DDA54700DCBE4A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 95672B9225DDA54700DCBE4A /* Assets.xcassets */; };
 		95672B9625DDA54700DCBE4A /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 95672B9525DDA54700DCBE4A /* Preview Assets.xcassets */; };
@@ -105,6 +107,9 @@
 		9550444B26111FED000E0BCB /* StockItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StockItem.swift; sourceTree = "<group>"; };
 		955E73382623568F005652FF /* Home.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Home.swift; sourceTree = "<group>"; };
 		955E733B262356F3005652FF /* HomeResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeResponse.swift; sourceTree = "<group>"; };
+		9562404A263C72E800C6C511 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
+		9562404B263C766D00C6C511 /* CreateNewWatchlist.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreateNewWatchlist.swift; sourceTree = "<group>"; };
+		9562404D263C7D8800C6C511 /* NewWatchlistClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewWatchlistClass.swift; sourceTree = "<group>"; };
 		95672B8B25DDA54700DCBE4A /* LazyBear.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LazyBear.app; sourceTree = BUILT_PRODUCTS_DIR; };
 		95672B8E25DDA54700DCBE4A /* LazyBearApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LazyBearApp.swift; sourceTree = "<group>"; };
 		95672B9225DDA54700DCBE4A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@@ -284,6 +289,7 @@
 		95672B8225DDA54700DCBE4A = {
 			isa = PBXGroup;
 			children = (
+				9562404A263C72E800C6C511 /* README.md */,
 				950C572D2629F03A00F234FE /* LazyBear.xctestplan */,
 				95672B8D25DDA54700DCBE4A /* LazyBear */,
 				950C57112629EF9100F234FE /* LazyBearTests */,
@@ -390,6 +396,8 @@
 			isa = PBXGroup;
 			children = (
 				95BD2FAD26341BD1008B6752 /* TextfieldAlert.swift */,
+				9562404B263C766D00C6C511 /* CreateNewWatchlist.swift */,
+				9562404D263C7D8800C6C511 /* NewWatchlistClass.swift */,
 			);
 			path = Helpers;
 			sourceTree = "<group>";
@@ -581,6 +589,7 @@
 				951566E72613A2B6007C0F36 /* TradingDates.swift in Sources */,
 				955E73392623568F005652FF /* Home.swift in Sources */,
 				95721DA6262761E700EC527B /* CurrencyRow.swift in Sources */,
+				9562404C263C766D00C6C511 /* CreateNewWatchlist.swift in Sources */,
 				95672B8F25DDA54700DCBE4A /* LazyBearApp.swift in Sources */,
 				95E6C57626345CE700AC1518 /* WatchlistManagerTest.swift in Sources */,
 				95A7C066261639E0003E2EC1 /* SearchView.swift in Sources */,
@@ -603,6 +612,7 @@
 				95672B9B25DDA54800DCBE4A /* LazyBear.xcdatamodeld in Sources */,
 				95C8C0E0262A369F0082D1D9 /* ProfileResponse.swift in Sources */,
 				95AD4A2D26078C1400498079 /* ContentView.swift in Sources */,
+				9562404E263C7D8800C6C511 /* NewWatchlistClass.swift in Sources */,
 				95721DB4262787EF00EC527B /* ExtensiveList.swift in Sources */,
 				95C8C0DB262A36990082D1D9 /* Profile.swift in Sources */,
 				952045152610C7C600A76362 /* ConvertEpoch.swift in Sources */,
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyBear/Views/Profile/Helpers/CreateNewWatchlist.swift	Fri Apr 30 20:25:52 2021 +0200
@@ -0,0 +1,59 @@
+//
+//  CreateNewWatchlist.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 30/4/21.
+//
+
+import SwiftUI
+
+struct CreateNewWatchlist: View {
+    @State private var showSearchView = false
+    @ObservedObject var newWatchlistClass = NewWatchlistClass()
+    
+    var body: some View {
+        NavigationView {
+            Form {
+                Section(header: Text("Watchlist name")) {
+                    TextField("Technologies, banks", text: $newWatchlistClass.name)
+                }
+                
+                Section(header: Text("Companies")) {
+                    Button("Add companies", action: { showSearchView = true })
+                    
+                    ForEach(newWatchlistClass.companies, id: \.self) { company in
+                        HStack {
+                            VStack(alignment: .leading) {
+                                Text(company.symbol!.uppercased())
+                                    .fontWeight(.semibold)
+
+                                Text(company.securityName!.capitalized)
+                                    .lineLimit(1)
+                            }
+                        }
+                    }
+                }
+            }
+            .navigationTitle("New watchlist")
+            .navigationBarTitleDisplayMode(.inline)
+            .toolbar {
+                ToolbarItem(placement: .navigationBarTrailing) {
+                    Button("Done", action: {  })
+                }
+                
+                ToolbarItem(placement: .navigationBarLeading) {
+                    Button("Cancel", action: {  })
+                }
+            }
+        }
+        .sheet(isPresented: $showSearchView) {
+            SearchView(calledFromProfileView: true, newWatchlistClass: newWatchlistClass)
+        }
+    }
+}
+
+struct CreateNewWatchlist_Previews: PreviewProvider {
+    static var previews: some View {
+        CreateNewWatchlist()
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyBear/Views/Profile/Helpers/NewWatchlistClass.swift	Fri Apr 30 20:25:52 2021 +0200
@@ -0,0 +1,13 @@
+//
+//  NewWatchlistClass.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 30/4/21.
+//
+
+import SwiftUI
+
+class NewWatchlistClass: ObservableObject {
+    @Published var name = String()
+    @Published var companies = [SearchResponse]()
+}
--- a/LazyBear/Views/Profile/ProfileView.swift	Sun Apr 25 19:52:04 2021 +0200
+++ b/LazyBear/Views/Profile/ProfileView.swift	Fri Apr 30 20:25:52 2021 +0200
@@ -12,12 +12,15 @@
     @ObservedObject var profile = Profile()
     @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
     var watchlistCompanies: FetchedResults<WatchlistCompany>
+    
+    @State private var showCreateNewWatchlist = false
 
     var body: some View {
         if profile.showView {
             NavigationView {
-                // Get Watchlist names -> Create rows for each watchlist -> in each row, show companies
                 List {
+                    
+                    // Get Watchlist names -> Create rows for each watchlist -> in each row, show companies
                     let watchlists = Set(watchlistCompanies.map { $0.watchlist })  // Set -> avoid duplicates names
                     
                     ForEach(Array(watchlists), id: \.self) { watchlist in
@@ -39,12 +42,15 @@
                 .navigationBarTitleDisplayMode(.inline)
                 .toolbar {
                     ToolbarItem(placement: .navigationBarTrailing) {
-                        Button(action: {}) {
+                        Button(action: { showCreateNewWatchlist = true }) {
                             Image(systemName: "plus")
                         }
                     }
                 }
             }
+            .sheet(isPresented: $showCreateNewWatchlist) {
+                CreateNewWatchlist()
+            }
         } else {
             ProgressView()
                 .onAppear { prepareUrl(isInitRequest: true) }
--- a/LazyBear/Views/Search/CompanyList.swift	Sun Apr 25 19:52:04 2021 +0200
+++ b/LazyBear/Views/Search/CompanyList.swift	Fri Apr 30 20:25:52 2021 +0200
@@ -10,10 +10,33 @@
 struct CompanyList: View {
     var searchResult: [SearchResponse]
     
+    // Only unseful when it's called from Profile View
+    var calledFromProfileView: Bool?
+    var newWatchlistClass: NewWatchlistClass?
+    
     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) }) {
+                        content
+                    }
+                }
+        }
+    }
+}
+
+/*
+ Wrap view on condition
+ */
+extension View {
+   @ViewBuilder
+   func `if`<Content: View>(_ conditional: Bool, content: (Self) -> Content) -> some View {
+        if conditional {
+            content(self)
+        } else {
             NavigationLink(destination: Text("Hello")) {
-                SearchedCompanyItem(company: company)
+                self
             }
         }
     }
--- a/LazyBear/Views/Search/Helpers/SearchedCompanyItem.swift	Sun Apr 25 19:52:04 2021 +0200
+++ b/LazyBear/Views/Search/Helpers/SearchedCompanyItem.swift	Fri Apr 30 20:25:52 2021 +0200
@@ -10,6 +10,9 @@
 struct SearchedCompanyItem: View {
     var company: SearchResponse
     
+    // Only unseful when it's called from Profile View
+    var calledFromProfileView: Bool?
+    
     @Environment(\.managedObjectContext) private var moc
     @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
     var watchlistCompany: FetchedResults<WatchlistCompany>
@@ -19,18 +22,20 @@
     var body: some View {
         let watchlistSymbols = watchlistCompany.map { $0.symbol }
         HStack {
-            Button(action: { self.showingActionSheet = true }) {
-                if watchlistSymbols.contains(company.symbol!) {
-                    Image(systemName: "star.fill")
-                        .foregroundColor(.yellow)
-                        .imageScale(.large)
-                } else {
-                    Image(systemName: "star")
-                        .foregroundColor(.yellow)
-                        .imageScale(.large)
+            if !(calledFromProfileView ?? false) {
+                Button(action: { self.showingActionSheet = true }) {
+                    if watchlistSymbols.contains(company.symbol!) {
+                        Image(systemName: "star.fill")
+                            .foregroundColor(.yellow)
+                            .imageScale(.large)
+                    } else {
+                        Image(systemName: "star")
+                            .foregroundColor(.yellow)
+                            .imageScale(.large)
+                    }
                 }
+                .buttonStyle(PlainButtonStyle())
             }
-            .buttonStyle(PlainButtonStyle())
             
             VStack(alignment: .leading) {
                 Text(company.symbol!.uppercased())
--- a/LazyBear/Views/Search/SearchView.swift	Sun Apr 25 19:52:04 2021 +0200
+++ b/LazyBear/Views/Search/SearchView.swift	Fri Apr 30 20:25:52 2021 +0200
@@ -9,6 +9,10 @@
 import SwiftlySearch
 
 struct SearchView: View {
+    // Only unseful when it's called from Profile View
+    var calledFromProfileView: Bool?
+    var newWatchlistClass: NewWatchlistClass?
+    
     @ObservedObject var search = Search()
     @Environment(\.presentationMode) private var presentationMode
     @State private var searchedText = String()
@@ -17,7 +21,7 @@
         NavigationView {
             VStack(alignment: .leading) {
                 if search.showSearchList {
-                    CompanyList(searchResult: search.data)
+                    CompanyList(searchResult: search.data, calledFromProfileView: calledFromProfileView, newWatchlistClass: newWatchlistClass)
                 } else {
                     VStack(alignment: .center) {
                         Image("bearSleeping")