Search in sources :

Example 16 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project Anki-Android by ankidroid.

the class CardBrowser method showEditTagsDialog.

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);
}
Also used : TemplateRenderOutput(com.ichi2.libanki.TemplateManager.TemplateRenderContext.TemplateRenderOutput) Arrays(java.util.Arrays) Bundle(android.os.Bundle) SortOrder(com.ichi2.libanki.SortOrder) Deck(com.ichi2.libanki.Deck) NonNull(androidx.annotation.NonNull) Decks(com.ichi2.libanki.Decks) Map(java.util.Map) JSONException(com.ichi2.utils.JSONException) Upgrade(com.ichi2.upgrade.Upgrade) IntentFilter(android.content.IntentFilter) Set(java.util.Set) SearchView(androidx.appcompat.widget.SearchView) JSONObject(com.ichi2.utils.JSONObject) LanguageUtil.getLocaleCompat(com.ichi2.utils.LanguageUtil.getLocaleCompat) Unit(kotlin.Unit) Nullable(androidx.annotation.Nullable) TagsDialogListener(com.ichi2.anki.dialogs.tags.TagsDialogListener) Consts(com.ichi2.libanki.Consts) ActivityResultContracts(androidx.activity.result.contract.ActivityResultContracts) CompatHelper(com.ichi2.compat.CompatHelper) TagsDialogFactory(com.ichi2.anki.dialogs.tags.TagsDialogFactory) RustCleanup(net.ankiweb.rsdroid.RustCleanup) HandlerUtils(com.ichi2.utils.HandlerUtils) TagsDialog(com.ichi2.anki.dialogs.tags.TagsDialog) Direction(com.ichi2.anim.ActivityTransitionAnimation.Direction) SystemClock(android.os.SystemClock) RescheduleDialog(com.ichi2.anki.dialogs.RescheduleDialog) SdCardReceiver(com.ichi2.anki.receiver.SdCardReceiver) Column(com.ichi2.anki.CardBrowser.Column) ArrayList(java.util.ArrayList) Menu(android.view.Menu) SimpleMessageDialog(com.ichi2.anki.dialogs.SimpleMessageDialog) LinkedHashSet(java.util.LinkedHashSet) IntegerDialog(com.ichi2.anki.dialogs.IntegerDialog) CheckResult(androidx.annotation.CheckResult) TextUtils(android.text.TextUtils) CardBrowserSearchView(com.ichi2.ui.CardBrowserSearchView) ArrayAdapter(android.widget.ArrayAdapter) SharedPreferences(android.content.SharedPreferences) TypedValue(android.util.TypedValue) SECONDS_PER_DAY(com.ichi2.libanki.stats.Stats.SECONDS_PER_DAY) LanguageUtil(com.ichi2.utils.LanguageUtil) SchedulerService(com.ichi2.anki.servicelayer.SchedulerService) CardBrowserOrderDialog(com.ichi2.anki.dialogs.CardBrowserOrderDialog) TaskManager(com.ichi2.async.TaskManager) WindowManager(android.view.WindowManager) Compat(com.ichi2.compat.Compat) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog) Permissions(com.ichi2.utils.Permissions) FunctionalInterfaces(com.ichi2.utils.FunctionalInterfaces) CheckBox(android.widget.CheckBox) Locale(java.util.Locale) View(android.view.View) AdapterView(android.widget.AdapterView) CardBrowserMySearchesDialog(com.ichi2.anki.dialogs.CardBrowserMySearchesDialog) HashUtil(com.ichi2.utils.HashUtil) BroadcastReceiver(android.content.BroadcastReceiver) Collectors(java.util.stream.Collectors) ViewGroup(android.view.ViewGroup) Timber(timber.log.Timber) AlertDialog(android.app.AlertDialog) UndoService(com.ichi2.anki.servicelayer.UndoService) List(java.util.List) TextView(android.widget.TextView) OnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener) BaseAdapter(android.widget.BaseAdapter) TaskListenerWithContext(com.ichi2.async.TaskListenerWithContext) ListView(android.widget.ListView) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Snackbar(com.google.android.material.snackbar.Snackbar) SearchCardsResult(com.ichi2.anki.servicelayer.SearchService.SearchCardsResult) Typeface(android.graphics.Typeface) Note(com.ichi2.libanki.Note) Context(android.content.Context) NextCard(com.ichi2.anki.servicelayer.SchedulerService.NextCard) KeyEvent(android.view.KeyEvent) Pair(android.util.Pair) Intent(android.content.Intent) HashMap(java.util.HashMap) Collection(com.ichi2.libanki.Collection) MenuItem(android.view.MenuItem) HashSet(java.util.HashSet) DeckDropDownAdapter(com.ichi2.anki.widgets.DeckDropDownAdapter) WidgetStatus(com.ichi2.widget.WidgetStatus) Utils(com.ichi2.libanki.Utils) NoteService(com.ichi2.anki.servicelayer.NoteService) Computation(com.ichi2.utils.Computation) Iterator(java.util.Iterator) ActivityResultLauncher(androidx.activity.result.ActivityResultLauncher) LayoutInflater(android.view.LayoutInflater) AbsListView(android.widget.AbsListView) CollectionTask(com.ichi2.async.CollectionTask) Themes(com.ichi2.themes.Themes) Spinner(android.widget.Spinner) Consumer(java.util.function.Consumer) TagsUtil(com.ichi2.utils.TagsUtil) Card(com.ichi2.libanki.Card) VisibleForTesting(androidx.annotation.VisibleForTesting) Collections(java.util.Collections) DeckSelectionDialog(com.ichi2.anki.dialogs.DeckSelectionDialog) Note(com.ichi2.libanki.Note) ArrayList(java.util.ArrayList) TagsDialog(com.ichi2.anki.dialogs.tags.TagsDialog) ArrayList(java.util.ArrayList) List(java.util.List)

