Small refactoring p.2

This commit is contained in:
Nazar-Kutz 2020-07-08 14:36:11 +03:00
parent 17ab336cfc
commit a08ac1fa01
3 changed files with 13 additions and 38 deletions

View file

@ -5,14 +5,12 @@ public class WikiImage {
private String imageName; private String imageName;
private String imageStubUrl; private String imageStubUrl;
private String imageHiResUrl; private String imageHiResUrl;
private String datatype;
public WikiImage(String imageName, String imageStubUrl, public WikiImage(String imageName, String imageStubUrl,
String imageHiResUrl, String datatype) { String imageHiResUrl) {
this.imageName = imageName; this.imageName = imageName;
this.imageStubUrl = imageStubUrl; this.imageStubUrl = imageStubUrl;
this.imageHiResUrl = imageHiResUrl; this.imageHiResUrl = imageHiResUrl;
this.datatype = datatype;
} }
public String getImageName() { public String getImageName() {
@ -27,7 +25,4 @@ public class WikiImage {
return imageHiResUrl; return imageHiResUrl;
} }
public String getDatatype() {
return datatype;
}
} }

View file

@ -7,19 +7,18 @@ import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard; import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import org.json.JSONObject;
public class WikiImageCard extends ImageCard { public class WikiImageCard extends ImageCard {
public WikiImageCard(final MapActivity mapActivity, final JSONObject imageObject, public WikiImageCard(final MapActivity mapActivity,
final WikiImage wikiImage) { final WikiImage wikiImage) {
super(mapActivity, imageObject); super(mapActivity, null);
if (topIconId == 0) { if (topIconId == 0) {
topIconId = R.drawable.ic_logo_wikimedia; topIconId = R.drawable.ic_logo_wikimedia;
} }
this.imageUrl = wikiImage.getImageStubUrl(); this.imageUrl = wikiImage.getImageStubUrl();
this.title = wikiImage.getImageName();
this.url = this.imageUrl; this.url = this.imageUrl;
View.OnClickListener onClickListener = new View.OnClickListener() { View.OnClickListener onClickListener = new View.OnClickListener() {

View file

@ -41,7 +41,7 @@ public class WikiImageHelper {
Gson gson = new Gson(); Gson gson = new Gson();
WikipediaResponse response = gson.fromJson(rawResponse.toString(), WikipediaResponse.class); WikipediaResponse response = gson.fromJson(rawResponse.toString(), WikipediaResponse.class);
for (WikiImage img : getImageData(response)) { for (WikiImage img : getImageData(response)) {
images.add(new WikiImageCard(mapActivity, null, img)); images.add(new WikiImageCard(mapActivity, img));
} }
return; return;
} catch (JsonSyntaxException e) { } catch (JsonSyntaxException e) {
@ -54,7 +54,7 @@ public class WikiImageHelper {
} }
} }
public static List<WikiImage> getImageData(WikipediaResponse response) { private static List<WikiImage> getImageData(WikipediaResponse response) {
List<WikiImage> images = new ArrayList<>(); List<WikiImage> images = new ArrayList<>();
for (P18 p18 : response.claims.p18) { for (P18 p18 : response.claims.p18) {
String imageFileName = p18.mainsnak.datavalue.value; String imageFileName = p18.mainsnak.datavalue.value;
@ -72,8 +72,7 @@ public class WikiImageHelper {
urlHashParts[0] + "/" + urlHashParts[1] + "/" + urlHashParts[0] + "/" + urlHashParts[1] + "/" +
imageFileName + "/" + THUMB_SIZE + "px-" + imageFileName + "/" + THUMB_SIZE + "px-" +
imageFileName; imageFileName;
images.add(new WikiImage(imageName, imageStubUrl, images.add(new WikiImage(imageName, imageStubUrl, imageHiResUrl));
imageHiResUrl, p18.mainsnak.datatype));
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
LOG.error(e.getLocalizedMessage()); LOG.error(e.getLocalizedMessage());
@ -84,18 +83,18 @@ public class WikiImageHelper {
} }
@NonNull @NonNull
public static String[] getHash(@NonNull String s) { private static String[] getHash(@NonNull String s) {
String md5 = new String(Hex.encodeHex(DigestUtils.md5(s))); String md5 = new String(Hex.encodeHex(DigestUtils.md5(s)));
return new String[]{md5.substring(0, 1), md5.substring(0, 2)}; return new String[]{md5.substring(0, 1), md5.substring(0, 2)};
} }
private class Claims { private static class Claims {
@SerializedName("P18") @SerializedName("P18")
@Expose @Expose
private List<P18> p18 = null; private List<P18> p18 = null;
} }
public class Datavalue { private static class Datavalue {
@SerializedName("value") @SerializedName("value")
@Expose @Expose
private String value; private String value;
@ -104,16 +103,7 @@ public class WikiImageHelper {
private String type; private String type;
} }
public class Mainsnak { private static class Mainsnak {
@SerializedName("snaktype")
@Expose
private String snaktype;
@SerializedName("property")
@Expose
private String property;
@SerializedName("hash")
@Expose
private String hash;
@SerializedName("datavalue") @SerializedName("datavalue")
@Expose @Expose
private Datavalue datavalue; private Datavalue datavalue;
@ -122,22 +112,13 @@ public class WikiImageHelper {
private String datatype; private String datatype;
} }
public class P18 { private static class P18 {
@SerializedName("mainsnak") @SerializedName("mainsnak")
@Expose @Expose
private Mainsnak mainsnak; private Mainsnak mainsnak;
@SerializedName("type")
@Expose
private String type;
@SerializedName("id")
@Expose
private String id;
@SerializedName("rank")
@Expose
private String rank;
} }
public class WikipediaResponse { private static class WikipediaResponse {
@SerializedName("claims") @SerializedName("claims")
@Expose @Expose
private Claims claims; private Claims claims;