Preview and minor fixes

This commit is contained in:
2024-04-10 14:36:38 +02:00
parent 823d28637a
commit 2e8945e8d2
9 changed files with 97 additions and 52 deletions

View File

@@ -17,8 +17,39 @@ class DataController: ObservableObject {
container.viewContext container.viewContext
} }
static var preview: DataController = {
let result = DataController(inMemory: true)
let viewContext = result.container.viewContext
for _ in 0..<10 {
let flashcard = Flashcard(context: viewContext)
flashcard.id = UUID()
flashcard.name = ["This is a name", "This is another name", "This is a third name", "This is a fourth name"].randomElement()!
flashcard.desc = [
"This is a very long description that should be even longer maybe even lorem ipsum to cover all cases?",
"This is a very short description",
"This is a medium length description that should be long enough to cover all cases"
].randomElement()!
flashcard.nextSpacedRepetitionMilestone = SpacedRepetitionMilestoneEnum.allCases.randomElement()!.rawValue
flashcard.lastSeenOn = [nil, Date(), Date().addingTimeInterval([-86400, -24000, -100000].randomElement()!)].randomElement()!
flashcard.shownCount = [0, 1, 2, 3, 4, 5].randomElement()!
flashcard.dateAdded = [Date(), Date().addingTimeInterval(-86400), Date().addingTimeInterval(-172800)].randomElement()!
}
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
return result
}()
init() {
init(inMemory: Bool = false) {
if inMemory {
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
}
container.loadPersistentStores { description, error in container.loadPersistentStores { description, error in
if let error = error { if let error = error {
print("Core data failed to load: \(error.localizedDescription)") print("Core data failed to load: \(error.localizedDescription)")
@@ -40,40 +71,40 @@ class DataController: ObservableObject {
} }
} }
// public func getFlashCardsToDisplay() -> Flashcard? { // public func getFlashCardsToDisplay() -> Flashcard? {
// let flashcards = self.getAllFlashcards() // let flashcards = self.getAllFlashcards()
// //
// if flashcards.count > 0 { // if flashcards.count > 0 {
// let notShownFlashCards = flashcards.filter({!$0.shown}) // let notShownFlashCards = flashcards.filter({!$0.shown})
// // if today is the date they're supposed to be shown // // if today is the date they're supposed to be shown
// //
// let displayToday = flashcards.filter({ // let displayToday = flashcards.filter({
// $0.lastSeenOn != nil && // $0.lastSeenOn != nil &&
// $0.lastSeenOn!.addSpacedRepetitionMilestone( // $0.lastSeenOn!.addSpacedRepetitionMilestone(
// milestone: SpacedRepetitionMilestoneEnum.getMilestoneFromInt( // milestone: SpacedRepetitionMilestoneEnum.getMilestoneFromInt(
// value: $0.nextSpacedRepetitionMilestone)) // value: $0.nextSpacedRepetitionMilestone))
// .isBeforeTodayOrToday() // .isBeforeTodayOrToday()
// }) // })
// if displayToday.count > 0 { // if displayToday.count > 0 {
// return displayToday.first! // return displayToday.first!
// } // }
// //
//// let shownWords = words.filter({ $0.shown }) //// let shownWords = words.filter({ $0.shown })
//// if shownWords.count == 0 { //// if shownWords.count == 0 {
// if notShownFlashCards.count == 0 { // if notShownFlashCards.count == 0 {
// return nil // return nil
// } // }
// return notShownFlashCards.sorted(by: {$0.id < $1.id}).first // return notShownFlashCards.sorted(by: {$0.id < $1.id}).first
//// } //// }
// // if today is the day to show a new word // // if today is the day to show a new word
//// let settings = model.settings //// let settings = model.settings
//// if shownWords.count == 0 || //// if shownWords.count == 0 ||
//// settings.lastShownNew == nil || //// settings.lastShownNew == nil ||
//// settings.lastShownNew!.addFrequency(frequency: settings.frequency).isAfterToday() { //// settings.lastShownNew!.addFrequency(frequency: settings.frequency).isAfterToday() {
//// return words.first! //// return words.first!
//// } //// }
// } // }
// // otherwise show nothing // // otherwise show nothing
// return nil // return nil
// } // }
} }

View File

@@ -12,13 +12,18 @@ struct AddFlashCard: View {
@State var description: String = "" @State var description: String = ""
@Binding var isShowing: Bool @Binding var isShowing: Bool
@Environment(\.managedObjectContext) var moc @Environment(\.managedObjectContext) var moc
@FocusState private var focus: Bool
var body: some View { var body: some View {
NavigationStack { NavigationStack {
List { List {
Section(header: Text("Flashcard details") ) { Section(header: Text("Flashcard details") ) {
TextField("Name", text: $text) TextField("Name", text: $text)
.focused($focus)
TextField("Description", text: $description, axis: .vertical) TextField("Description", text: $description, axis: .vertical)
} }
.onAppear {
self.focus = true
}
} }
.toolbar { .toolbar {
ToolbarItemGroup(placement: .topBarLeading) { ToolbarItemGroup(placement: .topBarLeading) {
@@ -55,4 +60,5 @@ struct AddFlashCard: View {
#Preview { #Preview {
@State var isShowing = true @State var isShowing = true
return AddFlashCard(isShowing: $isShowing) return AddFlashCard(isShowing: $isShowing)
.environment(\.managedObjectContext, DataController.preview.container.viewContext)
} }

View File

@@ -83,4 +83,5 @@ struct AnkiView: View {
#Preview { #Preview {
AnkiView() AnkiView()
.environmentObject(WordAXModelView()) .environmentObject(WordAXModelView())
.environment(\.managedObjectContext, DataController.preview.container.viewContext)
} }

View File

@@ -46,16 +46,16 @@ struct FlashCardListRowView: View {
} }
#Preview { #Preview {
let fc = Flashcard() let flashcard = try? DataController.preview.viewContext.fetch(Flashcard.fetchRequest()).first
fc.id = UUID()
fc.name = "Mesmerizing"
fc.desc = "Some very long description like Lorem Ipsum which I'm to lazy to copy"
return Group { return Group {
FlashCardListRowView(flashcard: fc) FlashCardListRowView(flashcard: flashcard!)
.environmentObject(WordAXModelView()) .environmentObject(WordAXModelView())
FlashCardListRowView(flashcard: fc) .environment(\.managedObjectContext, DataController.preview.container.viewContext)
FlashCardListRowView(flashcard: flashcard!)
.environmentObject(WordAXModelView()) .environmentObject(WordAXModelView())
FlashCardListRowView(flashcard: fc) .environment(\.managedObjectContext, DataController.preview.container.viewContext)
FlashCardListRowView(flashcard: flashcard!)
.environmentObject(WordAXModelView()) .environmentObject(WordAXModelView())
.environment(\.managedObjectContext, DataController.preview.container.viewContext)
} }
} }

View File

@@ -11,8 +11,7 @@ struct FlashCardListView: View {
@EnvironmentObject var model: WordAXModelView @EnvironmentObject var model: WordAXModelView
@State var showDescription = true @State var showDescription = true
@State var addFlashcard = false @State var addFlashcard = false
// @ObservedObject var flashcards = DataController.shared.getAllFlashcards() @FetchRequest(sortDescriptors: [NSSortDescriptor(key: "dateAdded", ascending: false)]) var flashcards: FetchedResults<Flashcard>
@FetchRequest(sortDescriptors: []) var flashcards: FetchedResults<Flashcard>
var body: some View { var body: some View {
GeometryReader { geometry in GeometryReader { geometry in
NavigationSplitView { NavigationSplitView {
@@ -52,7 +51,8 @@ struct FlashCardListView: View {
} }
} }
//#Preview { #Preview {
// FlashCardListView() FlashCardListView()
// .environmentObject(WordAXModelView()) .environmentObject(WordAXModelView())
//} .environment(\.managedObjectContext, DataController.preview.container.viewContext)
}

View File

@@ -7,6 +7,7 @@
import SwiftUI import SwiftUI
import UIKit import UIKit
import CoreData
struct FlashCardView: View { struct FlashCardView: View {
var flashcard: Flashcard var flashcard: Flashcard
@@ -47,6 +48,8 @@ struct FlashCardView: View {
#Preview { #Preview {
@State var showDescription = false @State var showDescription = false
return FlashCardView(flashcard: DataController().getAllFlashcards()[0], showDescription: $showDescription) let flashcard = try? DataController.preview.viewContext.fetch(Flashcard.fetchRequest()).first
return FlashCardView(flashcard: flashcard!, showDescription: $showDescription)
.environmentObject(WordAXModelView()) .environmentObject(WordAXModelView())
.environment(\.managedObjectContext, DataController.preview.container.viewContext)
} }

View File

@@ -45,4 +45,5 @@ struct MainView: View {
#Preview { #Preview {
MainView() MainView()
.environmentObject(WordAXModelView()) .environmentObject(WordAXModelView())
.environment(\.managedObjectContext, DataController.preview.container.viewContext)
} }

View File

@@ -62,6 +62,8 @@ extension ShapeStyle where Self == Color {
//#Preview { //#Preview {
// @State var showDescription = false // @State var showDescription = false
// return NextRepetitionButtonView(buttonText: "Excellent", nextMilestone: Flashcard.SpacedRepetitionMilestoneEnum.OneDay, wordId: 0, showDescription: $showDescription) // let flashcard = try? DataController.preview.viewContext.fetch(Flashcard.fetchRequest()).first
// return NextRepetitionButtonView(buttonText: "Excellent", nextMilestone: Flashcard.SpacedRepetitionMilestoneEnum.OneDay, showDescription: $showDescription)
// .environmentObject(WordAXModelView()) // .environmentObject(WordAXModelView())
// .environment(\.managedObjectContext, DataController.preview.container.viewContext)
//} //}

View File

@@ -16,4 +16,5 @@ struct SettingsView: View {
#Preview { #Preview {
SettingsView() SettingsView()
.environmentObject(WordAXModelView()) .environmentObject(WordAXModelView())
.environment(\.managedObjectContext, DataController.preview.container.viewContext)
} }