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
|
import CoreData
|
||||||
|
|
||||||
struct AnkiView: View {
|
struct AnkiView: View {
|
||||||
@EnvironmentObject var model: WordAXModelView
|
// @EnvironmentObject var model: WordAXModelView
|
||||||
@Environment(\.managedObjectContext) var moc
|
@Environment(\.managedObjectContext) var moc
|
||||||
|
|
||||||
// get flashcards to display
|
// get flashcards to display
|
||||||
@FetchRequest(sortDescriptors: [
|
// @FetchRequest(sortDescriptors: [
|
||||||
NSSortDescriptor(key: "nextSpacedRepetitionMilestone", ascending: false),
|
// NSSortDescriptor(key: "nextSpacedRepetitionMilestone", ascending: false),
|
||||||
NSSortDescriptor(key: "lastSeenOn", ascending: true)
|
// NSSortDescriptor(key: "lastSeenOn", ascending: true)
|
||||||
], predicate: NSCompoundPredicate(type: .or, subpredicates: [
|
// ], predicate: NSCompoundPredicate(type: .or, subpredicates: [
|
||||||
NSCompoundPredicate(type: .and, subpredicates: [
|
// NSCompoundPredicate(type: .and, subpredicates: [
|
||||||
NSPredicate(format: "%K != nil", "lastSeenOn"),
|
// NSPredicate(format: "%K != nil", "lastSeenOn"),
|
||||||
NSPredicate(format: "lastSeenOn + nextSpacedRepetitionMilestone < %@", Date() as CVarArg)
|
// NSPredicate(format: "lastSeenOn + nextSpacedRepetitionMilestone < %@", Date() as CVarArg)
|
||||||
]),
|
// ]),
|
||||||
NSPredicate(format: "lastSeenOn == nil")
|
// NSPredicate(format: "lastSeenOn == nil")
|
||||||
])) var flashcards: FetchedResults<Flashcard>
|
// ])) var flashcards: FetchedResults<Flashcard>
|
||||||
|
|
||||||
// get the most recent flashcard
|
// get the most recent flashcard
|
||||||
@FetchRequest(sortDescriptors: [],
|
// @FetchRequest(sortDescriptors: [],
|
||||||
predicate: NSPredicate(format: "%K != nil", "lastSeenOn")) var soonestFlashcard: FetchedResults<Flashcard>
|
// predicate: NSPredicate(format: "%K != nil", "lastSeenOn")) var soonestFlashcard: FetchedResults<Flashcard>
|
||||||
|
|
||||||
@State private var timeRemaining = 10
|
@State private var timeRemaining = 10
|
||||||
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
|
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
|
||||||
|
@State var flashcards: [Flashcard] = []
|
||||||
@State var sortedFlashcards: [Flashcard] = []
|
@State var sortedFlashcards: [Flashcard] = []
|
||||||
|
@State var soonestFlashcard: [Flashcard] = []
|
||||||
@State var showDescription = false
|
@State var showDescription = false
|
||||||
|
|
||||||
@Environment(\.colorScheme) var colorScheme
|
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
if !flashcards.isEmpty && flashcards.first != nil {
|
Group {
|
||||||
GeometryReader { geometry in
|
if !flashcards.isEmpty && flashcards.first != nil {
|
||||||
VStack {
|
GeometryReader { geometry in
|
||||||
if flashcards.first != nil {
|
VStack {
|
||||||
FlashCardView(flashcard: flashcards.first!, showDescription: $showDescription)
|
if flashcards.first != nil {
|
||||||
}
|
FlashCardView(flashcard: flashcards.first!, showDescription: $showDescription)
|
||||||
if showDescription && flashcards.first != nil {
|
}
|
||||||
// Text("How did you do?")
|
if showDescription && flashcards.first != nil {
|
||||||
// .font(.subheadline)
|
// Text("How did you do?")
|
||||||
// .foregroundStyle(.gray)
|
// .font(.subheadline)
|
||||||
ButtonHStackView(flashcard: flashcards.first!, geometry: geometry, showDescription: $showDescription)
|
// .foregroundStyle(.gray)
|
||||||
.padding([.bottom, .trailing, .leading])
|
ButtonHStackView(flashcard: flashcards.first!, geometry: geometry, showDescription: $showDescription)
|
||||||
|
.padding([.bottom, .trailing, .leading])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
} else {
|
if !soonestFlashcard.isEmpty {
|
||||||
if !soonestFlashcard.isEmpty {
|
Group {
|
||||||
Group {
|
Text("Next flashcard in: \(timeRemaining.convertDurationSecondsToCountdown())")
|
||||||
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)
|
.foregroundStyle(.black)
|
||||||
.padding()
|
.padding()
|
||||||
.frame(maxWidth: .infinity - 50)
|
// .frame(maxWidth: .infinity - 50)
|
||||||
.background(.yellow)
|
.background(.yellow)
|
||||||
.clipShape(.buttonBorder)
|
.clipShape(.buttonBorder)
|
||||||
.padding(.vertical, 50)
|
.padding(.vertical, 50)
|
||||||
.padding(.horizontal)
|
.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