Search in sources :

Example 1 with SgListList

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

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

the class AddListTask method doBackgroundAction.

@Override
protected Integer doBackgroundAction(Void... params) {
    String listId = getListId();
    if (isSendingToHexagon()) {
        HexagonTools hexagonTools = SgApp.getServicesComponent(getContext()).hexagonTools();
        Lists listsService = hexagonTools.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) {
            Errors.logAndReportHexagon("add list", e);
            return ERROR_HEXAGON_API;
        }
    }
    // update local state
    if (!doDatabaseUpdate(getContext().getContentResolver(), 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) HexagonTools(com.battlelancer.seriesguide.backend.HexagonTools) IOException(java.io.IOException)

Example 3 with SgListList

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

the class ReorderListsTask method doBackgroundAction.

@Override
protected Integer doBackgroundAction(Void... params) {
    if (isSendingToHexagon()) {
        HexagonTools hexagonTools = SgApp.getServicesComponent(getContext()).hexagonTools();
        Lists listsService = hexagonTools.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) {
            Errors.logAndReportHexagon("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) HexagonTools(com.battlelancer.seriesguide.backend.HexagonTools) IOException(java.io.IOException)

Example 4 with SgListList

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

the class HexagonListsSync method download.

public boolean download(boolean hasMergedLists) {
    long currentTime = System.currentTimeMillis();
    DateTime lastSyncTime = new DateTime(HexagonSettings.getLastListsSyncTime(context));
    if (hasMergedLists) {
        Timber.d("download: lists changed since %s.", lastSyncTime);
    } else {
        Timber.d("download: all lists.");
    }
    HashSet<String> localListIds = ListsTools.getListIds(context);
    List<SgList> lists;
    String cursor = null;
    do {
        try {
            // get service each time to check if auth was removed
            Lists listsService = hexagonTools.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("download: failed, response is null.");
                break;
            }
            cursor = response.getCursor();
            lists = response.getLists();
        } catch (IOException | IllegalArgumentException e) {
            // Note: JSON parser may throw IllegalArgumentException.
            Errors.logAndReportHexagon("get lists", e);
            return false;
        }
        if (lists == null || lists.size() == 0) {
            // empty response, assume we are done
            break;
        }
        if (!doListsDatabaseUpdate(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(context).edit().putLong(HexagonSettings.KEY_LAST_SYNC_LISTS, currentTime).apply();
    }
    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)

Example 5 with SgListList

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

Aggregations

SgList (com.uwetrottmann.seriesguide.backend.lists.model.SgList)8 SgListList (com.uwetrottmann.seriesguide.backend.lists.model.SgListList)8 Lists (com.uwetrottmann.seriesguide.backend.lists.Lists)6 IOException (java.io.IOException)6 HexagonTools (com.battlelancer.seriesguide.backend.HexagonTools)4 SuppressLint (android.annotation.SuppressLint)2 Cursor (android.database.Cursor)2 DateTime (com.google.api.client.util.DateTime)2 SgListItem (com.uwetrottmann.seriesguide.backend.lists.model.SgListItem)2 ArrayList (java.util.ArrayList)2