From 2704ff0ecf85f6b6cdba81272a9d706425b1cbb0 Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Thu, 17 Aug 2023 08:43:10 +0200 Subject: [PATCH] Show community and user display name Closes #34 --- .../HistoryPostRecyclerViewAdapter.java | 6 -- .../PostDetailRecyclerViewAdapter.java | 14 +-- .../adapters/PostRecyclerViewAdapter.java | 24 ++---- .../infinityforlemmy/post/ParsePost.java | 62 ++++++------- .../eu/toldi/infinityforlemmy/post/Post.java | 86 ++++++++----------- .../utils/SharedPreferencesUtils.java | 4 + .../community/BasicCommunityInfo.kt | 43 ++++++++++ .../infinityforlemmy/user/BasicUserInfo.kt | 43 ++++++++++ app/src/main/res/values/strings.xml | 2 + .../main/res/xml/post_details_preferences.xml | 8 +- app/src/main/res/xml/post_preferences.xml | 7 +- 11 files changed, 188 insertions(+), 111 deletions(-) create mode 100644 app/src/main/kotlin/eu/toldi/infinityforlemmy/community/BasicCommunityInfo.kt create mode 100644 app/src/main/kotlin/eu/toldi/infinityforlemmy/user/BasicUserInfo.kt diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/HistoryPostRecyclerViewAdapter.java b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/HistoryPostRecyclerViewAdapter.java index f845eee9..268d397b 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/HistoryPostRecyclerViewAdapter.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/HistoryPostRecyclerViewAdapter.java @@ -554,9 +554,6 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter= 0) { - post.setSubredditIconUrl(iconUrl); - } } }); } else if (!post.getSubredditIconUrl().equals("")) { @@ -1082,9 +1079,6 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter= 0) { - post.setSubredditIconUrl(iconUrl); - } } }); } else if (!post.getSubredditIconUrl().equals("")) { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/PostDetailRecyclerViewAdapter.java b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/PostDetailRecyclerViewAdapter.java index ea2ca6b3..f3265949 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/PostDetailRecyclerViewAdapter.java @@ -172,6 +172,8 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter= 0) { - post.setSubredditIconUrl(iconUrl); - } } }); } else if (!post.getSubredditIconUrl().equals("")) { @@ -1191,9 +1186,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter= 0) { - post.setSubredditIconUrl(iconUrl); - } } }); } else if (!post.getSubredditIconUrl().equals("")) { @@ -1210,10 +1202,8 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter previews = new ArrayList<>(); - if(!post.isNull("thumbnail_url")) { + ArrayList previews = new ArrayList<>(); + if (!post.isNull("thumbnail_url")) { String thumbnail = post.getString("thumbnail_url"); //int[] wh_array = getImageDimension(thumbnail); previews.add(new Post.Preview(thumbnail, 0, 0, "", "")); } + BasicUserInfo authorInfo = new BasicUserInfo(creator.getInt("id"), author, authorFull, creator.optString("avatar", ""), creator.optString("display_name", author)); + BasicCommunityInfo communityInfo = new BasicCommunityInfo(community.getInt("id"), subredditName, subredditNamePrefixed, community.optString("icon", ""), community.optString("title", subredditName)); - - return parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed, - author,authorFull, postTimeMillis, title, previews, - downvotes,upvotes, voteType, nComments, upvoteRatio, nsfw, locked, saved, - distinguished, suggestedSort); + return parseData(data, permalink, id, communityInfo, + authorInfo, postTimeMillis, title, previews, + downvotes, upvotes, voteType, nComments, upvoteRatio, nsfw, locked, saved, + distinguished, suggestedSort); } - private static Post parseData(JSONObject data, String permalink, int id, String fullName, - String subredditName, String subredditNamePrefixed, String author, String authorFull, + private static Post parseData(JSONObject data, String permalink, int id, BasicCommunityInfo communityInfo, BasicUserInfo author, long postTimeMillis, String title, ArrayList previews, - int downvotes,int upvotes, int voteType, int nComments, int upvoteRatio, + int downvotes, int upvotes, int voteType, int nComments, int upvoteRatio, boolean nsfw, boolean locked, boolean saved, String distinguished, String suggestedSort) throws JSONException { @@ -228,7 +230,7 @@ public class ParsePost { if (!data.getJSONObject("post").isNull("body") && url.equals("")) { //Text post int postType = Post.TEXT_TYPE; - post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull, + post = new Post(id, communityInfo, author, postTimeMillis, title, permalink, downvotes, upvotes, postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort); @@ -241,9 +243,9 @@ public class ParsePost { //Image post int postType = Post.IMAGE_TYPE; - post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull, + post = new Post(id, communityInfo, author, postTimeMillis, title, url, permalink, downvotes, upvotes, - postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort); + postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort); if (previews.isEmpty()) { previews.add(new Post.Preview(url, 0, 0, "", "")); @@ -254,7 +256,7 @@ public class ParsePost { //No preview video post int postType = Post.VIDEO_TYPE; - post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull, postTimeMillis, title, permalink, downvotes,upvotes, postType, voteType, + post = new Post(id, communityInfo, author, postTimeMillis, title, permalink, downvotes, upvotes, postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort); post.setVideoUrl(url); @@ -262,7 +264,7 @@ public class ParsePost { } else if (!url.equals("")) { //No preview link post int postType = Post.NO_PREVIEW_LINK_TYPE; - post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull, + post = new Post(id, communityInfo, author, postTimeMillis, title, url, permalink, downvotes, upvotes, postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort); if (data.isNull(JSONUtils.SELFTEXT_KEY)) { @@ -296,7 +298,7 @@ public class ParsePost { } } else { int postType = Post.TEXT_TYPE; - post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull, + post = new Post(id, communityInfo, author, postTimeMillis, title, permalink, downvotes, upvotes, postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort); @@ -335,7 +337,7 @@ public class ParsePost { String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.HLS_URL_KEY)).toString(); String videoDownloadUrl = redditVideoObject.getString(JSONUtils.FALLBACK_URL_KEY); - post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull, postTimeMillis, title, permalink, downvotes, upvotes, postType, voteType, + post = new Post(id, communityInfo, author, postTimeMillis, title, permalink, downvotes, upvotes, postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort); post.setPreviews(previews); @@ -346,8 +348,8 @@ public class ParsePost { //Image post int postType = Post.IMAGE_TYPE; - post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFull, postTimeMillis, title, url, permalink, downvotes, upvotes, + post = new Post(id, communityInfo, author, + postTimeMillis, title, url, permalink, downvotes, upvotes, postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort); @@ -358,7 +360,7 @@ public class ParsePost { } else if (path.endsWith(".gif")) { //Gif post int postType = Post.GIF_TYPE; - post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull, + post = new Post(id, communityInfo, author, postTimeMillis, title, url, permalink, downvotes, upvotes, postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, @@ -374,7 +376,7 @@ public class ParsePost { url = url.substring(0, url.length() - 5) + ".mp4"; } - post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull, + post = new Post(id, communityInfo, author, postTimeMillis, title, url, permalink, downvotes, upvotes, postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, @@ -387,7 +389,7 @@ public class ParsePost { //Video post int postType = Post.VIDEO_TYPE; - post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull, + post = new Post(id, communityInfo, author, postTimeMillis, title, url, permalink, downvotes, upvotes, postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort); @@ -398,7 +400,7 @@ public class ParsePost { //Link post int postType = Post.LINK_TYPE; - post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull, + post = new Post(id, communityInfo, author, postTimeMillis, title, url, permalink, downvotes, upvotes, postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort); @@ -439,7 +441,7 @@ public class ParsePost { //Image post int postType = Post.IMAGE_TYPE; - post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,authorFull, + post = new Post(id, communityInfo, author, postTimeMillis, title, url, permalink, downvotes, upvotes, postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort); @@ -451,7 +453,7 @@ public class ParsePost { //Video post int postType = Post.VIDEO_TYPE; - post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,authorFull, + post = new Post(id, communityInfo, author, postTimeMillis, title, url, permalink, downvotes, upvotes, postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort); post.setPreviews(previews); @@ -461,9 +463,9 @@ public class ParsePost { //CP No Preview Link post int postType = Post.NO_PREVIEW_LINK_TYPE; - post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,authorFull, + post = new Post(id, communityInfo, author, postTimeMillis, title, url, permalink, downvotes, upvotes, - postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort); + postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort); //Need attention if (data.isNull(JSONUtils.SELFTEXT_KEY)) { post.setSelfText(""); @@ -579,7 +581,7 @@ public class ParsePost { singleGalleryObject.getJSONObject(JSONUtils.S_KEY).getInt(JSONUtils.Y_KEY), galleryItemCaption, galleryItemCaptionUrl)); } - Post.Gallery postGalleryItem = new Post.Gallery(mimeType, galleryItemUrl, "", subredditName + "-" + galleryId + "." + mimeType.substring(mimeType.lastIndexOf("/") + 1), galleryItemCaption, galleryItemCaptionUrl); + Post.Gallery postGalleryItem = new Post.Gallery(mimeType, galleryItemUrl, "", communityInfo.getDisplayName() + "-" + galleryId + "." + mimeType.substring(mimeType.lastIndexOf("/") + 1), galleryItemCaption, galleryItemCaptionUrl); // For issue #558 // Construct a fallback image url @@ -675,8 +677,8 @@ public class ParsePost { if (data.getJSONObject("post").getBoolean("featured_local")) { post.setFeaturedOnInstance(true); } - post.setAuthorIconUrl(authorAvatar); - post.setSubredditIconUrl(communityURL); + + return post; } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/post/Post.java b/app/src/main/java/eu/toldi/infinityforlemmy/post/Post.java index ef45dec4..ec03a8d4 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/post/Post.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/post/Post.java @@ -8,6 +8,9 @@ import androidx.annotation.Nullable; import java.util.ArrayList; +import eu.toldi.infinityforlemmy.community.BasicCommunityInfo; +import eu.toldi.infinityforlemmy.user.BasicUserInfo; + /** * Created by alex on 3/1/18. */ @@ -33,13 +36,8 @@ public class Post implements Parcelable { } }; private int id; - private String fullName; - private String subredditName; - private String subredditNamePrefixed; - private String subredditIconUrl; - private String author; - private String authorNamePrefixed; - private String authorIconUrl; + private BasicCommunityInfo communityInfo; + private BasicUserInfo author; private String title; private String selfText; private String selfTextPlain; @@ -80,18 +78,15 @@ public class Post implements Parcelable { private ArrayList previews = new ArrayList<>(); private ArrayList gallery = new ArrayList<>(); - public Post(int id, String fullName, String subredditName, String subredditNamePrefixed, - String author,String authorNamePrefixed, long postTimeMillis, - String title, String permalink, int downvotes,int upvotes, int postType, int voteType, int nComments, + public Post(int id, BasicCommunityInfo communityInfo, + BasicUserInfo userInfo, long postTimeMillis, + String title, String permalink, int downvotes, int upvotes, int postType, int voteType, int nComments, int upvoteRatio, boolean nsfw, boolean locked, boolean saved, String distinguished, String suggestedSort) { this.id = id; - this.fullName = fullName; - this.subredditName = subredditName; - this.subredditNamePrefixed = subredditNamePrefixed; - this.author = author; - this.authorNamePrefixed = authorNamePrefixed; + this.communityInfo = communityInfo; + this.author = userInfo; this.postTimeMillis = postTimeMillis; this.title = title; this.permalink = permalink; @@ -112,17 +107,14 @@ public class Post implements Parcelable { isRead = false; } - public Post(int id, String fullName, String subredditName, String subredditNamePrefixed, - String author, String authorNamePrefixed, long postTimeMillis, String title, - String url, String permalink, int downvotes,int upvotes, int postType, int voteType, int nComments, + public Post(int id, BasicCommunityInfo communityInfo, + BasicUserInfo author, long postTimeMillis, String title, + String url, String permalink, int downvotes, int upvotes, int postType, int voteType, int nComments, int upvoteRatio, - boolean nsfw, boolean locked, boolean saved, String distinguished, String suggestedSort) { + boolean nsfw, boolean locked, boolean saved, String distinguished, String suggestedSort) { this.id = id; - this.fullName = fullName; - this.subredditName = subredditName; - this.subredditNamePrefixed = subredditNamePrefixed; + this.communityInfo = communityInfo; this.author = author; - this.authorNamePrefixed = authorNamePrefixed; this.postTimeMillis = postTimeMillis; this.title = title; this.url = url; @@ -146,13 +138,8 @@ public class Post implements Parcelable { protected Post(Parcel in) { id = in.readInt(); - fullName = in.readString(); - subredditName = in.readString(); - subredditNamePrefixed = in.readString(); - subredditIconUrl = in.readString(); - author = in.readString(); - authorNamePrefixed = in.readString(); - authorIconUrl = in.readString(); + communityInfo = in.readParcelable(BasicCommunityInfo.class.getClassLoader()); + author = in.readParcelable(BasicUserInfo.class.getClassLoader()); postTimeMillis = in.readLong(); title = in.readString(); selfText = in.readString(); @@ -195,27 +182,24 @@ public class Post implements Parcelable { } public String getFullName() { - return fullName; + return communityInfo.getQualifiedName(); } public String getSubredditName() { - return subredditName; + return communityInfo.getDisplayName(); } public String getSubredditNamePrefixed() { - return subredditNamePrefixed; + return communityInfo.getQualifiedName(); } public String getSubredditIconUrl() { - return subredditIconUrl; + return communityInfo.getIcon(); } - public void setSubredditIconUrl(String subredditIconUrl) { - this.subredditIconUrl = subredditIconUrl; - } public String getAuthor() { - return author; + return author.getDisplayName(); } public boolean isAuthorDeleted() { @@ -223,20 +207,19 @@ public class Post implements Parcelable { } public void setAuthor(String author) { - this.author = author; - this.authorNamePrefixed = "u/" + author; + } public String getAuthorNamePrefixed() { - return authorNamePrefixed; + return author.getQualifiedName(); } public String getAuthorIconUrl() { - return authorIconUrl; + return (author.getAvatar() == null) ? "" : author.getAvatar(); } public void setAuthorIconUrl(String authorIconUrl) { - this.authorIconUrl = authorIconUrl; + } public long getPostTimeMillis() { @@ -512,16 +495,19 @@ public class Post implements Parcelable { this.gallery = gallery; } + public BasicCommunityInfo getCommunityInfo() { + return communityInfo; + } + + public BasicUserInfo getAuthorInfo() { + return author; + } + @Override public void writeToParcel(Parcel parcel, int i) { parcel.writeInt(id); - parcel.writeString(fullName); - parcel.writeString(subredditName); - parcel.writeString(subredditNamePrefixed); - parcel.writeString(subredditIconUrl); - parcel.writeString(author); - parcel.writeString(authorNamePrefixed); - parcel.writeString(authorIconUrl); + parcel.writeParcelable(communityInfo, i); + parcel.writeParcelable(author, i); parcel.writeLong(postTimeMillis); parcel.writeString(title); parcel.writeString(selfText); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/utils/SharedPreferencesUtils.java b/app/src/main/java/eu/toldi/infinityforlemmy/utils/SharedPreferencesUtils.java index 5031ce82..4b4de07b 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/utils/SharedPreferencesUtils.java @@ -54,6 +54,10 @@ public class SharedPreferencesUtils { public static final String COMMENT_SEPARATE_UP_AND_DOWN_VOTES = "comment_separate_down_and_up_votes"; + public static final String POST_DISPLAY_NAME_INSTEAD_OF_USERNAME = "post_display_name_instead_of_user_name"; + + public static final String POST_DETAIL_DISPLAY_NAME_INSTEAD_OF_USERNAME = "post_detail_display_name_instead_of_user_name"; + public static final String SORT_TYPE_SHARED_PREFERENCES_FILE = "eu.toldi.infinityforlemmy.sort_type"; public static final String SORT_TYPE_BEST_POST = "sort_type_best_post"; public static final String SORT_TIME_BEST_POST = "sort_time_best_post"; diff --git a/app/src/main/kotlin/eu/toldi/infinityforlemmy/community/BasicCommunityInfo.kt b/app/src/main/kotlin/eu/toldi/infinityforlemmy/community/BasicCommunityInfo.kt new file mode 100644 index 00000000..6867b9c4 --- /dev/null +++ b/app/src/main/kotlin/eu/toldi/infinityforlemmy/community/BasicCommunityInfo.kt @@ -0,0 +1,43 @@ +package eu.toldi.infinityforlemmy.community + +import android.os.Parcel +import android.os.Parcelable + +data class BasicCommunityInfo( + val id: Int, + val name: String, + val qualifiedName: String, + val icon: String?, + val displayName: String +) : Parcelable { + constructor(parcel: Parcel) : this( + parcel.readInt(), + parcel.readString()!!, + parcel.readString()!!, + parcel.readString(), + parcel.readString()!! + ) { + } + + override fun writeToParcel(parcel: Parcel, flags: Int) { + parcel.writeInt(id) + parcel.writeString(name) + parcel.writeString(qualifiedName) + parcel.writeString(icon) + parcel.writeString(displayName) + } + + override fun describeContents(): Int { + return 0 + } + + companion object CREATOR : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): BasicCommunityInfo { + return BasicCommunityInfo(parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } +} diff --git a/app/src/main/kotlin/eu/toldi/infinityforlemmy/user/BasicUserInfo.kt b/app/src/main/kotlin/eu/toldi/infinityforlemmy/user/BasicUserInfo.kt new file mode 100644 index 00000000..1573013d --- /dev/null +++ b/app/src/main/kotlin/eu/toldi/infinityforlemmy/user/BasicUserInfo.kt @@ -0,0 +1,43 @@ +package eu.toldi.infinityforlemmy.user + +import android.os.Parcel +import android.os.Parcelable + +data class BasicUserInfo( + val id: Int, + val username: String, + val qualifiedName: String, + val avatar: String?, + val displayName: String +) : Parcelable { + constructor(parcel: Parcel) : this( + parcel.readInt(), + parcel.readString()!!, + parcel.readString()!!, + parcel.readString(), + parcel.readString()!! + ) { + } + + override fun writeToParcel(parcel: Parcel, flags: Int) { + parcel.writeInt(id) + parcel.writeString(username) + parcel.writeString(qualifiedName) + parcel.writeString(avatar) + parcel.writeString(displayName) + } + + override fun describeContents(): Int { + return 0 + } + + companion object CREATOR : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): BasicUserInfo { + return BasicUserInfo(parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 46fc01a9..8238e3d7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1365,4 +1365,6 @@ Unblock User Unblocked Failed to unblock user + Hide community and user instance + Show community and user display names diff --git a/app/src/main/res/xml/post_details_preferences.xml b/app/src/main/res/xml/post_details_preferences.xml index 816e7c7c..984606a6 100644 --- a/app/src/main/res/xml/post_details_preferences.xml +++ b/app/src/main/res/xml/post_details_preferences.xml @@ -25,7 +25,13 @@ + app:title="@string/settings_hide_community_and_user_instance" /> + + + + app:title="@string/settings_hide_community_and_user_instance" /> + +