Example 17 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project Anki-Android by ankidroid.

the class CardBrowser method editSelectedCardsTags.

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));
    }
    Timber.i("CardBrowser:: editSelectedCardsTags: Saving note/s tags...");
    TaskManager.launchCollectionTask(new CollectionTask.UpdateMultipleNotes(selectedNotes), updateMultipleNotesHandler());
}
Also used : Note(com.ichi2.libanki.Note) CollectionTask(com.ichi2.async.CollectionTask)

Example 18 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project Anki-Android by ankidroid.

the class NoteEditor method onCreate.

// ----------------------------------------------------------------------------
// ANDROID METHODS
// ----------------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
    if (showedActivityFailedScreen(savedInstanceState)) {
        return;
    }
    Timber.d("onCreate()");
    mTagsDialogFactory = new TagsDialogFactory(this).attachToActivity(this);
    mMediaRegistration = new MediaRegistration(this);
    super.onCreate(savedInstanceState);
    mFieldState.setInstanceState(savedInstanceState);
    setContentView(R.layout.note_editor);
    Intent intent = getIntent();
    if (savedInstanceState != null) {
        mCaller = savedInstanceState.getInt("caller");
        mAddNote = savedInstanceState.getBoolean("addNote");
        mCurrentDid = savedInstanceState.getLong("did");
        mSelectedTags = savedInstanceState.getStringArrayList("tags");
        mReloadRequired = savedInstanceState.getBoolean("reloadRequired");
        mPastedImageCache = (HashMap<String, String>) savedInstanceState.getSerializable("imageCache");
        mToggleStickyText = (HashMap<Integer, String>) savedInstanceState.getSerializable("toggleSticky");
        mChanged = savedInstanceState.getBoolean("changed");
    } else {
        mCaller = intent.getIntExtra(EXTRA_CALLER, CALLER_NOCALLER);
        if (mCaller == CALLER_NOCALLER) {
            String action = intent.getAction();
            if ((ACTION_CREATE_FLASHCARD.equals(action) || ACTION_CREATE_FLASHCARD_SEND.equals(action) || ACTION_PROCESS_TEXT.equals(action))) {
                mCaller = CALLER_CARDEDITOR_INTENT_ADD;
            }
        }
    }
    startLoadingCollection();
    mOnboarding.onCreate();
}
Also used : Intent(android.content.Intent) TagsDialogFactory(com.ichi2.anki.dialogs.tags.TagsDialogFactory)

Example 19 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project Anki-Android by ankidroid.

the class NoteEditor method hasUnsavedChanges.

