Search in sources :

Example 1 with MwQueryResponse

use of org.wikipedia.dataclient.mwapi.MwQueryResponse in project apps-android-wikipedia by wikimedia.

the class DisambigListAdapter method fetchDescriptions.

/**
 * Start getting Wikidata descriptions (directly from the current Wikipedia wiki).
 */
private void fetchDescriptions() {
    final List<PageTitle> titleList = new ArrayList<>();
    for (DisambigResult r : items) {
        titleList.add(r.getTitle());
    }
    if (titleList.isEmpty()) {
        return;
    }
    new DescriptionClient().request(wiki, titleList, new DescriptionClient.Callback() {

        @Override
        public void success(@NonNull Call<MwQueryResponse> call, @NonNull List<MwQueryPage> results) {
            for (MwQueryPage page : results) {
                PageTitle pageTitle = new PageTitle(null, page.title(), wiki);
                for (PageTitle title : titleList) {
                    if (title.getPrefixedText().equals(pageTitle.getPrefixedText()) || title.getDisplayText().equals(pageTitle.getDisplayText())) {
                        title.setDescription(page.description());
                        break;
                    }
                }
            }
            notifyDataSetChanged();
        }

        @Override
        public void failure(@NonNull Call<MwQueryResponse> call, @NonNull Throwable caught) {
        // descriptions are expendable
        }
    });
}
Also used : DescriptionClient(org.wikipedia.wikidata.DescriptionClient) MwQueryPage(org.wikipedia.dataclient.mwapi.MwQueryPage) ArrayList(java.util.ArrayList) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse)

Example 2 with MwQueryResponse

use of org.wikipedia.dataclient.mwapi.MwQueryResponse in project apps-android-wikipedia by wikimedia.

the class DisambigListAdapter method requestPageImages.

private void requestPageImages() {
    List<PageTitle> titleList = new ArrayList<>();
    for (DisambigResult r : items) {
        if (pageImagesCache.get(r.getTitle().getPrefixedText()) == null) {
            // not in our cache yet
            titleList.add(r.getTitle());
        }
    }
    if (titleList.isEmpty()) {
        return;
    }
    new PageImagesClient().request(wiki, titleList, new PageImagesClient.Callback() {

        @Override
        public void success(@NonNull Call<MwQueryResponse> call, @NonNull Map<PageTitle, PageImage> results) {
            for (Map.Entry<PageTitle, PageImage> entry : results.entrySet()) {
                if (entry.getValue() == null || entry.getValue().getImageName() == null) {
                    continue;
                }
                pageImagesCache.put(entry.getKey().getPrefixedText(), entry.getValue().getImageName());
            }
            notifyDataSetInvalidated();
        }

        @Override
        public void failure(@NonNull Call<MwQueryResponse> call, @NonNull Throwable caught) {
        // Don't actually do anything.
        // Thumbnails are expendable
        }
    });
}
Also used : ArrayList(java.util.ArrayList) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) PageImage(org.wikipedia.pageimages.PageImage) PageImagesClient(org.wikipedia.pageimages.PageImagesClient)

Example 3 with MwQueryResponse

use of org.wikipedia.dataclient.mwapi.MwQueryResponse in project apps-android-wikipedia by wikimedia.

the class BottomContentView method requestReadMoreItems.

private void requestReadMoreItems(final HistoryEntry entry) {
    if (entry == null || TextUtils.isEmpty(entry.getTitle().getPrefixedText())) {
        hideReadMore();
        return;
    }
    final long timeMillis = System.currentTimeMillis();
    new FullTextSearchClient().requestMoreLike(entry.getTitle().getWikiSite(), entry.getTitle().getPrefixedText(), null, null, Constants.MAX_SUGGESTION_RESULTS * 2, new FullTextSearchClient.Callback() {

        @Override
        public void success(@NonNull Call<MwQueryResponse> call, @NonNull SearchResults results) {
            funnel.setLatency(System.currentTimeMillis() - timeMillis);
            readMoreItems = SearchResults.filter(results, entry.getTitle().getPrefixedText(), true);
            if (!readMoreItems.getResults().isEmpty()) {
                readMoreAdapter.setResults(results.getResults());
                showReadMore();
            } else {
                // If there's no results, just hide the section
                hideReadMore();
            }
        }

        @Override
        public void failure(@NonNull Call<MwQueryResponse> call, @NonNull Throwable caught) {
            // Read More titles are expendable.
            L.w("Error while fetching Read More titles.", caught);
        }
    });
}
Also used : FullTextSearchClient(org.wikipedia.search.FullTextSearchClient) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) SearchResults(org.wikipedia.search.SearchResults)

