use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.
the class AbstractFlashcardViewer method showTagsDialog.
protected void showTagsDialog() {
ArrayList<String> tags = new ArrayList<>(getCol().getTags().all());
ArrayList<String> selTags = new ArrayList<>(mCurrentCard.note().getTags());
TagsDialog.TagsDialogListener tagsDialogListener = (selectedTags, option) -> {
if (!mCurrentCard.note().getTags().equals(selectedTags)) {
String tagString = TextUtils.join(" ", selectedTags);
Note note = mCurrentCard.note();
note.setTagsFromStr(tagString);
note.flush();
// Reload current card to reflect tag changes
displayCardQuestion(true);
}
};
TagsDialog dialog = TagsDialog.newInstance(TagsDialog.TYPE_ADD_TAG, selTags, tags);
dialog.setTagsDialogListener(tagsDialogListener);
showDialogFragment(dialog);
}
use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.
the class ContentProviderTest method testUpdateTags.
/**
* Update tags on a note
*/
@Test
public void testUpdateTags() {
// get the first card due
// ----------------------
Collection col = getCol();
Card card = getFirstCardFromScheduler(col);
Note note = card.note();
long noteId = note.getId();
// make sure the tag is what we expect initially
// ---------------------------------------------
List<String> tagList = note.getTags();
assertEquals("only one tag", 1, tagList.size());
assertEquals("check tag value", TEST_TAG, tagList.get(0));
// update tags
// -----------
String tag2 = "mynewtag";
ContentResolver cr = InstrumentationRegistry.getInstrumentation().getTargetContext().getContentResolver();
Uri updateNoteUri = Uri.withAppendedPath(FlashCardsContract.Note.CONTENT_URI, Long.toString(noteId));
ContentValues values = new ContentValues();
values.put(FlashCardsContract.Note.TAGS, TEST_TAG + " " + tag2);
int updateCount = cr.update(updateNoteUri, values, null, null);
assertEquals("updateCount is 1", 1, updateCount);
// lookup the note now and verify tags
// -----------------------------------
Note noteAfterUpdate = col.getNote(noteId);
List<String> newTagList = noteAfterUpdate.getTags();
assertEquals("two tags", 2, newTagList.size());
assertEquals("check first tag", TEST_TAG, newTagList.get(0));
assertEquals("check second tag", tag2, newTagList.get(1));
}
use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.
the class ImportTest method testCsv.
@Test
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
public void testCsv() throws IOException {
String file = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "text-2fields.txt");
TextImporter i = new TextImporter(testCol, file);
i.initMapping();
i.run();
if (TestEnvironment.isDisplayingDefaultEnglishStrings()) {
assertThat(i.getLog(), contains("‘多すぎる too many fields’ had 3 fields, expected 2", "‘not, enough, fields’ had 1 fields, expected 2", "Appeared twice in file: 飲む", "Empty first field: to play", "5 notes added, 0 notes updated, 0 notes unchanged."));
} else {
assertThat(i.getLog(), hasSize(5));
}
assertEquals(5, i.getTotal());
// if we run the import again, it should update instead
i.run();
if (TestEnvironment.isDisplayingDefaultEnglishStrings()) {
assertThat(i.getLog(), contains("‘多すぎる too many fields’ had 3 fields, expected 2", "‘not, enough, fields’ had 1 fields, expected 2", "Appeared twice in file: 飲む", "Empty first field: to play", "0 notes added, 0 notes updated, 5 notes unchanged.", "First field matched: 食べる", "First field matched: 飲む", "First field matched: テスト", "First field matched: to eat", "First field matched: 遊ぶ"));
} else {
assertThat(i.getLog(), hasSize(10));
}
assertEquals(5, i.getTotal());
// but importing should not clobber tags if they're unmapped
Note n = testCol.getNote(testCol.getDb().queryLongScalar("select id from notes"));
n.addTag("test");
n.flush();
i.run();
n.load();
assertThat(n.getTags(), contains("test"));
assertThat(n.getTags(), hasSize(1));
// if add-only mode, count will be 0
i.setImportMode(NoteImporter.ImportMode.IGNORE_MODE);
i.run();
assertEquals(0, i.getTotal());
// and if dupes mode, will reimport everything
assertEquals(5, testCol.cardCount());
i.setImportMode(NoteImporter.ImportMode.ADD_MODE);
i.run();
// includes repeated field
assertEquals(6, i.getTotal());
assertEquals(11, testCol.cardCount());
}
use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.
the class NoteEditor method showTagsDialog.
private void showTagsDialog() {
if (mSelectedTags == null) {
mSelectedTags = new ArrayList<>(0);
}
ArrayList<String> tags = new ArrayList<>(getCol().getTags().all());
ArrayList<String> selTags = new ArrayList<>(mSelectedTags);
TagsDialog.TagsDialogListener tagsDialogListener = (selectedTags, option) -> {
if (!mSelectedTags.equals(selectedTags)) {
mTagsEdited = true;
}
mSelectedTags = selectedTags;
updateTags();
};
TagsDialog dialog = TagsDialog.newInstance(TagsDialog.TYPE_ADD_TAG, selTags, tags);
dialog.setTagsDialogListener(tagsDialogListener);
showDialogFragment(dialog);
}
use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.
the class NoteEditor method saveNote.
@VisibleForTesting
void saveNote() {
final Resources res = getResources();
if (mSelectedTags == null) {
mSelectedTags = new ArrayList<>(0);
}
// treat add new note and edit existing note independently
if (mAddNote) {
// DEFECT: This does not block addition if cloze transpositions are in non-cloze fields.
if (isClozeType() && !hasClozeDeletions()) {
displayErrorSavingNote();
return;
}
// load all of the fields into the note
for (FieldEditText f : mEditFields) {
updateField(f);
}
// Save deck to model
mEditorNote.model().put("did", mCurrentDid);
// Save tags to model
mEditorNote.setTagsFromStr(tagsAsString(mSelectedTags));
JSONArray tags = new JSONArray();
for (String t : mSelectedTags) {
tags.put(t);
}
getCol().getModels().current().put("tags", tags);
getCol().getModels().setChanged();
mReloadRequired = true;
CollectionTask.launchCollectionTask(ADD_NOTE, saveNoteHandler(), new TaskData(mEditorNote));
} else {
// Check whether note type has been changed
final Model newModel = getCurrentlySelectedModel();
final Model oldModel = (mCurrentEditedCard == null) ? null : mCurrentEditedCard.model();
File target = new File(FileUtil.createTmpDir(this), mCurrentEditedCard.getId() + ".wav");
if (target.exists()) {
Timber.i("editing card audio is exists,delete it");
target.delete();
}
if (!newModel.equals(oldModel)) {
mReloadRequired = true;
if (mModelChangeCardMap.size() < mEditorNote.numberOfCards() || mModelChangeCardMap.containsValue(null)) {
// If cards will be lost via the new mapping then show a confirmation dialog before proceeding with the change
ConfirmationDialog dialog = new ConfirmationDialog();
dialog.setArgs(res.getString(R.string.confirm_map_cards_to_nothing));
Runnable confirm = () -> {
// Bypass the check once the user confirms
changeNoteTypeWithErrorHandling(oldModel, newModel);
};
dialog.setConfirm(confirm);
showDialogFragment(dialog);
} else {
// Otherwise go straight to changing note type
changeNoteTypeWithErrorHandling(oldModel, newModel);
}
return;
}
// Regular changes in note content
boolean modified = false;
// changed did? this has to be done first as remFromDyn() involves a direct write to the database
if (mCurrentEditedCard != null && mCurrentEditedCard.getDid() != mCurrentDid) {
mReloadRequired = true;
// remove card from filtered deck first (if relevant)
getCol().getSched().remFromDyn(new long[] { mCurrentEditedCard.getId() });
// refresh the card object to reflect the database changes in remFromDyn()
mCurrentEditedCard.load();
// also reload the note object
mEditorNote = mCurrentEditedCard.note();
// then set the card ID to the new deck
mCurrentEditedCard.setDid(mCurrentDid);
modified = true;
}
// now load any changes to the fields from the form
for (FieldEditText f : mEditFields) {
modified = modified | updateField(f);
}
// added tag?
for (String t : mSelectedTags) {
modified = modified || !mEditorNote.hasTag(t);
}
// removed tag?
modified = modified || mEditorNote.getTags().size() > mSelectedTags.size();
if (modified) {
mEditorNote.setTagsFromStr(tagsAsString(mSelectedTags));
mChanged = true;
}
closeNoteEditor();
}
}
Aggregations