Search in sources :

Example 16 with ReadingListPage

use of org.wikipedia.readinglist.database.ReadingListPage in project apps-android-wikipedia by wikimedia.

the class SavedPageSyncService method savePages.

private int savePages(List<ReadingListPage> queue) {
    int itemsTotal = queue.size();
    int itemsSaved = 0;
    while (!queue.isEmpty()) {
        // Pick off the DB row that we'll be working on...
        ReadingListPage page = queue.remove(0);
        if (savedPageSyncNotification.isSyncPaused()) {
            // Remaining transactions will be picked up again when the service is resumed.
            break;
        } else if (savedPageSyncNotification.isSyncCanceled()) {
            // Mark remaining pages as online-only!
            queue.add(page);
            ReadingListDbHelper.instance().markPagesForOffline(queue, false);
            break;
        }
        savedPageSyncNotification.setNotificationProgress(getApplicationContext(), itemsTotal, itemsSaved);
        boolean success = false;
        long totalSize = 0;
        try {
            // Lengthy operation during which the db state may change...
            totalSize = savePageFor(page);
            success = true;
        } catch (InterruptedException e) {
        // fall through
        } catch (Exception e) {
            // This can be an IOException from the storage media, or several types
            // of network exceptions from malformed URLs, timeouts, etc.
            e.printStackTrace();
            if (!ThrowableUtil.isOffline(e) && !ThrowableUtil.isNetworkError(e)) {
                // If it's anything but a transient network error, let's log it aggressively,
                // to make sure we've fixed any other errors with saving pages.
                L.logRemoteError(e);
            }
        }
        if (ReadingListSyncAdapter.inProgress()) {
            // Reading list sync was started in the meantime, so bail.
            break;
        }
        if (success) {
            page.status(ReadingListPage.STATUS_SAVED);
            page.sizeBytes(totalSize);
            ReadingListDbHelper.instance().updatePage(page);
            itemsSaved++;
            sendSyncEvent();
        }
    }
    return itemsSaved;
}
Also used : ReadingListPage(org.wikipedia.readinglist.database.ReadingListPage) IOException(java.io.IOException)

Example 17 with ReadingListPage

use of org.wikipedia.readinglist.database.ReadingListPage in project apps-android-wikipedia by wikimedia.

the class SavedPageSyncService method onHandleWork.

@Override
protected void onHandleWork(@NonNull Intent intent) {
    if (ReadingListSyncAdapter.inProgress()) {
        // Reading list sync was started in the meantime, so bail.
        return;
    }
    List<ReadingListPage> pagesToSave = ReadingListDbHelper.instance().getAllPagesToBeSaved();
    List<ReadingListPage> pagesToUnsave = ReadingListDbHelper.instance().getAllPagesToBeUnsaved();
    List<ReadingListPage> pagesToDelete = ReadingListDbHelper.instance().getAllPagesToBeDeleted();
    boolean shouldSendSyncEvent = false;
    try {
        for (ReadingListPage page : pagesToDelete) {
            deletePageContents(page);
        }
        for (ReadingListPage page : pagesToUnsave) {
            deletePageContents(page);
        }
    } catch (Exception e) {
        L.e("Error while deleting page: " + e.getMessage());
    } finally {
        if (!pagesToDelete.isEmpty()) {
            ReadingListDbHelper.instance().purgeDeletedPages();
            shouldSendSyncEvent = true;
        }
        if (!pagesToUnsave.isEmpty()) {
            ReadingListDbHelper.instance().resetUnsavedPageStatus();
            shouldSendSyncEvent = true;
        }
    }
    int itemsTotal = pagesToSave.size();
    if (itemsTotal > 0) {
        shouldSendSyncEvent = true;
    }
    int itemsSaved = 0;
    try {
        itemsSaved = savePages(pagesToSave);
    } finally {
        if (savedPageSyncNotification.isSyncPaused()) {
            savedPageSyncNotification.setNotificationPaused(getApplicationContext(), itemsTotal, itemsSaved);
        } else {
            savedPageSyncNotification.cancelNotification(getApplicationContext());
            savedPageSyncNotification.setSyncCanceled(false);
            if (shouldSendSyncEvent) {
                sendSyncEvent();
            }
        }
    }
}
Also used : ReadingListPage(org.wikipedia.readinglist.database.ReadingListPage) IOException(java.io.IOException)

Example 18 with ReadingListPage

use of org.wikipedia.readinglist.database.ReadingListPage in project apps-android-wikipedia by wikimedia.

the class DeveloperSettingsPreferenceLoader method createTestReadingList.

private void createTestReadingList(String listName, int numOfLists, int numOfArticles) {
    int index = 0;
    List<ReadingList> lists = ReadingListDbHelper.instance().getAllListsWithoutContents();
    for (int i = lists.size() - 1; i >= 0; i--) {
        ReadingList lastReadingList = lists.get(i);
        if (lastReadingList.title().contains(listName)) {
            String trimmedListTitle = lastReadingList.title().substring(listName.length()).trim();
            index = (trimmedListTitle.isEmpty()) ? index : (Integer.valueOf(trimmedListTitle) > index ? Integer.valueOf(trimmedListTitle) : index);
            break;
        }
    }
    for (int i = 0; i < numOfLists; i++) {
        index += 1;
        ReadingList list = ReadingListDbHelper.instance().createList(listName + " " + index, "");
        List<ReadingListPage> pages = new ArrayList<>();
        for (int j = 0; j < numOfArticles; j++) {
            PageTitle pageTitle = new PageTitle("" + (j + 1), WikipediaApp.getInstance().getWikiSite());
            pages.add(new ReadingListPage(pageTitle));
        }
        ReadingListDbHelper.instance().addPagesToList(list, pages, true);
    }
}
Also used : ArrayList(java.util.ArrayList) PageTitle(org.wikipedia.page.PageTitle) ReadingList(org.wikipedia.readinglist.database.ReadingList) ReadingListPage(org.wikipedia.readinglist.database.ReadingListPage)

Aggregations

ReadingListPage (org.wikipedia.readinglist.database.ReadingListPage)18 ArrayList (java.util.ArrayList)8 PageTitle (org.wikipedia.page.PageTitle)5 ReadingList (org.wikipedia.readinglist.database.ReadingList)3 Snackbar (android.support.design.widget.Snackbar)2 IOException (java.io.IOException)2 Bundle (android.os.Bundle)1 NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 OnClick (butterknife.OnClick)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 CallbackTask (org.wikipedia.concurrency.CallbackTask)1 WikiSite (org.wikipedia.dataclient.WikiSite)1 ReadingListsEnableDialogEvent (org.wikipedia.events.ReadingListsEnableDialogEvent)1 ReadingListsEnabledStatusEvent (org.wikipedia.events.ReadingListsEnabledStatusEvent)1 ReadingListsMergeLocalDialogEvent (org.wikipedia.events.ReadingListsMergeLocalDialogEvent)1 ReadingListsNoLongerSyncedEvent (org.wikipedia.events.ReadingListsNoLongerSyncedEvent)1 ReadingListBookmarkMenu (org.wikipedia.readinglist.ReadingListBookmarkMenu)1 RemoteReadingList (org.wikipedia.readinglist.sync.SyncedReadingLists.RemoteReadingList)1