Skip to content

Commit

Permalink
favorites fix
Browse files Browse the repository at this point in the history
  • Loading branch information
m1rosh committed May 21, 2024
1 parent 9e7921d commit 243a213
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 23 deletions.
45 changes: 42 additions & 3 deletions LiveRecipes/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -2886,7 +2886,20 @@

},
"Да" : {

"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Yes"
}
},
"ru" : {
"stringUnit" : {
"state" : "translated",
"value" : "Да"
}
}
}
},
"Дессерт" : {
"extractionState" : "manual",
Expand Down Expand Up @@ -2960,13 +2973,39 @@
}
},
"Отмена" : {

"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Cancel"
}
},
"ru" : {
"stringUnit" : {
"state" : "needs_review",
"value" : "Отмена"
}
}
}
},
"Очистить избранное?" : {

},
"Очистить мои рецепты?" : {

"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Clear my recipes?"
}
},
"ru" : {
"stringUnit" : {
"state" : "translated",
"value" : "Очистить мои рецепты?"
}
}
}
},
"Первое блюдо" : {
"extractionState" : "manual",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct TimerView: View {

@State var timeForProgress: Int
@State var activityStarted: Bool = false
@State var identifier = ""

var step: String?
var stepsCount: Int?
Expand All @@ -62,8 +63,9 @@ struct TimerView: View {
return (hours, minutes, seconds)
}

func addNotification(time: Double, title: String) {
func addNotification(time: Double, title: String) -> String {
let center = UNUserNotificationCenter.current()
let identifier = UUID().uuidString

let addRequest = {
let content = UNMutableNotificationContent()
Expand All @@ -78,24 +80,35 @@ struct TimerView: View {

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: time, repeats: false)

let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.add(request)
print("add")

}
addRequest()
return identifier



center.getNotificationSettings { settings in
if settings.authorizationStatus == .authorized {
addRequest()
} else {
center.requestAuthorization(options: [.alert, .badge, .sound]) { success, _ in
if success {
addRequest()
} else {
print("Authorization declined")
}
}
}
}
// center.getNotificationSettings { settings in
// if settings.authorizationStatus == .authorized {
// addRequest()
// } else {
// center.requestAuthorization(options: [.alert, .badge, .sound]) { success, _ in
// if success {
// addRequest()
// } else {
// print("Authorization declined")
// }
// }
// }
// }
}

func removeNotification(identifier: String) {
let center = UNUserNotificationCenter.current()
center.removePendingNotificationRequests(withIdentifiers: [identifier])
print("remove")
}

func notify() -> Void {
Expand All @@ -114,6 +127,7 @@ struct TimerView: View {
}

func stopActivity() {
removeNotification(identifier: identifier)
Task {
await currentActivity?.end(nil, dismissalPolicy: .immediate)
activityStarted = false
Expand All @@ -123,9 +137,7 @@ struct TimerView: View {
func updateActivity() {
Task {
let state = TimerAttributes.TimeState(progress: progress, totalTime: totalTime, timeRemaining: timeForProgress, currentStep: step ?? "", stepsCount: stepsCount ?? 0, interval: nil)
//await currentActivity?.update(using: state)
await currentActivity?.update(ActivityContent<TimerAttributes.TimeState>(state: state, staleDate: nil))
//await currentActivity?.update(using: state)
}
}

Expand Down Expand Up @@ -204,7 +216,7 @@ struct TimerView: View {
Spacer()

Button(action: {
addNotification(time: Double(totalTime), title: "LiveRecipes")
identifier = addNotification(time: Double(totalTime), title: "LiveRecipes")
isTimerRunning.toggle()
if activityStarted {
// updateActivity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ struct RecipeBigCardView: View {
.frame(width: UIScreen.main.bounds.width - 24, height: 170)
.clipped()
}
else {
Image("mockDishImage")
.resizable()
.scaledToFill()
.frame(width: UIScreen.main.bounds.width - 24, height: 170)
.clipped()
}
VStack {
HStack {
Text(recipe.name)
Expand Down
7 changes: 7 additions & 0 deletions LiveRecipes/Modules/Recipes/Components/RecipeCardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ struct RecipeCardView: View {
.frame(width: 180, height: 110)
.clipped()
}
else {
Image("mockDishImage")
.resizable()
.scaledToFill()
.frame(width: 180, height: 110)
.clipped()
}
VStack{
HStack {
Text(recipe.name)
Expand Down
1 change: 1 addition & 0 deletions LiveRecipes/Modules/Recipes/RecipesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import UserNotifications

struct RecipesView: View {
@StateObject var viewModel: RecipesViewModel
@Environment(\.presentationMode) var presentationMode

var body: some View {
NavigationView {
Expand Down
3 changes: 1 addition & 2 deletions LiveRecipes/Modules/Recipes/RecipesViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,7 @@ final class RecipesViewModel: ObservableObject, RecipesViewModelProtocol {
favoritesID = UserDefaults.standard.array(forKey: "favoritesID") as? [Int] ?? []
coreData.fetch { dishes in
for dish in dishes {
if self.favoritesID.contains(dish.netId) {

if recipe.id == dish.id {
if let index = self.favoritesID.firstIndex(of: dish.netId) {
self.favoritesID.remove(at: index)
UserDefaults.standard.setValue(self.favoritesID, forKey: "favoritesID")
Expand Down

0 comments on commit 243a213

Please sign in to comment.