Search in sources :

Example 6 with SgListItem

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

the class HexagonListsSync method uploadAll.

public boolean uploadAll() {
    Timber.d("uploadAll");
    SgListList listsWrapper = new SgListList();
    List<SgList> lists = new ArrayList<>(LISTS_MAX_BATCH_SIZE);
    listsWrapper.setLists(lists);
    Cursor listsQuery = context.getContentResolver().query(SeriesGuideContract.Lists.CONTENT_URI, ListsTools.Query.PROJECTION_LIST, null, null, null);
    if (listsQuery == null) {
        // query failed
        return false;
    }
    while (listsQuery.moveToNext()) {
        SgList list = new SgList();
        // add list properties
        String listId = listsQuery.getString(ListsTools.Query.LIST_ID);
        String listName = listsQuery.getString(ListsTools.Query.NAME);
        if (TextUtils.isEmpty(listId)) {
            // skip, no list id
            continue;
        }
        list.setListId(listId);
        list.setName(listName);
        int order = listsQuery.getInt(ListsTools.Query.ORDER);
        if (order != 0) {
            list.setOrder(order);
        }
        // add list items
        List<SgListItem> listItems = ListsTools.getListItems(context, listId);
        if (listItems != null) {
            list.setListItems(listItems);
        } else {
            Timber.d("uploadAll: no items to upload for list %s.", listId);
        }
        lists.add(list);
        if (lists.size() == LISTS_MAX_BATCH_SIZE || listsQuery.isLast()) {
            if (doUploadSomeLists(listsWrapper)) {
                lists.clear();
            } else {
                // 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)

Example 7 with SgListItem

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

the class HexagonListsSync method doListsDatabaseUpdate.

private boolean doListsDatabaseUpdate(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 = ListsTools.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) OperationApplicationException(android.content.OperationApplicationException)

Example 8 with SgListItem

use of com.uwetrottmann.seriesguide.backend.lists.model.SgListItem 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(itemStableId, 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(androidx.annotation.NonNull)

Aggregations

SgListItem (com.uwetrottmann.seriesguide.backend.lists.model.SgListItem)8 ArrayList (java.util.ArrayList)8 SgList (com.uwetrottmann.seriesguide.backend.lists.model.SgList)6 Cursor (android.database.Cursor)4 SuppressLint (android.annotation.SuppressLint)3 ContentProviderOperation (android.content.ContentProviderOperation)2 OperationApplicationException (android.content.OperationApplicationException)2 NonNull (androidx.annotation.NonNull)2 SgListList (com.uwetrottmann.seriesguide.backend.lists.model.SgListList)2 Nullable (android.support.annotation.Nullable)1 Nullable (androidx.annotation.Nullable)1