view LazyBear/Views/Company/CompanyView.swift @ 417:5f21f7c23c5e

Add comments and clean code
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 11 Jun 2021 11:37:42 +0200
parents c804ce7a1560
children 277197ce1416
line wrap: on
line source

//
//  CompanyView.swift
//  LazyBear
//
//  Created by Dennis Concepción Martín on 8/5/21.
//

import SwiftUI
import StockCharts

struct CompanyView: View {
    var symbol: String
    @ObservedObject var company = Company()
    @State private var showViewSelector = false
    
    /*
     Views
     */
    @State private var showChartView = true
    @State private var showInsiderView = false
    
    var body: some View {
        ScrollView {
            VStack {
                CompanyHeader(symbol: symbol, showViewSelector: $showViewSelector)
                    .padding(.bottom)

                if showChartView {
                    Chart(company: company, symbol: symbol)
                } else if showInsiderView {
                    Insiders(company: company, symbol: symbol)
                }
            }
            .padding()
        }
        .actionSheet(isPresented: $showViewSelector) {
            ActionSheet(title: Text("Select an option"), buttons: [
                .default(Text("Chart & News")) { resetViews(); showChartView = true },
                .default(Text("Insiders")) { resetViews(); showInsiderView = true },
                .cancel()
            ])
        }
    }
    
    /*
     Hide all views to show the one selected by user
     */
    private func resetViews() {
        showChartView = false
        showInsiderView = false
    }
}

struct CompanyView_Previews: PreviewProvider {
    static var previews: some View {
        CompanyView(symbol: "AAPL")
    }
}