view LazyBear/Views/Home/Helpers/StockRow.swift @ 428:8c58ce834d95

Bug fixes and change assets
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 18 Jun 2021 12:43:17 +0200
parents 4effac4733b0
children 3ca32ff79630
line wrap: on
line source

//
//  StockRow.swift
//  LazyBear
//
//  Created by Dennis Concepción Martín on 28/3/21.
//

import SwiftUI


struct StockRow: View {
    var listName: String
    var companies: [CompanyModel]
    var showWatchlistSheet: Bool?

    @State private var showList = false
    @Environment(\.managedObjectContext) private var moc
    
    var body: some View {
        VStack(alignment: .leading) {
            HStack(alignment: .bottom) {
                VStack(alignment: .leading) {
                    Text(adaptListTitle(listName))
                        .font(.title3)
                        .fontWeight(.semibold)
                        .padding([.top, .horizontal])
                    
                    Text("Real-time quotes")
                        .font(.caption)
                        .opacity(0.5)
                        .padding(.horizontal)
                }
                
                Spacer()
                Button("See all", action: { showList = true })
                    .buttonStyle(BorderlessButtonStyle())
                    .padding(.horizontal)
            }
            
            ScrollView(.horizontal, showsIndicators: false) {
                HStack(spacing: 20) {
                    ForEach(companies, id: \.self) { company in
                       StockItem(company: company)
                    }
                }
                .padding()
            }
            .frame(height: 250)
        }
        .padding(.bottom)
        .sheet(isPresented: $showList) {
            if showWatchlistSheet ?? false {
                WatchlistSheet(listName: listName, apiCompanies: companies)
                    .environment(\.managedObjectContext, self.moc)
            } else {
                StockSheet(listName: adaptListTitle(listName), companies: companies)
            }
        }
    }
    
    /*
     Get list keys (mostactive, losers, active) and adapt them to diplay
     */
    private func adaptListTitle(_ title: String) -> String {
        if title == "mostactive" {
            return "Most active"
        } else {
            return title.capitalized
        }
    }
}


struct StockRectangleRow_Previews: PreviewProvider {
    static var previews: some View {
        StockRow(
            listName: "mostactive",
             companies: [CompanyModel(symbol: "aapl", companyName: "Apple Inc", latestPrice: 120.3, changePercent: 0.03, intradayPrices: [120.3])]
        )
    }
}