Created
June 23, 2019 12:42
-
-
Save NinoDLC/a5ffd051a1a142ff99dc3a08dcbc15bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.delcey.p5.pojo.query; | |
import android.content.Context; | |
import android.os.Parcel; | |
import android.os.Parcelable; | |
import android.text.TextUtils; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.Nullable; | |
import com.google.gson.annotations.Expose; | |
import com.google.gson.annotations.SerializedName; | |
import fr.delcey.p5.utils.ScreenUtils; | |
import java.text.DateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
@SuppressWarnings("unused") | |
public class Article implements Parcelable { | |
private static final transient DateFormat sHumanDateFormatter = DateFormat.getDateInstance(); | |
@SerializedName("section") | |
@Expose | |
private String section; | |
// From Top Stories API | |
@SerializedName("subsection") | |
@Expose | |
private String subsection; | |
// From Most Popular API | |
@SerializedName("column") | |
@Expose | |
private String column; | |
@SerializedName("title") | |
@Expose | |
private String title; | |
@SerializedName("url") | |
@Expose | |
private String url; | |
@SerializedName("published_date") | |
@Expose | |
private Date publishedDate; | |
// From Top Stories API | |
@SerializedName("multimedia") | |
@Expose | |
private List<MediaMetadata> mediaMetadatas; | |
// From Most Popular API | |
@SerializedName("media") | |
@Expose | |
private List<Media> medias; | |
// Lazy initialization | |
@Nullable | |
private String mThumbnailUrl; | |
@NonNull | |
public String getTheme() { | |
StringBuilder result = new StringBuilder(); | |
result.append(section); | |
if (result.length() > 0) { | |
if (!TextUtils.isEmpty(subsection)) { | |
result.append(" > "); | |
result.append(subsection); | |
} else if (!TextUtils.isEmpty(column)) { | |
result.append(" > "); | |
result.append(column); | |
} | |
} | |
return result.toString(); | |
} | |
public String getTitle() { | |
return title; | |
} | |
@Nullable | |
public String getUrl() { | |
return url; | |
} | |
public String getDate() { | |
return publishedDate == null ? null : sHumanDateFormatter.format(publishedDate); | |
} | |
@Nullable | |
public String getThumbnailUrl(Context context) { | |
if (mThumbnailUrl == null) { | |
mThumbnailUrl = computeBestThumbnailUrl(context); | |
} | |
return mThumbnailUrl; | |
} | |
@Nullable | |
private String computeBestThumbnailUrl(Context context) { | |
int maximumImageSizeInPixel = ScreenUtils.getArticleImageSizeInPixel(context); | |
maximumImageSizeInPixel *= 1.2; // Allow up to 20% margin error "too large" image | |
ThumbnailValues thumbnailValues = new ThumbnailValues(); | |
List<MediaMetadata> mediaMetadatas = getMediadatas(); | |
if (mediaMetadatas != null) { | |
for (MediaMetadata mediaMetadata : mediaMetadatas) { | |
selectBestThumbnailValues(thumbnailValues, | |
maximumImageSizeInPixel, | |
mediaMetadata.url, | |
mediaMetadata.width, | |
mediaMetadata.height); | |
} | |
} | |
return thumbnailValues.imageUrl; | |
} | |
@Nullable | |
private List<MediaMetadata> getMediadatas() { | |
// From Top Stories API | |
if (mediaMetadatas != null) { | |
return mediaMetadatas; | |
} else if (medias != null && !medias.isEmpty()) { // From Most Popular API | |
return medias.get(0).mediaMetadata; | |
} | |
return null; | |
} | |
private void selectBestThumbnailValues(@NonNull ThumbnailValues thumbnail, | |
int maximumImageSizeInPixel, | |
@Nullable String url, | |
@Nullable Integer width, | |
@Nullable Integer height) { | |
if (TextUtils.isEmpty(url)) { | |
return; | |
} | |
// First line | |
if (TextUtils.isEmpty(thumbnail.imageUrl)) { | |
thumbnail.setThumbnailValues(url, width, height); | |
return; | |
} | |
// We're looking for square images only | |
if (height != null && height.equals(width)) { | |
if (thumbnail.imageWidth == null || thumbnail.imageHeight == null | |
|| (height > thumbnail.imageHeight && height <= maximumImageSizeInPixel)) { | |
thumbnail.setThumbnailValues(url, width, height); | |
} | |
} | |
} | |
private class ThumbnailValues { | |
@Nullable | |
private String imageUrl; | |
@Nullable | |
private Integer imageWidth; | |
@Nullable | |
private Integer imageHeight; | |
void setThumbnailValues(String imageUrl, @Nullable Integer imageWidth, @Nullable Integer imageHeight) { | |
this.imageUrl = imageUrl; | |
this.imageWidth = imageWidth; | |
this.imageHeight = imageHeight; | |
} | |
} | |
// Used by NYTimesAdapter.DiffUtils | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) { return true; } | |
if (o == null || getClass() != o.getClass()) { return false; } | |
Article article = (Article) o; | |
if (section != null ? !section.equals(article.section) : article.section != null) { return false; } | |
if (subsection != null ? !subsection.equals(article.subsection) : article.subsection != null) { return false; } | |
if (column != null ? !column.equals(article.column) : article.column != null) { return false; } | |
if (title != null ? !title.equals(article.title) : article.title != null) { return false; } | |
if (url != null ? !url.equals(article.url) : article.url != null) { return false; } | |
if (publishedDate != null ? !publishedDate.equals(article.publishedDate) : article.publishedDate != null) { | |
return false; | |
} | |
List<MediaMetadata> mediaMetadata = getMediadatas(); | |
return mediaMetadata != null ? !mediaMetadata.equals(article.getMediadatas()) : article.getMediadatas() != null; | |
} | |
// region Parcelable | |
@Override | |
public int describeContents() { | |
return 0; | |
} | |
@Override | |
public void writeToParcel(Parcel dest, int flags) { | |
dest.writeString(this.section); | |
dest.writeString(this.subsection); | |
dest.writeString(this.column); | |
dest.writeString(this.title); | |
dest.writeString(this.url); | |
dest.writeLong(this.publishedDate != null ? this.publishedDate.getTime() : -1); | |
dest.writeList(this.mediaMetadatas); | |
dest.writeList(this.medias); | |
} | |
private Article(Parcel in) { | |
this.section = in.readString(); | |
this.subsection = in.readString(); | |
this.column = in.readString(); | |
this.title = in.readString(); | |
this.url = in.readString(); | |
long tmpPublishedDate = in.readLong(); | |
this.publishedDate = tmpPublishedDate == -1 ? null : new Date(tmpPublishedDate); | |
this.mediaMetadatas = new ArrayList<>(); | |
in.readList(this.mediaMetadatas, MediaMetadata.class.getClassLoader()); | |
this.medias = new ArrayList<>(); | |
in.readList(this.medias, Media.class.getClassLoader()); | |
} | |
public static final Creator<Article> CREATOR = new Creator<Article>() { | |
@Override | |
public Article createFromParcel(Parcel source) { | |
return new Article(source); | |
} | |
@Override | |
public Article[] newArray(int size) { | |
return new Article[size]; | |
} | |
}; | |
// endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment