Search in sources :

Example 6 with MwQueryPage

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

the class PageImage method imageMapFromPages.

public static Map<PageTitle, PageImage> imageMapFromPages(@NonNull WikiSite wiki, @NonNull List<PageTitle> titles, @NonNull List<MwQueryPage> pages) {
    Map<PageTitle, PageImage> pageImagesMap = new ArrayMap<>();
    // nominal case
    Map<String, PageTitle> titlesMap = new ArrayMap<>();
    for (PageTitle title : titles) {
        titlesMap.put(title.getPrefixedText(), title);
    }
    Map<String, String> thumbnailSourcesMap = new ArrayMap<>();
    Map<String, String> convertedTitleMap = new ArrayMap<>();
    // noinspection ConstantConditions
    for (MwQueryPage page : pages) {
        thumbnailSourcesMap.put(new PageTitle(null, page.title(), wiki).getPrefixedText(), page.thumbUrl());
        if (!TextUtils.isEmpty(page.convertedFrom())) {
            PageTitle pageTitle = new PageTitle(null, page.convertedFrom(), wiki);
            convertedTitleMap.put(pageTitle.getPrefixedText(), page.convertedTo());
            thumbnailSourcesMap.put(pageTitle.getPrefixedText(), page.thumbUrl());
        }
        if (!TextUtils.isEmpty(page.redirectFrom())) {
            thumbnailSourcesMap.put(new PageTitle(null, page.redirectFrom(), wiki).getPrefixedText(), page.thumbUrl());
        }
    }
    for (String key : titlesMap.keySet()) {
        if (thumbnailSourcesMap.containsKey(key)) {
            PageTitle title = titlesMap.get(key);
            title.setConvertedText(convertedTitleMap.get(key));
            pageImagesMap.put(title, new PageImage(title, thumbnailSourcesMap.get(key)));
        }
    }
    return pageImagesMap;
}
Also used : MwQueryPage(org.wikipedia.dataclient.mwapi.MwQueryPage) PageTitle(org.wikipedia.page.PageTitle) ArrayMap(android.support.v4.util.ArrayMap)

Example 7 with MwQueryPage

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

the class PageImagesClient method request.

@VisibleForTesting
Call<MwQueryResponse> request(@NonNull final WikiSite wiki, @NonNull Service service, @NonNull final 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) {
            Map<PageTitle, PageImage> pageImagesMap = new ArrayMap<>();
            // error cases
            if (response.body().success()) {
                // nominal case
                Map<String, PageTitle> titlesMap = new ArrayMap<>();
                for (PageTitle title : titles) {
                    titlesMap.put(title.getPrefixedText(), title);
                }
                Map<String, String> thumbnailSourcesMap = new ArrayMap<>();
                // noinspection ConstantConditions
                for (MwQueryPage page : response.body().query().pages()) {
                    thumbnailSourcesMap.put(new PageTitle(null, page.title(), wiki).getPrefixedText(), page.thumbUrl());
                    if (!TextUtils.isEmpty(page.convertedFrom())) {
                        thumbnailSourcesMap.put(new PageTitle(null, page.convertedFrom(), wiki).getPrefixedText(), page.thumbUrl());
                    }
                    if (!TextUtils.isEmpty(page.redirectFrom())) {
                        thumbnailSourcesMap.put(new PageTitle(null, page.redirectFrom(), wiki).getPrefixedText(), page.thumbUrl());
                    }
                }
                for (String key : titlesMap.keySet()) {
                    if (thumbnailSourcesMap.containsKey(key)) {
                        PageTitle title = titlesMap.get(key);
                        pageImagesMap.put(title, new PageImage(title, thumbnailSourcesMap.get(key)));
                    }
                }
                cb.success(call, pageImagesMap);
            } 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."));
            }
        }

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

Aggregations

MwQueryPage (org.wikipedia.dataclient.mwapi.MwQueryPage)7 MwQueryResponse (org.wikipedia.dataclient.mwapi.MwQueryResponse)5 PageTitle (org.wikipedia.page.PageTitle)3 VisibleForTesting (android.support.annotation.VisibleForTesting)2 ArrayMap (android.support.v4.util.ArrayMap)2 IOException (java.io.IOException)2 List (java.util.List)2 Test (org.junit.Test)2 MwException (org.wikipedia.dataclient.mwapi.MwException)2 MockWebServerTest (org.wikipedia.test.MockWebServerTest)2 Context (android.content.Context)1 Bitmap (android.graphics.Bitmap)1 NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 ActionMode (android.view.ActionMode)1 LayoutInflater (android.view.LayoutInflater)1 Menu (android.view.Menu)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 ImageView (android.widget.ImageView)1