From ceef2c0f8e06310d08b55527994bfd68441153d8 Mon Sep 17 00:00:00 2001 From: oliverhnat Date: Sun, 7 Apr 2024 16:44:33 +0200 Subject: [PATCH] Add ability to create flashcards --- WordAX.xcodeproj/project.pbxproj | 24 ++++++--- WordAX/Views/AddFlashCard.swift | 53 +++++++++++++++++++ WordAX/{ => Views}/AnkiView.swift | 0 WordAX/{ => Views}/FlashCardListRowView.swift | 0 WordAX/{ => Views}/FlashCardListView.swift | 11 ++++ WordAX/{ => Views}/FlashCardView.swift | 0 WordAX/{ => Views}/MainView.swift | 0 WordAX/{ => Views}/SettingsView.swift | 0 WordAX/WordAX.swift | 2 +- WordAX/WordAXModelView.swift | 4 ++ 10 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 WordAX/Views/AddFlashCard.swift rename WordAX/{ => Views}/AnkiView.swift (100%) rename WordAX/{ => Views}/FlashCardListRowView.swift (100%) rename WordAX/{ => Views}/FlashCardListView.swift (66%) rename WordAX/{ => Views}/FlashCardView.swift (100%) rename WordAX/{ => Views}/MainView.swift (100%) rename WordAX/{ => Views}/SettingsView.swift (100%) diff --git a/WordAX.xcodeproj/project.pbxproj b/WordAX.xcodeproj/project.pbxproj index 77fbcd4..cd2c925 100644 --- a/WordAX.xcodeproj/project.pbxproj +++ b/WordAX.xcodeproj/project.pbxproj @@ -15,6 +15,7 @@ 6C8185082B8B523E0033CF46 /* NextRepetitionButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C8185072B8B523D0033CF46 /* NextRepetitionButtonView.swift */; }; 6C81850A2B8BA5740033CF46 /* FlashCardListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C8185092B8BA5740033CF46 /* FlashCardListView.swift */; }; 6C81850C2B8BA6BC0033CF46 /* FlashCardListRowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C81850B2B8BA6BC0033CF46 /* FlashCardListRowView.swift */; }; + 6CEF7F522BC2DBF800E205F6 /* AddFlashCard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CEF7F512BC2DBF800E205F6 /* AddFlashCard.swift */; }; 6CF439522B83541D004C3543 /* WordAXApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CF439512B83541D004C3543 /* WordAXApp.swift */; }; 6CF439542B83541D004C3543 /* MainView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CF439532B83541D004C3543 /* MainView.swift */; }; 6CF439562B83541E004C3543 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6CF439552B83541E004C3543 /* Assets.xcassets */; }; @@ -30,6 +31,7 @@ 6C8185072B8B523D0033CF46 /* NextRepetitionButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NextRepetitionButtonView.swift; sourceTree = ""; }; 6C8185092B8BA5740033CF46 /* FlashCardListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlashCardListView.swift; sourceTree = ""; }; 6C81850B2B8BA6BC0033CF46 /* FlashCardListRowView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlashCardListRowView.swift; sourceTree = ""; }; + 6CEF7F512BC2DBF800E205F6 /* AddFlashCard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddFlashCard.swift; sourceTree = ""; }; 6CF4394E2B83541D004C3543 /* WordAX.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WordAX.app; sourceTree = BUILT_PRODUCTS_DIR; }; 6CF439512B83541D004C3543 /* WordAXApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WordAXApp.swift; sourceTree = ""; }; 6CF439532B83541D004C3543 /* MainView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainView.swift; sourceTree = ""; }; @@ -48,6 +50,20 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 6C4632342BC2D99E003ED0E9 /* Views */ = { + isa = PBXGroup; + children = ( + 6CEF7F512BC2DBF800E205F6 /* AddFlashCard.swift */, + 6CF439532B83541D004C3543 /* MainView.swift */, + 6C8185092B8BA5740033CF46 /* FlashCardListView.swift */, + 6C81850B2B8BA6BC0033CF46 /* FlashCardListRowView.swift */, + 6C8185012B88C9FB0033CF46 /* SettingsView.swift */, + 6C8185032B88CA210033CF46 /* AnkiView.swift */, + 6C8185052B8A537F0033CF46 /* FlashCardView.swift */, + ); + path = Views; + sourceTree = ""; + }; 6CF439452B83541D004C3543 = { isa = PBXGroup; children = ( @@ -68,12 +84,7 @@ isa = PBXGroup; children = ( 6CF439512B83541D004C3543 /* WordAXApp.swift */, - 6CF439532B83541D004C3543 /* MainView.swift */, - 6C8185092B8BA5740033CF46 /* FlashCardListView.swift */, - 6C81850B2B8BA6BC0033CF46 /* FlashCardListRowView.swift */, - 6C8185012B88C9FB0033CF46 /* SettingsView.swift */, - 6C8185032B88CA210033CF46 /* AnkiView.swift */, - 6C8185052B8A537F0033CF46 /* FlashCardView.swift */, + 6C4632342BC2D99E003ED0E9 /* Views */, 6C8185072B8B523D0033CF46 /* NextRepetitionButtonView.swift */, 6C8184FD2B88C9580033CF46 /* WordAX.swift */, 6C8184FF2B88C9660033CF46 /* WordAXModelView.swift */, @@ -169,6 +180,7 @@ 6C8185002B88C9660033CF46 /* WordAXModelView.swift in Sources */, 6C8185042B88CA210033CF46 /* AnkiView.swift in Sources */, 6CF439522B83541D004C3543 /* WordAXApp.swift in Sources */, + 6CEF7F522BC2DBF800E205F6 /* AddFlashCard.swift in Sources */, 6C8184FE2B88C9580033CF46 /* WordAX.swift in Sources */, 6C81850C2B8BA6BC0033CF46 /* FlashCardListRowView.swift in Sources */, ); diff --git a/WordAX/Views/AddFlashCard.swift b/WordAX/Views/AddFlashCard.swift new file mode 100644 index 0000000..03636ea --- /dev/null +++ b/WordAX/Views/AddFlashCard.swift @@ -0,0 +1,53 @@ +// +// addFlashCard.swift +// WordAX +// +// Created by Oliver Hnát on 07.04.2024. +// + +import SwiftUI + +struct AddFlashCard: View { + @State var text: String = "" + @State var description: String = "" + @Binding var isShowing: Bool + var addFlashCard: (String, String) -> Void + var body: some View { + NavigationStack { + List { + Section(header: Text("Flashcard details") ) { + TextField("Name", text: $text) + TextField("Description", text: $description, axis: .vertical) + } + } + .toolbar { + ToolbarItemGroup(placement: .topBarLeading) { + Button(action: { + self.isShowing = false + }, label: { + Text("Cancel") + .foregroundStyle(.red) + }) + } + ToolbarItemGroup(placement: .topBarTrailing) { + Button(action: { + self.addFlashCard(self.text, self.description) + self.isShowing = false + }, label: { + Text("Create") + .bold() + }) + } + } + .navigationTitle("Add Flashcard") + } + } +} + +#Preview { + @State var isShowing = true + func add(name: String, desc: String) { + return + } + return AddFlashCard(isShowing: $isShowing, addFlashCard: add) +} diff --git a/WordAX/AnkiView.swift b/WordAX/Views/AnkiView.swift similarity index 100% rename from WordAX/AnkiView.swift rename to WordAX/Views/AnkiView.swift diff --git a/WordAX/FlashCardListRowView.swift b/WordAX/Views/FlashCardListRowView.swift similarity index 100% rename from WordAX/FlashCardListRowView.swift rename to WordAX/Views/FlashCardListRowView.swift diff --git a/WordAX/FlashCardListView.swift b/WordAX/Views/FlashCardListView.swift similarity index 66% rename from WordAX/FlashCardListView.swift rename to WordAX/Views/FlashCardListView.swift index 1904768..98d3b27 100644 --- a/WordAX/FlashCardListView.swift +++ b/WordAX/Views/FlashCardListView.swift @@ -10,6 +10,7 @@ import SwiftUI struct FlashCardListView: View { @EnvironmentObject var model: WordAXModelView @State var showDescription = true + @State var addFlashcard = false var body: some View { NavigationSplitView { List(model.flashcards) { word in @@ -20,9 +21,19 @@ struct FlashCardListView: View { } } .navigationTitle("Word List") + .toolbar { + Button(action: { + self.addFlashcard = true + }) { + Image(systemName: "plus") + } + } } detail: { Text("Select word to get details about") } + .sheet(isPresented: $addFlashcard, content: { + AddFlashCard(isShowing: $addFlashcard, addFlashCard: model.addFlashCard) + }) } } diff --git a/WordAX/FlashCardView.swift b/WordAX/Views/FlashCardView.swift similarity index 100% rename from WordAX/FlashCardView.swift rename to WordAX/Views/FlashCardView.swift diff --git a/WordAX/MainView.swift b/WordAX/Views/MainView.swift similarity index 100% rename from WordAX/MainView.swift rename to WordAX/Views/MainView.swift diff --git a/WordAX/SettingsView.swift b/WordAX/Views/SettingsView.swift similarity index 100% rename from WordAX/SettingsView.swift rename to WordAX/Views/SettingsView.swift diff --git a/WordAX/WordAX.swift b/WordAX/WordAX.swift index e2033fb..af4294d 100644 --- a/WordAX/WordAX.swift +++ b/WordAX/WordAX.swift @@ -12,7 +12,7 @@ struct WordAX { var id: Int var name: String var description: String - var shown: Bool + var shown: Bool = false var nextSpacedRepetitionMilestone: SpacedRepetitionMilestoneEnum? var lastSeenOn: Date? var shownCount: Int = 0 diff --git a/WordAX/WordAXModelView.swift b/WordAX/WordAXModelView.swift index b5d4871..9f15014 100644 --- a/WordAX/WordAXModelView.swift +++ b/WordAX/WordAXModelView.swift @@ -57,6 +57,10 @@ class WordAXModelView: ObservableObject { model.setSpacedRepetitionMilestone(flashcardId: flashcardId, milestone: milestone) model.flashcardShown(flashcardId: flashcardId) } + + public func addFlashCard(name: String, description: String) { + self.model.add(flashcard: FlashCard(id: (self.flashcards.map{$0.id}.max() ?? -1) + 1, name: name, description: description)) + } }