Search in sources :

Example 46 with CHANGED

use of com.ichi2.anki.CardBrowser.Column.CHANGED in project AnkiChinaAndroid by ankichinateam.

the class Models method flush.

/**
 * Flush the registry if any models were changed.
 */
public void flush() {
    if (mChanged) {
        ensureNotEmpty();
        JSONObject array = new JSONObject();
        for (Map.Entry<Long, Model> o : mModels.entrySet()) {
            array.put(Long.toString(o.getKey()), o.getValue());
        }
        ContentValues val = new ContentValues();
        val.put("models", Utils.jsonToString(array));
        mCol.getDb().update("col", val);
        mChanged = false;
    }
}
Also used : ContentValues(android.content.ContentValues) JSONObject(com.ichi2.utils.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 47 with CHANGED

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

the class CardBrowserTest method flagsAreShownInBigDecksTest.

@Test
public void flagsAreShownInBigDecksTest() {
    int numberOfNotes = 75;
    CardBrowser cardBrowser = getBrowserWithNotes(numberOfNotes);
    // select a random card
    Random random = new Random(1);
    int cardPosition = random.nextInt(numberOfNotes);
    assumeThat("card position to select is 60", cardPosition, is(60));
    cardBrowser.checkCardsAtPositions(cardPosition);
    assumeTrue("card at position 60 is selected", cardBrowser.hasCheckedCardAtPosition(cardPosition));
    // flag the selected card with flag = 1
    final int flag = 1;
    cardBrowser.flagTask(flag);
    advanceRobolectricLooperWithSleep();
    // check if card flag turned to flag = 1
    assertThat("Card should be flagged", getCheckedCard(cardBrowser).getCard().userFlag(), is(flag));
    // unflag the selected card with flag = 0
    final int unflagFlag = 0;
    cardBrowser.flagTask(unflagFlag);
    advanceRobolectricLooperWithSleep();
    // check if card flag actually changed from flag = 1
    assertThat("Card flag should be removed", getCheckedCard(cardBrowser).getCard().userFlag(), not(flag));
    // deselect and select all cards
    cardBrowser.onSelectNone();
    cardBrowser.onSelectAll();
    // flag all the cards with flag = 3
    final int flagForAll = 3;
    cardBrowser.flagTask(flagForAll);
    advanceRobolectricLooperWithSleep();
    // check if all card flags turned to flag = 3
    assertThat("All cards should be flagged", stream(cardBrowser.getCardIds()).map(cardId -> getCardFlagAfterFlagChangeDone(cardBrowser, cardId)).noneMatch(flag1 -> flag1 != flagForAll));
}
Also used : ActivityController(org.robolectric.android.controller.ActivityController) Bundle(android.os.Bundle) Deck(com.ichi2.libanki.Deck) SortOrder(com.ichi2.libanki.SortOrder) TaskManager(com.ichi2.async.TaskManager) Matchers.not(org.hamcrest.Matchers.not) Random(java.util.Random) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) ApplicationProvider(androidx.test.core.app.ApplicationProvider) ShadowApplication(org.robolectric.shadows.ShadowApplication) Manifest(android.Manifest) Locale(java.util.Locale) Is.is(org.hamcrest.core.Is.is) View(android.view.View) AdapterView(android.widget.AdapterView) AnkiAssert(com.ichi2.testutils.AnkiAssert) ShadowActivity(org.robolectric.shadows.ShadowActivity) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Timber(timber.log.Timber) Objects(java.util.Objects) List(java.util.List) StringRes(androidx.annotation.StringRes) Consts(com.ichi2.libanki.Consts) Application(android.app.Application) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) ListView(android.widget.ListView) Arrays.stream(java.util.Arrays.stream) Matchers.containsString(org.hamcrest.Matchers.containsString) Note(com.ichi2.libanki.Note) RunWith(org.junit.runner.RunWith) Config(org.robolectric.annotation.Config) Intent(android.content.Intent) Shadows.shadowOf(org.robolectric.Shadows.shadowOf) MenuItem(android.view.MenuItem) HashSet(java.util.HashSet) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) IntentAssert(com.ichi2.testutils.IntentAssert) ComponentName(android.content.ComponentName) Robolectric(org.robolectric.Robolectric) Assert.assertTrue(org.junit.Assert.assertTrue) CollectionTask(com.ichi2.async.CollectionTask) Test(org.junit.Test) CheckReturnValue(javax.annotation.CheckReturnValue) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Ignore(org.junit.Ignore) Card(com.ichi2.libanki.Card) Assert(org.junit.Assert) Activity(android.app.Activity) Collections(java.util.Collections) Random(java.util.Random) Test(org.junit.Test)

