view LazyBear/Views/Profile/ProfileView.swift @ 389:db8bc3ed526a

Implementing add to watchlist feature from SearchView
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sun, 25 Apr 2021 16:42:26 +0200
parents 79c39987aaa4
children 6303385b3629
line wrap: on
line source

//
//  ProfileView.swift
//  LazyBear
//
//  Created by Dennis Concepción Martín on 4/4/21.
//

import SwiftUI
import CoreData

struct ProfileView: View {
    @ObservedObject var profile = Profile()
    @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
    var watchlistCompanies: FetchedResults<WatchlistCompany>
    
    // Refresh view when watchlistCompanies change
    @State private var refreshing = false
    private var didSave =  NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave)

    var body: some View {
        if profile.showView {
            NavigationView {
                List {
                    // Take all the different watchlist created
                    let watchlists = Set(watchlistCompanies.map { $0.watchlist })  // Set -> avoid duplicates names
                    ForEach(Array(watchlists), id: \.self) { watchlist in
                        
                        // Get all the symbols of this watchlist
                        let symbols = watchlistCompanies.filter({ $0.watchlist == watchlist }).map { $0.symbol }
                        
                        if let companies = profile.data.quotes {
                            let filteredCompanies = companies.filter({ symbols.contains($0.key) })
                            StockRow(listName: watchlist,
                                     list: filteredCompanies,
                                     intradayPrices: profile.data.intradayPrices,
                                     addOnDelete: true
                            )
                            .listRowInsets(EdgeInsets())
                        }
                    }
                    // The listener for refresh the view
                    .onReceive(self.didSave) { _ in
                        self.refreshing.toggle()
                    }
                }
                .navigationTitle("My profile")
                .navigationBarTitleDisplayMode(.inline)
                .toolbar {
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Button(action: {}) {
                            Image(systemName: "plus")
                        }
                    }
                }
            }
        } else {
            ProgressView()
                .onAppear { prepareUrl() }
        }
    }
    
    private func prepareUrl() {
        if watchlistCompanies.isEmpty {
            profile.showView = true
        } else {
            let symbols = watchlistCompanies.map { $0.symbol }  // Get symbols in watchlists
            var url = "https://api.lazybear.app/profile/type=init/symbols="

            var counter = 0
            for symbol in symbols {
                counter += 1
                if counter == 1 {
                    url += symbol
                } else {
                    url += ",\(symbol)"
                }
            }
            profile.request(url)
        }
    }
}

struct ProfileView_Previews: PreviewProvider {
    static var previews: some View {
        ProfileView()
    }
}