Mercurial > public > simoleon
diff Simoleon/Helpers/FavoriteButton.swift @ 187:13d5a8deb6c2
add AboutView and FavoritesView
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Thu, 23 Dec 2021 16:12:22 +0100 |
parents | 2fc95efcb1ee |
children |
line wrap: on
line diff
--- a/Simoleon/Helpers/FavoriteButton.swift Thu Dec 23 11:30:38 2021 +0100 +++ b/Simoleon/Helpers/FavoriteButton.swift Thu Dec 23 16:12:22 2021 +0100 @@ -6,12 +6,21 @@ // import SwiftUI +import CoreData struct FavoriteButton: View { + @Binding var baseCurrency: SupportedCurrencyResult + @Binding var quoteCurrency: SupportedCurrencyResult @State private var scale: CGFloat = 1 + @Environment(\.managedObjectContext) private var viewContext + @FetchRequest(sortDescriptors: []) private var favoritePairs: FetchedResults<FavoritePair> var body: some View { - Button(action: {}) { + Button(action: { + animate() + if isFavorite() { remove() } + else { add() } + }) { RoundedRectangle(cornerRadius: 15) .foregroundColor(Color(.secondarySystemBackground)) .frame(width: 60, height: 60) @@ -33,17 +42,49 @@ // Add currency conversion to favorites private func add() { + let favoritePair = FavoritePair(context: viewContext) + favoritePair.baseCurrency = baseCurrency.code + favoritePair.quoteCurrency = quoteCurrency.code + do { + try viewContext.save() + } catch { + // Replace this implementation with code to handle the error appropriately. + // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. + let nsError = error as NSError + fatalError("Unresolved error \(nsError), \(nsError.userInfo)") + } } // Remove currency conversion from favorites private func remove() { + let favoritePair = favoritePairs.first( + where: { + $0.baseCurrency == baseCurrency.code && $0.quoteCurrency == quoteCurrency.code + }) + viewContext.delete(favoritePair!) + + do { + try viewContext.save() + } catch { + // Replace this implementation with code to handle the error appropriately. + // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. + let nsError = error as NSError + fatalError("Unresolved error \(nsError), \(nsError.userInfo)") + } } // Check if currency conversion is in favorites private func isFavorite() -> Bool { - return false + let favoritePair = favoritePairs.first( + where: { + $0.baseCurrency == baseCurrency.code && $0.quoteCurrency == quoteCurrency.code + }) + + guard let _ = favoritePair else { return false } + + return true } // Animate favorite button @@ -57,6 +98,10 @@ struct FavoriteButton_Previews: PreviewProvider { static var previews: some View { - FavoriteButton() + FavoriteButton( + baseCurrency: .constant(SupportedCurrencyResult(code: "EUR", name: "Euro", isCrypto: 0)), + quoteCurrency: .constant(SupportedCurrencyResult(code: "USD", name: "U.S. Dollar", isCrypto: 0)) + ) + .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) } }