Browse Source

ProfileSummary optimizations

pull/70/head
Ezerous 4 years ago
parent
commit
b28e402c3b
  1. 1
      app/src/main/assets/style_light.css
  2. 127
      app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java
  3. 3
      app/src/main/res/layout/fragment_profile_summary.xml

1
app/src/main/assets/style_light.css

@ -8,4 +8,5 @@ body {
.customSignature { .customSignature {
background: #434649 !important; background: #434649 !important;
margin-top: 0.5em;
} }

127
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.nodes.Element;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
import java.util.ArrayList; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Objects; import java.util.Objects;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
@ -35,10 +36,10 @@ public class SummaryFragment extends Fragment {
*/ */
private static final String PROFILE_DOCUMENT = "PROFILE_DOCUMENT"; 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}. * {@link SummaryTask}.
*/ */
private ArrayList<String> parsedProfileSummaryData; private LinkedHashMap<String, String> parsedProfileSummaryData;
/** /**
* A {@link Document} holding this profile's source code. * A {@link Document} holding this profile's source code.
*/ */
@ -69,7 +70,7 @@ public class SummaryFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
profileSummaryDocument = Jsoup.parse(requireArguments().getString(PROFILE_DOCUMENT)); profileSummaryDocument = Jsoup.parse(requireArguments().getString(PROFILE_DOCUMENT));
parsedProfileSummaryData = new ArrayList<>(); parsedProfileSummaryData = new LinkedHashMap<>();
} }
@Override @Override
@ -123,36 +124,42 @@ public class SummaryFragment extends Fragment {
* @return ArrayList containing this profile's parsed information * @return ArrayList containing this profile's parsed information
* @see org.jsoup.Jsoup Jsoup * @see org.jsoup.Jsoup Jsoup
*/ */
ArrayList<String> parseProfileSummary(Document profile) { LinkedHashMap<String, String> parseProfileSummary(Document profile) {
//Method's variables LinkedHashMap<String, String> parsedInformation = new LinkedHashMap<>();
ArrayList<String> parsedInformation = new ArrayList<>();
//Contains all summary's rows //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) { for (Element summaryRow : summaryRows) {
String rowText = summaryRow.text(), pHtml = ""; String key, value;
if (summaryRow.select("td").size() == 1) //Horizontal rule rows int tdSize = summaryRow.select("td").size();
pHtml = "";
else if (summaryRow.text().contains("Current Status") if (tdSize > 1){
|| summaryRow.text().contains("Κατάσταση")) continue; key = summaryRow.select("td").first().text().trim();
else if (rowText.contains("Signature") || rowText.contains("Υπογραφή")) {
//This needs special handling since it may have css if (key.startsWith("Name") || key.startsWith("Όνομα"))
pHtml = ParseHelpers.emojiTagToHtml(ParseHelpers.youtubeEmbeddedFix(summaryRow));
//Add stuff to make it work in WebView
//style.css
pHtml = ("<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n" +
"<link rel=\"stylesheet\" type=\"text/css\" href=\"style_light.css\" />\n" +
"<div class=\"customSignature\">\n" + pHtml + "\n</div>");
} else if (!rowText.contains("Name") && !rowText.contains("Όνομα")) { //Doesn't add username twice
if (Objects.equals(summaryRow.select("td").get(1).text(), ""))
continue; continue;
//Style parsed information with html else if (key.startsWith("Signature") || key.startsWith("Υπογραφή")){
pHtml = "<b>" + summaryRow.select("td").first().text() + "</b> " key = summaryRow.selectFirst("td > table > tbody > tr > td").text().trim();
+ summaryRow.select("td").get(1).text(); 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 = ("<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n" +
"<link rel=\"stylesheet\" type=\"text/css\" href=\"style_light.css\" />\n" +
"<div class=\"customSignature\">\n" + value + "\n</div>");
}
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; return parsedInformation;
} }
@ -165,38 +172,54 @@ public class SummaryFragment extends Fragment {
* {@link #parsedProfileSummaryData}</p> * {@link #parsedProfileSummaryData}</p>
*/ */
private void populateLayout() { private void populateLayout() {
for (String profileSummaryRow : parsedProfileSummaryData) { for (LinkedHashMap.Entry<String, String> entry : parsedProfileSummaryData.entrySet()) {
if (profileSummaryRow.contains("Signature") String key = entry.getKey();
|| profileSummaryRow.contains("Υπογραφή")) { //This may contain css String value = entry.getValue();
ReactiveWebView signatureEntry = new ReactiveWebView(this.getContext());
signatureEntry.setBackgroundColor(Color.argb(1, 255, 255, 255)); if (key.startsWith("Current Status") || key.startsWith("Κατάσταση")){
signatureEntry.loadDataWithBaseURL("file:///android_asset/", profileSummaryRow, addEmptyView();
"text/html", "UTF-8", null);
mainContent.addView(signatureEntry);
continue; continue;
} }
TextView entry = new TextView(this.getContext());
TextView textView = new TextView(this.getContext());
if (profileSummaryRow.contains("@") &&
(profileSummaryRow.contains("Email") || profileSummaryRow.contains("E-mail"))) { if (((key.startsWith("Email") || key.startsWith("E-mail"))
String email = profileSummaryRow.substring(profileSummaryRow.indexOf(":</b> ") + 6); && value.contains("@")) || key.startsWith("Website") || key.startsWith("Ιστοτόπος"))
profileSummaryRow = profileSummaryRow.replace(email, textView.setMovementMethod(LinkMovementMethod.getInstance());
"<a href=\"mailto:" + email + "\">" + email + "</a>");
entry.setMovementMethod(LinkMovementMethod.getInstance());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 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 else
entry.setTextColor(getResources().getColor(R.color.primary_text)); textView.setTextColor(getResources().getColor(R.color.primary_text));
String textViewContent = "<b>" + key + "</b> " + value;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (key.startsWith("Signature") || key.startsWith("Υπογραφή")){
entry.setText(Html.fromHtml(profileSummaryRow, Html.FROM_HTML_MODE_LEGACY)); addEmptyView();
} else { textViewContent = "<b>" + key + "</b>";
entry.setText(Html.fromHtml(profileSummaryRow));
} }
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()));
}
} }

3
app/src/main/res/layout/fragment_profile_summary.xml

@ -5,8 +5,9 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/background_lighter" android:background="@color/background_lighter"
android:paddingEnd="16dp"
android:paddingStart="16dp" android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingTop="16dp"
android:scrollbars="none"> android:scrollbars="none">
<LinearLayout <LinearLayout

Loading…
Cancel
Save