diff 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 diff
--- a/LazyBear/Views/Global Helpers/PriceView.swift	Tue Jun 08 11:46:58 2021 +0200
+++ b/LazyBear/Views/Global Helpers/PriceView.swift	Wed Jun 09 10:23:52 2021 +0200
@@ -13,20 +13,58 @@
     var style: PriceViewStyle
     
     var body: some View {
-        VStack(alignment: style.alignment) {
-            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)
+        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 {
@@ -34,11 +72,14 @@
             latestPrice: 120.30,
             changePercent: 0.03,
             style: PriceViewStyle(
-                alignment: .leading,
+                horizontalAlignment: .leading,
+                verticalAlignment: .center,
+                orientation: .VStack,
                 priceFont: .body,
                 priceFontWeight: .semibold,
                 percentFont: .callout,
-                percentFontWeight: .semibold
+                percentFontWeight: .semibold,
+                showBackground: true
             )
         )
     }