Search in sources :

Example 1 with PageRemaining

use of org.wikipedia.dataclient.page.PageRemaining in project apps-android-wikipedia by wikimedia.

the class SavedPageSyncService method savePageFor.

private long savePageFor(@NonNull ReadingListPage page) throws IOException, InterruptedException {
    PageTitle pageTitle = ReadingListPage.toPageTitle(page);
    long totalSize = 0;
    Call<PageLead> leadCall = reqPageLead(CacheControl.FORCE_NETWORK, OfflineCacheInterceptor.SAVE_HEADER_SAVE, pageTitle);
    Call<PageRemaining> sectionsCall = reqPageSections(CacheControl.FORCE_NETWORK, OfflineCacheInterceptor.SAVE_HEADER_SAVE, pageTitle);
    retrofit2.Response<PageLead> leadRsp = leadCall.execute();
    totalSize += responseSize(leadRsp);
    retrofit2.Response<PageRemaining> sectionsRsp = sectionsCall.execute();
    totalSize += responseSize(sectionsRsp);
    Set<String> imageUrls = new HashSet<>(pageImageUrlParser.parse(leadRsp.body()));
    imageUrls.addAll(pageImageUrlParser.parse(sectionsRsp.body()));
    if (!TextUtils.isEmpty(leadRsp.body().getThumbUrl())) {
        page.thumbUrl(UriUtil.resolveProtocolRelativeUrl(pageTitle.getWikiSite(), leadRsp.body().getThumbUrl()));
        persistPageThumbnail(pageTitle, page.thumbUrl());
        imageUrls.add(page.thumbUrl());
    }
    page.description(leadRsp.body().getDescription());
    if (Prefs.isImageDownloadEnabled()) {
        totalSize += reqSaveImages(pageTitle.getWikiSite(), imageUrls);
    }
    String title = pageTitle.getPrefixedText();
    L.i("Saved page " + title + " (" + totalSize + ")");
    return totalSize;
}
Also used : PageTitle(org.wikipedia.page.PageTitle) PageLead(org.wikipedia.dataclient.page.PageLead) PageRemaining(org.wikipedia.dataclient.page.PageRemaining) HashSet(java.util.HashSet)

Example 2 with PageRemaining

use of org.wikipedia.dataclient.page.PageRemaining in project apps-android-wikipedia by wikimedia.

the class SavedPageSyncService method deletePageContents.

private void deletePageContents(@NonNull ReadingListPage page) {
    PageTitle pageTitle = ReadingListPage.toPageTitle(page);
    PageLead lead = null;
    Call<PageLead> leadCall = reqPageLead(CacheControl.FORCE_CACHE, OfflineCacheInterceptor.SAVE_HEADER_DELETE, pageTitle);
    try {
        lead = leadCall.execute().body();
    } catch (IOException ignore) {
    }
    if (lead != null) {
        List<String> imageUrls = pageImageUrlParser.parse(lead);
        if (!TextUtils.isEmpty(pageTitle.getThumbUrl())) {
            imageUrls.add(pageTitle.getThumbUrl());
        }
        for (String url : imageUrls) {
            try {
                Request request = makeImageRequest(pageTitle.getWikiSite(), url).addHeader(OfflineCacheInterceptor.SAVE_HEADER, OfflineCacheInterceptor.SAVE_HEADER_DELETE).build();
                OkHttpConnectionFactory.getClient().newCall(request).execute();
            } catch (IOException ignore) {
            }
        }
    }
    Call<PageRemaining> sectionsCall = reqPageSections(CacheControl.FORCE_CACHE, OfflineCacheInterceptor.SAVE_HEADER_DELETE, pageTitle);
    PageRemaining sections = null;
    try {
        sections = sectionsCall.execute().body();
    } catch (IOException ignore) {
    }
    if (sections != null) {
        for (String url : pageImageUrlParser.parse(sections)) {
            try {
                Request request = makeImageRequest(pageTitle.getWikiSite(), url).addHeader(OfflineCacheInterceptor.SAVE_HEADER, OfflineCacheInterceptor.SAVE_HEADER_DELETE).build();
                OkHttpConnectionFactory.getClient().newCall(request).execute();
            } catch (IOException ignore) {
            }
        }
    }
}
Also used : PageTitle(org.wikipedia.page.PageTitle) Request(okhttp3.Request) PageLead(org.wikipedia.dataclient.page.PageLead) IOException(java.io.IOException) PageRemaining(org.wikipedia.dataclient.page.PageRemaining)

Aggregations

PageLead (org.wikipedia.dataclient.page.PageLead)2 PageRemaining (org.wikipedia.dataclient.page.PageRemaining)2 PageTitle (org.wikipedia.page.PageTitle)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Request (okhttp3.Request)1