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);
}
}
}
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;
}
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, ""));
}
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()));
}
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));
}
Aggregations