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