Mercurial > public > simoleon
diff Simoleon/Helpers/FavouriteButton.swift @ 42:d25b02d439d4
Minor updates subscription and legal requirements
author | Dennis Concepción Martín <dennisconcepcionmartin@gmail.com> |
---|---|
date | Mon, 26 Jul 2021 15:35:06 +0100 |
parents | d95582268b44 |
children | 75c1a05176f6 |
line wrap: on
line diff
--- a/Simoleon/Helpers/FavouriteButton.swift Sun Jul 25 10:59:51 2021 +0100 +++ b/Simoleon/Helpers/FavouriteButton.swift Mon Jul 26 15:35:06 2021 +0100 @@ -9,49 +9,55 @@ struct FavouriteButton: View { var currencyPair: String + + @State private var starSymbol = "star" @Environment(\.managedObjectContext) private var viewContext @FetchRequest(sortDescriptors: []) private var favourite: FetchedResults<Favourite> var body: some View { - Button(action: { - if isFavourite() { - removeFromFavourites() - simpleSuccess() - } else { - addToFavourites() - simpleSuccess() - } - }) { + let favouriteCurrencyPairs = favourite.map { $0.currencyPair } + Button(action: { favouriteAction(favouriteCurrencyPairs) }) { RoundedRectangle(cornerRadius: 15) .foregroundColor(Color(.secondarySystemBackground)) .frame(width: 60, height: 60) .overlay( - Image(systemName: generateStar()) + Image(systemName: generateStar(favouriteCurrencyPairs)) .font(.system(size: 28)) .foregroundColor(Color(.systemYellow)) - ) } } - private func isFavourite() -> Bool { - let favouriteCurrencyPairs = favourite.map { $0.currencyPair } - + /* + If currency pair is favourite -> button action is to remove from favourites + else -> button action is to add to favourites + */ + private func favouriteAction(_ favouriteCurrencyPairs: [String]) { if favouriteCurrencyPairs.contains(currencyPair) { - return true + removeFromFavourites() } else { - return false + addToFavourites() } + + simpleSuccess() } - private func generateStar() -> String { - if isFavourite() { + /* + if currency pair is favourite -> return "star.fill" symbol + else -> return "star" + */ + private func generateStar(_ favouriteCurrencyPairs: [String]) -> String { + if favouriteCurrencyPairs.contains(currencyPair) { return "star.fill" } else { return "star" } } + /* + 1) Get first favourite core data object that matches the specified currency pair + 2) Delete it + */ private func removeFromFavourites() { withAnimation { let favouriteObject = favourite.first(where: { $0.currencyPair == currencyPair }) @@ -66,6 +72,10 @@ } } + /* + 1) Create a favourite core data object + 2) Save it + */ private func addToFavourites() { withAnimation { let favourite = Favourite(context: viewContext)