Mercurial > public > lazybear
changeset 2:9f11c3e32bad
10/01/21
committer: Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Sun, 10 Jan 2021 11:45:14 +0000 |
parents | 07410566b51b |
children | 2da6e88699a5 3bd2e5d6e89d |
files | Company.swift ContentView.swift Insiders.swift Stock.swift |
diffstat | 4 files changed, 85 insertions(+), 50 deletions(-) [+] |
line wrap: on
line diff
--- a/Company.swift Wed Jan 06 18:43:39 2021 +0000 +++ b/Company.swift Sun Jan 10 11:45:14 2021 +0000 @@ -21,9 +21,10 @@ VStack { if showingMain { Stock(cik: cik, symbol: symbol, name: name) - } else { + } + else { Insiders(cik: cik, symbol: symbol, name: name) - } + } // Start bottom selection Rectangle()
--- a/ContentView.swift Wed Jan 06 18:43:39 2021 +0000 +++ b/ContentView.swift Sun Jan 10 11:45:14 2021 +0000 @@ -27,7 +27,7 @@ } Spacer() - + /* Button(action: {self.showingUser.toggle() }) { Image(systemName: "person") @@ -35,6 +35,7 @@ }.sheet(isPresented: $showingUser) { User() } + */ } .transition(.move(edge: .top)) .animation(.default)
--- a/Insiders.swift Wed Jan 06 18:43:39 2021 +0000 +++ b/Insiders.swift Sun Jan 10 11:45:14 2021 +0000 @@ -26,9 +26,9 @@ @State private var selectedDate = Date().addingTimeInterval(-6*30*24*60*60) // month*days*hours*minutes*seconds var body: some View { - GeometryReader { geo in - VStack { - if transaction.showingView { + if transaction.showingView { + GeometryReader { geo in + VStack { // Graph let cumBuys = cumSum(array: getTransactions(acquisitionOrDisposition: "A")) let cumSells = cumSum(array: getTransactions(acquisitionOrDisposition: "D")) @@ -52,18 +52,22 @@ } .offset(y: 10) } - else { - Spacer() - ProgressView() - Spacer() - } + } + } + else { + VStack { + Spacer() + ProgressView() + Spacer() } .onAppear { transaction.request(cik: String(cik), date: dateFormatter.string(from: selectedDate)) } .alert(isPresented: $transaction.showingAlert) { - Alert(title: Text("There is no data available"), message: Text("We have no data about this company. Try another one."), dismissButton: .default(Text("Got it!"))) - } + Alert(title: Text("There is no data available"), + message: Text("We have no data about this company. Try another one."), + dismissButton: .default(Text("Got it!"))) + } } } // Function to sum an array and return a cumulative sum array
--- a/Stock.swift Wed Jan 06 18:43:39 2021 +0000 +++ b/Stock.swift Sun Jan 10 11:45:14 2021 +0000 @@ -10,64 +10,74 @@ struct Stock: View { @ObservedObject var price = Price() + @State var isFavourite = UserDefaults.standard // Save favourite company in default settings // Company @State var cik: Int @State var symbol: String @State var name: String + var body: some View { + if price.showingView { GeometryReader { geo in VStack { - if price.showingView { - HStack { - Text("$ " + String(price.result.last!.close)) - .font(.title) - .fontWeight(.bold) - - let pct = price.result.last!.changePercent * 100 - Text(String(format: "%.2f", pct) + " %") - .font(.headline) - .foregroundColor(whichColor()) - - Spacer() - } - .padding([.leading, .top]) + HStack { + Text("$ " + String(price.result.last!.close)) + .font(.title) + .fontWeight(.bold) - HStack { - Text(String(price.result.last!.date) + " last price") - .font(.caption) - .padding([.leading]) - .opacity(0.5) - Spacer() - } - - // Stock Price - let width = geo.size.height*0.6 - let prices = price.result.map { $0.close } // Get an array of a variable in the struct - LineChartView(data: prices, title: "Stock price", legend: "Last 20 days", form: CGSize(width: width, height: width/2), rateValue: nil) - .padding() + let pct = price.result.last!.changePercent * 100 + Text(String(format: "%.2f", pct) + " %") + .font(.headline) + .foregroundColor(whichColor()) Spacer() + - // Volume - let volume = price.result.map { $0.volume } - BarChartView(data: ChartData(points: volume), title: "Volume", form: CGSize(width: width, height: width/2)) + + } + .padding([.leading, .top, .trailing]) + + HStack { + Text(String(price.result.last!.date) + " last price") + .font(.caption) + .padding([.leading]) + .opacity(0.5) + + Spacer() + } + + // Stock Price + let width = geo.size.height*0.6 + let prices = price.result.map { $0.close } // Get an array of a variable in the struct + LineChartView(data: prices, title: "Stock price", legend: "Last 20 days", form: CGSize(width: width, height: width/1.8), rateValue: nil) + .padding() + + Spacer() + + // Volume + let volume = price.result.map { $0.volume } + BarChartView(data: ChartData(points: volume), title: "Volume", form: CGSize(width: width, height: width/1.6)) .padding() } - else { - Spacer() - ProgressView() - Spacer() - } + } + } + else { + VStack{ + Spacer() + ProgressView() + Spacer() } .onAppear { price.request(symbol: symbol) } .alert(isPresented: $price.showingAlert) { - Alert(title: Text("There is no data available"), message: Text("We have no data about this company. Try another one."), dismissButton: .default(Text("Got it!"))) - } + Alert(title: Text("There is no data available"), + message: Text("We have no data about this company. Try another one."), + dismissButton: .default(Text("Got it!"))) + } } } @@ -79,6 +89,25 @@ return Color(.green) } } + + func favourite() { + if isFavourite.bool(forKey: String(cik)) { + isFavourite.set(false, forKey: String(cik)) + + } + else { + isFavourite.set(true, forKey: String(cik)) + + } + } +} +extension Image { + func favouriteIcon() -> some View { + self + .resizable() + .frame(width: 25, height: 25) + .foregroundColor(.yellow) + } } struct Stock_Previews: PreviewProvider {