Search in sources :

Example 1 with ItemMap

use of email.schaal.ocreader.api.json.v12.ItemMap in project ocreader by schaal.

the class APIv12 method markItems.

private boolean markItems(@NonNull final MarkAction action, final Realm realm) throws IOException {
    final RealmResults<Item> results = realm.where(Item.class).equalTo(action.getChangedKey(), true).equalTo(action.getKey(), action.getValue()).findAll();
    if (results.size() == 0) {
        // Nothing to do, countdown and return
        return true;
    }
    ItemIds ids = null;
    ItemMap itemMap = null;
    if (action == MarkAction.MARK_UNREAD || action == MarkAction.MARK_READ) {
        ids = new ItemIds(results);
    } else {
        itemMap = new ItemMap(results);
    }
    Response<Void> response;
    switch(action) {
        case MARK_READ:
            response = api.markItemsRead(ids).execute();
            break;
        case MARK_UNREAD:
            response = api.markItemsUnread(ids).execute();
            break;
        case MARK_STARRED:
            response = api.markItemsStarred(itemMap).execute();
            break;
        case MARK_UNSTARRED:
            response = api.markItemsUnstarred(itemMap).execute();
            break;
        default:
            throw new IllegalArgumentException("Unkown mark action");
    }
    if (response.isSuccessful()) {
        realm.executeTransaction(realm1 -> {
            if (action == MarkAction.MARK_READ || action == MarkAction.MARK_UNREAD) {
                for (Item item : results) {
                    item.setUnreadChanged(false);
                }
            } else {
                for (Item item : results) {
                    item.setStarredChanged(false);
                }
            }
        });
    }
    return response.isSuccessful();
}
Also used : Item(email.schaal.ocreader.database.model.Item) ItemMap(email.schaal.ocreader.api.json.v12.ItemMap) ItemIds(email.schaal.ocreader.api.json.v12.ItemIds)

Aggregations

ItemIds (email.schaal.ocreader.api.json.v12.ItemIds)1 ItemMap (email.schaal.ocreader.api.json.v12.ItemMap)1 Item (email.schaal.ocreader.database.model.Item)1