comparison LazyBear/Views/Global Helpers/Company list/ExtensiveList.swift @ 389:db8bc3ed526a

Implementing add to watchlist feature from SearchView
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sun, 25 Apr 2021 16:42:26 +0200
parents LazyBear/Views/Global Helpers/ExtensiveList.swift@79c39987aaa4
children 6303385b3629
comparison
equal deleted inserted replaced
388:79c39987aaa4 389:db8bc3ed526a
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 showRenameAction = false
24 @State private var showDeleteAlert = 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(showRenameAction ? 0.2: 0)
58 .animation(.easeInOut)
59 .onTapGesture { showRenameAction = false }
60
61 // Show rename Action Sheet
62 RenameSheet(listName: listName, showRenameAction: $showRenameAction, presentationMode: presentationMode)
63 .offset(y: showRenameAction ? 0: 700)
64 .animation(.easeInOut)
65 }
66 // Show delete list alert
67 .alert(isPresented: $showDeleteAlert) {
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 .sheet(isPresented: $showSearchView) {
76 SearchView()
77 }
78 .navigationTitle(listName)
79 .navigationBarTitleDisplayMode(.inline)
80 .toolbar {
81 ToolbarItem(placement: .cancellationAction) {
82 if addOnDelete {
83 EditButton()
84 } else {
85 Button(action: { presentationMode.wrappedValue.dismiss() }) {
86 Image(systemName: "multiply")
87 .imageScale(.large)
88 }
89 }
90 }
91 ToolbarItem(placement: .navigationBarTrailing) {
92 if addOnDelete {
93 ToolbarMenu(showRenameAction: $showRenameAction,
94 showSearchView: $showSearchView,
95 showDeleteAlert: $showDeleteAlert
96 )
97 }
98 }
99 }
100 .environment(\.editMode, self.$isEditMode) // Always after Toolbar
101 }
102 }
103
104 // Delete company from watchlist
105 private func deleteCompany(at offsets: IndexSet) {
106 for index in offsets {
107 let company = watchlistCompany[index]
108 moc.delete(company)
109 }
110 do {
111 try moc.save()
112 print("Company deleted")
113 } catch {
114 print(error.localizedDescription)
115 }
116 }
117
118 // Remove entire watchlist
119 private func deleteList() {
120 let selectedWatchlist = watchlistCompany.filter({ $0.watchlist == listName })
121 for company in selectedWatchlist {
122 moc.delete(company)
123 }
124 do {
125 try moc.save()
126 print("List deleted")
127 presentationMode.wrappedValue.dismiss() // Dismiss view
128 } catch {
129 print(error.localizedDescription)
130 }
131 }
132 }
133
134 struct ExtensiveList_Previews: PreviewProvider {
135 static var previews: some View {
136 ExtensiveList(listName: "List name", addOnDelete: false)
137 }
138 }