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.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,26 +228,19 @@ 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()
.url(pageUrl)
.build();
try {
Response response = client.newCall(request).execute();
profilePage = ParseHelpers.parse(response.body().string());
Elements contentsTable = profilePage. Elements contentsTable = profilePage.
select(".bordercolor > tbody:nth-child(1) > tr:nth-child(2) tbody"); 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")) usernameSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#26A69A"))
, 2, usernameSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); , 2, usernameSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} }
return true; return null;
} catch (SSLHandshakeException e) {
Timber.w("Certificate problem (please switch to unsafe connection).");
} catch (Exception e) {
Timber.e(e, "Exception");
}
return false;
} }
//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();
Timber.d("Parse failed!"); if (result == NetworkResultCodes.SUCCESSFUL) {
Toast.makeText(getBaseContext(), "Fatal error!\n Aborting..."
, Toast.LENGTH_LONG).show();
finish();
}
//Parse was successful //Parse was successful
if (pmFAB.getVisibility() != View.GONE) pmFAB.setEnabled(true); if (pmFAB.getVisibility() != View.GONE) pmFAB.setEnabled(true);
progressBar.setVisibility(ProgressBar.INVISIBLE); progressBar.setVisibility(ProgressBar.INVISIBLE);
@ -337,6 +325,22 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment
TabLayout.Tab tab = tabLayout.getTabAt(tabSelect); TabLayout.Tab tab = tabLayout.getTabAt(tabSelect);
if (tab != null) tab.select(); 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(); 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