Preview and minor fixes
This commit is contained in:
		| @@ -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 | ||||
|     //    } | ||||
| } | ||||
|   | ||||
| @@ -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) | ||||
| } | ||||
|   | ||||
| @@ -83,4 +83,5 @@ struct AnkiView: View { | ||||
| #Preview { | ||||
|     AnkiView() | ||||
|         .environmentObject(WordAXModelView()) | ||||
|         .environment(\.managedObjectContext, DataController.preview.container.viewContext) | ||||
| } | ||||
|   | ||||
| @@ -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) | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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<Flashcard> | ||||
|     @FetchRequest(sortDescriptors: [NSSortDescriptor(key: "dateAdded", ascending: false)]) var flashcards: FetchedResults<Flashcard> | ||||
|     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) | ||||
| } | ||||
|   | ||||
| @@ -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) | ||||
| } | ||||
|   | ||||
| @@ -45,4 +45,5 @@ struct MainView: View { | ||||
| #Preview { | ||||
|     MainView() | ||||
|         .environmentObject(WordAXModelView()) | ||||
|         .environment(\.managedObjectContext, DataController.preview.container.viewContext) | ||||
| } | ||||
|   | ||||
| @@ -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) | ||||
| //} | ||||
|   | ||||
| @@ -16,4 +16,5 @@ struct SettingsView: View { | ||||
| #Preview { | ||||
|     SettingsView() | ||||
|         .environmentObject(WordAXModelView()) | ||||
|         .environment(\.managedObjectContext, DataController.preview.container.viewContext) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user