Search in sources :

Example 6 with SgList

use of com.uwetrottmann.seriesguide.backend.lists.model.SgList in project SeriesGuide by UweTrottmann.

the class ReorderListsTask method doBackgroundAction.

@Override
protected Integer doBackgroundAction(Void... params) {
    if (isSendingToHexagon()) {
        Lists listsService = getContext().getHexagonTools().getListsService();
        if (listsService == null) {
            // no longer signed in
            return ERROR_HEXAGON_API;
        }
        // send lists with updated order to hexagon
        SgListList wrapper = new SgListList();
        List<SgList> lists = buildListsList(listIdsInOrder);
        wrapper.setLists(lists);
        try {
            listsService.save(wrapper).execute();
        } catch (IOException e) {
            HexagonTools.trackFailedRequest(getContext(), "reorder lists", e);
            return ERROR_HEXAGON_API;
        }
    }
    // update local state
    if (!doDatabaseUpdate()) {
        return ERROR_DATABASE;
    }
    return SUCCESS;
}
Also used : SgList(com.uwetrottmann.seriesguide.backend.lists.model.SgList) SgListList(com.uwetrottmann.seriesguide.backend.lists.model.SgListList) Lists(com.uwetrottmann.seriesguide.backend.lists.Lists) IOException(java.io.IOException)

Example 7 with SgList

use of com.uwetrottmann.seriesguide.backend.lists.model.SgList in project SeriesGuide by UweTrottmann.

the class ListsTools method downloadFromHexagon.

@SuppressLint("ApplySharedPref")
public static boolean downloadFromHexagon(SgApp app, boolean hasMergedLists) {
    long currentTime = System.currentTimeMillis();
    DateTime lastSyncTime = new DateTime(HexagonSettings.getLastListsSyncTime(app));
    if (hasMergedLists) {
        Timber.d("downloadFromHexagon: downloading lists changed since %s.", lastSyncTime);
    } else {
        Timber.d("downloadFromHexagon: downloading all lists.");
    }
    HashSet<String> localListIds = getListIds(app);
    List<SgList> lists;
    String cursor = null;
    do {
        try {
            Lists listsService = app.getHexagonTools().getListsService();
            if (listsService == null) {
                // no longer signed in
                return false;
            }
            // use default server limit
            Lists.Get request = listsService.get();
            if (hasMergedLists) {
                request.setUpdatedSince(lastSyncTime);
            }
            if (!TextUtils.isEmpty(cursor)) {
                request.setCursor(cursor);
            }
            SgListList response = request.execute();
            if (response == null) {
                Timber.d("downloadFromHexagon: failed, response is null.");
                break;
            }
            cursor = response.getCursor();
            lists = response.getLists();
        } catch (IOException e) {
            HexagonTools.trackFailedRequest(app, "get lists", e);
            return false;
        }
        if (lists == null || lists.size() == 0) {
            // empty response, assume we are done
            break;
        }
        if (!doListsDatabaseUpdate(app, lists, localListIds, hasMergedLists)) {
            // database update failed, abort
            return false;
        }
    } while (// fetch next batch
    !TextUtils.isEmpty(cursor));
    // set new last sync time
    if (hasMergedLists) {
        PreferenceManager.getDefaultSharedPreferences(app).edit().putLong(HexagonSettings.KEY_LAST_SYNC_LISTS, currentTime).commit();
    }
    return true;
}
Also used : SgList(com.uwetrottmann.seriesguide.backend.lists.model.SgList) SgListList(com.uwetrottmann.seriesguide.backend.lists.model.SgListList) Lists(com.uwetrottmann.seriesguide.backend.lists.Lists) IOException(java.io.IOException) DateTime(com.google.api.client.util.DateTime) SuppressLint(android.annotation.SuppressLint)

Example 8 with SgList

use of com.uwetrottmann.seriesguide.backend.lists.model.SgList in project SeriesGuide by UweTrottmann.

