From b28e402c3bf8777b37515d8cf39d2b662713f535 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 24 Aug 2020 19:32:13 +0300 Subject: [PATCH] ProfileSummary optimizations --- app/src/main/assets/style_light.css | 1 + .../profile/summary/SummaryFragment.java | 127 +++++++++++------- .../res/layout/fragment_profile_summary.xml | 3 +- 3 files changed, 78 insertions(+), 53 deletions(-) diff --git a/app/src/main/assets/style_light.css b/app/src/main/assets/style_light.css index a3cafed2..b70e8585 100644 --- a/app/src/main/assets/style_light.css +++ b/app/src/main/assets/style_light.css @@ -8,4 +8,5 @@ body { .customSignature { background: #434649 !important; + margin-top: 0.5em; } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java index d58112ea..ea257924 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java @@ -19,7 +19,8 @@ import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; -import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Objects; import gr.thmmy.mthmmy.R; @@ -35,10 +36,10 @@ public class SummaryFragment extends Fragment { */ private static final String PROFILE_DOCUMENT = "PROFILE_DOCUMENT"; /** - * {@link ArrayList} of Strings used to hold profile's information. Data are added in + * {@link HashMap} used to hold profile's information. Data are added in * {@link SummaryTask}. */ - private ArrayList parsedProfileSummaryData; + private LinkedHashMap parsedProfileSummaryData; /** * A {@link Document} holding this profile's source code. */ @@ -69,7 +70,7 @@ public class SummaryFragment extends Fragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); profileSummaryDocument = Jsoup.parse(requireArguments().getString(PROFILE_DOCUMENT)); - parsedProfileSummaryData = new ArrayList<>(); + parsedProfileSummaryData = new LinkedHashMap<>(); } @Override @@ -123,36 +124,42 @@ public class SummaryFragment extends Fragment { * @return ArrayList containing this profile's parsed information * @see org.jsoup.Jsoup Jsoup */ - ArrayList parseProfileSummary(Document profile) { - //Method's variables - ArrayList parsedInformation = new ArrayList<>(); + LinkedHashMap parseProfileSummary(Document profile) { + LinkedHashMap parsedInformation = new LinkedHashMap<>(); //Contains all summary's rows - Elements summaryRows = profile.select(".bordercolor > tbody:nth-child(1) > tr:nth-child(2) tr"); + Elements summaryRows = profile.select("td.windowbg > table > tbody > tr"); for (Element summaryRow : summaryRows) { - String rowText = summaryRow.text(), pHtml = ""; - - if (summaryRow.select("td").size() == 1) //Horizontal rule rows - pHtml = ""; - else if (summaryRow.text().contains("Current Status") - || summaryRow.text().contains("Κατάσταση")) continue; - else if (rowText.contains("Signature") || rowText.contains("Υπογραφή")) { - //This needs special handling since it may have css - pHtml = ParseHelpers.emojiTagToHtml(ParseHelpers.youtubeEmbeddedFix(summaryRow)); - //Add stuff to make it work in WebView - //style.css - pHtml = ("\n" + - "\n" + - "
\n" + pHtml + "\n
"); - } else if (!rowText.contains("Name") && !rowText.contains("Όνομα")) { //Doesn't add username twice - if (Objects.equals(summaryRow.select("td").get(1).text(), "")) + String key, value; + + int tdSize = summaryRow.select("td").size(); + + if (tdSize > 1){ + key = summaryRow.select("td").first().text().trim(); + + if (key.startsWith("Name") || key.startsWith("Όνομα")) continue; - //Style parsed information with html - pHtml = "" + summaryRow.select("td").first().text() + " " - + summaryRow.select("td").get(1).text(); + else if (key.startsWith("Signature") || key.startsWith("Υπογραφή")){ + key = summaryRow.selectFirst("td > table > tbody > tr > td").text().trim(); + summaryRow.selectFirst("td > table > tbody > tr").remove(); //key not needed, outer html needed for CSS + value = ParseHelpers.emojiTagToHtml(ParseHelpers.youtubeEmbeddedFix(summaryRow)); // Is emojiTagToHtml() really needed here? + if(summaryRow.text().trim().isEmpty()) + continue; + value = ("\n" + + "\n" + + "
\n" + value + "\n
"); + } + else { + if (summaryRow.select("td").get(1).text().isEmpty()) + continue; + if (key.startsWith("Date Registered") || key.startsWith("Ημερομηνία εγγραφής") || key.startsWith("Last Active") || key.startsWith("Τελευταία σύνδεση")) + value = summaryRow.select("td").get(1).text().trim(); + else + value = summaryRow.select("td").get(1).html().trim(); + } + parsedInformation.put(key, value); } - parsedInformation.add(pHtml); } return parsedInformation; } @@ -165,38 +172,54 @@ public class SummaryFragment extends Fragment { * {@link #parsedProfileSummaryData}

*/ private void populateLayout() { - for (String profileSummaryRow : parsedProfileSummaryData) { - if (profileSummaryRow.contains("Signature") - || profileSummaryRow.contains("Υπογραφή")) { //This may contain css - ReactiveWebView signatureEntry = new ReactiveWebView(this.getContext()); - signatureEntry.setBackgroundColor(Color.argb(1, 255, 255, 255)); - signatureEntry.loadDataWithBaseURL("file:///android_asset/", profileSummaryRow, - "text/html", "UTF-8", null); - mainContent.addView(signatureEntry); + for (LinkedHashMap.Entry entry : parsedProfileSummaryData.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + + if (key.startsWith("Current Status") || key.startsWith("Κατάσταση")){ + addEmptyView(); continue; } - TextView entry = new TextView(this.getContext()); - - if (profileSummaryRow.contains("@") && - (profileSummaryRow.contains("Email") || profileSummaryRow.contains("E-mail"))) { - String email = profileSummaryRow.substring(profileSummaryRow.indexOf(": ") + 6); - profileSummaryRow = profileSummaryRow.replace(email, - "" + email + ""); - entry.setMovementMethod(LinkMovementMethod.getInstance()); - } + + TextView textView = new TextView(this.getContext()); + + if (((key.startsWith("Email") || key.startsWith("E-mail")) + && value.contains("@")) || key.startsWith("Website") || key.startsWith("Ιστοτόπος")) + textView.setMovementMethod(LinkMovementMethod.getInstance()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) - entry.setTextColor(getResources().getColor(R.color.primary_text, null)); + textView.setTextColor(getResources().getColor(R.color.primary_text, null)); else - entry.setTextColor(getResources().getColor(R.color.primary_text)); + textView.setTextColor(getResources().getColor(R.color.primary_text)); + + String textViewContent = "" + key + " " + value; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - entry.setText(Html.fromHtml(profileSummaryRow, Html.FROM_HTML_MODE_LEGACY)); - } else { - entry.setText(Html.fromHtml(profileSummaryRow)); + if (key.startsWith("Signature") || key.startsWith("Υπογραφή")){ + addEmptyView(); + textViewContent = "" + key + ""; } - mainContent.addView(entry); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) + textView.setText(Html.fromHtml(textViewContent, Html.FROM_HTML_MODE_LEGACY)); + else + textView.setText(Html.fromHtml(textViewContent)); + + mainContent.addView(textView); + + if (key.startsWith("Last Active") || key.startsWith("Τελευταία σύνδεση")) + addEmptyView(); + + if (key.startsWith("Signature") || key.startsWith("Υπογραφή")) { + ReactiveWebView signatureEntry = new ReactiveWebView(this.getContext()); + signatureEntry.setBackgroundColor(Color.argb(1, 255, 255, 255)); + signatureEntry.loadDataWithBaseURL("file:///android_asset/", value, + "text/html", "UTF-8", null); + mainContent.addView(signatureEntry); + } } } + + private void addEmptyView(){ + mainContent.addView(new TextView(this.getContext())); + } } diff --git a/app/src/main/res/layout/fragment_profile_summary.xml b/app/src/main/res/layout/fragment_profile_summary.xml index 098cad5a..65cdf1a8 100644 --- a/app/src/main/res/layout/fragment_profile_summary.xml +++ b/app/src/main/res/layout/fragment_profile_summary.xml @@ -5,8 +5,9 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/background_lighter" - android:paddingEnd="16dp" android:paddingStart="16dp" + android:paddingEnd="16dp" + android:paddingTop="16dp" android:scrollbars="none">