Mercurial > public > simoleon
diff Simoleon/Helpers/FavouriteButton.swift @ 21:c3dda63f50ed v1.1
Added Core Data and UI changes
- Implement Watchlist
- Change conversion design
- Improve UX
author | Dennis Concepción Martín <dennisconcepcionmartin@gmail.com> |
---|---|
date | Mon, 19 Jul 2021 19:27:12 +0100 |
parents | |
children | 3596690dda73 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Simoleon/Helpers/FavouriteButton.swift Mon Jul 19 19:27:12 2021 +0100 @@ -0,0 +1,86 @@ +// +// FavouriteButton.swift +// Simoleon +// +// Created by Dennis Concepción Martín on 19/07/2021. +// + +import SwiftUI + +struct FavouriteButton: View { + var currencyPair: String + @Environment(\.managedObjectContext) private var viewContext + @FetchRequest(sortDescriptors: []) private var favourite: FetchedResults<Favourite> + + var body: some View { + Button(action: { + if isFavourite() { + removeFromFavourites() + } else { + addToFavourites() + } + }) { + RoundedRectangle(cornerRadius: 25) + .foregroundColor(Color(.secondarySystemBackground)) + .frame(width: 75, height: 75) + .overlay( + Image(systemName: generateStar()) + .font(.system(size: 28)) + .foregroundColor(Color(.systemYellow)) + + ) + } + } + + private func isFavourite() -> Bool { + let favouriteCurrencyPairs = favourite.map { $0.currencyPair } + + if favouriteCurrencyPairs.contains(currencyPair) { + return true + } else { + return false + } + } + + private func generateStar() -> String { + if isFavourite() { + return "star.fill" + } else { + return "star" + } + } + + private func removeFromFavourites() { + withAnimation { + let favouriteObject = favourite.first(where: { $0.currencyPair == currencyPair }) + viewContext.delete(favouriteObject ?? Favourite()) + + do { + try viewContext.save() + } catch { + let nsError = error as NSError + fatalError("Unresolved error \(nsError), \(nsError.userInfo)") + } + } + } + + private func addToFavourites() { + withAnimation { + let favourite = Favourite(context: viewContext) + favourite.currencyPair = currencyPair + + do { + try viewContext.save() + } catch { + let nsError = error as NSError + fatalError("Unresolved error \(nsError), \(nsError.userInfo)") + } + } + } +} + +struct FavouriteButton_Previews: PreviewProvider { + static var previews: some View { + FavouriteButton(currencyPair: "USD/GBP") + } +}