Example 48 with CHANGED

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

the class DeckOptionsTest method changeHardFactor.

@Test
public void changeHardFactor() {
    Collection col = getCol();
    // Verify that for newly created deck hardFactor is default.
    double hardFactor = col.get_config("hardFactor", 1.2);
    Assert.assertEquals(1.2, hardFactor, 0.01);
    // Modify hard factor.
    col.set_config("hardFactor", 1.0);
    // Verify that hardFactor value has changed.
    hardFactor = col.get_config("hardFactor", 1.2);
    Assert.assertEquals(1.0, hardFactor, 0.01);
}
Also used : Collection(com.ichi2.libanki.Collection) Test(org.junit.Test)

Example 49 with CHANGED

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

the class Decks method update.

/**
 * {@inheritDoc}
 */
@Override
public void update(@NonNull Deck g) {
    long id = g.getLong("id");
    JSONObject oldDeck = get(id, false);
    if (oldDeck != null) {
        // In case where another update got the name
        // `oldName`, it would be a mistake to remove it from nameMap
        mNameMap.remove(oldDeck.getString("name"), oldDeck);
    }
    mNameMap.add(g);
    mDecks.put(g.getLong("id"), g);
    maybeAddToActive();
    // mark registry changed, but don't bump mod time
    save();
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Example 50 with CHANGED

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

the class NoteEditor method saveNote.

@VisibleForTesting
void saveNote() {
    final Resources res = getResources();
    if (mSelectedTags == null) {
        mSelectedTags = new ArrayList<>(0);
    }
    saveToggleStickyMap();
    // 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;
        TaskManager.launchCollectionTask(new CollectionTask.AddNote(mEditorNote), saveNoteHandler());
        updateFieldsFromStickyText();
    } else {
        // Check whether note type has been changed
        final Model newModel = getCurrentlySelectedModel();
        final Model oldModel = (mCurrentEditedCard == null) ? null : mCurrentEditedCard.model();
        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();
    }
}
Also used : JSONArray(com.ichi2.utils.JSONArray) Model(com.ichi2.libanki.Model) Resources(android.content.res.Resources) CollectionTask(com.ichi2.async.CollectionTask) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog) VisibleForTesting(androidx.annotation.VisibleForTesting)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)19 Test (org.junit.Test)19 Note (com.ichi2.libanki.Note)16 Card (com.ichi2.libanki.Card)15 Collection (com.ichi2.libanki.Collection)15 Model (com.ichi2.libanki.Model)12 Intent (android.content.Intent)9 JSONArray (com.ichi2.utils.JSONArray)9 JSONException (com.ichi2.utils.JSONException)8 List (java.util.List)7 ShadowActivity (org.robolectric.shadows.ShadowActivity)7 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 ShadowIntent (org.robolectric.shadows.ShadowIntent)6 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)5 Cursor (android.database.Cursor)4 MatrixCursor (android.database.MatrixCursor)4 TaskData (com.ichi2.async.TaskData)4 Deck (com.ichi2.libanki.Deck)4 AbstractSched (com.ichi2.libanki.sched.AbstractSched)4