Mercurial > public > lazybear
changeset 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 |
files | LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate LazyBear/Views/Profile/Helpers/CreateNewWatchlist.swift LazyBear/Views/Profile/ProfileView.swift LazyBear/Views/Search/CompanyList.swift LazyBear/Views/Search/SearchView.swift |
diffstat | 5 files changed, 65 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- a/LazyBear/Views/Profile/Helpers/CreateNewWatchlist.swift Fri Apr 30 20:25:52 2021 +0200 +++ b/LazyBear/Views/Profile/Helpers/CreateNewWatchlist.swift Sat May 01 13:11:32 2021 +0200 @@ -10,6 +10,10 @@ struct CreateNewWatchlist: View { @State private var showSearchView = false @ObservedObject var newWatchlistClass = NewWatchlistClass() + @State private var showCancelAlert = false + + @Environment(\.managedObjectContext) private var moc + @Environment(\.presentationMode) private var presentationMode var body: some View { NavigationView { @@ -30,6 +34,11 @@ Text(company.securityName!.capitalized) .lineLimit(1) } + + Spacer() + Button(action: { remove(company) }, label: { + Image(systemName: "multiply.circle") + }) } } } @@ -38,16 +47,55 @@ .navigationBarTitleDisplayMode(.inline) .toolbar { ToolbarItem(placement: .navigationBarTrailing) { - Button("Done", action: { }) + Button("Done", action: { saveWatchlist(newWatchlistClass.companies) }) } ToolbarItem(placement: .navigationBarLeading) { - Button("Cancel", action: { }) + Button("Cancel", action: { self.showCancelAlert = true }) } } } + // Show delete list alert + .alert(isPresented: $showCancelAlert) { + Alert( + title: Text("Your watchlist won't be saved"), + message: Text("This action can't be undo"), + primaryButton: .destructive(Text("Exit")) { presentationMode.wrappedValue.dismiss() }, + secondaryButton: .cancel() + ) + } .sheet(isPresented: $showSearchView) { SearchView(calledFromProfileView: true, newWatchlistClass: newWatchlistClass) + .environment(\.managedObjectContext, self.moc) + } + } + + /* + Search company in array and get the index -> Delete company at + this index from the array + */ + private func remove(_ company: SearchResponse) { + let index = newWatchlistClass.companies.firstIndex(of: company) + newWatchlistClass.companies.remove(at: index!) + } + + /* + Save companies to Core Data and create watchlist + */ + private func saveWatchlist(_ companies: [SearchResponse]) { + for company in companies { + let watchlistCompany = WatchlistCompany(context: moc) + watchlistCompany.name = company.securityName ?? "-" + watchlistCompany.symbol = company.symbol! + watchlistCompany.watchlist = newWatchlistClass.name + } + + do { + try moc.save() + print("Watchlist created") + presentationMode.wrappedValue.dismiss() // Dismiss view + } catch { + print(error.localizedDescription) } } }
--- a/LazyBear/Views/Profile/ProfileView.swift Fri Apr 30 20:25:52 2021 +0200 +++ b/LazyBear/Views/Profile/ProfileView.swift Sat May 01 13:11:32 2021 +0200 @@ -10,6 +10,8 @@ struct ProfileView: View { @ObservedObject var profile = Profile() + + @Environment(\.managedObjectContext) private var moc @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) var watchlistCompanies: FetchedResults<WatchlistCompany> @@ -48,8 +50,9 @@ } } } - .sheet(isPresented: $showCreateNewWatchlist) { + .fullScreenCover(isPresented: $showCreateNewWatchlist) { CreateNewWatchlist() + .environment(\.managedObjectContext, self.moc) } } else { ProgressView()
--- 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) } }
--- a/LazyBear/Views/Search/SearchView.swift Fri Apr 30 20:25:52 2021 +0200 +++ b/LazyBear/Views/Search/SearchView.swift Sat May 01 13:11:32 2021 +0200 @@ -21,7 +21,7 @@ NavigationView { VStack(alignment: .leading) { if search.showSearchList { - CompanyList(searchResult: search.data, calledFromProfileView: calledFromProfileView, newWatchlistClass: newWatchlistClass) + CompanyList(searchResult: search.data, calledFromProfileView: calledFromProfileView, newWatchlistClass: newWatchlistClass, presentationMode: presentationMode) } else { VStack(alignment: .center) { Image("bearSleeping")