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