Search in sources :

Example 1 with SgList

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

the class ListsTools method uploadAllToHexagon.

public static boolean uploadAllToHexagon(SgApp app) {
    Timber.d("uploadAllToHexagon");
    SgListList listsWrapper = new SgListList();
    List<SgList> lists = new ArrayList<>(LISTS_MAX_BATCH_SIZE);
    listsWrapper.setLists(lists);
    Cursor listsQuery = app.getContentResolver().query(SeriesGuideContract.Lists.CONTENT_URI, Query.PROJECTION_LIST, null, null, null);
    if (listsQuery == null) {
        // query failed
        return false;
    }
    while (listsQuery.moveToNext()) {
        SgList list = new SgList();
        lists.add(list);
        // add list properties
        String listId = listsQuery.getString(Query.LIST_ID);
        list.setListId(listId);
        list.setName(listsQuery.getString(Query.NAME));
        int order = listsQuery.getInt(Query.ORDER);
        if (order != 0) {
            list.setOrder(order);
        }
        // add list items
        List<SgListItem> listItems = getListItems(app, listId);
        if (listItems != null) {
            list.setListItems(listItems);
        }
        if (lists.size() == LISTS_MAX_BATCH_SIZE || listsQuery.isLast()) {
            if (!doUploadSomeLists(app, listsWrapper)) {
                // part upload failed, next sync will try again
                return false;
            }
        }
    }
    listsQuery.close();
    return true;
}
Also used : SgList(com.uwetrottmann.seriesguide.backend.lists.model.SgList) SgListList(com.uwetrottmann.seriesguide.backend.lists.model.SgListList) SgListItem(com.uwetrottmann.seriesguide.backend.lists.model.SgListItem) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) SuppressLint(android.annotation.SuppressLint)

Example 2 with SgList

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

the class ListsTools method doListsDatabaseUpdate.

private static boolean doListsDatabaseUpdate(Context context, List<SgList> lists, HashSet<String> localListIds, boolean hasMergedLists) {
    ArrayList<ContentProviderOperation> batch = new ArrayList<>();
    for (SgList list : lists) {
        // add or update the list
        String listId = list.getListId();
        ContentProviderOperation.Builder builder = null;
        if (localListIds.contains(listId)) {
            // update
            if (hasMergedLists) {
                // only overwrite name and order if data was already merged
                // use case: user disconnected for a while, changed lists, then reconnects
                builder = ContentProviderOperation.newUpdate(SeriesGuideContract.Lists.buildListUri(listId));
            }
        } else {
            // insert
            builder = ContentProviderOperation.newInsert(SeriesGuideContract.Lists.CONTENT_URI).withValue(SeriesGuideContract.Lists.LIST_ID, listId);
        }
        if (builder != null) {
            builder.withValue(SeriesGuideContract.Lists.NAME, list.getName());
            if (list.getOrder() != null) {
                builder.withValue(SeriesGuideContract.Lists.ORDER, list.getOrder());
            }
            batch.add(builder.build());
        }
        // keep track of items not in the list on hexagon
        HashSet<String> listItemsToRemove = null;
        if (hasMergedLists) {
            listItemsToRemove = getListItemIds(context, listId);
            if (listItemsToRemove == null) {
                // list item query failed
                return false;
            }
        }
        // add or update items of the list
        List<SgListItem> listItems = list.getListItems();
        if (listItems != null) {
            for (SgListItem listItem : listItems) {
                String listItemId = listItem.getListItemId();
                String[] brokenUpId = SeriesGuideContract.ListItems.splitListItemId(listItemId);
                if (brokenUpId == null) {
                    // could not break up list item id
                    continue;
                }
                int itemTvdbId = -1;
                int itemType = -1;
                try {
                    itemTvdbId = Integer.parseInt(brokenUpId[0]);
                    itemType = Integer.parseInt(brokenUpId[1]);
                } catch (NumberFormatException ignored) {
                }
                if (itemTvdbId == -1 || !SeriesGuideContract.ListItems.isValidItemType(itemType)) {
                    // failed to extract item TVDB id or item type not known
                    continue;
                }
                // just insert the list item, if the id already exists it will be replaced
                builder = ContentProviderOperation.newInsert(SeriesGuideContract.ListItems.CONTENT_URI).withValue(SeriesGuideContract.ListItems.LIST_ITEM_ID, listItemId).withValue(SeriesGuideContract.ListItems.ITEM_REF_ID, itemTvdbId).withValue(SeriesGuideContract.ListItems.TYPE, itemType).withValue(SeriesGuideContract.Lists.LIST_ID, listId);
                batch.add(builder.build());
                if (hasMergedLists) {
                    // do not remove this list item
                    listItemsToRemove.remove(listItemId);
                }
            }
        }
        if (hasMergedLists) {
            // remove items no longer in the list
            for (String listItemId : listItemsToRemove) {
                builder = ContentProviderOperation.newDelete(SeriesGuideContract.ListItems.buildListItemUri(listItemId));
                batch.add(builder.build());
            }
        }
    }
    try {
        DBUtils.applyInSmallBatches(context, batch);
    } catch (OperationApplicationException e) {
        Timber.e(e, "doListsDatabaseUpdate: failed.");
        return false;
    }
    return true;
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) SgList(com.uwetrottmann.seriesguide.backend.lists.model.SgList) SgListItem(com.uwetrottmann.seriesguide.backend.lists.model.SgListItem) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint) OperationApplicationException(android.content.OperationApplicationException)

Example 3 with SgList

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

the class AddListTask method doBackgroundAction.

@Override
protected Integer doBackgroundAction(Void... params) {
    String listId = getListId();
    if (isSendingToHexagon()) {
        Lists listsService = getContext().getHexagonTools().getListsService();
        if (listsService == null) {
            // no longer signed in
            return ERROR_HEXAGON_API;
        }
        // send list to be added to hexagon
        SgListList wrapper = new SgListList();
        List<SgList> lists = buildList(listId, listName);
        wrapper.setLists(lists);
        try {
            listsService.save(wrapper).execute();
        } catch (IOException e) {
            HexagonTools.trackFailedRequest(getContext(), "add list", e);
            return ERROR_HEXAGON_API;
        }
    }
    // update local state
    if (!doDatabaseUpdate(listId)) {
        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 4 with SgList

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

the class RemoveListItemTask method buildListItemLists.

@NonNull
private static List<SgList> buildListItemLists(String listId, String listItemId) {
    List<SgList> lists = new ArrayList<>(1);
    SgList list = new SgList();
    list.setListId(listId);
    lists.add(list);
    List<SgListItem> items = new ArrayList<>(1);
    list.setListItems(items);
    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)

Example 5 with SgList

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

the class ReorderListsTask method buildListsList.

@NonNull
private List<SgList> buildListsList(List<String> listsToChange) {
    List<SgList> lists = new ArrayList<>(listsToChange.size());
    for (int position = 0; position < listsToChange.size(); position++) {
        String listId = listsToChange.get(position);
        SgList list = new SgList();
        list.setListId(listId);
        list.setOrder(position);
        lists.add(list);
    }
    return lists;
}
Also used : SgList(com.uwetrottmann.seriesguide.backend.lists.model.SgList) 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