From e508b4d5cf34f1c8b342d8f1ab523c6bd37c8ad0 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Fri, 12 Jul 2019 12:07:41 +0300 Subject: [PATCH] configure extra user info expandable --- .../mthmmy/activities/inbox/InboxAdapter.java | 117 +++++++++++++++++- .../mthmmy/activities/topic/TopicAdapter.java | 5 +- .../MessageAnimations.java} | 6 +- .../mthmmy/viewmodel/InboxViewModel.java | 21 ++++ .../mthmmy/viewmodel/TopicViewModel.java | 6 +- 5 files changed, 145 insertions(+), 10 deletions(-) rename app/src/main/java/gr/thmmy/mthmmy/{activities/topic/TopicAnimations.java => utils/MessageAnimations.java} (94%) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java index c8a9ae74..f1126895 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java @@ -4,9 +4,11 @@ import android.annotation.TargetApi; import android.content.Context; import android.content.Intent; import android.graphics.Color; +import android.graphics.Typeface; import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -32,10 +34,12 @@ import java.util.Objects; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.activities.board.BoardActivity; import gr.thmmy.mthmmy.activities.profile.ProfileActivity; +import gr.thmmy.mthmmy.utils.MessageAnimations; import gr.thmmy.mthmmy.activities.topic.TopicActivity; import gr.thmmy.mthmmy.model.PM; import gr.thmmy.mthmmy.model.ThmmyPage; import gr.thmmy.mthmmy.utils.CircleTransform; +import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; import gr.thmmy.mthmmy.viewmodel.InboxViewModel; import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; @@ -45,6 +49,8 @@ import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_ import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_URL; import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_USERNAME; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL; +import static gr.thmmy.mthmmy.utils.parsing.ParseHelpers.USER_COLOR_WHITE; +import static gr.thmmy.mthmmy.utils.parsing.ParseHelpers.USER_COLOR_YELLOW; public class InboxAdapter extends RecyclerView.Adapter { @@ -90,6 +96,115 @@ public class InboxAdapter extends RecyclerView.Adapter holder.subject.setText(currentPM.getSubject()); holder.pm.loadDataWithBaseURL("file:///android_asset/", currentPM.getContent(), "text/html", "UTF-8", null); + + // author info + if (currentPM.getAuthorSpecialRank() != null && !currentPM.getAuthorSpecialRank().equals("")) { + holder.specialRank.setText(currentPM.getAuthorSpecialRank()); + holder.specialRank.setVisibility(View.VISIBLE); + } else holder.specialRank.setVisibility(View.GONE); + if (currentPM.getAuthorRank() != null && !currentPM.getAuthorRank().equals("")) { + holder.rank.setText(currentPM.getAuthorRank()); + holder.rank.setVisibility(View.VISIBLE); + } else holder.rank.setVisibility(View.GONE); + if (currentPM.getAuthorGender() != null && !currentPM.getAuthorGender().equals("")) { + holder.gender.setText(currentPM.getAuthorGender()); + holder.gender.setVisibility(View.VISIBLE); + } else holder.gender.setVisibility(View.GONE); + if (currentPM.getAuthorNumberOfPosts() != null && !currentPM.getAuthorNumberOfPosts().equals("")) { + holder.numberOfPosts.setText(currentPM.getAuthorNumberOfPosts()); + holder.numberOfPosts.setVisibility(View.VISIBLE); + } else holder.numberOfPosts.setVisibility(View.GONE); + if (currentPM.getAuthorPersonalText() != null && !currentPM.getAuthorPersonalText().equals("")) { + holder.personalText.setText(currentPM.getAuthorPersonalText()); + holder.personalText.setVisibility(View.VISIBLE); + } else holder.personalText.setVisibility(View.GONE); + if (currentPM.getAuthorColor() != USER_COLOR_YELLOW) + holder.username.setTextColor(currentPM.getAuthorColor()); + else holder.username.setTextColor(USER_COLOR_WHITE); + + if (currentPM.getAuthorNumberOfStars() > 0) { + holder.stars.setTypeface(Typeface.createFromAsset(context.getAssets() + , "fonts/fontawesome-webfont.ttf")); + + String aStar = context.getResources().getString(R.string.fa_icon_star); + StringBuilder usersStars = new StringBuilder(); + for (int i = 0; i < currentPM.getAuthorNumberOfStars(); ++i) { + usersStars.append(aStar); + } + holder.stars.setText(usersStars.toString()); + holder.stars.setTextColor(currentPM.getAuthorColor()); + holder.stars.setVisibility(View.VISIBLE); + } else holder.stars.setVisibility(View.GONE); + + if (currentPM.isUserMentioned()) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + holder.cardChildLinear.setBackground(context.getResources(). + getDrawable(R.drawable.mention_card, null)); + } else + holder.cardChildLinear.setBackground(context.getResources(). + getDrawable(R.drawable.mention_card)); + } else if (currentPM.getAuthorColor() == ParseHelpers.USER_COLOR_PINK) { + //Special card for special member of the month! + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + holder.cardChildLinear.setBackground(context.getResources(). + getDrawable(R.drawable.member_of_the_month_card, null)); + } else + holder.cardChildLinear.setBackground(context.getResources(). + getDrawable(R.drawable.member_of_the_month_card)); + } else holder.cardChildLinear.setBackground(null); + + //Avoid's view's visibility recycling + if (inboxViewModel.isUserExtraInfoVisible(holder.getAdapterPosition())) { + holder.userExtraInfo.setVisibility(View.VISIBLE); + holder.userExtraInfo.setAlpha(1.0f); + + holder.username.setMaxLines(Integer.MAX_VALUE); + holder.username.setEllipsize(null); + + holder.subject.setTextColor(Color.parseColor("#FFFFFF")); + holder.subject.setMaxLines(Integer.MAX_VALUE); + holder.subject.setEllipsize(null); + } else { + holder.userExtraInfo.setVisibility(View.GONE); + holder.userExtraInfo.setAlpha(0.0f); + + holder.username.setMaxLines(1); + holder.username.setEllipsize(TextUtils.TruncateAt.END); + + holder.subject.setTextColor(Color.parseColor("#757575")); + holder.subject.setMaxLines(1); + holder.subject.setEllipsize(TextUtils.TruncateAt.END); + } + + //Sets graphics behavior + holder.thumbnail.setOnClickListener(view -> { + //Clicking the thumbnail opens user's profile + Intent intent = new Intent(context, ProfileActivity.class); + Bundle extras = new Bundle(); + extras.putString(BUNDLE_PROFILE_URL, currentPM.getAuthorProfileUrl()); + if (currentPM.getThumbnailUrl() == null) + extras.putString(BUNDLE_PROFILE_THUMBNAIL_URL, ""); + else + extras.putString(BUNDLE_PROFILE_THUMBNAIL_URL, currentPM.getAuthorProfileUrl()); + extras.putString(BUNDLE_PROFILE_USERNAME, currentPM.getAuthor()); + intent.putExtras(extras); + intent.setFlags(FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent); + }); + holder.header.setOnClickListener(v -> { + //Clicking the header makes it expand/collapse + inboxViewModel.toggleUserInfo(holder.getAdapterPosition()); + MessageAnimations.animateUserExtraInfoVisibility(holder.username, + holder.subject, Color.parseColor("#FFFFFF"), + Color.parseColor("#757575"), holder.userExtraInfo); + }); + //Clicking the expanded part of a header (the extra info) makes it collapse + holder.userExtraInfo.setOnClickListener(v -> { + inboxViewModel.hideUserInfo(holder.getAdapterPosition()); + MessageAnimations.animateUserExtraInfoVisibility(holder.username, + holder.subject, Color.parseColor("#FFFFFF"), + Color.parseColor("#757575"), (LinearLayout) v); + }); } @Override @@ -139,9 +254,7 @@ public class InboxAdapter extends RecyclerView.Adapter * This class is used to handle link clicks in WebViews. When link url is one that the app can * handle internally, it does. Otherwise user is prompt to open the link in a browser. */ - @SuppressWarnings("unchecked") private class LinkLauncher extends WebViewClient { - @SuppressWarnings("deprecation") @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { final Uri uri = Uri.parse(url); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java index 4d11aa92..636d7632 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java @@ -72,6 +72,7 @@ import gr.thmmy.mthmmy.model.ThmmyFile; import gr.thmmy.mthmmy.model.ThmmyPage; import gr.thmmy.mthmmy.model.TopicItem; import gr.thmmy.mthmmy.utils.CircleTransform; +import gr.thmmy.mthmmy.utils.MessageAnimations; import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; import gr.thmmy.mthmmy.utils.parsing.ThmmyParser; import gr.thmmy.mthmmy.viewmodel.TopicViewModel; @@ -544,14 +545,14 @@ class TopicAdapter extends RecyclerView.Adapter { holder.header.setOnClickListener(v -> { //Clicking the header makes it expand/collapse viewModel.toggleUserInfo(holder.getAdapterPosition()); - TopicAnimations.animateUserExtraInfoVisibility(holder.username, + MessageAnimations.animateUserExtraInfoVisibility(holder.username, holder.subject, Color.parseColor("#FFFFFF"), Color.parseColor("#757575"), holder.userExtraInfo); }); //Clicking the expanded part of a header (the extra info) makes it collapse holder.userExtraInfo.setOnClickListener(v -> { viewModel.hideUserInfo(holder.getAdapterPosition()); - TopicAnimations.animateUserExtraInfoVisibility(holder.username, + MessageAnimations.animateUserExtraInfoVisibility(holder.username, holder.subject, Color.parseColor("#FFFFFF"), Color.parseColor("#757575"), (LinearLayout) v); }); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAnimations.java b/app/src/main/java/gr/thmmy/mthmmy/utils/MessageAnimations.java similarity index 94% rename from app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAnimations.java rename to app/src/main/java/gr/thmmy/mthmmy/utils/MessageAnimations.java index 4cabbdfb..236d042c 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAnimations.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/MessageAnimations.java @@ -1,4 +1,4 @@ -package gr.thmmy.mthmmy.activities.topic; +package gr.thmmy.mthmmy.utils; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; @@ -7,11 +7,11 @@ import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; -class TopicAnimations { +public class MessageAnimations { /** * Method that animates view's visibility changes for user's extra info */ - static void animateUserExtraInfoVisibility(final TextView username, final TextView subject, + public static void animateUserExtraInfoVisibility(final TextView username, final TextView subject, int expandedColor, final int collapsedColor, final LinearLayout userExtraInfo) { //If the view is currently gone it fades it in diff --git a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/InboxViewModel.java b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/InboxViewModel.java index 7214658c..f6138806 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/InboxViewModel.java +++ b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/InboxViewModel.java @@ -2,11 +2,17 @@ package gr.thmmy.mthmmy.viewmodel; import androidx.lifecycle.ViewModel; +import java.util.ArrayList; + import gr.thmmy.mthmmy.activities.inbox.tasks.InboxTask; import gr.thmmy.mthmmy.model.Inbox; public class InboxViewModel extends ViewModel implements InboxTask.OnNetworkTaskFinishedListener { private static final String INBOX_URL = "https://www.thmmy.gr/smf/index.php?action=pm"; + /** + * caches the expand/collapse state of the user extra info in the current page for the recyclerview + */ + private ArrayList userExtraInfoVisibile = new ArrayList<>(); private InboxTask currentInboxTask; @@ -32,10 +38,25 @@ public class InboxViewModel extends ViewModel implements InboxTask.OnNetworkTask @Override public void onNetworkTaskFinished(int resultCode, Inbox inbox) { this.inbox = inbox; + userExtraInfoVisibile.clear(); + for (int i = 0; i < inbox.getPms().size(); i++) + userExtraInfoVisibile.add(false); onInboxTaskFinishedListener.onNetworkTaskFinished(resultCode, inbox); } public Inbox getInbox() { return inbox; } + + public boolean isUserExtraInfoVisible(int position) { + return userExtraInfoVisibile.get(position); + } + + public void hideUserInfo(int position) { + userExtraInfoVisibile.set(position, false); + } + + public void toggleUserInfo(int position) { + userExtraInfoVisibile.set(position, !userExtraInfoVisibile.get(position)); + } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java index a7e72883..3ce30dfd 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java +++ b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java @@ -111,7 +111,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa public void resetPage() { if (topicUrl == null) throw new NullPointerException("No topic task has been requested yet!"); Timber.i("Resetting page"); - loadUrl(ParseHelpers.getBaseURL(topicUrl) + "." + String.valueOf(currentPageIndex * 15)); + loadUrl(ParseHelpers.getBaseURL(topicUrl) + "." + currentPageIndex * 15); } public void resetPageThen(Runnable runnable) { @@ -122,7 +122,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa TopicViewModel.this.onTopicTaskCompleted(result); runnable.run(); }); - currentTopicTask.execute(ParseHelpers.getBaseURL(topicUrl) + "." + String.valueOf(currentPageIndex * 15)); + currentTopicTask.execute(ParseHelpers.getBaseURL(topicUrl) + "." + currentPageIndex * 15); } public void loadPageIndicated() { @@ -131,7 +131,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa int pageRequested = pageIndicatorIndex.getValue() - 1; if (pageRequested != currentPageIndex - 1) { Timber.i("Changing to page " + pageRequested + 1); - loadUrl(ParseHelpers.getBaseURL(topicUrl) + "." + String.valueOf(pageRequested * 15)); + loadUrl(ParseHelpers.getBaseURL(topicUrl) + "." + pageRequested * 15); pageIndicatorIndex.setValue(pageRequested + 1); } else { stopLoading();