view lazybear/Views/WatchlistRow.swift @ 121:c7532d18d6be

Fix bugs NavigationView
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sat, 06 Feb 2021 12:33:57 +0100
parents 2070372fea68
children 2d6df8debf12
line wrap: on
line source

//
//  WatchlistRow.swift
//  LazyBear
//
//  Created by Dennis Concepción Martín on 25/1/21.
//

import SwiftUI
import CoreData
import SDWebImageSwiftUI

struct WatchlistRow: View {
    var company: WatchlistCompany
    
    @EnvironmentObject var apiAccess: ApiAccess
    @Environment(\.editMode) var editMode  // EditButton list
    let persistenceController = PersistenceController.shared
    
    var body: some View {
        HStack {
            let url = apiAccess.results[0].url
            let path = "/iex/api/logos/\(company.symbol ?? "").png"
            let endpoint = url! + path
            
            WebImage(url: URL(string: endpoint))
                .resizable()
                .placeholder { LogoPlaceholder() }  // If there is no logo
                .indicator(.activity)
                .modifier(LogoModifier())
            
            VStack(alignment: .leading) {
                Text(company.symbol ?? "".uppercased())
                    .fontWeight(.semibold)
                
                Text(company.name ?? "".capitalized)
                    .font(.subheadline)
            }
            
            Spacer()
            if self.editMode?.wrappedValue.isEditing ?? true { } else { // If EditButton() is not clicked -> show prices
                Price(symbol: company.symbol ?? "", showHorizontal: false)
            }
        }
        .padding([.top, .bottom], 6)
    }
}

struct LogoModifier: ViewModifier {
    func body(content: Content) -> some View {
        content
            .aspectRatio(contentMode: .fit)
            .frame(maxWidth: 40, maxHeight: 40)
            .clipShape(RoundedRectangle(cornerRadius: 3))
    }
}


struct WatchlistRow_Previews: PreviewProvider {
    // Avoid preview crashing
    static let moc = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
    
    static var previews: some View {
        let watchlistCompany = WatchlistCompany(context: moc)
        watchlistCompany.name = "apple inc"
        watchlistCompany.symbol = "aapl"
        return WatchlistRow(company: watchlistCompany)
    }
}