Mercurial > public > lazybear
comparison LazyBear/Views/Global Helpers/Extensive List/ExtensiveList.swift @ 417:5f21f7c23c5e
Add comments and clean code
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Fri, 11 Jun 2021 11:37:42 +0200 |
parents | LazyBear/Views/Global Helpers/Company list/ExtensiveList.swift@a0cf8fe47044 |
children | 9b7af8e83d12 |
comparison
equal
deleted
inserted
replaced
416:1662a41e2c1a | 417:5f21f7c23c5e |
---|---|
1 // | |
2 // ExtensiveList.swift | |
3 // LazyBear | |
4 // | |
5 // Created by Dennis Concepción Martín on 14/4/21. | |
6 // | |
7 | |
8 import SwiftUI | |
9 | |
10 struct ExtensiveList: View { | |
11 var listName: String | |
12 var list: [String: QuoteModel]? | |
13 var intradayPrices: [String: [IntradayPriceModel]]? | |
14 var latestCurrencies: [String: CurrencyModel]? | |
15 var addOnDelete: Bool | |
16 | |
17 @Environment(\.presentationMode) private var presentationMode | |
18 @Environment(\.managedObjectContext) private var moc | |
19 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) | |
20 var watchlistCompany: FetchedResults<WatchlistCompany> | |
21 | |
22 @State private var isEditMode: EditMode = .inactive | |
23 @State private var showRenameListAction = false | |
24 @State private var showDeleteListAlert = false | |
25 @State private var showSearchView = false | |
26 | |
27 var body: some View { | |
28 NavigationView { | |
29 ZStack { | |
30 VStack { | |
31 if let list = list { | |
32 List { | |
33 ForEach(Array(list.keys.sorted()), id: \.self) { companySymbol in | |
34 StockItem(symbol: companySymbol, | |
35 company: list[companySymbol]!, | |
36 intradayPrices: intradayPrices?[companySymbol], | |
37 orientation: .horizontal, | |
38 hidePriceView: self.isEditMode == .active // Hide on EditMode | |
39 ) | |
40 | |
41 } | |
42 .onDelete(perform: addOnDelete ? deleteCompany: nil) | |
43 } | |
44 } | |
45 | |
46 if let latestCurrencies = latestCurrencies { | |
47 List(Array(latestCurrencies.keys.sorted()), id: \.self) { currencySymbol in | |
48 CurrencyListItem(currencySymbol: currencySymbol, currency: latestCurrencies[currencySymbol]!) | |
49 | |
50 } | |
51 } | |
52 } | |
53 | |
54 // Blur background | |
55 Color(.black) | |
56 .edgesIgnoringSafeArea(.all) | |
57 .opacity(showRenameListAction ? 0.2: 0) | |
58 .animation(.easeInOut) | |
59 .onTapGesture { showRenameListAction = false } | |
60 | |
61 // Show rename Action Sheet | |
62 TextfieldAlert(listName: listName, showRenameAction: $showRenameListAction, presentationMode: presentationMode) | |
63 .offset(y: showRenameListAction ? 0: 700) | |
64 .animation(.easeInOut) | |
65 } | |
66 // Show delete list alert | |
67 .alert(isPresented: $showDeleteListAlert) { | |
68 Alert( | |
69 title: Text("Are you sure you want to delete this list?"), | |
70 message: Text("This action can't be undo"), | |
71 primaryButton: .destructive(Text("Delete")) { deleteList() }, | |
72 secondaryButton: .cancel() | |
73 ) | |
74 } | |
75 .navigationTitle(listName) | |
76 .navigationBarTitleDisplayMode(.inline) | |
77 .toolbar { | |
78 ToolbarItem(placement: .cancellationAction) { | |
79 if addOnDelete { | |
80 EditButton() | |
81 } else { | |
82 Button(action: { presentationMode.wrappedValue.dismiss() }) { | |
83 Image(systemName: "multiply") | |
84 .imageScale(.large) | |
85 } | |
86 } | |
87 } | |
88 ToolbarItem(placement: .navigationBarTrailing) { | |
89 if addOnDelete { | |
90 ToolbarMenu(showRenameListAction: $showRenameListAction, showDeleteListAlert: $showDeleteListAlert) | |
91 } | |
92 } | |
93 } | |
94 .environment(\.editMode, self.$isEditMode) // Always after Toolbar | |
95 } | |
96 } | |
97 | |
98 /* | |
99 Delete company from watchlist | |
100 */ | |
101 private func deleteCompany(at offsets: IndexSet) { | |
102 for index in offsets { | |
103 let company = watchlistCompany[index] | |
104 moc.delete(company) | |
105 } | |
106 do { | |
107 try moc.save() | |
108 print("Company deleted") | |
109 } catch { | |
110 print(error.localizedDescription) | |
111 } | |
112 } | |
113 | |
114 /* | |
115 Remove entire list if it's not the last one. It can't be zero watchlists | |
116 */ | |
117 private func deleteList() { | |
118 let selectedWatchlist = watchlistCompany.filter({ $0.watchlist == listName }) | |
119 for company in selectedWatchlist { | |
120 moc.delete(company) | |
121 } | |
122 do { | |
123 try moc.save() | |
124 print("List deleted") | |
125 presentationMode.wrappedValue.dismiss() // Dismiss view | |
126 } catch { | |
127 print(error.localizedDescription) | |
128 } | |
129 } | |
130 } | |
131 | |
132 struct ExtensiveList_Previews: PreviewProvider { | |
133 static var previews: some View { | |
134 ExtensiveList(listName: "List name", addOnDelete: false) | |
135 } | |
136 } |