Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion EatHub/EatHub/Application/AppDependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ struct AppDependencies {
}

func makeFavoriteViewModel() -> FavoriteViewModel {
FavoriteViewModel()
FavoriteViewModel(favoritesManager: FavoritesManager(), mealsService: mealsService)
}
}
13 changes: 13 additions & 0 deletions EatHub/EatHub/Design/Extensions/UserDefaults+KeyValueStore.swift
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]) ?? []
}
}
6 changes: 6 additions & 0 deletions EatHub/EatHub/Mappers/MealMapper/MealMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ extension MealItemResponseModel {
)
}
}

extension Meal {
func mapToRecipe() -> RecipeViewModel {
RecipeViewModel(id: id, name: name, imageName: thumbnail ?? "MealTemplate")
}
}
16 changes: 7 additions & 9 deletions EatHub/EatHub/Modules/Favorite/FavoriteView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ struct FavoriteView: View {
}
.background(Color(.systemGroupedBackground))
.navigationBarHidden(true)
.onAppear {
viewModel.refreshFavorites()
}
}
}
}
Expand All @@ -27,25 +30,20 @@ private extension FavoriteView {

var favoritesList: some View {
LazyVStack(spacing: 8) {
ForEach(viewModel.recipes) { recipe in
// TODO: Добавить переход
// NavigationLink(destination: RecipeDetailView(recipe: recipe)) {
ForEach(viewModel.likedRecipes) { recipe in
// TODO: Добавить переход
// NavigationLink(destination: RecipeDetailView(recipe: recipe)) {
RecipeRow(
recipe: recipe,
onToggleFavorite: {
viewModel.toggleFavorite(for: recipe)
}
)
// }
// }
.buttonStyle(PlainButtonStyle())
}
}
.padding(.horizontal)
.padding(.top, 8)
}
}

#Preview {
let viewModel = FavoriteViewModel()
FavoriteView(viewModel: viewModel)
}
71 changes: 49 additions & 22 deletions EatHub/EatHub/Modules/Favorite/FavoriteViewModel.swift
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)
}
}
}
8 changes: 0 additions & 8 deletions EatHub/EatHub/Modules/Favorite/Models/Recipe.swift

This file was deleted.

14 changes: 14 additions & 0 deletions EatHub/EatHub/Modules/Favorite/Models/RecipeViewModel.swift
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
}
}
15 changes: 11 additions & 4 deletions EatHub/EatHub/Modules/Favorite/RecipeRow.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SwiftUI

struct RecipeRow: View {
let recipe: Recipe
@ObservedObject var recipe: RecipeViewModel
let onToggleFavorite: () -> Void

private enum Constants {
Expand All @@ -14,12 +14,19 @@ struct RecipeRow: View {

var body: some View {
HStack {
Image(recipe.imageName)
.resizable()
.aspectRatio(contentMode: .fill)
if let url = URL(string: recipe.imageName) {
CachedAsyncImage(url: url) { image in
image
.resizable()
.aspectRatio(contentMode: .fill)
} placeholder: {
Color.gray.opacity(0.3)
.skeletonable(true)
}
.frame(width: Constants.imageWidth, height: Constants.imageHeight)
.clipped()
.cornerRadius(Constants.imageCornerRadius)
}

Text(recipe.name)
.font(.headline)
Expand Down
49 changes: 49 additions & 0 deletions EatHub/EatHub/Services/FavoritesManager/FavoritesManager.swift
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)
}
}
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 EatHub/EatHub/Services/FavoritesManager/KeyValueStore.swift
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]
}