view Simoleon/FavoritesView.swift @ 160:0c589138a6f3

Implement Conversion Box
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Sun, 29 Aug 2021 19:04:34 +0100
parents 84137052813d
children e4cbb1eea394
line wrap: on
line source

////
////  FavoritesView.swift
////  Simoleon
////
////  Created by Dennis Concepción Martín on 19/07/2021.
////
//
//import SwiftUI
//
//struct FavoritesView: View {
//    @Environment(\.managedObjectContext) private var viewContext
//    @FetchRequest(
//        sortDescriptors: [NSSortDescriptor(keyPath: \Favorite.currencyPair, ascending: true)],
//        animation: .default) private var favorites: FetchedResults<Favorite>
//    
//    var body: some View {
//        VStack {
//            if favorites.isEmpty {
//                Group {
//                    Image(systemName: "star")
//                        .font(.title)
//                    
//                    Text("Search a currency pair and add it to favorites.")
//                        .padding(.top, 5)
//                }
//                .multilineTextAlignment(.center)
//                .foregroundColor(.secondary)
//                .padding(.horizontal, 50)
//            } else {
//                List {
//                    ForEach(favorites) { favorite in
//                        NavigationLink(destination: Conversion()) {
////                            CurrencyRow(currencyPairName: favorite.currencyPair)
//                        }
//                    }
//                    .onDelete(perform: removeFromFavorites)
//                }
//                .listStyle(PlainListStyle())
//                .accessibilityIdentifier("FavoritesList")
//            }
//        }
//        .navigationTitle("Favorites")
//        .toolbar {
//            #if os(iOS)
//            EditButton()
//            #endif
//        }
//        .if(UIDevice.current.userInterfaceIdiom == .phone) { content in
//            NavigationView { content }
//        }
//        .onAppear {
//            #if SCREENSHOTS
//            generateFavoritesForScreenshots()
//            #endif
//        }
//    }
//    
//    private func removeFromFavorites(offsets: IndexSet) {
//        withAnimation {
//            offsets.map { favorites[$0] }.forEach(viewContext.delete)
//            
//            do {
//                try viewContext.save()
//            } catch {
//                let nsError = error as NSError
//                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
//            }
//        }
//    }
//
//    #if SCREENSHOTS
//    /*
//     Save currencies to favourites to take screenshots for the App Store
//     */
//    private func generateFavoritesForScreenshots() {
//        let favoriteCurrencies = [
//            "EUR/USD", "BTC/USD", "USD/HKD", "USD/JPY", "AUD/USD",
//            "XAU/GBP", "DASH/ETH", "EUR/USD", "XAG/CAD"
//        ]
//        
//        let coreDataCurrencyPairs = favorites.map { $0.currencyPair }
//        
//        for favoriteCurrency in favoriteCurrencies {
//            if !coreDataCurrencyPairs.contains(favoriteCurrency) {
//                let favorites = Favorite(context: viewContext)
//                favorites.currencyPair = favoriteCurrency
//                
//                do {
//                    try viewContext.save()
//                } catch {
//                    let nsError = error as NSError
//                    fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
//                }
//            }
//        }
//    }
//    #endif
//}
//
//struct FavoritesView_Previews: PreviewProvider {
//    static var previews: some View {
//        FavoritesView()
//            .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
//    }
//}