private boolean hasUnsavedChanges() {
    if (!collectionHasLoaded()) {
        return false;
    }
    // changed note type?
    if (!mAddNote && mCurrentEditedCard != null) {
        final JSONObject newModel = getCurrentlySelectedModel();
        final JSONObject oldModel = mCurrentEditedCard.model();
        if (!newModel.equals(oldModel)) {
            return true;
        }
    }
    // changed deck?
    if (!mAddNote && mCurrentEditedCard != null && mCurrentEditedCard.getDid() != mCurrentDid) {
        return true;
    }
    // changed fields?
    if (mFieldEdited) {
        return true;
    }
    // changed tags?
    return mTagsEdited;
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Example 20 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project Anki-Android by ankidroid.

the class Collection method _renderQA.

@RustCleanup("#8951 - Remove FrontSide added to the front")
@NonNull
public HashMap<String, String> _renderQA(long cid, Model model, long did, int ord, String tags, String[] flist, int flags, boolean browser, String qfmt, String afmt) {
    // data is [cid, nid, mid, did, ord, tags, flds, cardFlags]
    // unpack fields and create dict
    Map<String, Pair<Integer, JSONObject>> fmap = Models.fieldMap(model);
    Set<Map.Entry<String, Pair<Integer, JSONObject>>> maps = fmap.entrySet();
    Map<String, String> fields = HashUtil.HashMapInit(maps.size() + 8);
    for (Map.Entry<String, Pair<Integer, JSONObject>> entry : maps) {
        fields.put(entry.getKey(), flist[entry.getValue().first]);
    }
    int cardNum = ord + 1;
    fields.put("Tags", tags.trim());
    fields.put("Type", model.getString("name"));
    fields.put("Deck", mDecks.name(did));
    String baseName = Decks.basename(fields.get("Deck"));
    fields.put("Subdeck", baseName);
    fields.put("CardFlag", _flagNameFromCardFlags(flags));
    JSONObject template;
    if (model.isStd()) {
        template = model.getJSONArray("tmpls").getJSONObject(ord);
    } else {
        template = model.getJSONArray("tmpls").getJSONObject(0);
    }
    fields.put("Card", template.getString("name"));
    fields.put(String.format(Locale.US, "c%d", cardNum), "1");
    // render q & a
    HashMap<String, String> d = HashUtil.HashMapInit(2);
    d.put("id", Long.toString(cid));
    qfmt = TextUtils.isEmpty(qfmt) ? template.getString("qfmt") : qfmt;
    afmt = TextUtils.isEmpty(afmt) ? template.getString("afmt") : afmt;
    for (Pair<String, String> p : new Pair[] { new Pair<>("q", qfmt), new Pair<>("a", afmt) }) {
        String type = p.first;
        String format = p.second;
        if ("q".equals(type)) {
            format = fClozePatternQ.matcher(format).replaceAll(String.format(Locale.US, "{{$1cq-%d:", cardNum));
            format = fClozeTagStart.matcher(format).replaceAll(String.format(Locale.US, "<%%cq:%d:", cardNum));
            fields.put("FrontSide", "");
        } else {
            format = fClozePatternA.matcher(format).replaceAll(String.format(Locale.US, "{{$1ca-%d:", cardNum));
            format = fClozeTagStart.matcher(format).replaceAll(String.format(Locale.US, "<%%ca:%d:", cardNum));
            // the following line differs from libanki // TODO: why?
            // fields.put("FrontSide", mMedia.stripAudio(d.get("q")));
            fields.put("FrontSide", d.get("q"));
        }
        String html;
        try {
            html = ParsedNode.parse_inner(format).render(fields, "q".equals(type), getContext());
        } catch (TemplateError er) {
            Timber.w(er);
            html = er.message(getContext());
        }
        html = ChessFilter.fenToChessboard(html, getContext());
        if (!browser) {
            // browser don't show image. So compiling LaTeX actually remove information.
            html = LaTeX.mungeQA(html, this, model);
        }
        d.put(type, html);
        // empty cloze?
        if ("q".equals(type) && model.isCloze()) {
            if (Models._availClozeOrds(model, flist, false).isEmpty()) {
                String link = String.format("<a href=\"%s\">%s</a>", mContext.getResources().getString(R.string.link_ankiweb_docs_cloze_deletion), "help");
                System.out.println(link);
                d.put("q", mContext.getString(R.string.empty_cloze_warning, link));
            }
        }
    }
    return d;
}
Also used : SuppressLint(android.annotation.SuppressLint) JSONObject(com.ichi2.utils.JSONObject) TemplateError(com.ichi2.libanki.template.TemplateError) Map(java.util.Map) HashMap(java.util.HashMap) Pair(android.util.Pair) RustCleanup(net.ankiweb.rsdroid.RustCleanup) NonNull(androidx.annotation.NonNull)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)27 Note (com.ichi2.libanki.Note)18 JSONArray (com.ichi2.utils.JSONArray)18 ArrayList (java.util.ArrayList)13 Card (com.ichi2.libanki.Card)12 Collection (com.ichi2.libanki.Collection)12 Model (com.ichi2.libanki.Model)11 Map (java.util.Map)10 HashMap (java.util.HashMap)9 SuppressLint (android.annotation.SuppressLint)8 Resources (android.content.res.Resources)8 Deck (com.ichi2.libanki.Deck)8 JSONException (com.ichi2.utils.JSONException)8 JSONException (org.json.JSONException)8 Intent (android.content.Intent)7 View (android.view.View)7 TextView (android.widget.TextView)7 Pair (android.util.Pair)5 ContentValues (android.content.ContentValues)4 Uri (android.net.Uri)4