Example 4 with MwQueryResponse

use of org.wikipedia.dataclient.mwapi.MwQueryResponse in project apps-android-wikipedia by wikimedia.

the class ShareHandler method shareSnippet.

private void shareSnippet(@NonNull CharSequence input) {
    final String selectedText = StringUtil.sanitizeText(input.toString());
    final PageTitle title = fragment.getTitle();
    final String leadImageNameText = fragment.getPage().getPageProperties().getLeadImageName() != null ? fragment.getPage().getPageProperties().getLeadImageName() : "";
    new ImageLicenseFetchClient().request(title.getWikiSite(), new PageTitle(Namespace.FILE.toLegacyString(), leadImageNameText, title.getWikiSite()), new ImageLicenseFetchClient.Callback() {

        @Override
        public void success(@NonNull Call<MwQueryResponse> call, @NonNull ImageLicense result) {
            final Bitmap snippetBitmap = SnippetImage.getSnippetImage(fragment.getContext(), fragment.getLeadImageBitmap(), title.getDisplayText(), fragment.getPage().isMainPage() ? "" : StringUtils.capitalize(title.getDescription()), selectedText, result);
            fragment.showBottomSheet(new PreviewDialog(fragment.getContext(), snippetBitmap, title, selectedText, funnel));
        }

        @Override
        public void failure(@NonNull Call<MwQueryResponse> call, @NonNull Throwable caught) {
            // If we failed to get license info for the lead image, just share the text
            PreviewDialog.shareAsText(fragment.getContext(), title, selectedText, funnel);
            L.e("Error fetching image license info for " + title.getDisplayText(), caught);
        }
    });
}
Also used : ImageLicense(org.wikipedia.gallery.ImageLicense) Bitmap(android.graphics.Bitmap) ImageLicenseFetchClient(org.wikipedia.gallery.ImageLicenseFetchClient) PageTitle(org.wikipedia.page.PageTitle) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse)

Example 5 with MwQueryResponse

use of org.wikipedia.dataclient.mwapi.MwQueryResponse in project apps-android-wikipedia by wikimedia.

the class ReadingListPageInfoClient method request.

@VisibleForTesting
public Call<MwQueryResponse> request(@NonNull Service service, @NonNull List<PageTitle> titles, @NonNull final Callback cb) {
    Call<MwQueryResponse> call = service.request(TextUtils.join("|", titles));
    call.enqueue(new retrofit2.Callback<MwQueryResponse>() {

        @Override
        public void onResponse(@NonNull Call<MwQueryResponse> call, @NonNull Response<MwQueryResponse> response) {
            if (response.isSuccessful()) {
                if (response.body().success()) {
                    // noinspection ConstantConditions
                    cb.success(call, response.body().query().pages());
                } else if (response.body().hasError()) {
                    // noinspection ConstantConditions
                    cb.failure(call, new MwException(response.body().getError()));
                } else {
                    cb.failure(call, new IOException("An unknown error occurred."));
                }
            } else {
                cb.failure(call, RetrofitException.httpError(response));
            }
        }

        @Override
        public void onFailure(@NonNull Call<MwQueryResponse> call, @NonNull Throwable t) {
            cb.failure(call, t);
        }
    });
    return call;
}
Also used : MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) MwException(org.wikipedia.dataclient.mwapi.MwException) IOException(java.io.IOException) VisibleForTesting(android.support.annotation.VisibleForTesting)

Aggregations

MwQueryResponse (org.wikipedia.dataclient.mwapi.MwQueryResponse)27 MwException (org.wikipedia.dataclient.mwapi.MwException)14 VisibleForTesting (android.support.annotation.VisibleForTesting)12 IOException (java.io.IOException)12 Test (org.junit.Test)7 MockWebServerTest (org.wikipedia.test.MockWebServerTest)7 MwQueryPage (org.wikipedia.dataclient.mwapi.MwQueryPage)5 Callback (org.wikipedia.gallery.GalleryItemClient.Callback)5 ArrayList (java.util.ArrayList)3 NonNull (android.support.annotation.NonNull)2 List (java.util.List)2 Map (java.util.Map)2 PageTitle (org.wikipedia.page.PageTitle)2 PageImage (org.wikipedia.pageimages.PageImage)2 PageImagesClient (org.wikipedia.pageimages.PageImagesClient)2 Bitmap (android.graphics.Bitmap)1 ArrayMap (android.support.v4.util.ArrayMap)1 JsonParseException (com.google.gson.JsonParseException)1 HashMap (java.util.HashMap)1 ServiceError (org.wikipedia.dataclient.ServiceError)1