diff --git a/WordAX/Model/DataController.swift b/WordAX/Model/DataController.swift index 2c8a688..54ebe83 100644 --- a/WordAX/Model/DataController.swift +++ b/WordAX/Model/DataController.swift @@ -17,8 +17,39 @@ class DataController: ObservableObject { 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 if let error = error { print("Core data failed to load: \(error.localizedDescription)") @@ -40,40 +71,40 @@ class DataController: ObservableObject { } } -// public func getFlashCardsToDisplay() -> Flashcard? { -// let flashcards = self.getAllFlashcards() -// -// if flashcards.count > 0 { -// let notShownFlashCards = flashcards.filter({!$0.shown}) -// // if today is the date they're supposed to be shown -// -// let displayToday = flashcards.filter({ -// $0.lastSeenOn != nil && -// $0.lastSeenOn!.addSpacedRepetitionMilestone( -// milestone: SpacedRepetitionMilestoneEnum.getMilestoneFromInt( -// value: $0.nextSpacedRepetitionMilestone)) -// .isBeforeTodayOrToday() -// }) -// if displayToday.count > 0 { -// return displayToday.first! -// } -// -//// let shownWords = words.filter({ $0.shown }) -//// if shownWords.count == 0 { -// if notShownFlashCards.count == 0 { -// return nil -// } -// return notShownFlashCards.sorted(by: {$0.id < $1.id}).first -//// } -// // if today is the day to show a new word -//// let settings = model.settings -//// if shownWords.count == 0 || -//// settings.lastShownNew == nil || -//// settings.lastShownNew!.addFrequency(frequency: settings.frequency).isAfterToday() { -//// return words.first! -//// } -// } -// // otherwise show nothing -// return nil -// } + // public func getFlashCardsToDisplay() -> Flashcard? { + // let flashcards = self.getAllFlashcards() + // + // if flashcards.count > 0 { + // let notShownFlashCards = flashcards.filter({!$0.shown}) + // // if today is the date they're supposed to be shown + // + // let displayToday = flashcards.filter({ + // $0.lastSeenOn != nil && + // $0.lastSeenOn!.addSpacedRepetitionMilestone( + // milestone: SpacedRepetitionMilestoneEnum.getMilestoneFromInt( + // value: $0.nextSpacedRepetitionMilestone)) + // .isBeforeTodayOrToday() + // }) + // if displayToday.count > 0 { + // return displayToday.first! + // } + // + //// let shownWords = words.filter({ $0.shown }) + //// if shownWords.count == 0 { + // if notShownFlashCards.count == 0 { + // return nil + // } + // return notShownFlashCards.sorted(by: {$0.id < $1.id}).first + //// } + // // if today is the day to show a new word + //// let settings = model.settings + //// if shownWords.count == 0 || + //// settings.lastShownNew == nil || + //// settings.lastShownNew!.addFrequency(frequency: settings.frequency).isAfterToday() { + //// return words.first! + //// } + // } + // // otherwise show nothing + // return nil + // } } diff --git a/WordAX/Views/AddFlashCard.swift b/WordAX/Views/AddFlashCard.swift index 999d235..0adf647 100644 --- a/WordAX/Views/AddFlashCard.swift +++ b/WordAX/Views/AddFlashCard.swift @@ -12,13 +12,18 @@ struct AddFlashCard: View { @State var description: String = "" @Binding var isShowing: Bool @Environment(\.managedObjectContext) var moc + @FocusState private var focus: Bool var body: some View { NavigationStack { List { Section(header: Text("Flashcard details") ) { TextField("Name", text: $text) + .focused($focus) TextField("Description", text: $description, axis: .vertical) } + .onAppear { + self.focus = true + } } .toolbar { ToolbarItemGroup(placement: .topBarLeading) { @@ -55,4 +60,5 @@ struct AddFlashCard: View { #Preview { @State var isShowing = true return AddFlashCard(isShowing: $isShowing) + .environment(\.managedObjectContext, DataController.preview.container.viewContext) } diff --git a/WordAX/Views/AnkiView.swift b/WordAX/Views/AnkiView.swift index 81bb6fc..cad5c3b 100644 --- a/WordAX/Views/AnkiView.swift +++ b/WordAX/Views/AnkiView.swift @@ -83,4 +83,5 @@ struct AnkiView: View { #Preview { AnkiView() .environmentObject(WordAXModelView()) + .environment(\.managedObjectContext, DataController.preview.container.viewContext) } diff --git a/WordAX/Views/FlashCardListRowView.swift b/WordAX/Views/FlashCardListRowView.swift index efd7efc..eb0c94b 100644 --- a/WordAX/Views/FlashCardListRowView.swift +++ b/WordAX/Views/FlashCardListRowView.swift @@ -46,16 +46,16 @@ struct FlashCardListRowView: View { } #Preview { - let fc = Flashcard() - fc.id = UUID() - fc.name = "Mesmerizing" - fc.desc = "Some very long description like Lorem Ipsum which I'm to lazy to copy" + let flashcard = try? DataController.preview.viewContext.fetch(Flashcard.fetchRequest()).first return Group { - FlashCardListRowView(flashcard: fc) + FlashCardListRowView(flashcard: flashcard!) .environmentObject(WordAXModelView()) - FlashCardListRowView(flashcard: fc) + .environment(\.managedObjectContext, DataController.preview.container.viewContext) + FlashCardListRowView(flashcard: flashcard!) .environmentObject(WordAXModelView()) - FlashCardListRowView(flashcard: fc) + .environment(\.managedObjectContext, DataController.preview.container.viewContext) + FlashCardListRowView(flashcard: flashcard!) .environmentObject(WordAXModelView()) + .environment(\.managedObjectContext, DataController.preview.container.viewContext) } } diff --git a/WordAX/Views/FlashCardListView.swift b/WordAX/Views/FlashCardListView.swift index e50378c..8135eb4 100644 --- a/WordAX/Views/FlashCardListView.swift +++ b/WordAX/Views/FlashCardListView.swift @@ -11,8 +11,7 @@ struct FlashCardListView: View { @EnvironmentObject var model: WordAXModelView @State var showDescription = true @State var addFlashcard = false -// @ObservedObject var flashcards = DataController.shared.getAllFlashcards() - @FetchRequest(sortDescriptors: []) var flashcards: FetchedResults + @FetchRequest(sortDescriptors: [NSSortDescriptor(key: "dateAdded", ascending: false)]) var flashcards: FetchedResults var body: some View { GeometryReader { geometry in NavigationSplitView { @@ -52,7 +51,8 @@ struct FlashCardListView: View { } } -//#Preview { -// FlashCardListView() -// .environmentObject(WordAXModelView()) -//} +#Preview { + FlashCardListView() + .environmentObject(WordAXModelView()) + .environment(\.managedObjectContext, DataController.preview.container.viewContext) +} diff --git a/WordAX/Views/FlashCardView.swift b/WordAX/Views/FlashCardView.swift index 1bfdc09..60a8148 100644 --- a/WordAX/Views/FlashCardView.swift +++ b/WordAX/Views/FlashCardView.swift @@ -7,6 +7,7 @@ import SwiftUI import UIKit +import CoreData struct FlashCardView: View { var flashcard: Flashcard @@ -47,6 +48,8 @@ struct FlashCardView: View { #Preview { @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()) + .environment(\.managedObjectContext, DataController.preview.container.viewContext) } diff --git a/WordAX/Views/MainView.swift b/WordAX/Views/MainView.swift index 0bac8ac..d4dea6d 100644 --- a/WordAX/Views/MainView.swift +++ b/WordAX/Views/MainView.swift @@ -45,4 +45,5 @@ struct MainView: View { #Preview { MainView() .environmentObject(WordAXModelView()) + .environment(\.managedObjectContext, DataController.preview.container.viewContext) } diff --git a/WordAX/Views/NextRepetitionButtonView.swift b/WordAX/Views/NextRepetitionButtonView.swift index 2c12811..c651b11 100644 --- a/WordAX/Views/NextRepetitionButtonView.swift +++ b/WordAX/Views/NextRepetitionButtonView.swift @@ -62,6 +62,8 @@ extension ShapeStyle where Self == Color { //#Preview { // @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()) +// .environment(\.managedObjectContext, DataController.preview.container.viewContext) //} diff --git a/WordAX/Views/SettingsView.swift b/WordAX/Views/SettingsView.swift index a2f82f3..96a7160 100644 --- a/WordAX/Views/SettingsView.swift +++ b/WordAX/Views/SettingsView.swift @@ -16,4 +16,5 @@ struct SettingsView: View { #Preview { SettingsView() .environmentObject(WordAXModelView()) + .environment(\.managedObjectContext, DataController.preview.container.viewContext) }