# HG changeset patch # User Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> # Date 1615645944 -3600 # Node ID cd902f3f7f33239a3e86c8c896049ca99ff1e67e # Parent f9d6a324e3d2e274ebe7148ed7618eaf17a02215 Implement PriceView in CompanyRow diff -r f9d6a324e3d2 -r cd902f3f7f33 LazyBear/UI/CompanyRow.swift --- a/LazyBear/UI/CompanyRow.swift Sat Mar 13 14:23:05 2021 +0100 +++ b/LazyBear/UI/CompanyRow.swift Sat Mar 13 15:32:24 2021 +0100 @@ -11,6 +11,8 @@ var symbol: String var name: String var rowNumber: Int + var showPrice: Bool +// @Environment(\.editMode) var mode var body: some View { HStack { @@ -18,18 +20,29 @@ VStack(alignment: .leading) { Text(symbol.uppercased()) .fontWeight(.semibold) - + Text(name.capitalized) .lineLimit(1) } + + Spacer() + if showPrice { + PriceView(symbol: symbol, showVertical: true) + .animation(.easeInOut) + } } + +// if self.mode?.wrappedValue.isEditing ?? true { +// return PriceView(symbol: symbol) +// } + } } struct Row_Previews: PreviewProvider { static var previews: some View { List { - CompanyRow(symbol: "aapl", name: "apple inc", rowNumber: 2) + CompanyRow(symbol: "aapl", name: "apple inc", rowNumber: 2, showPrice: true) } } } diff -r f9d6a324e3d2 -r cd902f3f7f33 LazyBear/UI/CompanyView.swift --- a/LazyBear/UI/CompanyView.swift Sat Mar 13 14:23:05 2021 +0100 +++ b/LazyBear/UI/CompanyView.swift Sat Mar 13 15:32:24 2021 +0100 @@ -24,7 +24,7 @@ GeometryReader { geo in ScrollView { if companyOption.view == .stock { - PriceView(symbol: symbol) + PriceView(symbol: symbol, showVertical: false) ChartView(symbol: symbol, chartHeight: geo.size.width / 2) NewsView(symbol: symbol) diff -r f9d6a324e3d2 -r cd902f3f7f33 LazyBear/UI/PriceView.swift --- a/LazyBear/UI/PriceView.swift Sat Mar 13 14:23:05 2021 +0100 +++ b/LazyBear/UI/PriceView.swift Sat Mar 13 15:32:24 2021 +0100 @@ -9,28 +9,39 @@ struct PriceView: View { var symbol: String + var showVertical: Bool @State private var latestPrice = Float() @State private var changePercent = Double() @State private var negativeChange = false @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() // Set recurrent price request var body: some View { - VStack(alignment: .leading) { - HStack { - Text("\(latestPrice, specifier: "%.2f")") - .font(.title3) - .fontWeight(.bold) - .padding(.horizontal) + VStack { + if showVertical { + VStack(alignment: .trailing) { + Text("\(latestPrice, specifier: "%.2f")") + .fontWeight(.semibold) + .padding(.horizontal) - Text("\(changePercent*100, specifier: "%.2f")%") - .font(.headline) - .foregroundColor(negativeChange ? Color(.systemRed) : Color(.systemGreen)) - .padding(.trailing) - - Spacer() - + Text("\(changePercent*100, specifier: "%.2f")%") + .foregroundColor(negativeChange ? Color(.systemRed) : Color(.systemGreen)) + .padding(.trailing) + } + } else { + HStack { + Text("\(latestPrice, specifier: "%.2f")") + .font(.title3) + .fontWeight(.bold) + .padding(.horizontal) + + Text("\(changePercent*100, specifier: "%.2f")%") + .font(.headline) + .foregroundColor(negativeChange ? Color(.systemRed) : Color(.systemGreen)) + .padding(.trailing) + + Spacer() + } } - } .onReceive(timer) { _ in call(); print("requested") } .onAppear { @@ -56,6 +67,6 @@ struct PriceView_Previews: PreviewProvider { static var previews: some View { - PriceView(symbol: "aapl") + PriceView(symbol: "aapl", showVertical: false) } } diff -r f9d6a324e3d2 -r cd902f3f7f33 LazyBear/UI/Search.swift --- a/LazyBear/UI/Search.swift Sat Mar 13 14:23:05 2021 +0100 +++ b/LazyBear/UI/Search.swift Sat Mar 13 15:32:24 2021 +0100 @@ -22,7 +22,7 @@ NavigationLink(destination: CompanyView(name: name, symbol: symbol) .navigationTitle(symbol) ) { - CompanyRow(symbol: symbol, name: name, rowNumber: i % 5) + CompanyRow(symbol: symbol, name: name, rowNumber: i % 5, showPrice: true) } } .navigationBarSearch(self.$company) diff -r f9d6a324e3d2 -r cd902f3f7f33 LazyBear/UI/Watchlist.swift --- a/LazyBear/UI/Watchlist.swift Sat Mar 13 14:23:05 2021 +0100 +++ b/LazyBear/UI/Watchlist.swift Sat Mar 13 15:32:24 2021 +0100 @@ -16,13 +16,14 @@ var body: some View { NavigationView { List { - ForEach(companies.indices, id: \.self) { i in - let name = companies[i].name - let symbol = companies[i].symbol + ForEach(companies, id: \.symbol) { company in + let index = companies.firstIndex(of: company) + let name = company.name + let symbol = company.symbol NavigationLink(destination: CompanyView(name: name, symbol: symbol) .navigationTitle(symbol) ) { - CompanyRow(symbol: symbol, name: name, rowNumber: i % 5) + CompanyRow(symbol: symbol, name: name, rowNumber: index! % 5, showPrice: true) } } .onDelete(perform: removeCompany)