From c08068eeea12e39598a2929a9ef005e415d52b1e Mon Sep 17 00:00:00 2001 From: seven332 Date: Sat, 23 Feb 2019 16:23:14 +0800 Subject: [PATCH] Show heart icon if the gallery is favourited --- .../ehviewer/client/data/GalleryDetail.java | 3 -- .../ehviewer/client/data/GalleryInfo.java | 10 ++++- .../client/parser/GalleryListParser.java | 43 +++++++++++++++++++ .../ehviewer/ui/scene/GalleryAdapter.java | 27 ++++++++---- .../ehviewer/ui/scene/GalleryHolder.java | 19 ++++---- app/src/main/res/drawable/spacer_x6.xml | 28 ++++++++++++ app/src/main/res/drawable/v_heart_x16.xml | 29 +++++++++++++ app/src/main/res/layout/item_gallery_list.xml | 43 ++++++++++++------- 8 files changed, 165 insertions(+), 37 deletions(-) create mode 100644 app/src/main/res/drawable/spacer_x6.xml create mode 100644 app/src/main/res/drawable/v_heart_x16.xml diff --git a/app/src/main/java/com/hippo/ehviewer/client/data/GalleryDetail.java b/app/src/main/java/com/hippo/ehviewer/client/data/GalleryDetail.java index a8889aca1..5b11dfbf8 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/data/GalleryDetail.java +++ b/app/src/main/java/com/hippo/ehviewer/client/data/GalleryDetail.java @@ -40,7 +40,6 @@ public class GalleryDetail extends GalleryInfo { public GalleryComment[] comments; public int previewPages; public PreviewSet previewSet; - public String favoriteName; @Override public int describeContents() { @@ -65,7 +64,6 @@ public class GalleryDetail extends GalleryInfo { dest.writeParcelableArray(this.comments, 0); dest.writeInt(this.previewPages); dest.writeParcelable(previewSet, flags); - dest.writeString(this.favoriteName); } public GalleryDetail() { @@ -98,7 +96,6 @@ public class GalleryDetail extends GalleryInfo { } this.previewPages = in.readInt(); this.previewSet = in.readParcelable(PreviewSet.class.getClassLoader()); - this.favoriteName = in.readString(); } public static final Creator CREATOR = new Creator() { diff --git a/app/src/main/java/com/hippo/ehviewer/client/data/GalleryInfo.java b/app/src/main/java/com/hippo/ehviewer/client/data/GalleryInfo.java index 0e1d5b35b..292cd100d 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/data/GalleryInfo.java +++ b/app/src/main/java/com/hippo/ehviewer/client/data/GalleryInfo.java @@ -104,6 +104,7 @@ public class GalleryInfo implements Parcelable { public String posted; public String uploader; public float rating; + public boolean rated; @Nullable public String[] simpleTags; public int pages; @@ -120,7 +121,8 @@ public class GalleryInfo implements Parcelable { */ public String simpleLanguage; - public boolean rated; + public int favoriteSlot = -2; + public String favoriteName; public final void generateSLang() { if (simpleTags != null) { @@ -168,6 +170,7 @@ public class GalleryInfo implements Parcelable { dest.writeString(this.posted); dest.writeString(this.uploader); dest.writeFloat(this.rating); + dest.writeByte(this.rated ? (byte) 1 : (byte) 0); dest.writeString(this.simpleLanguage); dest.writeStringArray(this.simpleTags); dest.writeInt(this.thumbWidth); @@ -175,6 +178,8 @@ public class GalleryInfo implements Parcelable { dest.writeInt(this.spanSize); dest.writeInt(this.spanIndex); dest.writeInt(this.spanGroupIndex); + dest.writeInt(this.favoriteSlot); + dest.writeString(this.favoriteName); } public GalleryInfo() {} @@ -189,6 +194,7 @@ public class GalleryInfo implements Parcelable { this.posted = in.readString(); this.uploader = in.readString(); this.rating = in.readFloat(); + this.rated = in.readByte() != 0; this.simpleLanguage = in.readString(); this.simpleTags = in.createStringArray(); this.thumbWidth = in.readInt(); @@ -196,6 +202,8 @@ public class GalleryInfo implements Parcelable { this.spanSize = in.readInt(); this.spanIndex = in.readInt(); this.spanGroupIndex = in.readInt(); + this.favoriteSlot = in.readInt(); + this.favoriteName = in.readString(); } public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java index 5857ed9d8..d4f133d05 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java @@ -19,6 +19,7 @@ package com.hippo.ehviewer.client.parser; import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import com.hippo.ehviewer.EhDB; import com.hippo.ehviewer.client.EhUtils; import com.hippo.ehviewer.client.data.GalleryInfo; import com.hippo.ehviewer.client.exception.ParseException; @@ -42,6 +43,7 @@ public class GalleryListParser { private static final Pattern PATTERN_RATING = Pattern.compile("\\d+px"); private static final Pattern PATTERN_THUMB_SIZE = Pattern.compile("height:(\\d+)px; width:(\\d+)px"); + private static final Pattern PATTERN_FAVORITE_SLOT = Pattern.compile("background-position:0px -(\\d+)px;"); public static class Result { public int pages; @@ -176,6 +178,24 @@ public class GalleryListParser { Log.w(TAG, "Can't parse gallery info uploader"); gi.uploader = ""; } + // Favourite + Element it3 = JsoupUtils.getElementByClass(e, "it3"); + if (it3 != null) { + for (Element element : it3.children()) { + if (!"i".equals(element.className())) { + continue; + } + Matcher m = PATTERN_FAVORITE_SLOT.matcher(element.attr("style")); + if (m.find()) { + gi.favoriteSlot = (NumberUtils.parseIntSafely(m.group(1), -36) - 2) / 19; + gi.favoriteName = element.attr("title"); + break; + } + } + } + if (gi.favoriteSlot == -2) { + gi.favoriteSlot = EhDB.containLocalFavorites(gi.gid) ? -1 : -2; + } gi.generateSLang(); @@ -233,6 +253,29 @@ public class GalleryListParser { gi.rating = -1.0f; } + // Rating + Element id44 = JsoupUtils.getElementByClass(e, "id44"); + if (id44 != null) { + Elements elements = id44.children(); + if (elements.size() > 0) { + Element first = elements.first(); + for (Element element : first.children()) { + if (!"i".equals(element.className())) { + continue; + } + Matcher m = PATTERN_FAVORITE_SLOT.matcher(element.attr("style")); + if (m.find()) { + gi.favoriteSlot = (NumberUtils.parseIntSafely(m.group(1), -36) - 2) / 19; + gi.favoriteName = element.attr("title"); + break; + } + } + } + } + if (gi.favoriteSlot == -2) { + gi.favoriteSlot = EhDB.containLocalFavorites(gi.gid) ? -1 : -2; + } + gi.generateSLang(); return gi; diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryAdapter.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryAdapter.java index 9d76aa75a..308dac179 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryAdapter.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryAdapter.java @@ -19,10 +19,10 @@ package com.hippo.ehviewer.ui.scene; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Build; +import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.RelativeLayout; import android.widget.TextView; import androidx.annotation.IntDef; import androidx.annotation.NonNull; @@ -191,23 +191,32 @@ abstract class GalleryAdapter extends RecyclerView.Adapter { holder.rating.setRating(gi.rating); TextView category = holder.category; String newCategoryText = EhUtils.getCategory(gi.category); - if (!newCategoryText.equals(category.getText())) { + if (!newCategoryText.equals(category.getText().toString())) { category.setText(newCategoryText); category.setBackgroundColor(EhUtils.getCategoryColor(gi.category)); } holder.posted.setText(gi.posted); if (gi.pages == 0 || !Settings.getShowGalleryPages()) { holder.pages.setText(null); - RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.simpleLanguage.getLayoutParams(); - lp.addRule(RelativeLayout.LEFT_OF, 0); - lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); + holder.pages.setVisibility(View.GONE); } else { holder.pages.setText(Integer.toString(gi.pages) + "P"); - RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.simpleLanguage.getLayoutParams(); - lp.addRule(RelativeLayout.LEFT_OF, R.id.pages); - lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0); + holder.pages.setVisibility(View.VISIBLE); + } + if (TextUtils.isEmpty(gi.simpleLanguage)) { + holder.simpleLanguage.setText(null); + holder.simpleLanguage.setVisibility(View.GONE); + } else { + holder.simpleLanguage.setText(gi.simpleLanguage); + holder.simpleLanguage.setVisibility(View.VISIBLE); + } + if (gi.favoriteSlot < -1 || gi.favoriteSlot > 10) { + holder.favouriteSlot.setImageDrawable(null); + holder.favouriteSlot.setVisibility(View.GONE); + } else { + holder.favouriteSlot.setImageResource(R.drawable.v_heart_x16); + holder.favouriteSlot.setVisibility(View.VISIBLE); } - holder.simpleLanguage.setText(gi.simpleLanguage); break; } case TYPE_GRID: { diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryHolder.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryHolder.java index cab11dbeb..65b4869da 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryHolder.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryHolder.java @@ -18,6 +18,7 @@ package com.hippo.ehviewer.ui.scene; import android.view.View; import android.widget.TextView; +import androidx.appcompat.widget.AppCompatImageView; import androidx.recyclerview.widget.RecyclerView; import com.hippo.ehviewer.R; import com.hippo.ehviewer.widget.SimpleRatingView; @@ -33,17 +34,19 @@ class GalleryHolder extends RecyclerView.ViewHolder { public final TextView posted; public final TextView pages; public final TextView simpleLanguage; + public final AppCompatImageView favouriteSlot; public GalleryHolder(View itemView) { super(itemView); - thumb = (LoadImageView) itemView.findViewById(R.id.thumb); - title = (TextView) itemView.findViewById(R.id.title); - uploader = (TextView) itemView.findViewById(R.id.uploader); - rating = (SimpleRatingView) itemView.findViewById(R.id.rating); - category = (TextView) itemView.findViewById(R.id.category); - posted = (TextView) itemView.findViewById(R.id.posted); - pages = (TextView) itemView.findViewById(R.id.pages); - simpleLanguage = (TextView) itemView.findViewById(R.id.simple_language); + thumb = itemView.findViewById(R.id.thumb); + title = itemView.findViewById(R.id.title); + uploader = itemView.findViewById(R.id.uploader); + rating = itemView.findViewById(R.id.rating); + category = itemView.findViewById(R.id.category); + posted = itemView.findViewById(R.id.posted); + pages = itemView.findViewById(R.id.pages); + simpleLanguage = itemView.findViewById(R.id.simple_language); + favouriteSlot = itemView.findViewById(R.id.favourite_slot); } } diff --git a/app/src/main/res/drawable/spacer_x6.xml b/app/src/main/res/drawable/spacer_x6.xml new file mode 100644 index 000000000..9beea5d49 --- /dev/null +++ b/app/src/main/res/drawable/spacer_x6.xml @@ -0,0 +1,28 @@ + + + + + + + + + + diff --git a/app/src/main/res/drawable/v_heart_x16.xml b/app/src/main/res/drawable/v_heart_x16.xml new file mode 100644 index 000000000..cb2dfc70e --- /dev/null +++ b/app/src/main/res/drawable/v_heart_x16.xml @@ -0,0 +1,29 @@ + + + + + + + + diff --git a/app/src/main/res/layout/item_gallery_list.xml b/app/src/main/res/layout/item_gallery_list.xml index 2fb815276..6c95b131d 100644 --- a/app/src/main/res/layout/item_gallery_list.xml +++ b/app/src/main/res/layout/item_gallery_list.xml @@ -87,23 +87,34 @@ android:layout_marginRight="6dp" android:layout_marginBottom="4dp"/> - + - + + + + + +