view LazyBear/UI/Watchlist.swift @ 184:ee9f3aff962f

Minor UI updates
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sun, 21 Feb 2021 19:42:00 +0100
parents c1aa75608c27
children 062fcab540ee
line wrap: on
line source

//
//  Watchlist.swift
//  LazyBear
//
//  Created by Dennis Concepción Martín on 19/2/21.
//

import SwiftUI

struct Watchlist: View {
    @ObservedObject var hudManager: HUDManager
    @Environment(\.managedObjectContext) private var moc
    @FetchRequest(entity: Company.entity(), sortDescriptors: []) var companies: FetchedResults<Company>
    
    var body: some View {
        NavigationView {
            List {
                ForEach(companies, id: \.self) { company in
                    NavigationLink(destination: CompanyView(hudManager: hudManager, name: company.name, symbol: company.symbol)
                                    .navigationTitle(company.symbol)
                    ) {
                        CompanyRow(symbol: company.symbol, name: company.name)
                    }
                }
                .onDelete(perform: removeCompany)
            }
            .navigationTitle("Watchlist 👀")
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    EditButton()
                }
            }
        }
    }
    
    private func removeCompany(at offsets: IndexSet) {
        for index in offsets {
            let company = companies[index]
            moc.delete(company)
        }
        do {
            try moc.save()
        } catch {
            // Error
        }
    }
}

struct Watchlist_Previews: PreviewProvider {
    static var previews: some View {
        Watchlist(hudManager: HUDManager())
    }
}