Skip to content

Commit b031784

Browse files
committed
Don't allow creating duplicate notes
1 parent c6823c0 commit b031784

4 files changed

Lines changed: 12 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Also, I haven't decided on a license yet.
3232
- [x] Look up words in the app
3333
- [x] Look up words from other apps
3434
- Use the "Wordbase" action, or set it as your translator app
35-
- [ ] Add cards to Anki
35+
- [x] Add cards to Anki
3636
- [ ] Profiles
3737
- Desktop app (`wordbase-desktop`)
3838
- WIP

crates/wordbase/src/anki.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ impl Engine {
4343
};
4444
let glossaries = glossaries(&entries);
4545

46+
let key = term_part(term.headword());
4647
Ok(TermNote {
4748
fields: [
48-
("Expression", term_part(term.headword())),
49+
("Expression", key.clone()),
4950
("ExpressionReading", term_part(term.reading())),
5051
("ExpressionFurigana", term_ruby_plain(term)),
5152
(
@@ -74,6 +75,7 @@ impl Engine {
7475
.map(|audio| ("ExpressionAudio".to_string(), NoteField::Audio(audio))),
7576
)
7677
.collect::<HashMap<_, _>>(),
78+
key,
7779
})
7880
}
7981

@@ -111,6 +113,7 @@ impl Engine {
111113
#[derive(Debug, Serialize)]
112114
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
113115
pub struct TermNote {
116+
pub key: String,
114117
pub fields: HashMap<String, NoteField>,
115118
}
116119

wordbase-android/app/src/main/java/io/github/aecsocket/wordbase/RecordsView.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ fun addNoteFn(
9898
val textNoNoteType = stringResource(R.string.add_note_no_note_type)
9999
val textErrGetFields = stringResource(R.string.add_note_err_get_fields)
100100
val textErrBuildNote = stringResource(R.string.add_note_err_build_note)
101+
val textErrDuplicate = stringResource(R.string.add_note_err_duplicate)
101102
val textErrAdd = stringResource(R.string.add_note_err_add)
102103
val textAdded = stringResource(R.string.add_note_added)
103104

@@ -148,6 +149,11 @@ fun addNoteFn(
148149
return@fn
149150
}
150151

152+
if (anki.findDuplicateNotes(noteTypeId, termNote.key).isNotEmpty()) {
153+
toast(textErrDuplicate)
154+
return@fn
155+
}
156+
151157
val fields = fieldNames.map { fieldName ->
152158
when (val field = termNote.fields[fieldName]) {
153159
is NoteField.String -> field.v1

wordbase-android/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<string name="add_note_no_note_type">No Anki note type ‘%s’</string>
1717
<string name="add_note_err_get_fields">Failed to get fields for ‘%s’</string>
1818
<string name="add_note_err_build_note">Failed to build term note</string>
19+
<string name="add_note_err_duplicate">Note already exists</string>
1920
<string name="add_note_err_add">Failed to add note</string>
2021
<string name="add_note_added">Added note %s</string>
2122

0 commit comments

Comments
 (0)