view Simoleon/FavoritesView.swift @ 195:7b009649a063 default tip

Move to mercurial
author Dennis C. M. <dennis@denniscm.com>
date Tue, 03 Jun 2025 14:43:48 +0100
parents 13d5a8deb6c2
children
line wrap: on
line source

//
//  FavoritesView.swift
//  Simoleon
//
//  Created by Dennis Concepción Martín on 23/12/21.
//

import SwiftUI
import CoreData

struct FavoritesView: View {
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \FavoritePair.baseCurrency, ascending: true)], animation: .default
    ) private var favoritePairs: FetchedResults<FavoritePair>
    
    var body: some View {
        VStack {
            if favoritePairs.isEmpty {
                FavoritesPlaceholder()
            } else {
                List {
                    ForEach(favoritePairs, id:\.self) { favoritePair in
                        FavoriteRow(baseCurrency: favoritePair.baseCurrency!, quoteCurrency: favoritePair.quoteCurrency!)
                    }
                    .onDelete(perform: remove)
                }
            }
        }
        .toolbar {
            #if os(iOS)
            EditButton()
            #endif
        }
        .navigationTitle("Favorites")
        .if(UIDevice.current.userInterfaceIdiom == .phone) { content in
            NavigationView { content }
        }
    }
    
    // Remove favorite pair from favorites
    private func remove(offsets: IndexSet) {
        withAnimation {
            offsets.map { favoritePairs[$0] }.forEach(viewContext.delete)

            do {
                try viewContext.save()
            } catch {
                let nsError = error as NSError
                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
            }
        }
    }
}

struct FavoritesView_Previews: PreviewProvider {
    static var previews: some View {
        FavoritesView()
    }
}