Skip to content

Commit

Permalink
WIP: gui multi tags edit
Browse files Browse the repository at this point in the history
this is wip commit
  • Loading branch information
TarekkMA committed May 2, 2021
1 parent b2f875e commit 0ee0b30
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
68 changes: 67 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import com.ichi2.libanki.Collection;
import com.ichi2.libanki.Consts;
import com.ichi2.libanki.Decks;
import com.ichi2.libanki.Note;
import com.ichi2.libanki.Utils;
import com.ichi2.libanki.Deck;
import com.ichi2.themes.Themes;
Expand All @@ -90,6 +91,7 @@
import com.ichi2.utils.PairWithBoolean;
import com.ichi2.utils.PairWithCard;
import com.ichi2.utils.Permissions;
import com.ichi2.utils.TagsUtil;
import com.ichi2.widget.WidgetStatus;

import com.ichi2.utils.JSONException;
Expand All @@ -107,6 +109,7 @@
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import timber.log.Timber;

Expand Down Expand Up @@ -140,7 +143,8 @@ enum Column {
}

private enum TagsDialogListenerAction {
FILTER
FILTER,
EDIT_TAGS,
}

/** List of cards in the browser.
Expand Down Expand Up @@ -1262,6 +1266,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
startActivityWithAnimation(intent, FADE);
}
return true;
} else if (itemId == R.id.action_edit_tags) {
showEditTagsDialog();
}
return super.onOptionsItemSelected(item);
}
Expand Down Expand Up @@ -1429,6 +1435,48 @@ private long getReviewerCardId() {
}
}

private void showEditTagsDialog() {
if (getSelectedCardIds().isEmpty()) {
Timber.d("showEditTagsDialog: called with empty selection");
}

final ArrayList<String> allTags = new ArrayList<>(getCol().getTags().all());


List<Note> selectedNotes = getSelectedCardIds()
.stream()
.map(cardId -> getCol().getCard(cardId).note())
.distinct()
.collect(Collectors.toList());

final ArrayList<String> checkedTags = selectedNotes
.stream()
.flatMap(note -> note.getTags().stream())
.collect(Collectors.toCollection(ArrayList::new));

if (selectedNotes.size() == 1) {
Timber.d("showEditTagsDialog: edit tags for one note");
mTagsDialogListenerAction = TagsDialogListenerAction.EDIT_TAGS;
TagsDialog dialog = mTagsDialogFactory.newTagsDialog().withArguments(TagsDialog.DialogType.EDIT_TAGS, checkedTags, allTags);
showDialogFragment(dialog);
return;
}

final ArrayList<String> uncheckedTags = selectedNotes
.stream()
.flatMap(note -> {
final List<String> noteTags = note.getTags();
return allTags.stream().filter(t -> !noteTags.contains(t));
})
.collect(Collectors.toCollection(ArrayList::new));

Timber.d("showEditTagsDialog: edit tags for multiple note");
mTagsDialogListenerAction = TagsDialogListenerAction.EDIT_TAGS;
TagsDialog dialog = mTagsDialogFactory.newTagsDialog().withArguments(TagsDialog.DialogType.EDIT_TAGS,
checkedTags, uncheckedTags, allTags);
showDialogFragment(dialog);
}

private void showFilterByTagsDialog() {
mTagsDialogListenerAction = TagsDialogListenerAction.FILTER;
TagsDialog dialog = mTagsDialogFactory.newTagsDialog().withArguments(
Expand Down Expand Up @@ -1611,6 +1659,24 @@ public void onSelectedTags(List<String> selectedTags, List<String> indeterminate
case FILTER:
filterByTags(selectedTags, option);
break;
case EDIT_TAGS:
editSelectedCardsTags(selectedTags, indeterminateTags);
break;
}
}


private void editSelectedCardsTags(List<String> selectedTags, List<String> indeterminateTags) {
List<Note> selectedNotes = getSelectedCardIds()
.stream()
.map(cardId -> getCol().getCard(cardId).note())
.distinct()
.collect(Collectors.toList());

for (Note note : selectedNotes) {
List<String> previousTags = note.getTags();
List<String> updatedTags = TagsUtil.getUpdatedTags(previousTags, selectedTags, indeterminateTags);
note.setTagsFromStr(getCol().getTags().join(updatedTags));
}
}

Expand Down
4 changes: 4 additions & 0 deletions AnkiDroid/src/main/res/menu/card_browser_multiselect.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
android:id="@+id/action_reschedule_cards"
android:title="@string/card_editor_reschedule_card"/>

<item
android:id="@+id/action_edit_tags"
android:title="@string/menu_edit_tags"/>

<item
android:id="@+id/action_reset_cards_progress"
android:title="@string/card_editor_reset_card"/>
Expand Down

0 comments on commit 0ee0b30

Please sign in to comment.