comparison LazyBear/Views/Profile/Helpers/WatchlistSheet.swift @ 453:37c13ebda381

Improve hierarchy and minor bugs fixed
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Sun, 27 Jun 2021 14:18:29 +0200
parents ffbb1dbab531
children
comparison
equal deleted inserted replaced
452:bb69f9d1d20f 453:37c13ebda381
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 apiCompanies: [CompanyModel] 12 var apiCompanies: [CompanyModel]
13 @Binding var willRenameWatchlist: Bool 13
14
15 @Environment(\.presentationMode) private var watchlistSheetPresentation
16 @Environment(\.managedObjectContext) private var moc 14 @Environment(\.managedObjectContext) private var moc
17 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) 15 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
18 var watchlistCompanies: FetchedResults<WatchlistCompany> 16 var watchlistCompanies: FetchedResults<WatchlistCompany>
19 17
20 @State private var showDeleteListAlert = false 18 @State private var showDeleteListAlert = false
21 19
22 var body: some View { 20 var body: some View {
23 NavigationView { 21 VStack {
24 VStack { 22 List {
25 List { 23 ForEach(watchlistCompanies.filter { $0.watchlistName == listName }, id: \.self) { watchlistCompany in
26 ForEach(watchlistCompanies.filter { $0.watchlistName == listName }, id: \.self) { watchlistCompany in 24 let apiCompany = apiCompanies.first(where: { $0.symbol == watchlistCompany.symbol })
27 let apiCompany = apiCompanies.first(where: { $0.symbol == watchlistCompany.symbol }) 25 WatchlistSheetRow(apiCompany: apiCompany!, watchlistCompany: watchlistCompany)
28 WatchlistSheetRow(apiCompany: apiCompany!, watchlistCompany: watchlistCompany) 26 }
27 .onDelete(perform: deleteCompany)
28 }
29 }
30 .navigationTitle(listName)
31 .toolbar {
32 ToolbarItem(placement: .navigationBarLeading) {
33 // Button(action: { willRenameWatchlist = false; watchlistSheetPresentation.wrappedValue.dismiss()} ) {
34 // Image(systemName: "multiply")
35 // }
36 }
37
38 ToolbarItem(placement: .navigationBarTrailing) {
39 Menu {
40 Section {
41 // Button(action: { willRenameWatchlist = true; watchlistSheetPresentation.wrappedValue.dismiss() }) {
42 // Label("Rename list", systemImage: "square.and.pencil")
43 // }
29 } 44 }
30 .onDelete(perform: deleteCompany) 45
31 } 46 if Set(watchlistCompanies.map { $0.watchlistName }).count > 1 { /// If there are only 1 watchlist (default) -> It cannot be deleted
32 } 47 Section(header: Text("Secondary actions")) {
33 .navigationTitle(listName) 48 Button(action: { showDeleteListAlert = true }) {
34 .toolbar { 49 Label("Delete list", systemImage: "trash")
35 ToolbarItem(placement: .navigationBarLeading) {
36 Button(action: { willRenameWatchlist = false; watchlistSheetPresentation.wrappedValue.dismiss()} ) {
37 Image(systemName: "multiply")
38 }
39 }
40
41 ToolbarItem(placement: .navigationBarTrailing) {
42 Menu {
43 Section {
44 Button(action: { willRenameWatchlist = true; watchlistSheetPresentation.wrappedValue.dismiss() }) {
45 Label("Rename list", systemImage: "square.and.pencil")
46 }
47 }
48
49 if Set(watchlistCompanies.map { $0.watchlistName }).count > 1 { /// If there are only 1 watchlist (default) -> It cannot be deleted
50 Section(header: Text("Secondary actions")) {
51 Button(action: { showDeleteListAlert = true }) {
52 Label("Delete list", systemImage: "trash")
53 }
54 } 50 }
55 } 51 }
56 } 52 }
57 label: { 53 }
58 Label("Options", systemImage: "ellipsis.circle") 54 label: {
59 .imageScale(.large) 55 Label("Options", systemImage: "ellipsis.circle")
60 } 56 .imageScale(.large)
61 } 57 }
62 } 58 }
63 } 59 }
64 .alert(isPresented: $showDeleteListAlert) { /// Show delete list alert 60 .alert(isPresented: $showDeleteListAlert) { /// Show delete list alert
65 Alert( 61 Alert(
97 moc.delete(company) 93 moc.delete(company)
98 } 94 }
99 do { 95 do {
100 try moc.save() 96 try moc.save()
101 print("List deleted") 97 print("List deleted")
102 watchlistSheetPresentation.wrappedValue.dismiss() /// Dismiss view 98 // watchlistSheetPresentation.wrappedValue.dismiss() /// Dismiss view
103 } catch { 99 } catch {
104 print(error.localizedDescription) 100 print(error.localizedDescription)
105 } 101 }
106 } 102 }
107 } 103 }
108 104
109 struct WatchlistSheet_Previews: PreviewProvider { 105 struct WatchlistSheet_Previews: PreviewProvider {
110 static var previews: some View { 106 static var previews: some View {
111 WatchlistSheet( 107 WatchlistSheet(
112 listName: "Most active", 108 listName: "Most active",
113 apiCompanies: [CompanyModel(symbol: "aapl", companyName: "Apple Inc", latestPrice: 120.3, changePercent: 0.03, intradayPrices: [120.3])], 109 apiCompanies: [CompanyModel(symbol: "aapl", companyName: "Apple Inc", latestPrice: 120.3, changePercent: 0.03, intradayPrices: [120.3])]
114 willRenameWatchlist: .constant(false)
115 ) 110 )
116 } 111 }
117 } 112 }