comparison LazyBear/Views/Profile/Helpers/WatchlistSheet.swift @ 428:8c58ce834d95

Bug fixes and change assets
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 18 Jun 2021 12:43:17 +0200
parents e707dbfc3115
children c78d5b5b3bda
comparison
equal deleted inserted replaced
427:e707dbfc3115 428:8c58ce834d95
7 7
8 import SwiftUI 8 import SwiftUI
9 9
10 struct WatchlistSheet: View { 10 struct WatchlistSheet: View {
11 var listName: String 11 var listName: String
12 var companies: [CompanyModel] 12 var apiCompanies: [CompanyModel]
13 13
14 @Environment(\.presentationMode) private var watchlistSheetPresentation 14 @Environment(\.presentationMode) private var watchlistSheetPresentationMode
15 @Environment(\.managedObjectContext) private var moc 15 @Environment(\.managedObjectContext) private var moc
16 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [NSSortDescriptor(key:"symbol", ascending:true)]) 16 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
17 var watchlistCompanies: FetchedResults<WatchlistCompany> 17 var watchlistCompanies: FetchedResults<WatchlistCompany>
18 18
19 /* 19 @State private var showDeleteListAlert = false
20 NSSortDescriptor and .sorted(by: {} in ForEach must coincide. If not .onDelete is not working well. 20 @State private var showRenameListSheet = false
21 */ 21
22 var body: some View { 22 var body: some View {
23 NavigationView { 23 NavigationView {
24 VStack { 24 VStack {
25 List { 25 List {
26 ForEach(companies.sorted(by: { $0.companyName < $1.companyName }), id: \.self) { company in 26 ForEach(watchlistCompanies.filter { $0.watchlistName == listName }, id: \.self) { watchlistCompany in
27 StockSheetRow(company: company) 27 let apiCompany = apiCompanies.first(where: { $0.symbol == watchlistCompany.symbol })
28 WatchlistSheetRow(apiCompany: apiCompany!, watchlistCompany: watchlistCompany)
28 } 29 }
29 .onDelete(perform: deleteCompany) 30 .onDelete(perform: deleteCompany)
30 } 31 }
31 } 32 }
32 .navigationTitle(listName) 33 .navigationTitle(listName)
33 .navigationBarTitleDisplayMode(.inline) 34 .navigationBarTitleDisplayMode(.inline)
34 .toolbar { 35 .toolbar {
35 ToolbarItem(placement: .navigationBarLeading) { 36 ToolbarItem(placement: .navigationBarLeading) {
36 Button(action: {watchlistSheetPresentation.wrappedValue.dismiss()}) { 37 Button(action: {watchlistSheetPresentationMode.wrappedValue.dismiss()}) {
37 Image(systemName: "multiply") 38 Image(systemName: "multiply")
38 } 39 }
39 } 40 }
41
42 ToolbarItem(placement: .navigationBarTrailing) {
43 ToolbarMenu(showRenameListSheet: $showRenameListSheet, showDeleteListAlert: $showDeleteListAlert)
44 }
40 } 45 }
46 }
47 .sheet(isPresented: $showRenameListSheet) {
48 RenameListSheet(oldWatchlistName: listName)
49 .environment(\.managedObjectContext, self.moc)
50 }
51 .alert(isPresented: $showDeleteListAlert) { /// Show delete list alert
52 Alert(
53 title: Text("Are you sure you want to delete this list?"),
54 message: Text("This action can't be undo"),
55 primaryButton: .destructive(Text("Delete")) { deleteList() },
56 secondaryButton: .cancel()
57 )
41 } 58 }
42 } 59 }
43 60
44 /* 61 /*
45 Delete company from watchlist 62 Delete company from watchlist.
46 */ 63 */
47 private func deleteCompany(at offsets: IndexSet) { 64 private func deleteCompany(at offsets: IndexSet) {
48 print(watchlistCompanies) 65 let watchlistCompaniesFiltered = watchlistCompanies.filter { $0.watchlistName == listName }
49 for index in offsets { 66 for index in offsets {
50 print(index) 67 let company = watchlistCompaniesFiltered[index]
51 let company = watchlistCompanies[index]
52 moc.delete(company) 68 moc.delete(company)
53 } 69 }
54 do { 70 do {
55 try moc.save() 71 try moc.save()
56 print("Company deleted") 72 print("Company deleted")
73 } catch {
74 print(error.localizedDescription)
75 }
76 }
77
78 /*
79 Remove entire list if it's not the last one.
80 */
81 private func deleteList() {
82 let watchlistCompaniesFiltered = watchlistCompanies.filter { $0.watchlistName == listName }
83 for company in watchlistCompaniesFiltered {
84 moc.delete(company)
85 }
86 do {
87 try moc.save()
88 print("List deleted")
89 watchlistSheetPresentationMode.wrappedValue.dismiss() /// Dismiss view
57 } catch { 90 } catch {
58 print(error.localizedDescription) 91 print(error.localizedDescription)
59 } 92 }
60 } 93 }
61 } 94 }
62 95
63 struct WatchlistSheet_Previews: PreviewProvider { 96 struct WatchlistSheet_Previews: PreviewProvider {
64 static var previews: some View { 97 static var previews: some View {
65 WatchlistSheet( 98 WatchlistSheet(
66 listName: "Most active", 99 listName: "Most active",
67 companies: [CompanyModel(symbol: "aapl", companyName: "Apple Inc", latestPrice: 120.3, changePercent: 0.03, intradayPrices: [120.3])] 100 apiCompanies: [CompanyModel(symbol: "aapl", companyName: "Apple Inc", latestPrice: 120.3, changePercent: 0.03, intradayPrices: [120.3])]
68 ) 101 )
69 } 102 }
70 } 103 }