-
Notifications
You must be signed in to change notification settings - Fork 1
#41: FavoritesManager(UserDefaults) #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
EatHub/EatHub/Design/Extensions/UserDefaults+KeyValueStore.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// UserDefaults+KeyValueStore.swift | ||
// EatHub | ||
// | ||
// Created by Stepan Chuiko on 30.03.2025. | ||
// | ||
import Foundation | ||
|
||
extension UserDefaults: KeyValueStore { | ||
func array<T>(forKey defaultName: String) -> [T] { | ||
(self.array(forKey: defaultName) as? [T]) ?? [] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,59 @@ | ||
import Foundation | ||
import Combine | ||
|
||
final class FavoriteViewModel: ObservableObject { | ||
@Published var likedRecipes: [RecipeViewModel] = [] | ||
var recipesIdentifiers: [String] = [] | ||
|
||
class FavoriteViewModel: ObservableObject { | ||
@Published var recipes: [Recipe] = [] | ||
let title = "Favourites" | ||
|
||
init() { | ||
loadMockData() | ||
private let favoritesManager: FavoritesManagerInterface | ||
private let mealsService: MealsServiceInterface | ||
private var cancellables = Set<AnyCancellable>() | ||
|
||
init(favoritesManager: FavoritesManagerInterface, mealsService: MealsServiceInterface) { | ||
self.favoritesManager = favoritesManager | ||
self.mealsService = mealsService | ||
|
||
// убрать | ||
favoritesManager.populateInitialFavorites(with: ["52943", "52869", "52883", "52823"]) | ||
} | ||
|
||
func toggleFavorite(for recipe: Recipe) { | ||
guard let index = recipes.firstIndex(of: recipe) else { return } | ||
recipes[index].isFavorite.toggle() | ||
func toggleFavorite(for recipe: RecipeViewModel) { | ||
if recipe.isFavorite { | ||
favoritesManager.remove(recipeID: recipe.id) | ||
} else { | ||
favoritesManager.add(recipeID: recipe.id) | ||
} | ||
|
||
recipe.isFavorite.toggle() | ||
} | ||
|
||
private func loadMockData() { | ||
recipes = [ | ||
Recipe(id: 0, name: "Паста Карбонара", imageName: "caesar"), | ||
Recipe(id: 1, name: "Пицца Маргарита", imageName: "caesar"), | ||
Recipe(id: 2, name: "Салат Цезарь", imageName: "caesar"), | ||
Recipe(id: 3, name: "Паста Карбонара", imageName: "caesar"), | ||
Recipe(id: 4, name: "Пицца Маргарита", imageName: "caesar"), | ||
Recipe(id: 5, name: "Салат Цезарь", imageName: "caesar"), | ||
Recipe(id: 6, name: "Паста Карбонара", imageName: "caesar"), | ||
Recipe(id: 7, name: "Пицца Маргарита", imageName: "caesar"), | ||
Recipe(id: 8, name: "Салат Цезарь", imageName: "caesar"), | ||
Recipe(id: 9, name: "Паста Карбонара", imageName: "caesar"), | ||
Recipe(id: 10, name: "Пицца Маргарита", imageName: "caesar"), | ||
Recipe(id: 11, name: "Салат Цезарь", imageName: "caesar") | ||
] | ||
func refreshFavorites() { | ||
cancellables.removeAll() | ||
|
||
let currentFavoriteIDs = Set(favoritesManager.allFavorites()) | ||
|
||
likedRecipes.removeAll { !currentFavoriteIDs.contains($0.id) } | ||
|
||
let existingIDs = Set(likedRecipes.map { $0.id }) | ||
let newIDs = currentFavoriteIDs.subtracting(existingIDs) | ||
|
||
loadNewRecipes(ids: Array(newIDs)) | ||
} | ||
|
||
private func loadNewRecipes(ids: [String]) { | ||
cancellables.removeAll() | ||
|
||
for id in ids { | ||
mealsService.fetchMeal(id: id) | ||
.receive(on: DispatchQueue.main) | ||
.sink { _ in } receiveValue: { [weak self] meal in | ||
if let recipe = meal?.mapToRecipe() { | ||
self?.likedRecipes.append(recipe) | ||
} | ||
} | ||
.store(in: &cancellables) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
EatHub/EatHub/Modules/Favorite/Models/RecipeViewModel.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Foundation | ||
|
||
class RecipeViewModel: ObservableObject, Identifiable { | ||
let id: String | ||
@Published var name: String | ||
@Published var imageName: String | ||
@Published var isFavorite: Bool = true | ||
|
||
init(id: String, name: String, imageName: String) { | ||
self.id = id | ||
self.name = name | ||
self.imageName = imageName | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
EatHub/EatHub/Services/FavoritesManager/FavoritesManager.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// FavoritesManager.swift | ||
// EatHub | ||
// | ||
// Created by Stepan Chuiko on 29.03.2025. | ||
// | ||
import Foundation | ||
|
||
final class FavoritesManager { | ||
private let store: KeyValueStore | ||
private let favoritesKey = "favorite_recipes_ids" | ||
|
||
init(store: KeyValueStore = UserDefaults.standard) { | ||
self.store = store | ||
} | ||
} | ||
|
||
extension FavoritesManager: FavoritesManagerInterface { | ||
func add(recipeID: String) { | ||
var current = allFavorites() | ||
guard !current.contains(recipeID) else { return } | ||
current.append(recipeID) | ||
save(current) | ||
} | ||
|
||
func remove(recipeID: String) { | ||
var current = allFavorites() | ||
current.removeAll { $0 == recipeID } | ||
save(current) | ||
} | ||
|
||
func isFavorite(recipeID: String) -> Bool { | ||
allFavorites().contains(recipeID) | ||
} | ||
|
||
func allFavorites() -> [String] { | ||
store.array(forKey: favoritesKey) | ||
} | ||
|
||
func populateInitialFavorites(with ids: [String]) { | ||
var current = Set(allFavorites()) | ||
ids.forEach { current.insert($0) } | ||
save(Array(current)) | ||
} | ||
|
||
private func save(_ ids: [String]) { | ||
store.set(ids, forKey: favoritesKey) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
EatHub/EatHub/Services/FavoritesManager/FavoritesManagerInterface.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// FavoritesManagerInterface.swift | ||
// EatHub | ||
// | ||
// Created by Stepan Chuiko on 29.03.2025. | ||
// | ||
|
||
protocol FavoritesManagerInterface { | ||
func add(recipeID: String) | ||
func remove(recipeID: String) | ||
func isFavorite(recipeID: String) -> Bool | ||
func allFavorites() -> [String] | ||
func populateInitialFavorites(with ids: [String]) | ||
} |
11 changes: 11 additions & 0 deletions
11
EatHub/EatHub/Services/FavoritesManager/KeyValueStore.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// | ||
// KeyValueStore.swift | ||
// EatHub | ||
// | ||
// Created by Stepan Chuiko on 30.03.2025. | ||
// | ||
|
||
protocol KeyValueStore { | ||
func set(_ value: Any?, forKey defaultName: String) | ||
func array<T>(forKey defaultName: String) -> [T] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.