Mercurial > public > lazybear
view LazyBear/Views/Global Helpers/PriceView.swift @ 413:2984d8946342
Minor UI changes
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Wed, 09 Jun 2021 10:23:52 +0200 |
parents | dc8dccd18e86 |
children |
line wrap: on
line source
// // PriceView.swift // LazyBear // // Created by Dennis Concepción Martín on 3/4/21. // import SwiftUI struct PriceView: View { var latestPrice: Double var changePercent: Double var style: PriceViewStyle var body: some View { if style.orientation == .VStack { VStack(alignment: style.horizontalAlignment) { Price(latestPrice: latestPrice, changePercent: changePercent, style: style) } } else { HStack(alignment: style.verticalAlignment) { Price(latestPrice: latestPrice, changePercent: changePercent, style: style) } .if(style.showBackground) { content in content .padding(10) .background( RoundedRectangle(cornerRadius: 15) .foregroundColor(Color(.tertiarySystemBackground)) ) } } } } /* Apply modifiers to the passed view on some condition */ extension View { @ViewBuilder func `if`<Content: View>(_ conditional: Bool, content: (Self) -> Content) -> some View { if conditional { content(self) } else { self } } } struct Price: View { var latestPrice: Double var changePercent: Double var style: PriceViewStyle var body: some View { Text("\(latestPrice, specifier: "%.2f")") .foregroundColor(changePercent < 0 ? .red: .green) .font(style.priceFont) .fontWeight(style.priceFontWeight) Text("\(changePercent*100, specifier: "%.2f")%") .foregroundColor(changePercent < 0 ? .red: .green) .font(style.percentFont) .fontWeight(style.percentFontWeight) } } struct PriceView_Previews: PreviewProvider { static var previews: some View { PriceView( latestPrice: 120.30, changePercent: 0.03, style: PriceViewStyle( horizontalAlignment: .leading, verticalAlignment: .center, orientation: .VStack, priceFont: .body, priceFontWeight: .semibold, percentFont: .callout, percentFontWeight: .semibold, showBackground: true ) ) } }