From 5110c1f4f33be28760f364b3bb33eee549935614 Mon Sep 17 00:00:00 2001 From: oogee Date: Tue, 23 Oct 2018 23:19:00 +0300 Subject: [PATCH] fix parallel posting, add/fix behavior when profile task fails --- .../activities/profile/ProfileActivity.java | 202 +++++++++--------- .../activities/topic/TopicActivity.java | 2 +- .../mthmmy/activities/topic/TopicAdapter.java | 2 + 3 files changed, 106 insertions(+), 100 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java index 70f95ac4..a9b11c7f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java @@ -31,8 +31,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; -import javax.net.ssl.SSLHandshakeException; - import androidx.appcompat.app.AppCompatDelegate; import androidx.core.content.res.ResourcesCompat; import androidx.fragment.app.Fragment; @@ -49,9 +47,11 @@ import gr.thmmy.mthmmy.model.PostSummary; import gr.thmmy.mthmmy.model.ThmmyPage; import gr.thmmy.mthmmy.utils.CenterVerticalSpan; import gr.thmmy.mthmmy.utils.CircleTransform; -import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; +import gr.thmmy.mthmmy.utils.NetworkResultCodes; +import gr.thmmy.mthmmy.utils.Parcel; +import gr.thmmy.mthmmy.utils.parsing.NewParseTask; +import gr.thmmy.mthmmy.utils.parsing.ParseException; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; -import okhttp3.Request; import okhttp3.Response; import timber.log.Timber; @@ -195,7 +195,7 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment } profileTask = new ProfileTask(); - profileTask.execute(profileUrl); //Attempts data parsing + profileTask.execute(profileUrl + ";wap"); //Attempts data parsing } @Override @@ -214,6 +214,11 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment startActivity(i); } + public void onProfileTaskStarted() { + progressBar.setVisibility(ProgressBar.VISIBLE); + if (pmFAB.getVisibility() != View.GONE) pmFAB.setEnabled(false); + } + /** * An {@link AsyncTask} that handles asynchronous fetching of a profile page and parsing this * user's personal text. The {@link Document} resulting from the parse is stored for use in @@ -223,120 +228,119 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment * * @see Jsoup */ - public class ProfileTask extends AsyncTask { + public class ProfileTask extends NewParseTask { //Class variables Document profilePage; Spannable usernameSpan; Boolean isOnline = false; - protected void onPreExecute() { - progressBar.setVisibility(ProgressBar.VISIBLE); - if (pmFAB.getVisibility() != View.GONE) pmFAB.setEnabled(false); + public ProfileTask() { + super(ProfileActivity.this::onProfileTaskStarted, null); } - protected Boolean doInBackground(String... profileUrl) { - String pageUrl = profileUrl[0] + ";wap"; //Profile's page wap url - - Request request = new Request.Builder() - .url(pageUrl) - .build(); - try { - Response response = client.newCall(request).execute(); - profilePage = ParseHelpers.parse(response.body().string()); - Elements contentsTable = profilePage. - select(".bordercolor > tbody:nth-child(1) > tr:nth-child(2) tbody"); - - //Finds username if missing - if (username == null || Objects.equals(username, "")) { - username = contentsTable.select("tr").first().select("td").last().text(); - } - if (thumbnailUrl == null || Objects.equals(thumbnailUrl, "")) { //Maybe there is an avatar - Element profileAvatar = profilePage.select("img.avatar").first(); - if (profileAvatar != null) thumbnailUrl = profileAvatar.attr("abs:src"); - } - { //Finds personal text - Element tmpEl = profilePage.select("td.windowbg:nth-child(2)").first(); - if (tmpEl != null) { - personalText = tmpEl.text().trim(); - } else { - //Should never get here! - //Something is wrong. - Timber.e("An error occurred while trying to find profile's personal text."); - personalText = null; - } + @Override + protected Void parse(Document document, Response response) throws ParseException { + profilePage = document; + Elements contentsTable = profilePage. + select(".bordercolor > tbody:nth-child(1) > tr:nth-child(2) tbody"); + + //Finds username if missing + if (username == null || Objects.equals(username, "")) { + username = contentsTable.select("tr").first().select("td").last().text(); + } + if (thumbnailUrl == null || Objects.equals(thumbnailUrl, "")) { //Maybe there is an avatar + Element profileAvatar = profilePage.select("img.avatar").first(); + if (profileAvatar != null) thumbnailUrl = profileAvatar.attr("abs:src"); + } + { //Finds personal text + Element tmpEl = profilePage.select("td.windowbg:nth-child(2)").first(); + if (tmpEl != null) { + personalText = tmpEl.text().trim(); + } else { + //Should never get here! + //Something is wrong. + Timber.e("An error occurred while trying to find profile's personal text."); + personalText = null; } - { //Finds status - usernameSpan = new SpannableString(getResources() - .getString(R.string.fa_circle) + " " + username); - usernameSpan.setSpan(new CenterVerticalSpan(), 0, 2, - Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - usernameSpan.setSpan(new RelativeSizeSpan(0.45f) - , 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); - - if (contentsTable.toString().contains("Online") - || contentsTable.toString().contains("Συνδεδεμένος")) { - isOnline = true; - } else { - isOnline = false; + } + { //Finds status + usernameSpan = new SpannableString(getResources() + .getString(R.string.fa_circle) + " " + username); + usernameSpan.setSpan(new CenterVerticalSpan(), 0, 2, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + usernameSpan.setSpan(new RelativeSizeSpan(0.45f) + , 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + + if (contentsTable.toString().contains("Online") + || contentsTable.toString().contains("Συνδεδεμένος")) { + isOnline = true; + } else { + isOnline = false; /*usernameSpan.setSpan(new ForegroundColorSpan(Color.GRAY) , 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);*/ - } - usernameSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#26A69A")) - , 2, usernameSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } - return true; - } catch (SSLHandshakeException e) { - Timber.w("Certificate problem (please switch to unsafe connection)."); - } catch (Exception e) { - Timber.e(e, "Exception"); + usernameSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#26A69A")) + , 2, usernameSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } - return false; + return null; } - //TODO: better parse error handling (ParseException etc.) - protected void onPostExecute(Boolean result) { - if (!result) { //Parse failed! //TODO report as ParseException? + @Override + protected void onPostExecute(Parcel parcel) { + int result = parcel.getResultCode(); + if (result == NetworkResultCodes.SUCCESSFUL) { + //Parse was successful + if (pmFAB.getVisibility() != View.GONE) pmFAB.setEnabled(true); + progressBar.setVisibility(ProgressBar.INVISIBLE); + + if (usernameSpan != null) { + if (isOnline) { + usernameView.setTextColor(Color.parseColor("#4CAF50")); + } else { + usernameView.setTextColor(Color.GRAY); + } + usernameView.setText(usernameSpan); + } else if (usernameView.getText() != username) usernameView.setText(username); + if (thumbnailUrl != null && !Objects.equals(thumbnailUrl, "")) + //noinspection ConstantConditions + Picasso.with(getApplicationContext()) + .load(thumbnailUrl) + .resize(THUMBNAIL_SIZE, THUMBNAIL_SIZE) + .centerCrop() + .error(ResourcesCompat.getDrawable(getResources() + , R.drawable.ic_default_user_thumbnail_white_24dp, null)) + .placeholder(ResourcesCompat.getDrawable(getResources() + , R.drawable.ic_default_user_thumbnail_white_24dp, null)) + .transform(new CircleTransform()) + .into(thumbnailView); + if (personalText != null) { + personalTextView.setText(personalText); + personalTextView.setVisibility(View.VISIBLE); + } + + setupViewPager(viewPager, profilePage); + TabLayout tabLayout = findViewById(R.id.profile_tabs); + tabLayout.setupWithViewPager(viewPager); + if (tabSelect != 0) { + TabLayout.Tab tab = tabLayout.getTabAt(tabSelect); + if (tab != null) tab.select(); + } + } else if (result == NetworkResultCodes.NETWORK_ERROR) { + Timber.w("Network error while excecuting profile activity"); + Toast.makeText(getBaseContext(), "Network error" + , Toast.LENGTH_LONG).show(); + finish(); + } else { Timber.d("Parse failed!"); Toast.makeText(getBaseContext(), "Fatal error!\n Aborting..." , Toast.LENGTH_LONG).show(); finish(); } - //Parse was successful - if (pmFAB.getVisibility() != View.GONE) pmFAB.setEnabled(true); - progressBar.setVisibility(ProgressBar.INVISIBLE); - - if (usernameSpan != null) { - if (isOnline) { - usernameView.setTextColor(Color.parseColor("#4CAF50")); - } else { - usernameView.setTextColor(Color.GRAY); - } - usernameView.setText(usernameSpan); - } else if (usernameView.getText() != username) usernameView.setText(username); - if (thumbnailUrl != null && !Objects.equals(thumbnailUrl, "")) - //noinspection ConstantConditions - Picasso.with(getApplicationContext()) - .load(thumbnailUrl) - .resize(THUMBNAIL_SIZE, THUMBNAIL_SIZE) - .centerCrop() - .error(ResourcesCompat.getDrawable(getResources() - , R.drawable.ic_default_user_thumbnail_white_24dp, null)) - .placeholder(ResourcesCompat.getDrawable(getResources() - , R.drawable.ic_default_user_thumbnail_white_24dp, null)) - .transform(new CircleTransform()) - .into(thumbnailView); - if (personalText != null) { - personalTextView.setText(personalText); - personalTextView.setVisibility(View.VISIBLE); - } + } - setupViewPager(viewPager, profilePage); - TabLayout tabLayout = findViewById(R.id.profile_tabs); - tabLayout.setupWithViewPager(viewPager); - if (tabSelect != 0) { - TabLayout.Tab tab = tabLayout.getTabAt(tabSelect); - if (tab != null) tab.select(); - } + @Override + protected int getResultCode(Response response, Void data) { + return NetworkResultCodes.SUCCESSFUL; } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java index 46c8678b..6f1935b9 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java @@ -528,7 +528,7 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo replyFAB.hide(); bottomNavBar.setVisibility(View.GONE); AlertDialog.Builder builder = new AlertDialog.Builder(TopicActivity.this, - R.style.AppCompatAlertDialogStyleAccent); + R.style.AppTheme_Dark_Dialog); builder.setMessage("A new reply was posted before you completed your new post." + " Please review it and send your reply again") .setNeutralButton(getString(R.string.ok), (dialog, which) -> dialog.dismiss()) 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 23073331..5a1fdc5a 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 @@ -586,6 +586,8 @@ class TopicAdapter extends RecyclerView.Adapter { .transform(new CircleTransform()) .into(holder.thumbnail); holder.username.setText(getSessionManager().getUsername()); + holder.itemView.setAlpha(1f); + holder.itemView.setEnabled(true); if (reply.getSubject() != null) { holder.quickReplySubject.setText(reply.getSubject()); } else {