Mercurial > public > lazybear
comparison LazyBear/Views/Profile/Helpers/WatchlistSheet.swift @ 427:e707dbfc3115
Fixing weird animation .onDelete
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Wed, 16 Jun 2021 13:46:40 +0200 |
parents | |
children | 8c58ce834d95 |
comparison
equal
deleted
inserted
replaced
426:4ec307505b3a | 427:e707dbfc3115 |
---|---|
1 // | |
2 // WatchlistSheet.swift | |
3 // LazyBear | |
4 // | |
5 // Created by Dennis Concepción Martín on 16/6/21. | |
6 // | |
7 | |
8 import SwiftUI | |
9 | |
10 struct WatchlistSheet: View { | |
11 var listName: String | |
12 var companies: [CompanyModel] | |
13 | |
14 @Environment(\.presentationMode) private var watchlistSheetPresentation | |
15 @Environment(\.managedObjectContext) private var moc | |
16 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [NSSortDescriptor(key:"symbol", ascending:true)]) | |
17 var watchlistCompanies: FetchedResults<WatchlistCompany> | |
18 | |
19 /* | |
20 NSSortDescriptor and .sorted(by: {} in ForEach must coincide. If not .onDelete is not working well. | |
21 */ | |
22 var body: some View { | |
23 NavigationView { | |
24 VStack { | |
25 List { | |
26 ForEach(companies.sorted(by: { $0.companyName < $1.companyName }), id: \.self) { company in | |
27 StockSheetRow(company: company) | |
28 } | |
29 .onDelete(perform: deleteCompany) | |
30 } | |
31 } | |
32 .navigationTitle(listName) | |
33 .navigationBarTitleDisplayMode(.inline) | |
34 .toolbar { | |
35 ToolbarItem(placement: .navigationBarLeading) { | |
36 Button(action: {watchlistSheetPresentation.wrappedValue.dismiss()}) { | |
37 Image(systemName: "multiply") | |
38 } | |
39 } | |
40 } | |
41 } | |
42 } | |
43 | |
44 /* | |
45 Delete company from watchlist | |
46 */ | |
47 private func deleteCompany(at offsets: IndexSet) { | |
48 print(watchlistCompanies) | |
49 for index in offsets { | |
50 print(index) | |
51 let company = watchlistCompanies[index] | |
52 moc.delete(company) | |
53 } | |
54 do { | |
55 try moc.save() | |
56 print("Company deleted") | |
57 } catch { | |
58 print(error.localizedDescription) | |
59 } | |
60 } | |
61 } | |
62 | |
63 struct WatchlistSheet_Previews: PreviewProvider { | |
64 static var previews: some View { | |
65 WatchlistSheet( | |
66 listName: "Most active", | |
67 companies: [CompanyModel(symbol: "aapl", companyName: "Apple Inc", latestPrice: 120.3, changePercent: 0.03, intradayPrices: [120.3])] | |
68 ) | |
69 } | |
70 } |