Mercurial > public > lazybear
view LazyBear/UI/CompanyView.swift @ 178:c1aa75608c27
Implement HUD
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Sun, 21 Feb 2021 17:38:23 +0100 |
parents | 8ed956c01a54 |
children | 4d677cfcaa91 |
line wrap: on
line source
// // CompanyView.swift // LazyBear // // Created by Dennis Concepción Martín on 19/2/21. // import SwiftUI struct CompanyView: View { @ObservedObject var hudManager: HUDManager @FetchRequest(entity: Company.entity(), sortDescriptors: []) var companies: FetchedResults<Company> @Environment(\.managedObjectContext) private var moc var name: String var symbol: String var body: some View { ScrollView { PriceView(symbol: symbol) NewsView(symbol: symbol) } .toolbar { ToolbarItem(placement: .principal) { Text("Change view") } ToolbarItem(placement: .navigationBarTrailing) { let symbols = companies.map { $0.symbol } if !symbols.contains(symbol) { Button(action: { add() }) { Image(systemName: "star") .imageScale(.large) } } } } } // Add to watchlist private func add() { let generator = UINotificationFeedbackGenerator() // Haptic let company = Company(context: moc) company.symbol = symbol company.name = name do { try moc.save() hudManager.show(text: "Company saved", image: "checkmark.circle") generator.notificationOccurred(.success) print("Company saved.") } catch { print(error.localizedDescription) } } } struct CompanyView_Previews: PreviewProvider { static var previews: some View { NavigationView { CompanyView(hudManager: HUDManager(), name: "apple inc", symbol: "aapl") } .navigationViewStyle(StackNavigationViewStyle()) } }