use of com.orgzly.org.parser.OrgParsedFile in project orgzly-android by orgzly.
the class Shelf method reParseNotesStateAndTitles.
/**
* Using current states configuration, update states and titles for all notes.
* Keywords that were part of the title can become states and vice versa.
*/
public void reParseNotesStateAndTitles() throws IOException {
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
/* Get all notes. */
Cursor cursor = mContext.getContentResolver().query(ProviderContract.Notes.ContentUri.notes(), null, null, null, null);
try {
OrgParser.Builder parserBuilder = new OrgParser.Builder().setTodoKeywords(AppPreferences.todoKeywordsSet(mContext)).setDoneKeywords(AppPreferences.doneKeywordsSet(mContext));
OrgParserWriter parserWriter = new OrgParserWriter();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
/* Get current heading string. */
Note note = NotesClient.fromCursor(cursor);
/* Skip root node. */
if (note.getPosition().getLevel() == 0) {
continue;
}
OrgHead head = note.getHead();
String headString = parserWriter.whiteSpacedHead(head, note.getPosition().getLevel(), false);
/* Re-parse heading using current setting of keywords. */
OrgParsedFile file = parserBuilder.setInput(headString).build().parse();
if (BuildConfig.LOG_DEBUG)
LogUtils.d(TAG, "Note " + note + " parsed has " + file.getHeadsInList().size() + " notes");
if (file.getHeadsInList().size() != 1) {
throw new IOException("Got " + file.getHeadsInList().size() + " notes after parsing \"" + headString + "\"");
}
OrgHead newHead = file.getHeadsInList().get(0).getHead();
ContentValues values = new ContentValues();
/* Update if state, title or priority are different. */
if (!TextUtils.equals(newHead.getState(), head.getState()) || !TextUtils.equals(newHead.getTitle(), head.getTitle()) || !TextUtils.equals(newHead.getPriority(), head.getPriority())) {
values.put(ProviderContract.Notes.UpdateParam.TITLE, newHead.getTitle());
values.put(ProviderContract.Notes.UpdateParam.STATE, newHead.getState());
values.put(ProviderContract.Notes.UpdateParam.PRIORITY, newHead.getPriority());
}
if (values.size() > 0) {
ops.add(ContentProviderOperation.newUpdate(ContentUris.withAppendedId(ProviderContract.Notes.ContentUri.notes(), cursor.getLong(0))).withValues(values).build());
}
}
} finally {
cursor.close();
}
/*
* Apply batch.
*/
try {
mContext.getContentResolver().applyBatch(ProviderContract.AUTHORITY, ops);
} catch (RemoteException | OperationApplicationException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
notifyDataChanged(mContext);
}
Aggregations