Search in sources :

Example 1 with SgListIds

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

the class ListsTools method removeListsRemovedOnHexagon.

public static boolean removeListsRemovedOnHexagon(SgApp app) {
    Timber.d("removeListsRemovedOnHexagon");
    HashSet<String> localListIds = getListIds(app);
    if (localListIds == null) {
        // query failed
        return false;
    }
    if (localListIds.size() <= 1) {
        // one or no list, can not remove any list
        return true;
    }
    // get list of ids of lists on hexagon
    List<String> hexagonListIds = new ArrayList<>(localListIds.size());
    String cursor = null;
    do {
        try {
            Lists listsService = app.getHexagonTools().getListsService();
            if (listsService == null) {
                // no longer signed in
                return false;
            }
            Lists.GetIds request = listsService.getIds();
            if (!TextUtils.isEmpty(cursor)) {
                request.setCursor(cursor);
            }
            SgListIds response = request.execute();
            if (response == null) {
                Timber.d("removeListsRemovedOnHexagon: failed, response is null.");
                return false;
            }
            List<String> listIds = response.getListIds();
            if (listIds == null || listIds.size() == 0) {
                // empty response, assume we got all ids
                break;
            }
            hexagonListIds.addAll(listIds);
            cursor = response.getCursor();
        } catch (IOException e) {
            HexagonTools.trackFailedRequest(app, "get list ids", e);
            return false;
        }
    } while (// fetch next batch
    !TextUtils.isEmpty(cursor));
    if (hexagonListIds.size() <= 1) {
        // one or no list on hexagon, can not remove any list
        return true;
    }
    // exclude any lists that are on hexagon
    for (String listId : hexagonListIds) {
        localListIds.remove(listId);
    }
    // remove any list not on hexagon
    if (localListIds.size() > 0) {
        ArrayList<ContentProviderOperation> batch = new ArrayList<>();
        for (String listId : localListIds) {
            batch.add(ContentProviderOperation.newDelete(SeriesGuideContract.Lists.buildListUri(listId)).build());
        }
        try {
            DBUtils.applyInSmallBatches(app, batch);
        } catch (OperationApplicationException e) {
            Timber.e(e, "removeListsRemovedOnHexagon: deleting lists failed.");
            return false;
        }
    }
    return true;
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) Lists(com.uwetrottmann.seriesguide.backend.lists.Lists) ArrayList(java.util.ArrayList) IOException(java.io.IOException) OperationApplicationException(android.content.OperationApplicationException) SgListIds(com.uwetrottmann.seriesguide.backend.lists.model.SgListIds)

Aggregations

ContentProviderOperation (android.content.ContentProviderOperation)1 OperationApplicationException (android.content.OperationApplicationException)1 Lists (com.uwetrottmann.seriesguide.backend.lists.Lists)1 SgListIds (com.uwetrottmann.seriesguide.backend.lists.model.SgListIds)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1