Mercurial > public > lazybear
comparison 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 |
comparison
equal
deleted
inserted
replaced
412:a7c9dd0c5822 | 413:2984d8946342 |
---|---|
11 var latestPrice: Double | 11 var latestPrice: Double |
12 var changePercent: Double | 12 var changePercent: Double |
13 var style: PriceViewStyle | 13 var style: PriceViewStyle |
14 | 14 |
15 var body: some View { | 15 var body: some View { |
16 VStack(alignment: style.alignment) { | 16 if style.orientation == .VStack { |
17 Text("\(latestPrice, specifier: "%.2f")") | 17 VStack(alignment: style.horizontalAlignment) { |
18 .foregroundColor(changePercent < 0 ? .red: .green) | 18 Price(latestPrice: latestPrice, changePercent: changePercent, style: style) |
19 .font(style.priceFont) | 19 } |
20 .fontWeight(style.priceFontWeight) | 20 } else { |
21 | 21 HStack(alignment: style.verticalAlignment) { |
22 Text("\(changePercent*100, specifier: "%.2f")%") | 22 Price(latestPrice: latestPrice, changePercent: changePercent, style: style) |
23 .foregroundColor(changePercent < 0 ? .red: .green) | 23 } |
24 .font(style.percentFont) | 24 .if(style.showBackground) { content in |
25 .fontWeight(style.percentFontWeight) | 25 content |
26 .padding(10) | |
27 .background( | |
28 RoundedRectangle(cornerRadius: 15) | |
29 .foregroundColor(Color(.tertiarySystemBackground)) | |
30 ) | |
31 } | |
26 } | 32 } |
33 } | |
34 } | |
35 | |
36 /* | |
37 Apply modifiers to the passed view on some condition | |
38 */ | |
39 extension View { | |
40 @ViewBuilder | |
41 func `if`<Content: View>(_ conditional: Bool, content: (Self) -> Content) -> some View { | |
42 if conditional { | |
43 content(self) | |
44 } else { | |
45 self | |
46 } | |
47 } | |
48 } | |
49 | |
50 struct Price: View { | |
51 var latestPrice: Double | |
52 var changePercent: Double | |
53 var style: PriceViewStyle | |
54 | |
55 var body: some View { | |
56 Text("\(latestPrice, specifier: "%.2f")") | |
57 .foregroundColor(changePercent < 0 ? .red: .green) | |
58 .font(style.priceFont) | |
59 .fontWeight(style.priceFontWeight) | |
60 | |
61 Text("\(changePercent*100, specifier: "%.2f")%") | |
62 .foregroundColor(changePercent < 0 ? .red: .green) | |
63 .font(style.percentFont) | |
64 .fontWeight(style.percentFontWeight) | |
27 } | 65 } |
28 } | 66 } |
29 | 67 |
30 | 68 |
31 struct PriceView_Previews: PreviewProvider { | 69 struct PriceView_Previews: PreviewProvider { |
32 static var previews: some View { | 70 static var previews: some View { |
33 PriceView( | 71 PriceView( |
34 latestPrice: 120.30, | 72 latestPrice: 120.30, |
35 changePercent: 0.03, | 73 changePercent: 0.03, |
36 style: PriceViewStyle( | 74 style: PriceViewStyle( |
37 alignment: .leading, | 75 horizontalAlignment: .leading, |
76 verticalAlignment: .center, | |
77 orientation: .VStack, | |
38 priceFont: .body, | 78 priceFont: .body, |
39 priceFontWeight: .semibold, | 79 priceFontWeight: .semibold, |
40 percentFont: .callout, | 80 percentFont: .callout, |
41 percentFontWeight: .semibold | 81 percentFontWeight: .semibold, |
82 showBackground: true | |
42 ) | 83 ) |
43 ) | 84 ) |
44 } | 85 } |
45 } | 86 } |