Search in sources :

Example 6 with R

use of com.ichi2.anki.R in project AnkiChinaAndroid by ankichinateam.

the class Syncer method mergeDecks.

private void mergeDecks(JSONArray rchg) {
    JSONArray decks = rchg.getJSONArray(0);
    for (int i = 0; i < decks.length(); i++) {
        Deck r = new Deck(decks.getJSONObject(i));
        Deck l = mCol.getDecks().get(r.getLong("id"), false);
        // if missing locally or server is newer, update
        if (l == null || r.getLong("mod") > l.getLong("mod")) {
            mCol.getDecks().update(r);
        }
    }
    JSONArray confs = rchg.getJSONArray(1);
    for (int i = 0; i < confs.length(); i++) {
        DeckConfig r = new DeckConfig(confs.getJSONObject(i));
        DeckConfig l = mCol.getDecks().getConf(r.getLong("id"));
        // if missing locally or server is newer, update
        if (l == null || r.getLong("mod") > l.getLong("mod")) {
            mCol.getDecks().updateConf(r);
        }
    }
}
Also used : JSONArray(com.ichi2.utils.JSONArray) Deck(com.ichi2.libanki.Deck) DeckConfig(com.ichi2.libanki.DeckConfig)

Example 7 with R

use of com.ichi2.anki.R in project AnkiChinaAndroid by ankichinateam.

the class Syncer method chunk.

public JSONObject chunk() {
    JSONObject buf = new JSONObject();
    buf.put("done", false);
    int lim = 250;
    List<Integer> colTypes = null;
    while (!mTablesLeft.isEmpty() && lim > 0) {
        String curTable = mTablesLeft.getFirst();
        if (mCursor == null) {
            mCursor = cursorForTable(curTable);
        }
        colTypes = columnTypesForQuery(curTable);
        JSONArray rows = new JSONArray();
        int count = mCursor.getColumnCount();
        int fetched = 0;
        while (mCursor.moveToNext()) {
            JSONArray r = new JSONArray();
            for (int i = 0; i < count; i++) {
                switch(colTypes.get(i)) {
                    case TYPE_STRING:
                        r.put(mCursor.getString(i));
                        break;
                    case TYPE_FLOAT:
                        r.put(mCursor.getDouble(i));
                        break;
                    case TYPE_INTEGER:
                        r.put(mCursor.getLong(i));
                        break;
                }
            }
            rows.put(r);
            if (++fetched == lim) {
                break;
            }
        }
        if (fetched != lim) {
            // table is empty
            mTablesLeft.removeFirst();
            mCursor.close();
            mCursor = null;
            // if we're the client, mark the objects as having been sent
            if (!mCol.getServer()) {
                mCol.getDb().execute("UPDATE " + curTable + " SET usn=" + mMaxUsn + " WHERE usn=-1");
            }
        }
        buf.put(curTable, rows);
        lim -= fetched;
    }
    if (mTablesLeft.isEmpty()) {
        buf.put("done", true);
    }
    return buf;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray)

Example 8 with R

use of com.ichi2.anki.R in project AnkiChinaAndroid by ankichinateam.

the class NoteEditorTest method stickyFieldsAreUnchangedAfterAdd.

@Test
public void stickyFieldsAreUnchangedAfterAdd() {
    // #6795 - newlines were converted to <br>
    Model basic = makeNoteForType(NoteType.BASIC);
    // Enable sticky "Front" field
    basic.getJSONArray("flds").getJSONObject(0).put("sticky", true);
    String initFirstField = "Hello";
    String initSecondField = "unused";
    // /r/n on Windows under Robolectric
    String newFirstField = "Hello" + FieldEditText.NEW_LINE + "World";
    NoteEditor editor = getNoteEditorAdding(NoteType.BASIC).withFirstField(initFirstField).withSecondField(initSecondField).build();
    assertThat(Arrays.asList(editor.getCurrentFieldStrings()), contains(initFirstField, initSecondField));
    editor.setFieldValueFromUi(0, newFirstField);
    assertThat(Arrays.asList(editor.getCurrentFieldStrings()), contains(newFirstField, initSecondField));
    editor.saveNote();
    this.waitForAsyncTasksToComplete();
    List<String> actual = Arrays.asList(editor.getCurrentFieldStrings());
    assertThat("newlines should be preserved, second field should be blanked", actual, contains(newFirstField, ""));
}
Also used : Model(com.ichi2.libanki.Model) Test(org.junit.Test)

Example 9 with R

use of com.ichi2.anki.R in project AnkiChinaAndroid by ankichinateam.

the class ReviewerTest method assertCounts.

private void assertCounts(Reviewer r, int newCount, int stepCount, int revCount) {
    List<String> countList = new ArrayList<>();
    JavaScriptFunction jsApi = r.javaScriptFunction();
    countList.add(jsApi.ankiGetNewCardCount());
    countList.add(jsApi.ankiGetLrnCardCount());
    countList.add(jsApi.ankiGetRevCardCount());
    List<Integer> expexted = new ArrayList<>();
    expexted.add(newCount);
    expexted.add(stepCount);
    expexted.add(revCount);
    // We use toString as hamcrest does not print the whole array and stops at [0].
    assertThat(countList.toString(), is(expexted.toString()));
}
Also used : JavaScriptFunction(com.ichi2.anki.AbstractFlashcardViewer.JavaScriptFunction) ArrayList(java.util.ArrayList) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString)

Example 10 with R

use of com.ichi2.anki.R in project Anki-Android by ankidroid.

the class MediaTest method testAdd.

@Test
public void testAdd() throws IOException, EmptyMediaException {
    // open new empty collection
    File dir = getTestDir();
    BackupManager.removeDir(dir);
    assertTrue(dir.mkdirs());
    File path = new File(dir, "foo.jpg");
    FileOutputStream os = new FileOutputStream(path, false);
    os.write("hello".getBytes());
    os.close();
    // new file, should preserve name
    String r = mTestCol.getMedia().addFile(path);
    assertEquals("foo.jpg", r);
    // adding the same file again should not create a duplicate
    assertEquals("foo.jpg", mTestCol.getMedia().addFile(path));
    // but if it has a different md5, it should
    os = new FileOutputStream(path, false);
    os.write("world".getBytes());
    os.close();
    assertNotEquals("foo.jpg", mTestCol.getMedia().addFile(path));
}
Also used : FileOutputStream(java.io.FileOutputStream) File(java.io.File) Test(org.junit.Test) InstrumentedTest(com.ichi2.anki.tests.InstrumentedTest)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)17 JSONArray (com.ichi2.utils.JSONArray)14 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)7 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)6 Model (com.ichi2.libanki.Model)6 EditText (android.widget.EditText)5 File (java.io.File)5 Random (java.util.Random)5 Nullable (androidx.annotation.Nullable)4 ConfirmationDialog (com.ichi2.anki.dialogs.ConfirmationDialog)4 Connection (com.ichi2.async.Connection)4 IOException (java.io.IOException)4 Intent (android.content.Intent)3 SharedPreferences (android.content.SharedPreferences)3 Cursor (android.database.Cursor)3 Bundle (android.os.Bundle)3 Timber (timber.log.Timber)3 Context (android.content.Context)2 LayoutInflater (android.view.LayoutInflater)2