Mholloway has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/354372 )
Change subject: Hygiene: Consolidate MwQueryImageLicensePage into MwQueryPage ...................................................................... Hygiene: Consolidate MwQueryImageLicensePage into MwQueryPage Change-Id: Ia70629d59bb5b5c76963d873afda8122bb277ac9 --- D app/src/main/java/org/wikipedia/dataclient/mwapi/MwQueryImageLicensePage.java M app/src/main/java/org/wikipedia/dataclient/mwapi/MwQueryPage.java M app/src/main/java/org/wikipedia/page/ImageLicense.java M app/src/main/java/org/wikipedia/page/ImageLicenseFetchClient.java M app/src/main/java/org/wikipedia/page/snippet/ShareHandler.java M app/src/test/java/org/wikipedia/page/ImageLicenseFetchClientTest.java 6 files changed, 38 insertions(+), 110 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia refs/changes/72/354372/1 diff --git a/app/src/main/java/org/wikipedia/dataclient/mwapi/MwQueryImageLicensePage.java b/app/src/main/java/org/wikipedia/dataclient/mwapi/MwQueryImageLicensePage.java deleted file mode 100644 index ff42026..0000000 --- a/app/src/main/java/org/wikipedia/dataclient/mwapi/MwQueryImageLicensePage.java +++ /dev/null @@ -1,76 +0,0 @@ -package org.wikipedia.dataclient.mwapi; - -import android.support.annotation.Nullable; - -import com.google.gson.annotations.SerializedName; - -import java.util.List; - -public class MwQueryImageLicensePage extends MwQueryPage { - @SuppressWarnings("unused") @Nullable private List<ImageInfo> imageinfo; - - public String imageLicense() { - return imageinfo != null - && imageinfo.get(0).extmetadata() != null - && imageinfo.get(0).extmetadata().license() != null - ? imageinfo.get(0).extmetadata().license().value() : ""; - } - - public String imageLicenseShortName() { - return imageinfo != null - && imageinfo.get(0).extmetadata() != null - && imageinfo.get(0).extmetadata().licenseShortName() != null - ? imageinfo.get(0).extmetadata().licenseShortName().value() : ""; - } - - public String imageLicenseUrl() { - return imageinfo != null - && imageinfo.get(0).extmetadata() != null - && imageinfo.get(0).extmetadata().licenseUrl() != null - ? imageinfo.get(0).extmetadata().licenseUrl().value() : ""; - } - - static class ImageInfo { - @SuppressWarnings("unused") private Extmetadata extmetadata; - Extmetadata extmetadata() { - return extmetadata; - } - } - - static class Extmetadata { - @SuppressWarnings("unused") @SerializedName("License") private License license; - @SuppressWarnings("unused") @SerializedName("LicenseShortName") private LicenseShortName licenseShortName; - @SuppressWarnings("unused") @SerializedName("LicenseUrl") private LicenseUrl licenseUrl; - - License license() { - return license; - } - LicenseShortName licenseShortName() { - return licenseShortName; - } - LicenseUrl licenseUrl() { - return licenseUrl; - } - } - - static class License { - @SuppressWarnings("unused") private String value; - String value() { - return value; - } - } - - static class LicenseShortName { - @SuppressWarnings("unused") private String value; - String value() { - return value; - } - } - - static class LicenseUrl { - @SuppressWarnings("unused") private String value; - String value() { - return value; - } - } -} diff --git a/app/src/main/java/org/wikipedia/dataclient/mwapi/MwQueryPage.java b/app/src/main/java/org/wikipedia/dataclient/mwapi/MwQueryPage.java index 2007a62..44dfed1 100644 --- a/app/src/main/java/org/wikipedia/dataclient/mwapi/MwQueryPage.java +++ b/app/src/main/java/org/wikipedia/dataclient/mwapi/MwQueryPage.java @@ -5,6 +5,7 @@ import com.google.gson.annotations.SerializedName; +import org.wikipedia.gallery.ImageInfo; import org.wikipedia.json.annotations.Required; import org.wikipedia.model.BaseModel; @@ -23,6 +24,7 @@ @SuppressWarnings("unused") @Nullable private List<Coordinates> coordinates; @SuppressWarnings("unused") @Nullable private Thumbnail thumbnail; @SuppressWarnings("unused") @Nullable private Terms terms; + @SuppressWarnings("unused") @SerializedName("imageinfo") @Nullable private List<ImageInfo> imageInfo; @NonNull public String title() { return title; @@ -53,6 +55,10 @@ return terms != null && terms.description() != null ? terms.description().get(0) : null; } + @Nullable public ImageInfo imageInfo() { + return imageInfo != null ? imageInfo.get(0) : null; + } + public static class Revision { @SuppressWarnings("unused,NullableProblems") @SerializedName("contentformat") @NonNull private String contentFormat; @SuppressWarnings("unused,NullableProblems") @SerializedName("contentmodel") @NonNull private String contentModel; diff --git a/app/src/main/java/org/wikipedia/page/ImageLicense.java b/app/src/main/java/org/wikipedia/page/ImageLicense.java index 6c222d8..68a34ed 100644 --- a/app/src/main/java/org/wikipedia/page/ImageLicense.java +++ b/app/src/main/java/org/wikipedia/page/ImageLicense.java @@ -4,6 +4,7 @@ import android.support.annotation.NonNull; import org.wikipedia.R; +import org.wikipedia.gallery.ExtMetadata; import static org.apache.commons.lang3.StringUtils.defaultString; @@ -15,8 +16,10 @@ @NonNull private final String licenseShortName; @NonNull private final String licenseUrl; - public ImageLicense() { - this("", "", ""); + public ImageLicense(@NonNull ExtMetadata metadata) { + this.license = metadata.license() != null ? metadata.license().value() : ""; + this.licenseShortName = metadata.licenseShortName() != null ? metadata.licenseShortName().value() : ""; + this.licenseUrl = metadata.licenseUrl() != null ? metadata.licenseUrl().value() : ""; } public ImageLicense(@NonNull String license, @NonNull String licenseShortName, @NonNull String licenseUrl) { @@ -25,6 +28,10 @@ this.licenseUrl = licenseUrl; } + public ImageLicense() { + this("", "", ""); + } + @NonNull public String getLicense() { return license; } diff --git a/app/src/main/java/org/wikipedia/page/ImageLicenseFetchClient.java b/app/src/main/java/org/wikipedia/page/ImageLicenseFetchClient.java index 8f5bb70..0346703 100644 --- a/app/src/main/java/org/wikipedia/page/ImageLicenseFetchClient.java +++ b/app/src/main/java/org/wikipedia/page/ImageLicenseFetchClient.java @@ -6,7 +6,7 @@ import org.wikipedia.dataclient.WikiSite; import org.wikipedia.dataclient.mwapi.MwException; -import org.wikipedia.dataclient.mwapi.MwQueryImageLicensePage; +import org.wikipedia.dataclient.mwapi.MwQueryPage; import org.wikipedia.dataclient.mwapi.MwQueryResponse; import org.wikipedia.dataclient.retrofit.MwCachedService; @@ -22,17 +22,17 @@ @NonNull private MwCachedService<Service> cachedService = new MwCachedService<>(Service.class); public interface Callback { - void success(@NonNull Call<MwQueryResponse<QueryResult>> call, @NonNull List<MwQueryImageLicensePage> results); + void success(@NonNull Call<MwQueryResponse<QueryResult>> call, @NonNull ImageLicense result); void failure(@NonNull Call<MwQueryResponse<QueryResult>> call, @NonNull Throwable caught); } public Call<MwQueryResponse<QueryResult>> request(@NonNull WikiSite wiki, @NonNull PageTitle title, @NonNull Callback cb) { - return request(wiki, cachedService.service(wiki), title, cb); + return request(cachedService.service(wiki), title, cb); } - @VisibleForTesting Call<MwQueryResponse<QueryResult>> request(final WikiSite wiki, @NonNull Service service, + @VisibleForTesting Call<MwQueryResponse<QueryResult>> request(@NonNull Service service, @NonNull final PageTitle title, @NonNull final Callback cb) { Call<MwQueryResponse<QueryResult>> call = service.request(title.toString()); @@ -40,9 +40,15 @@ call.enqueue(new retrofit2.Callback<MwQueryResponse<QueryResult>>() { @Override public void onResponse(Call<MwQueryResponse<QueryResult>> call, Response<MwQueryResponse<QueryResult>> response) { - if (response.body().success()) { - cb.success(call, response.body().query().pages()); + if (response.body().success() + && response.body().query().pages() != null + && response.body().query().pages().get(0) != null) { + MwQueryPage page = response.body().query().pages().get(0); + cb.success(call, page.imageInfo() != null && page.imageInfo().getMetadata() != null + ? new ImageLicense(page.imageInfo().getMetadata()) + : new ImageLicense()); } 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.")); @@ -58,10 +64,9 @@ return call; } - public class QueryResult { - @SuppressWarnings("unused") @Nullable private List<MwQueryImageLicensePage> pages; - @Nullable List<MwQueryImageLicensePage> pages() { + @SuppressWarnings("unused") @Nullable private List<MwQueryPage> pages; + @Nullable List<MwQueryPage> pages() { return pages; } } diff --git a/app/src/main/java/org/wikipedia/page/snippet/ShareHandler.java b/app/src/main/java/org/wikipedia/page/snippet/ShareHandler.java index 11cc744..c9df7d9 100755 --- a/app/src/main/java/org/wikipedia/page/snippet/ShareHandler.java +++ b/app/src/main/java/org/wikipedia/page/snippet/ShareHandler.java @@ -25,7 +25,6 @@ import org.wikipedia.activity.ActivityUtil; import org.wikipedia.analytics.ShareAFactFunnel; import org.wikipedia.bridge.CommunicationBridge; -import org.wikipedia.dataclient.mwapi.MwQueryImageLicensePage; import org.wikipedia.dataclient.mwapi.MwQueryResponse; import org.wikipedia.page.ImageLicense; import org.wikipedia.page.ImageLicenseFetchClient; @@ -45,7 +44,6 @@ import org.wikipedia.wiktionary.WiktionaryDialog; import java.util.Arrays; -import java.util.List; import retrofit2.Call; @@ -140,19 +138,13 @@ title.getWikiSite()), new ImageLicenseFetchClient.Callback() { @Override public void success(@NonNull Call<MwQueryResponse<ImageLicenseFetchClient.QueryResult>> call, - @NonNull List<MwQueryImageLicensePage> pages) { - MwQueryImageLicensePage page = pages.get(0); - ImageLicense leadImageLicense = new ImageLicense(page.imageLicense(), - page.imageLicenseShortName(), - page.imageLicenseUrl()); - + @NonNull ImageLicense result) { final Bitmap snippetBitmap = SnippetImage.getSnippetImage(fragment.getContext(), fragment.getLeadImageBitmap(), title.getDisplayText(), fragment.getPage().isMainPage() ? "" : StringUtils.capitalize(title.getDescription()), selectedText, - leadImageLicense); - + result); fragment.showBottomSheet(new PreviewDialog(fragment, snippetBitmap, title, selectedText, funnel)); } diff --git a/app/src/test/java/org/wikipedia/page/ImageLicenseFetchClientTest.java b/app/src/test/java/org/wikipedia/page/ImageLicenseFetchClientTest.java index 2259cc0..5dee839 100644 --- a/app/src/test/java/org/wikipedia/page/ImageLicenseFetchClientTest.java +++ b/app/src/test/java/org/wikipedia/page/ImageLicenseFetchClientTest.java @@ -8,12 +8,9 @@ import org.mockito.ArgumentCaptor; import org.wikipedia.dataclient.WikiSite; import org.wikipedia.dataclient.mwapi.MwException; -import org.wikipedia.dataclient.mwapi.MwQueryImageLicensePage; import org.wikipedia.dataclient.mwapi.MwQueryResponse; import org.wikipedia.dataclient.okhttp.HttpStatusException; import org.wikipedia.test.MockWebServerTest; - -import java.util.List; import retrofit2.Call; @@ -41,16 +38,14 @@ Call<MwQueryResponse<ImageLicenseFetchClient.QueryResult>> call = request(cb); server().takeRequest(); - ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); + ArgumentCaptor<ImageLicense> captor = ArgumentCaptor.forClass(ImageLicense.class); verify(cb).success(eq(call), captor.capture()); - List<MwQueryImageLicensePage> result = captor.getValue(); - MwQueryImageLicensePage mark = result.get(0); + ImageLicense result = captor.getValue(); - assertThat(mark.title(), is("File:Mark Selby at Snooker German Masters (DerHexer) 2015-02-04 02.jpg")); - assertThat(mark.imageLicense(), is("cc-by-sa-4.0")); - assertThat(mark.imageLicenseShortName(), is("CC BY-SA 4.0")); - assertThat(mark.imageLicenseUrl(), is("http://creativecommons.org/licenses/by-sa/4.0")); + assertThat(result.getLicense(), is("cc-by-sa-4.0")); + assertThat(result.getLicenseShortName(), is("CC BY-SA 4.0")); + assertThat(result.getLicenseUrl(), is("http://creativecommons.org/licenses/by-sa/4.0")); } @Test public void testRequestResponseApiError() throws Throwable { @@ -87,13 +82,12 @@ @NonNull ImageLicenseFetchClient.Callback cb, @NonNull Class<? extends Throwable> throwable) { //noinspection unchecked - verify(cb, never()).success(any(Call.class), any(List.class)); + verify(cb, never()).success(any(Call.class), any(ImageLicense.class)); verify(cb).failure(eq(call), isA(throwable)); } private Call<MwQueryResponse<ImageLicenseFetchClient.QueryResult>> request( @NonNull ImageLicenseFetchClient.Callback cb) { - return subject.request(WIKISITE_TEST, service(ImageLicenseFetchClient.Service.class), - PAGE_TITLE_MARK_SELBY, cb); + return subject.request(service(ImageLicenseFetchClient.Service.class), PAGE_TITLE_MARK_SELBY, cb); } } -- To view, visit https://gerrit.wikimedia.org/r/354372 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ia70629d59bb5b5c76963d873afda8122bb277ac9 Gerrit-PatchSet: 1 Gerrit-Project: apps/android/wikipedia Gerrit-Branch: master Gerrit-Owner: Mholloway <mhollo...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits