Browse Source

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

pull/61/merge
oogee 6 years ago
parent
commit
5110c1f4f3
  1. 70
      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

70
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,26 +228,19 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment
*
* @see Jsoup
*/
public class ProfileTask extends AsyncTask<String, Void, Boolean> {
public class ProfileTask extends NewParseTask<Void> {
//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());
@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");
@ -284,23 +282,13 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment
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");
}
return false;
return null;
}
//TODO: better parse error handling (ParseException etc.)
protected void onPostExecute(Boolean result) {
if (!result) { //Parse failed! //TODO report as ParseException?
Timber.d("Parse failed!");
Toast.makeText(getBaseContext(), "Fatal error!\n Aborting..."
, Toast.LENGTH_LONG).show();
finish();
}
@Override
protected void onPostExecute(Parcel<Void> parcel) {
int result = parcel.getResultCode();
if (result == NetworkResultCodes.SUCCESSFUL) {
//Parse was successful
if (pmFAB.getVisibility() != View.GONE) pmFAB.setEnabled(true);
progressBar.setVisibility(ProgressBar.INVISIBLE);
@ -337,6 +325,22 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment
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();
}
}
@Override
protected int getResultCode(Response response, Void data) {
return NetworkResultCodes.SUCCESSFUL;
}
}

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();
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())

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())
.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 {

Loading…
Cancel
Save