Replace fetch request anotation with on-appear requests
Because it was needed to refresh the request Algorithm is currently not working
This commit is contained in:
		| @@ -9,78 +9,127 @@ import SwiftUI | ||||
| import CoreData | ||||
|  | ||||
| struct AnkiView: View { | ||||
|     @EnvironmentObject var model: WordAXModelView | ||||
|     //    @EnvironmentObject var model: WordAXModelView | ||||
|     @Environment(\.managedObjectContext) var moc | ||||
|      | ||||
|     // get flashcards to display | ||||
|     @FetchRequest(sortDescriptors: [ | ||||
|         NSSortDescriptor(key: "nextSpacedRepetitionMilestone", ascending: false), | ||||
|         NSSortDescriptor(key: "lastSeenOn", ascending: true) | ||||
|     ], predicate: NSCompoundPredicate(type: .or, subpredicates: [ | ||||
|         NSCompoundPredicate(type: .and, subpredicates: [ | ||||
|             NSPredicate(format: "%K != nil", "lastSeenOn"), | ||||
|             NSPredicate(format: "lastSeenOn + nextSpacedRepetitionMilestone < %@", Date() as CVarArg) | ||||
|         ]), | ||||
|         NSPredicate(format: "lastSeenOn == nil") | ||||
|     ])) var flashcards: FetchedResults<Flashcard> | ||||
|     //    @FetchRequest(sortDescriptors: [ | ||||
|     //        NSSortDescriptor(key: "nextSpacedRepetitionMilestone", ascending: false), | ||||
|     //        NSSortDescriptor(key: "lastSeenOn", ascending: true) | ||||
|     //    ], predicate: NSCompoundPredicate(type: .or, subpredicates: [ | ||||
|     //        NSCompoundPredicate(type: .and, subpredicates: [ | ||||
|     //            NSPredicate(format: "%K != nil", "lastSeenOn"), | ||||
|     //            NSPredicate(format: "lastSeenOn + nextSpacedRepetitionMilestone < %@", Date() as CVarArg) | ||||
|     //        ]), | ||||
|     //        NSPredicate(format: "lastSeenOn == nil") | ||||
|     //    ])) var flashcards: FetchedResults<Flashcard> | ||||
|      | ||||
|     // get the most recent flashcard | ||||
|     @FetchRequest(sortDescriptors: [], | ||||
|                   predicate: NSPredicate(format: "%K != nil", "lastSeenOn")) var soonestFlashcard: FetchedResults<Flashcard> | ||||
|     //    @FetchRequest(sortDescriptors: [], | ||||
|     //                  predicate: NSPredicate(format: "%K != nil", "lastSeenOn")) var soonestFlashcard: FetchedResults<Flashcard> | ||||
|      | ||||
|     @State private var timeRemaining = 10 | ||||
|     let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() | ||||
|     @State var flashcards: [Flashcard] = [] | ||||
|     @State var sortedFlashcards: [Flashcard] = [] | ||||
|     @State var soonestFlashcard: [Flashcard] = [] | ||||
|     @State var showDescription = false | ||||
|      | ||||
|     @Environment(\.colorScheme) var colorScheme | ||||
|      | ||||
|     var body: some View { | ||||
|         if !flashcards.isEmpty && flashcards.first != nil { | ||||
|             GeometryReader { geometry in | ||||
|                 VStack { | ||||
|                     if flashcards.first != nil { | ||||
|                         FlashCardView(flashcard: flashcards.first!, showDescription: $showDescription) | ||||
|                     } | ||||
|                     if showDescription && flashcards.first != nil { | ||||
|                         //                    Text("How did you do?") | ||||
|                         //                        .font(.subheadline) | ||||
|                         //                        .foregroundStyle(.gray) | ||||
|                         ButtonHStackView(flashcard: flashcards.first!, geometry: geometry, showDescription: $showDescription) | ||||
|                             .padding([.bottom, .trailing, .leading]) | ||||
|         Group { | ||||
|             if !flashcards.isEmpty && flashcards.first != nil { | ||||
|                 GeometryReader { geometry in | ||||
|                     VStack { | ||||
|                         if flashcards.first != nil { | ||||
|                             FlashCardView(flashcard: flashcards.first!, showDescription: $showDescription) | ||||
|                         } | ||||
|                         if showDescription && flashcards.first != nil { | ||||
|                             //                    Text("How did you do?") | ||||
|                             //                        .font(.subheadline) | ||||
|                             //                        .foregroundStyle(.gray) | ||||
|                             ButtonHStackView(flashcard: flashcards.first!, geometry: geometry, showDescription: $showDescription) | ||||
|                                 .padding([.bottom, .trailing, .leading]) | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } else { | ||||
|             if !soonestFlashcard.isEmpty { | ||||
|                 Group { | ||||
|                     Text("Next flashcard in: \(timeRemaining.convertDurationSecondsToCountdown())") | ||||
|             } else { | ||||
|                 if !soonestFlashcard.isEmpty { | ||||
|                     Group { | ||||
|                         Text("Next flashcard in: \(timeRemaining.convertDurationSecondsToCountdown())") | ||||
|                             .foregroundStyle(.black) | ||||
|                             .padding() | ||||
|                             .frame(maxWidth: .infinity - 50) | ||||
|                             .background(.yellow) | ||||
|                             .clipShape(.buttonBorder) | ||||
|                             .padding(.vertical, 50) | ||||
|                             .padding(.horizontal) | ||||
|                             .onAppear { | ||||
|                                 if !soonestFlashcard.isEmpty { | ||||
|                                     sortedFlashcards = soonestFlashcard.sorted(by: { | ||||
|                                         ($0.lastSeenOn!.addSpacedRepetitionMilestone(milestone:$0.getSpacedRepetitionMilestone()).timeIntervalSinceNow) < ($1.lastSeenOn!.addSpacedRepetitionMilestone(milestone: $1.getSpacedRepetitionMilestone()).timeIntervalSinceNow) | ||||
|                                     }) | ||||
|                                     timeRemaining = Int(sortedFlashcards.first!.lastSeenOn!.addSpacedRepetitionMilestone(milestone:sortedFlashcards.first!.getSpacedRepetitionMilestone()).timeIntervalSinceNow) | ||||
|                                 } | ||||
|                                 refreshFlashcards() | ||||
|                             } | ||||
|                             .onReceive(timer) { time in | ||||
|                                 if timeRemaining > 1 { | ||||
|                                     timeRemaining -= 1 | ||||
|                                 } | ||||
|                                 if timeRemaining <= 3 { | ||||
|                                     refreshFlashcards() | ||||
|                                 } | ||||
|                             } | ||||
|                             .background(.gray.opacity(0.3)) | ||||
|                             .clipShape(.buttonBorder) | ||||
|                             .padding(.horizontal) | ||||
|                     } | ||||
|                 } | ||||
|                 else { | ||||
|                     Text("No flashcards available") | ||||
|                         .foregroundStyle(.black) | ||||
|                         .padding() | ||||
|                         .frame(maxWidth: .infinity - 50) | ||||
|                     //                    .frame(maxWidth: .infinity - 50) | ||||
|                         .background(.yellow) | ||||
|                         .clipShape(.buttonBorder) | ||||
|                         .padding(.vertical, 50) | ||||
|                         .padding(.horizontal) | ||||
|                         .onAppear { | ||||
|                             if !soonestFlashcard.isEmpty { | ||||
|                                 sortedFlashcards = soonestFlashcard.sorted(by: { | ||||
|                                     ($0.lastSeenOn!.addSpacedRepetitionMilestone(milestone:$0.getSpacedRepetitionMilestone()).timeIntervalSinceNow) < ($1.lastSeenOn!.addSpacedRepetitionMilestone(milestone: $1.getSpacedRepetitionMilestone()).timeIntervalSinceNow) | ||||
|                                 }) | ||||
|                                 timeRemaining = Int(sortedFlashcards.first!.lastSeenOn!.addSpacedRepetitionMilestone(milestone:sortedFlashcards.first!.getSpacedRepetitionMilestone()).timeIntervalSinceNow) | ||||
|                             } | ||||
|                         } | ||||
|                         .onReceive(timer) { time in | ||||
|                             if timeRemaining > 0 { | ||||
|                                 timeRemaining -= 1 | ||||
|                             } | ||||
|                         } | ||||
|                         .background(.gray.opacity(0.3)) | ||||
|                         .clipShape(.buttonBorder) | ||||
|                         .padding(.horizontal) | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|         .onAppear { | ||||
|             refreshFlashcards() | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     func refreshFlashcards() { | ||||
|         let request = NSFetchRequest<Flashcard>(entityName: "Flashcard") | ||||
|         request.predicate = NSCompoundPredicate(type: .or, subpredicates: [ | ||||
|             NSCompoundPredicate(type: .and, subpredicates: [ | ||||
|                 NSPredicate(format: "%K != nil", "lastSeenOn"), | ||||
|                 NSPredicate(format: "lastSeenOn + nextSpacedRepetitionMilestone < %@", Date() as CVarArg) | ||||
|             ]), | ||||
|             NSPredicate(format: "lastSeenOn == nil") | ||||
|         ]) | ||||
|         request.sortDescriptors = [ | ||||
|             NSSortDescriptor(key: "nextSpacedRepetitionMilestone", ascending: false), | ||||
|             NSSortDescriptor(key: "lastSeenOn", ascending: true) | ||||
|         ] | ||||
|         do { | ||||
|             flashcards = try moc.fetch(request) | ||||
|         } catch { | ||||
|             print("Something went wroooong") | ||||
|         } | ||||
|          | ||||
|          | ||||
|          | ||||
|         let req = NSFetchRequest<Flashcard>(entityName: "Flashcard") | ||||
|         req.predicate = NSPredicate(format: "%K != nil", "lastSeenOn") | ||||
|         do { | ||||
|             soonestFlashcard = try moc.fetch(request) | ||||
|         } catch { | ||||
|             print("Something went bum") | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user