Delete flashcards, refactor, todos, date format
Added ability to delete flashcards Extracted adding flashcards into a function Added TODOs Updated dateFormatter to include time based on user's 24/12 format
This commit is contained in:
		| @@ -36,15 +36,7 @@ struct AddFlashCard: View { | ||||
|                 } | ||||
|                 ToolbarItemGroup(placement: .topBarTrailing) { | ||||
|                     Button(action: { | ||||
|                         let flashcard = Flashcard(context: moc) | ||||
|                         flashcard.id = UUID() | ||||
|                         flashcard.name = self.text | ||||
|                         flashcard.desc = self.description | ||||
|                         flashcard.nextSpacedRepetitionMilestone = 0 | ||||
|                         flashcard.lastSeenOn = nil | ||||
|                         flashcard.shownCount = 0 | ||||
|                         flashcard.dateAdded = Date() | ||||
|                         try? moc.save() | ||||
|                         self.createFlashcard() | ||||
|                         self.isShowing = false | ||||
|                     }, label: { | ||||
|                         Text("Create") | ||||
| @@ -55,6 +47,19 @@ struct AddFlashCard: View { | ||||
|             .navigationTitle("Add Flashcard") | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     private func createFlashcard() { | ||||
|         let flashcard = Flashcard(context: moc) | ||||
|         flashcard.id = UUID() | ||||
|         flashcard.name = self.text | ||||
|         flashcard.desc = self.description | ||||
|         flashcard.nextSpacedRepetitionMilestone = 0 | ||||
|         flashcard.lastSeenOn = nil | ||||
|         flashcard.shownCount = 0 | ||||
|         flashcard.dateAdded = Date() | ||||
|         try? moc.save() | ||||
|  | ||||
|     } | ||||
| } | ||||
|  | ||||
| #Preview { | ||||
|   | ||||
| @@ -36,6 +36,7 @@ struct AnkiView: View { | ||||
|                         //                        .font(.subheadline) | ||||
|                         //                        .foregroundStyle(.gray) | ||||
|                         HStack(alignment: .center) { | ||||
|                             // TODO: Fix timeText, maybe using DateIntervallFormatter? | ||||
|                             NextRepetitionButtonView( | ||||
|                                 buttonText: "Wrong", | ||||
|                                 nextMilestone: flashcards.first!.getSpacedRepetitionMilestone(), | ||||
|   | ||||
| @@ -12,17 +12,27 @@ struct FlashCardListView: View { | ||||
|     @State var showDescription = true | ||||
|     @State var addFlashcard = false | ||||
|     @FetchRequest(sortDescriptors: [NSSortDescriptor(key: "dateAdded", ascending: false)]) var flashcards: FetchedResults<Flashcard> | ||||
|     @Environment(\.managedObjectContext) var moc | ||||
|     var body: some View { | ||||
|         GeometryReader { geometry in | ||||
|             NavigationSplitView { | ||||
|                 Group { | ||||
|                     if !flashcards.isEmpty { | ||||
|                         List(flashcards) { flashcard in | ||||
|                             NavigationLink { | ||||
|                                 FlashCardView(flashcard: flashcard, showDescription: $showDescription) | ||||
|                             } label: { | ||||
|                                 FlashCardListRowView(flashcard: flashcard) | ||||
|                         List { | ||||
|                             ForEach(flashcards) { flashcard in | ||||
|                                 NavigationLink { | ||||
|                                     FlashCardView(flashcard: flashcard, showDescription: $showDescription) | ||||
|                                 } label: { | ||||
|                                     FlashCardListRowView(flashcard: flashcard) | ||||
|                                 } | ||||
|                             } | ||||
|                             .onDelete(perform: { offsets in | ||||
|                                 for index in offsets { | ||||
|                                     let flashcard = flashcards[index] | ||||
|                                     moc.delete(flashcard) | ||||
|                                 } | ||||
|                                  | ||||
|                             }) | ||||
|                         } | ||||
|                     } | ||||
|                     else { | ||||
| @@ -35,6 +45,7 @@ struct FlashCardListView: View { | ||||
|                 } | ||||
|                 .navigationTitle("All Flashcards") | ||||
|                 .toolbar { | ||||
|                     EditButton() | ||||
|                     Button(action: { | ||||
|                         self.addFlashcard = true | ||||
|                     }) { | ||||
|   | ||||
| @@ -21,11 +21,17 @@ struct FlashCardView: View { | ||||
|             .font(.title) | ||||
|             .bold() | ||||
|         VStack { | ||||
|             // TODO: Figure out if this and create/edit menu could be more similar? | ||||
|             if flashcard.lastSeenOn != nil { | ||||
|                 Text("Last seen: " + model.getDateFormatter().string(from: flashcard.lastSeenOn!)) | ||||
|                     .font(.subheadline) | ||||
|             } | ||||
|             if showDescription { | ||||
|                 // TODO: Add more information here | ||||
|                 if flashcard.shownCount != 0 { | ||||
|                     Text("Already seen: \(flashcard.shownCount) \(flashcard.shownCount == 1 ? "time" : "times")") | ||||
|                         .padding(.bottom) | ||||
|                 } | ||||
|                 flashcardText | ||||
|                     .textSelection(.enabled) | ||||
|                 Divider() | ||||
| @@ -47,7 +53,7 @@ struct FlashCardView: View { | ||||
| } | ||||
|  | ||||
| #Preview { | ||||
|     @State var showDescription = false | ||||
|     @State var showDescription = true | ||||
|     let flashcard = try? DataController.preview.viewContext.fetch(Flashcard.fetchRequest()).first | ||||
|     return FlashCardView(flashcard: flashcard!, showDescription: $showDescription) | ||||
|         .environmentObject(WordAXModelView()) | ||||
|   | ||||
| @@ -14,8 +14,9 @@ class WordAXModelView: ObservableObject { | ||||
|     let settings: Settings | ||||
|      | ||||
|     init() { | ||||
|         let hourString = DateFormatter.dateFormat(fromTemplate: "j", options: 0, locale: NSLocale.current) | ||||
|         let dateFormatter = DateFormatter() | ||||
|         dateFormatter.dateFormat = "dd/MM/YYYY" | ||||
|         dateFormatter.dateFormat = "dd/MM/YYYY \(hourString?.contains("a") ?? true ? "hh" : "HH"):mm\(hourString?.contains("a") ?? true ? " a" : "")" | ||||
|         self.settings = Settings(dateFormatter: dateFormatter) | ||||
|     } | ||||
|      | ||||
|   | ||||
		Reference in New Issue
	
	Block a user