Search in sources :

Example 1 with SgListItem

use of com.uwetrottmann.seriesguide.backend.lists.model.SgListItem 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 SgListItem

use of com.uwetrottmann.seriesguide.backend.lists.model.SgListItem 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 SgListItem

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

the class ListsTools method getListItems.

@Nullable
private static List<SgListItem> getListItems(Context context, String listId) {
    SELECTION_ARG[0] = listId;
    Cursor query = context.getContentResolver().query(SeriesGuideContract.ListItems.CONTENT_URI, Query.PROJECTION_LIST_ITEMS, SeriesGuideContract.ListItems.SELECTION_LIST, SELECTION_ARG, null);
    if (query == null) {
        // query failed
        return null;
    }
    int itemCount = query.getCount();
    if (itemCount == 0) {
        query.close();
        Timber.d("getListItems: no lists to upload.");
        // no items in this list
        return null;
    }
    List<SgListItem> items = new ArrayList<>(itemCount);
    while (query.moveToNext()) {
        SgListItem item = new SgListItem();
        item.setListItemId(query.getString(Query.LIST_ITEM_ID));
        items.add(item);
    }
    query.close();
    return items;
}
Also used : SgListItem(com.uwetrottmann.seriesguide.backend.lists.model.SgListItem) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) SuppressLint(android.annotation.SuppressLint) Nullable(android.support.annotation.Nullable)

Example 4 with SgListItem

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

the class ListsTools method getListItems.

@Nullable
public static List<SgListItem> getListItems(Context context, String listId) {
    SELECTION_ARG[0] = listId;
    Cursor query = context.getContentResolver().query(SeriesGuideContract.ListItems.CONTENT_URI, Query.PROJECTION_LIST_ITEMS, SeriesGuideContract.ListItems.SELECTION_LIST, SELECTION_ARG, null);
    if (query == null) {
        // query failed
        return null;
    }
    int itemCount = query.getCount();
    if (itemCount == 0) {
        query.close();
        // no items in this list
        return null;
    }
    List<SgListItem> items = new ArrayList<>(itemCount);
    while (query.moveToNext()) {
        SgListItem item = new SgListItem();
        String itemId = query.getString(Query.LIST_ITEM_ID);
        if (TextUtils.isEmpty(itemId)) {
            // skip, no item id
            continue;
        }
        item.setListItemId(itemId);
        items.add(item);
    }
    query.close();
    return items;
}
Also used : SgListItem(com.uwetrottmann.seriesguide.backend.lists.model.SgListItem) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) Nullable(androidx.annotation.Nullable)

Example 5 with SgListItem

use of com.uwetrottmann.seriesguide.backend.lists.model.SgListItem 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(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