Browse Source

fix parallel posting, add/fix behavior when profile task fails

pull/61/merge
oogee 6 years ago
parent
commit
5110c1f4f3
  1. 202
      app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java
  2. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  3. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java

202
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.List;
import java.util.Objects; import java.util.Objects;
import javax.net.ssl.SSLHandshakeException;
import androidx.appcompat.app.AppCompatDelegate; import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.content.res.ResourcesCompat; import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.Fragment; 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.model.ThmmyPage;
import gr.thmmy.mthmmy.utils.CenterVerticalSpan; import gr.thmmy.mthmmy.utils.CenterVerticalSpan;
import gr.thmmy.mthmmy.utils.CircleTransform; 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 me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import timber.log.Timber; import timber.log.Timber;
@ -195,7 +195,7 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment
} }
profileTask = new ProfileTask(); profileTask = new ProfileTask();
profileTask.execute(profileUrl); //Attempts data parsing profileTask.execute(profileUrl + ";wap"); //Attempts data parsing
} }
@Override @Override
@ -214,6 +214,11 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment
startActivity(i); 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 * 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 * 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 * @see Jsoup
*/ */
public class ProfileTask extends AsyncTask<String, Void, Boolean> { public class ProfileTask extends NewParseTask<Void> {
//Class variables //Class variables
Document profilePage; Document profilePage;
Spannable usernameSpan; Spannable usernameSpan;
Boolean isOnline = false; Boolean isOnline = false;
protected void onPreExecute() { public ProfileTask() {
progressBar.setVisibility(ProgressBar.VISIBLE); super(ProfileActivity.this::onProfileTaskStarted, null);
if (pmFAB.getVisibility() != View.GONE) pmFAB.setEnabled(false);
} }
protected Boolean doInBackground(String... profileUrl) { @Override
String pageUrl = profileUrl[0] + ";wap"; //Profile's page wap url protected Void parse(Document document, Response response) throws ParseException {
profilePage = document;
Request request = new Request.Builder() Elements contentsTable = profilePage.
.url(pageUrl) select(".bordercolor > tbody:nth-child(1) > tr:nth-child(2) tbody");
.build();
try { //Finds username if missing
Response response = client.newCall(request).execute(); if (username == null || Objects.equals(username, "")) {
profilePage = ParseHelpers.parse(response.body().string()); username = contentsTable.select("tr").first().select("td").last().text();
Elements contentsTable = profilePage. }
select(".bordercolor > tbody:nth-child(1) > tr:nth-child(2) tbody"); if (thumbnailUrl == null || Objects.equals(thumbnailUrl, "")) { //Maybe there is an avatar
Element profileAvatar = profilePage.select("img.avatar").first();
//Finds username if missing if (profileAvatar != null) thumbnailUrl = profileAvatar.attr("abs:src");
if (username == null || Objects.equals(username, "")) { }
username = contentsTable.select("tr").first().select("td").last().text(); { //Finds personal text
} Element tmpEl = profilePage.select("td.windowbg:nth-child(2)").first();
if (thumbnailUrl == null || Objects.equals(thumbnailUrl, "")) { //Maybe there is an avatar if (tmpEl != null) {
Element profileAvatar = profilePage.select("img.avatar").first(); personalText = tmpEl.text().trim();
if (profileAvatar != null) thumbnailUrl = profileAvatar.attr("abs:src"); } else {
} //Should never get here!
{ //Finds personal text //Something is wrong.
Element tmpEl = profilePage.select("td.windowbg:nth-child(2)").first(); Timber.e("An error occurred while trying to find profile's personal text.");
if (tmpEl != null) { personalText = 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() { //Finds status
.getString(R.string.fa_circle) + " " + username); usernameSpan = new SpannableString(getResources()
usernameSpan.setSpan(new CenterVerticalSpan(), 0, 2, .getString(R.string.fa_circle) + " " + username);
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); usernameSpan.setSpan(new CenterVerticalSpan(), 0, 2,
usernameSpan.setSpan(new RelativeSizeSpan(0.45f) Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); usernameSpan.setSpan(new RelativeSizeSpan(0.45f)
, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (contentsTable.toString().contains("Online")
|| contentsTable.toString().contains("Συνδεδεμένος")) { if (contentsTable.toString().contains("Online")
isOnline = true; || contentsTable.toString().contains("Συνδεδεμένος")) {
} else { isOnline = true;
isOnline = false; } else {
isOnline = false;
/*usernameSpan.setSpan(new ForegroundColorSpan(Color.GRAY) /*usernameSpan.setSpan(new ForegroundColorSpan(Color.GRAY)
, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);*/ , 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);*/
}
usernameSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#26A69A"))
, 2, usernameSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} }
return true; usernameSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#26A69A"))
} catch (SSLHandshakeException e) { , 2, usernameSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Timber.w("Certificate problem (please switch to unsafe connection).");
} catch (Exception e) {
Timber.e(e, "Exception");
} }
return false; return null;
} }
//TODO: better parse error handling (ParseException etc.) @Override
protected void onPostExecute(Boolean result) { protected void onPostExecute(Parcel<Void> parcel) {
if (!result) { //Parse failed! //TODO report as ParseException? 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!"); Timber.d("Parse failed!");
Toast.makeText(getBaseContext(), "Fatal error!\n Aborting..." Toast.makeText(getBaseContext(), "Fatal error!\n Aborting..."
, Toast.LENGTH_LONG).show(); , Toast.LENGTH_LONG).show();
finish(); 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); @Override
TabLayout tabLayout = findViewById(R.id.profile_tabs); protected int getResultCode(Response response, Void data) {
tabLayout.setupWithViewPager(viewPager); return NetworkResultCodes.SUCCESSFUL;
if (tabSelect != 0) {
TabLayout.Tab tab = tabLayout.getTabAt(tabSelect);
if (tab != null) tab.select();
}
} }
} }

2
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(); replyFAB.hide();
bottomNavBar.setVisibility(View.GONE); bottomNavBar.setVisibility(View.GONE);
AlertDialog.Builder builder = new AlertDialog.Builder(TopicActivity.this, 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." + builder.setMessage("A new reply was posted before you completed your new post." +
" Please review it and send your reply again") " Please review it and send your reply again")
.setNeutralButton(getString(R.string.ok), (dialog, which) -> dialog.dismiss()) .setNeutralButton(getString(R.string.ok), (dialog, which) -> dialog.dismiss())

2
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java

@ -586,6 +586,8 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
.transform(new CircleTransform()) .transform(new CircleTransform())
.into(holder.thumbnail); .into(holder.thumbnail);
holder.username.setText(getSessionManager().getUsername()); holder.username.setText(getSessionManager().getUsername());
holder.itemView.setAlpha(1f);
holder.itemView.setEnabled(true);
if (reply.getSubject() != null) { if (reply.getSubject() != null) {
holder.quickReplySubject.setText(reply.getSubject()); holder.quickReplySubject.setText(reply.getSubject());
} else { } else {

Loading…
Cancel
Save