the class RemoveListItemTask method doBackgroundAction.

@Override
protected Integer doBackgroundAction(Void... params) {
    if (isSendingToHexagon()) {
        Lists listsService = getContext().getHexagonTools().getListsService();
        if (listsService == null) {
            // no longer signed in
            return ERROR_HEXAGON_API;
        }
        // extract the list id of this list item
        String[] splitListItemId = SeriesGuideContract.ListItems.splitListItemId(listItemId);
        if (splitListItemId == null) {
            return ERROR_DATABASE;
        }
        String removeFromListId = splitListItemId[2];
        // send the item to be removed from hexagon
        SgListList wrapper = new SgListList();
        List<SgList> lists = buildListItemLists(removeFromListId, listItemId);
        wrapper.setLists(lists);
        try {
            listsService.removeItems(wrapper).execute();
        } catch (IOException e) {
            HexagonTools.trackFailedRequest(getContext(), "remove list item", e);
            return ERROR_HEXAGON_API;
        }
    }
    // update local state
    if (!doDatabaseUpdate()) {
        return ERROR_DATABASE;
    }
    return SUCCESS;
}
Also used : SgList(com.uwetrottmann.seriesguide.backend.lists.model.SgList) SgListList(com.uwetrottmann.seriesguide.backend.lists.model.SgListList) Lists(com.uwetrottmann.seriesguide.backend.lists.Lists) IOException(java.io.IOException)

Example 9 with SgList

use of com.uwetrottmann.seriesguide.backend.lists.model.SgList in project SeriesGuide by UweTrottmann.

the class AddListTask method buildList.

@NonNull
private static List<SgList> buildList(@NonNull String listId, @NonNull String listName) {
    List<SgList> lists = new ArrayList<>(1);
    SgList list = new SgList();
    list.setListId(listId);
    list.setName(listName);
    lists.add(list);
    return lists;
}
Also used : SgList(com.uwetrottmann.seriesguide.backend.lists.model.SgList) ArrayList(java.util.ArrayList) NonNull(android.support.annotation.NonNull)

Example 10 with SgList

use of com.uwetrottmann.seriesguide.backend.lists.model.SgList in project SeriesGuide by UweTrottmann.

the class ChangeListItemListsTask method buildListItemLists.

@NonNull
private List<SgList> buildListItemLists(List<String> listsToChange) {
    List<SgList> lists = new ArrayList<>(listsToChange.size());
    for (String listId : listsToChange) {
        SgList list = new SgList();
        list.setListId(listId);
        lists.add(list);
        List<SgListItem> items = new ArrayList<>(1);
        list.setListItems(items);
        String listItemId = SeriesGuideContract.ListItems.generateListItemId(itemTvdbId, itemType, listId);
        SgListItem item = new SgListItem();
        items.add(item);
        item.setListItemId(listItemId);
    }
    return lists;
}
Also used : SgList(com.uwetrottmann.seriesguide.backend.lists.model.SgList) SgListItem(com.uwetrottmann.seriesguide.backend.lists.model.SgListItem) ArrayList(java.util.ArrayList) NonNull(android.support.annotation.NonNull)

Aggregations

SgList (com.uwetrottmann.seriesguide.backend.lists.model.SgList)11 SgListList (com.uwetrottmann.seriesguide.backend.lists.model.SgListList)6 ArrayList (java.util.ArrayList)6 Lists (com.uwetrottmann.seriesguide.backend.lists.Lists)5 IOException (java.io.IOException)5 NonNull (android.support.annotation.NonNull)4 SgListItem (com.uwetrottmann.seriesguide.backend.lists.model.SgListItem)4 SuppressLint (android.annotation.SuppressLint)3 ContentProviderOperation (android.content.ContentProviderOperation)1 OperationApplicationException (android.content.OperationApplicationException)1 Cursor (android.database.Cursor)1 DateTime (com.google.api.client.util.DateTime)1