comparison LazyBear/Views/Global Helpers/ExtensiveList.swift @ 388:79c39987aaa4

Implementing Watchlists in ProfileView
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sat, 24 Apr 2021 17:44:02 +0200
parents c206bd0bdb4e
children
comparison
equal deleted inserted replaced
387:c206bd0bdb4e 388:79c39987aaa4
12 var list: [String: QuoteModel]? 12 var list: [String: QuoteModel]?
13 var intradayPrices: [String: [IntradayPriceModel]]? 13 var intradayPrices: [String: [IntradayPriceModel]]?
14 var latestCurrencies: [String: CurrencyModel]? 14 var latestCurrencies: [String: CurrencyModel]?
15 var addOnDelete: Bool 15 var addOnDelete: Bool
16 16
17 @Environment(\.presentationMode) private var extensiveListPresent 17 @Environment(\.presentationMode) private var presentationMode
18 @Environment(\.managedObjectContext) private var moc
19 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
20 var watchlistCompany: FetchedResults<WatchlistCompany>
21
18 @State private var isEditMode: EditMode = .inactive 22 @State private var isEditMode: EditMode = .inactive
19 @State private var showRenameAction = false 23 @State private var showRenameAction = false
20 @State private var showDeleteAlert = false 24 @State private var showDeleteAlert = false
21 25
22 var body: some View { 26 var body: some View {
32 orientation: .horizontal, 36 orientation: .horizontal,
33 hidePriceView: self.isEditMode == .active // Hide on EditMode 37 hidePriceView: self.isEditMode == .active // Hide on EditMode
34 ) 38 )
35 39
36 } 40 }
37 .onDelete(perform: addOnDelete ? removeCompany: nil) 41 .onDelete(perform: addOnDelete ? deleteCompany: nil)
38 } 42 }
39 } 43 }
40 44
41 if let latestCurrencies = latestCurrencies { 45 if let latestCurrencies = latestCurrencies {
42 List(Array(latestCurrencies.keys.sorted()), id: \.self) { currencySymbol in 46 List(Array(latestCurrencies.keys.sorted()), id: \.self) { currencySymbol in
52 .opacity(showRenameAction ? 0.2: 0) 56 .opacity(showRenameAction ? 0.2: 0)
53 .animation(.easeInOut) 57 .animation(.easeInOut)
54 .onTapGesture { showRenameAction = false } 58 .onTapGesture { showRenameAction = false }
55 59
56 // Show rename Action Sheet 60 // Show rename Action Sheet
57 RenameSheet(showAction: $showRenameAction) 61 RenameSheet(listName: listName, showRenameAction: $showRenameAction, presentationMode: presentationMode)
58 .offset(y: showRenameAction ? 0: 700) 62 .offset(y: showRenameAction ? 0: 700)
59 .animation(.easeInOut) 63 .animation(.easeInOut)
60 } 64 }
61 // Show delete list alert 65 // Show delete list alert
62 .alert(isPresented: $showDeleteAlert) { 66 .alert(isPresented: $showDeleteAlert) {
63 Alert( 67 Alert(
64 title: Text("Are you sure you want to delete this list?"), 68 title: Text("Are you sure you want to delete this list?"),
65 message: Text("This action can't be undo"), 69 message: Text("This action can't be undo"),
66 primaryButton: .destructive(Text("Delete")) { 70 primaryButton: .destructive(Text("Delete")) { deleteList() },
67 print("Deleting...")
68 },
69 secondaryButton: .cancel() 71 secondaryButton: .cancel()
70 ) 72 )
71 } 73 }
72 .navigationTitle(listName) 74 .navigationTitle(listName)
73 .navigationBarTitleDisplayMode(.inline) 75 .navigationBarTitleDisplayMode(.inline)
74 .toolbar { 76 .toolbar {
75 ToolbarItem(placement: .cancellationAction) { 77 ToolbarItem(placement: .cancellationAction) {
76 if addOnDelete { 78 if addOnDelete {
77 EditButton() 79 EditButton()
78 } else { 80 } else {
79 Button(action: { extensiveListPresent.wrappedValue.dismiss() }) { 81 Button(action: { presentationMode.wrappedValue.dismiss() }) {
80 Image(systemName: "multiply") 82 Image(systemName: "multiply")
81 .imageScale(.large) 83 .imageScale(.large)
82 } 84 }
83 } 85 }
84 } 86 }
109 } 111 }
110 } 112 }
111 .environment(\.editMode, self.$isEditMode) // Always after Toolbar 113 .environment(\.editMode, self.$isEditMode) // Always after Toolbar
112 } 114 }
113 } 115 }
114 private func removeCompany(at offsets: IndexSet) { 116
115 print("Hello") 117 // Delete company from watchlist
118 private func deleteCompany(at offsets: IndexSet) {
119 for index in offsets {
120 let company = watchlistCompany[index]
121 moc.delete(company)
122 }
123 do {
124 try moc.save()
125 print("Company deleted")
126 } catch {
127 // Error
128 }
129 }
130
131 // Remove entire watchlist
132 private func deleteList() {
133 let selectedWatchlist = watchlistCompany.filter({ $0.watchlist == listName })
134 for company in selectedWatchlist {
135 moc.delete(company)
136 }
137 do {
138 try moc.save()
139 print("List deleted")
140 presentationMode.wrappedValue.dismiss() // Dismiss view
141 } catch {
142 print(error.localizedDescription)
143 }
116 } 144 }
117 } 145 }
118 146
119 struct ExtensiveList_Previews: PreviewProvider { 147 struct ExtensiveList_Previews: PreviewProvider {
120 static var previews: some View { 148 static var previews: some View {