Add edit function to flashcard
This commit is contained in:
		| @@ -48,7 +48,7 @@ public class Flashcard: NSManagedObject { | |||||||
|     } |     } | ||||||
|      |      | ||||||
|     func getSpacedRepetitionMilestone() -> SpacedRepetitionMilestoneEnum { |     func getSpacedRepetitionMilestone() -> SpacedRepetitionMilestoneEnum { | ||||||
|         var milestone = SpacedRepetitionMilestoneEnum.getMilestoneFromInt(value: self.nextSpacedRepetitionMilestone) |         let milestone = SpacedRepetitionMilestoneEnum.getMilestoneFromInt(value: self.nextSpacedRepetitionMilestone) | ||||||
|         return milestone == .Now ? .TenMinutes : milestone |         return milestone == .Now || milestone == .OneMinute ? .TenMinutes : milestone | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -16,6 +16,8 @@ struct AddFlashCardView: View { | |||||||
|     @FetchRequest(sortDescriptors: []) var decks: FetchedResults<Deck> |     @FetchRequest(sortDescriptors: []) var decks: FetchedResults<Deck> | ||||||
|     @State var selectedDeck: Deck? |     @State var selectedDeck: Deck? | ||||||
|     @State var createDisabled: Bool = true |     @State var createDisabled: Bool = true | ||||||
|  |     @State var flashcard: Flashcard | ||||||
|  |     var edit: Bool = false | ||||||
|     var body: some View { |     var body: some View { | ||||||
|         NavigationStack { |         NavigationStack { | ||||||
|             List { |             List { | ||||||
| @@ -54,7 +56,7 @@ struct AddFlashCardView: View { | |||||||
|                         self.createFlashcard() |                         self.createFlashcard() | ||||||
|                         self.isShowing = false |                         self.isShowing = false | ||||||
|                     }, label: { |                     }, label: { | ||||||
|                         Text("Create") |                         Text(edit ? "Save" : "Create") | ||||||
|                             .bold() |                             .bold() | ||||||
|                     }) |                     }) | ||||||
|                     .disabled(text.count == 0 || description.count == 0 || selectedDeck == nil) |                     .disabled(text.count == 0 || description.count == 0 || selectedDeck == nil) | ||||||
| @@ -65,15 +67,15 @@ struct AddFlashCardView: View { | |||||||
|     } |     } | ||||||
|      |      | ||||||
|     private func createFlashcard() { |     private func createFlashcard() { | ||||||
|         let flashcard = Flashcard(context: moc) |  | ||||||
|         flashcard.id = UUID() |  | ||||||
|         flashcard.name = self.text |         flashcard.name = self.text | ||||||
|         flashcard.desc = self.description |         flashcard.desc = self.description | ||||||
|  |         flashcard.deck = selectedDeck | ||||||
|  |         if !edit { | ||||||
|             flashcard.nextSpacedRepetitionMilestone = 0 |             flashcard.nextSpacedRepetitionMilestone = 0 | ||||||
|             flashcard.lastSeenOn = nil |             flashcard.lastSeenOn = nil | ||||||
|             flashcard.shownCount = 0 |             flashcard.shownCount = 0 | ||||||
|             flashcard.dateAdded = Date() |             flashcard.dateAdded = Date() | ||||||
|         flashcard.deck = selectedDeck |         } | ||||||
|         try? moc.save() |         try? moc.save() | ||||||
|  |  | ||||||
|     } |     } | ||||||
| @@ -81,6 +83,8 @@ struct AddFlashCardView: View { | |||||||
|  |  | ||||||
| #Preview { | #Preview { | ||||||
|     @State var isShowing = true |     @State var isShowing = true | ||||||
|     return AddFlashCardView(isShowing: $isShowing) |     let flashcard = Flashcard(context: DataController.preview.container.viewContext) | ||||||
|  |     flashcard.id = UUID() | ||||||
|  |     return AddFlashCardView(isShowing: $isShowing, flashcard: flashcard) | ||||||
|         .environment(\.managedObjectContext, DataController.preview.container.viewContext) |         .environment(\.managedObjectContext, DataController.preview.container.viewContext) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -75,11 +75,17 @@ struct FlashCardListView: View { | |||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             .sheet(isPresented: $addFlashcard, content: { |             .sheet(isPresented: $addFlashcard, content: { | ||||||
|                 AddFlashCardView(isShowing: $addFlashcard, selectedDeck: deck) |                 AddFlashCardView(isShowing: $addFlashcard, selectedDeck: deck, flashcard: createFlashcard()) | ||||||
|             }) |             }) | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|      |      | ||||||
|  |     private func createFlashcard() -> Flashcard { | ||||||
|  |         let flashcard = Flashcard(context:moc) | ||||||
|  |         flashcard.id = UUID() | ||||||
|  |         return flashcard | ||||||
|  |     } | ||||||
|  |      | ||||||
|     private func refreshFlashcards() { |     private func refreshFlashcards() { | ||||||
|         let request = NSFetchRequest<Flashcard>(entityName: "Flashcard") |         let request = NSFetchRequest<Flashcard>(entityName: "Flashcard") | ||||||
|         request.sortDescriptors = [NSSortDescriptor(key: "favorite", ascending: false), NSSortDescriptor(key: "dateAdded", ascending: false)] |         request.sortDescriptors = [NSSortDescriptor(key: "favorite", ascending: false), NSSortDescriptor(key: "dateAdded", ascending: false)] | ||||||
|   | |||||||
| @@ -13,6 +13,7 @@ struct FlashCardView: View { | |||||||
|     var flashcard: Flashcard |     var flashcard: Flashcard | ||||||
|     @Binding var showDescription: Bool |     @Binding var showDescription: Bool | ||||||
|     @Environment(\.colorScheme) var colorScheme |     @Environment(\.colorScheme) var colorScheme | ||||||
|  |     @State var editFlashcard: Bool = false | ||||||
|      |      | ||||||
|      |      | ||||||
|     var body: some View { |     var body: some View { | ||||||
| @@ -48,6 +49,16 @@ struct FlashCardView: View { | |||||||
|         .onTapGesture { |         .onTapGesture { | ||||||
|             self.showDescription = true |             self.showDescription = true | ||||||
|         } |         } | ||||||
|  |         .sheet(isPresented: $editFlashcard) { | ||||||
|  |             AddFlashCardView(text: flashcard.name ?? "", description: flashcard.desc ?? "", isShowing: $editFlashcard, selectedDeck: flashcard.deck, flashcard: flashcard, edit: true) | ||||||
|  |         } | ||||||
|  |         .toolbar { | ||||||
|  |             Button { | ||||||
|  |                 self.editFlashcard = true | ||||||
|  |             } label: { | ||||||
|  |                 Text("Edit") | ||||||
|  |             } | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user