Mercurial > public > lazybear
comparison LazyBear/Views/Profile/Helpers/WatchlistSheet.swift @ 432:3ca32ff79630
Fixes RenameSheetList bug
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Sat, 19 Jun 2021 20:13:25 +0200 |
parents | c78d5b5b3bda |
children | ffbb1dbab531 |
comparison
equal
deleted
inserted
replaced
431:871c10220a3d | 432:3ca32ff79630 |
---|---|
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 |
14 @Environment(\.presentationMode) private var watchlistSheetPresentation | 15 @Environment(\.presentationMode) private var watchlistSheetPresentation |
15 @Environment(\.managedObjectContext) private var moc | 16 @Environment(\.managedObjectContext) private var moc |
16 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) | 17 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) |
17 var watchlistCompanies: FetchedResults<WatchlistCompany> | 18 var watchlistCompanies: FetchedResults<WatchlistCompany> |
18 | 19 |
19 @State private var showDeleteListAlert = false | 20 @State private var showDeleteListAlert = false |
20 @State private var showRenameListSheet = false | |
21 | 21 |
22 var body: some View { | 22 var body: some View { |
23 NavigationView { | 23 NavigationView { |
24 VStack { | 24 VStack { |
25 List { | 25 List { |
32 } | 32 } |
33 .navigationTitle(listName) | 33 .navigationTitle(listName) |
34 .navigationBarTitleDisplayMode(.inline) | 34 .navigationBarTitleDisplayMode(.inline) |
35 .toolbar { | 35 .toolbar { |
36 ToolbarItem(placement: .navigationBarLeading) { | 36 ToolbarItem(placement: .navigationBarLeading) { |
37 Button(action: {watchlistSheetPresentation.wrappedValue.dismiss()}) { | 37 Button(action: { willRenameWatchlist = false; watchlistSheetPresentation.wrappedValue.dismiss()} ) { |
38 Image(systemName: "multiply") | 38 Image(systemName: "multiply") |
39 } | 39 } |
40 } | 40 } |
41 | 41 |
42 ToolbarItem(placement: .navigationBarTrailing) { | 42 ToolbarItem(placement: .navigationBarTrailing) { |
43 ToolbarMenu(showRenameListSheet: $showRenameListSheet, showDeleteListAlert: $showDeleteListAlert) | 43 Menu { |
44 Section { | |
45 Button(action: { willRenameWatchlist = true; watchlistSheetPresentation.wrappedValue.dismiss() }) { | |
46 Label("Rename list", systemImage: "square.and.pencil") | |
47 } | |
48 } | |
49 | |
50 if Set(watchlistCompanies.map { $0.watchlistName }).count > 1 { /// If there are only 1 watchlist (default) -> It cannot be deleted | |
51 Section(header: Text("Secondary actions")) { | |
52 Button(action: { showDeleteListAlert = true }) { | |
53 Label("Delete list", systemImage: "trash") | |
54 } | |
55 } | |
56 } | |
57 } | |
58 label: { | |
59 Label("Options", systemImage: "ellipsis.circle") | |
60 .imageScale(.large) | |
61 } | |
44 } | 62 } |
45 } | 63 } |
46 } | |
47 .sheet(isPresented: $showRenameListSheet) { | |
48 RenameListSheet(oldWatchlistName: listName) | |
49 .environment(\.managedObjectContext, self.moc) | |
50 } | 64 } |
51 .alert(isPresented: $showDeleteListAlert) { /// Show delete list alert | 65 .alert(isPresented: $showDeleteListAlert) { /// Show delete list alert |
52 Alert( | 66 Alert( |
53 title: Text("Are you sure you want to delete this list?"), | 67 title: Text("Are you sure you want to delete this list?"), |
54 message: Text("This action can't be undo"), | 68 message: Text("This action can't be undo"), |
95 | 109 |
96 struct WatchlistSheet_Previews: PreviewProvider { | 110 struct WatchlistSheet_Previews: PreviewProvider { |
97 static var previews: some View { | 111 static var previews: some View { |
98 WatchlistSheet( | 112 WatchlistSheet( |
99 listName: "Most active", | 113 listName: "Most active", |
100 apiCompanies: [CompanyModel(symbol: "aapl", companyName: "Apple Inc", latestPrice: 120.3, changePercent: 0.03, intradayPrices: [120.3])] | 114 apiCompanies: [CompanyModel(symbol: "aapl", companyName: "Apple Inc", latestPrice: 120.3, changePercent: 0.03, intradayPrices: [120.3])], |
115 willRenameWatchlist: .constant(false) | |
101 ) | 116 ) |
102 } | 117 } |
103 } | 118 } |