comparison LazyBear/CompanyView.swift @ 465:6953d83060a4

New design
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Sat, 17 Jul 2021 17:58:57 +0100
parents
children
comparison
equal deleted inserted replaced
464:04e430ef254a 465:6953d83060a4
1 //
2 // CompanyView.swift
3 // lazybear
4 //
5 // Created by Dennis Concepción Martín on 17/07/2021.
6 //
7
8 import SwiftUI
9 import Alamofire
10 import MapKit
11
12 struct CompanyView: View {
13 var symbol: String
14 @State private var companyInfo = CompanyInfoModel()
15 @State private var companyQuote = CompanyQuoteModel()
16 @Environment(\.managedObjectContext) private var viewContext
17 @FetchRequest(sortDescriptors: [], animation: .default) private var companies: FetchedResults<Company>
18
19 var body: some View {
20 ScrollView {
21 VStack {
22 MapSection(state: companyInfo.state ?? "New York")
23
24 VStack(alignment: .leading) {
25 Text(companyInfo.symbol ?? "")
26 .font(.title)
27 .fontWeight(.semibold)
28 .foregroundColor(.primary)
29
30 HStack {
31 Text(companyInfo.companyName ?? "")
32 Spacer()
33 Text(companyInfo.state ?? "")
34 }
35 .font(.subheadline)
36 .foregroundColor(.secondary)
37
38 Divider()
39 StockSection(companyQuote: companyQuote)
40 .padding(.bottom)
41
42 AboutSection(companyInfo: companyInfo)
43
44 Spacer()
45 }
46 .padding()
47 }
48 }
49 .ignoresSafeArea(edges: .top)
50 .toolbar {
51 ToolbarItem(placement: .navigationBarTrailing) {
52 let watchlistSymbols = companies.map { $0.symbol }
53 if watchlistSymbols.contains(symbol) {
54 Button(action: deleteCompanyFromWatchlist) {
55 Image(systemName: "star.fill")
56 .font(.system(size: 25))
57 }
58
59 } else {
60 Button(action: addCompanyToWatchlist) {
61 Image(systemName: "star")
62 .font(.system(size: 25))
63 }
64 }
65 }
66 }
67 .onAppear(perform: requestApi)
68 }
69
70 // MARK: - REQUEST API
71 private func requestApi() {
72 // Request company information
73 var url = "https://cloud.iexapis.com/stable/stock/\(symbol)/company?token=pk_58fd944e924e4a70acf8635bc335cec4"
74
75 AF.request(url).responseDecodable(of: CompanyInfoModel.self) { response in
76 if let value = response.value {
77 self.companyInfo = value
78 } else {
79 // Handle error
80 }
81 }
82
83 // Request quote
84 url = "https://cloud.iexapis.com/stable/stock/\(symbol)/quote?token=pk_58fd944e924e4a70acf8635bc335cec4"
85 AF.request(url).responseDecodable(of: CompanyQuoteModel.self) { response in
86 if let value = response.value {
87 self.companyQuote = value
88 }
89 else {
90 // Handle error
91 }
92 }
93 }
94
95 // MARK: - ADD CORE DATA
96 private func addCompanyToWatchlist() {
97 withAnimation {
98 let company = Company(context: viewContext)
99 company.symbol = symbol
100 company.companyName = companyQuote.companyName!
101
102 do {
103 try viewContext.save()
104 } catch {
105 let nsError = error as NSError
106 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
107 }
108 }
109 }
110
111 // MARK: - DELETE CORE DATA
112 private func deleteCompanyFromWatchlist() {
113 withAnimation {
114 let company = companies.first(where: { $0.symbol == symbol })
115 viewContext.delete(company!)
116 do {
117 try viewContext.save()
118 } catch {
119 let nsError = error as NSError
120 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
121 }
122 }
123 }
124 }
125
126 struct CompanyView_Previews: PreviewProvider {
127 static var previews: some View {
128 NavigationView {
129 CompanyView(symbol: "AAPL")
130 }
131 }
132 }