Mercurial > public > lazybear
view LazyBear/Views/Global Helpers/StockItem.swift @ 421:9b7af8e83d12
Change intraday prices model
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Sat, 12 Jun 2021 15:20:04 +0200 |
parents | 2984d8946342 |
children |
line wrap: on
line source
// // StockItem.swift // LazyBear // // Created by Dennis Concepción Martín on 28/3/21. // import SwiftUI import StockCharts enum OrientationView { case horizontal, vertical } struct StockItem: View { var symbol: String var company: QuoteModel var intradayPrices: [Double]? var orientation: OrientationView var hidePriceView: Bool? var body: some View { if orientation == .vertical { let verticalRow: AnyView = AnyView( NavigationLink(destination: CompanyView(symbol: symbol)) { VerticalStockRow(symbol: symbol, company: company, intradayPrices: intradayPrices) } .buttonStyle(PlainButtonStyle()) ) return verticalRow } else { let horizontalRow: AnyView = AnyView( NavigationLink(destination: CompanyView(symbol: symbol)) { HorizontalStockRow(symbol: symbol, company: company, intradayPrices: intradayPrices, hidePriceView: hidePriceView ?? false) } ) return horizontalRow } } } struct StockItem_Previews: PreviewProvider { static var previews: some View { StockItem( symbol: "AAPL", company: QuoteModel(changePercent: 0.03, companyName: "apple inc", latestPrice: 130.3), intradayPrices: [130.3, 132.3], orientation: .horizontal ) } } struct VerticalStockRow: View { var symbol: String var company: QuoteModel var intradayPrices: [Double]? var body: some View { RoundedRectangle(cornerRadius: 20) .foregroundColor(Color(.secondarySystemBackground)) .aspectRatio(0.8, contentMode: .fit) .clipShape(RoundedRectangle(cornerRadius: 20)) .overlay( VStack(alignment: .leading) { Group { Text(symbol.uppercased()) .fontWeight(.semibold) .padding(.top) Text(company.companyName.capitalized) .font(.callout) .fontWeight(.semibold) .opacity(0.6) .lineLimit(1) let priceViewStyle = PriceViewStyle( horizontalAlignment: .leading, verticalAlignment: .center, orientation: .VStack, priceFont: .body, priceFontWeight: .semibold, percentFont: .callout, percentFontWeight: .semibold, showBackground: false ) PriceView(latestPrice: company.latestPrice ?? 0, changePercent: company.changePercent ?? 0, style: priceViewStyle) .padding(.top) } .padding(.horizontal) Spacer() if let prices = intradayPrices { LineChartView(data: prices, dates: nil, hours: nil, dragGesture: false) .padding(.vertical) .clipShape(RoundedRectangle(cornerRadius: 20)) } } ,alignment: .leading ) } } struct HorizontalStockRow: View { var symbol: String var company: QuoteModel var intradayPrices: [Double]? var hidePriceView: Bool var body: some View { HStack { VStack(alignment: .leading) { Text(symbol.uppercased()) .fontWeight(.semibold) Text(company.companyName.capitalized) .font(.callout) .fontWeight(.semibold) .opacity(0.6) .lineLimit(1) } Spacer() if !hidePriceView { if let prices = intradayPrices { LineChartView(data: prices, dates: nil, hours: nil, dragGesture: false) .frame(width: 80) .padding(.vertical, 10) .padding(.leading) } let priceViewStyle = PriceViewStyle( horizontalAlignment: .leading, verticalAlignment: .center, orientation: .VStack, priceFont: .body, priceFontWeight: .semibold, percentFont: .callout, percentFontWeight: .semibold, showBackground: false ) PriceView(latestPrice: company.latestPrice ?? 0, changePercent: company.changePercent ?? 0, style: priceViewStyle) // Center PriceView with the other rows .frame(minWidth: 80, alignment: .trailing) } } .padding(5) } }