Skip to content

Commit

Permalink
Add support for importing directly from files
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Mar 30, 2024
1 parent a49c0ff commit 66deca2
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
11 changes: 10 additions & 1 deletion Sources/Flashcards.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,28 @@ struct Flashcards: App {

@State("sets", folder: "io.github.david_swift.Flashcards", forceUpdates: true)
private var sets: [FlashcardsSet] = []
@State private var importText = ""
let id = "io.github.david_swift.Flashcards"
var app: GTUIApp!

var scene: Scene {
Window(id: "main") { window in
ContentView(sets: $sets, app: app, window: window) { newValue in
ContentView(sets: $sets, importText: $importText, app: app, window: window) { newValue in
if let index = sets.firstIndex(where: { $0.id == newValue.id }) {
_sets.rawValue[index] = newValue
}
}
}
.title("Memorize")
.quitShortcut()
.overlay {
FileDialog(importer: "import") { url in
if let contents = try? String(contentsOf: url) {
importText = contents
}
} onClose: {
}
}
}

}
14 changes: 7 additions & 7 deletions Sources/Model/Localized.yml
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,11 @@ exportAnkiDeck:
fr: Exporter un paquet de cartes Anki

ankiDescription:
en: "Export decks under <tt><b> File > Export</b></tt>. Select the export format <tt><b> Cards in Plain Text </b></tt>. Uncheck <tt><b>Include HTML and media references</b></tt>. Copy the content of the text file."
de: "Exportiere Decks unter <tt><b> File > Export</b></tt>. Wähle das Format <tt><b> Cards in Plain Text </b></tt>. Deaktiviere <tt><b>Include HTML and media references</b></tt>. Kopiere den Inhalt der Textdatei."
it: "Esporta mazzi in <tt><b> File > Esporta</b></tt>. Seleziona il formato di esportazione <tt><b> Carte in testo semplice </b></tt>. Deseleziona <tt ><b>Includi riferimenti HTML e multimediali</b></tt>. Copia il contenuto del file di testo."
pt_BR: "Exporte decks em <<tt><b> Arquivo > Exportar</b></tt>. Selecionar o formato de exportação <tt><b> Cards in Plain Text. Desselecione <tt><b>Include HTML and media references</b></tt>. Copie o conteúdo do arquivo de texto."
fr: "Exportez des paquets dans <tt><b> Fichier > Exporter</b></tt>. Sélectionnez le format d’exportation <tt><b> Cartes en texte clair </b></tt>. Décochez la case <tt><b>Inclure les médias et références HTML</b></tt>. Copiez le contenu du ficher texte."
en: "Export decks under <tt><b> File > Export</b></tt>. Select the export format <tt><b> Cards in Plain Text </b></tt>. Uncheck <tt><b>Include HTML and media references</b></tt>."
de: "Exportiere Decks unter <tt><b> File > Export</b></tt>. Wähle das Format <tt><b> Cards in Plain Text </b></tt>. Deaktiviere <tt><b>Include HTML and media references</b></tt>."
it: "Esporta mazzi in <tt><b> File > Esporta</b></tt>. Seleziona il formato di esportazione <tt><b> Carte in testo semplice </b></tt>. Deseleziona <tt ><b>Includi riferimenti HTML e multimediali</b></tt>."
pt_BR: "Exporte decks em <<tt><b> Arquivo > Exportar</b></tt>. Selecionar o formato de exportação <tt><b> Cards in Plain Text. Desselecione <tt><b>Include HTML and media references</b></tt>."
fr: "Exportez des paquets dans <tt><b> Fichier > Exporter</b></tt>. Sélectionnez le format d’exportation <tt><b> Cartes en texte clair </b></tt>. Décochez la case <tt><b>Inclure les médias et références HTML</b></tt>."

cancel:
en: Cancel
Expand Down Expand Up @@ -739,8 +739,8 @@ csv:
de: CSV

csvDescription:
en: Paste the text into the text field in the next view. Flashcards are separated by newlines, the front and back by commas.
de: Füge den Text in das Textfeld der nächsten Ansicht ein. Karteikarten werden mit Zeilenumbrüchen, die Vorder- und Rückseite mit Kommas getrennt.
en: Open the file using the button next to the text field. Flashcards are separated by newlines, the front and back by commas.
de: Öffne die Datei mit der Schaltfläche neben dem Textfeld. Karteikarten werden mit Zeilenumbrüchen, die Vorder- und Rückseite mit Kommas getrennt.

importCSV:
en: Import CSV
Expand Down
4 changes: 4 additions & 0 deletions Sources/View/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation
struct ContentView: WindowView {

@Binding var sets: [FlashcardsSet]
@Binding var importText: String
@State("selected-set")
private var selectedSet = ""
@State private var flashcardsView: NavigationStack<FlashcardsView> = .init()
Expand Down Expand Up @@ -114,7 +115,10 @@ struct ContentView: WindowView {
searchFocused: $searchFocused,
flashcardsView: $flashcardsView,
sidebarVisible: $sidebarVisible,
importText: $importText,
smallWindow: smallWindow,
window: window,
app: app,
modifySet: modifySet
) {
sets = sets.filter { $0.id != set.id }
Expand Down
5 changes: 4 additions & 1 deletion Sources/View/EditView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ struct EditView: View {
@Binding var editMode: Bool
@Binding var editSearch: Search
@Binding var searchFocused: Bool
@Binding var importText: String
@State private var expanded = false
@State private var focusedFront: String?
@State private var importFlashcards = false
var window: GTUIWindow
var app: GTUIApp
var modifySet: (FlashcardsSet) -> Void

var view: Body {
Expand Down Expand Up @@ -144,7 +147,7 @@ struct EditView: View {
importFlashcards = true
}
.dialog(visible: $importFlashcards, width: 400, height: 450) {
ImportView(set: $set) { importFlashcards = false }
ImportView(set: $set, text: $importText, window: window, app: app) { importFlashcards = false }
}
}

Expand Down
12 changes: 11 additions & 1 deletion Sources/View/ImportView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ struct ImportView: View {
// swiftlint:enable large_tuple

@Binding var set: FlashcardsSet
@State private var text = ""
@Binding var text: String
@State private var switchSides = false
@State private var navigationStack = NavigationStack<ImportNavigationDestination>()
var window: GTUIWindow
var app: GTUIApp
var close: () -> Void

var view: Body {
Expand Down Expand Up @@ -74,6 +76,13 @@ struct ImportView: View {
var entry: View {
Form {
EntryRow(Loc.pasteText, text: $text)
.suffix {
Button(icon: .default(icon: .documentOpen)) {
app.addWindow("import", parent: window)
}
.padding(10, .vertical)
.style("flat")
}
SwitchRow(Loc.switchFrontBack, isOn: $switchSides)
}
.padding(20)
Expand Down Expand Up @@ -186,6 +195,7 @@ struct ImportView: View {
Button(label) {
if case let .paste(app) = destination {
set.flashcards += previewSet(app: app).flashcards
text = ""
close()
} else {
if case let .tutorial(app) = destination {
Expand Down
6 changes: 6 additions & 0 deletions Sources/View/SetOverview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ struct SetOverview: View {
@Binding var searchFocused: Bool
@Binding var flashcardsView: NavigationStack<FlashcardsView>
@Binding var sidebarVisible: Bool
@Binding var importText: String
@State private var export = false
@State private var deleteState = false
@State private var copied = Signal()
var smallWindow: Bool
var window: GTUIWindow
var app: GTUIApp
var modifySet: (FlashcardsSet) -> Void
var delete: () -> Void

Expand Down Expand Up @@ -76,6 +79,9 @@ struct SetOverview: View {
editMode: $editMode,
editSearch: $editSearch,
searchFocused: $searchFocused,
importText: $importText,
window: window,
app: app,
modifySet: modifySet
)
}
Expand Down

0 comments on commit 66deca2

Please sign in to comment.