changeset 262:cd902f3f7f33

Implement PriceView in CompanyRow
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sat, 13 Mar 2021 15:32:24 +0100
parents f9d6a324e3d2
children 2f9845d59a59
files LazyBear/UI/CompanyRow.swift LazyBear/UI/CompanyView.swift LazyBear/UI/PriceView.swift LazyBear/UI/Search.swift LazyBear/UI/Watchlist.swift
diffstat 5 files changed, 48 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- 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)
         }
     }
 }
--- 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)
                         
--- 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)
     }
 }
--- 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)
--- 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)