Browse Source

UnreadFragment possible fixes

pull/61/merge
Ezerous 6 years ago
parent
commit
2300e86b64
No known key found for this signature in database GPG Key ID: 262B2954BBA319E3
  1. 24
      app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java
  2. 6
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java
  3. 66
      app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java
  4. 4
      app/src/main/java/gr/thmmy/mthmmy/utils/ExternalAsyncTask.java

24
app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java

@ -100,12 +100,11 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
int preferredTab = Integer.parseInt(sharedPrefs.getString(SettingsActivity.DEFAULT_HOME_TAB, "0")); int preferredTab = Integer.parseInt(sharedPrefs.getString(SettingsActivity.DEFAULT_HOME_TAB, "0"));
if ((preferredTab != 3 && preferredTab != 4) || sessionManager.isLoggedIn()) { if ((preferredTab != 3 && preferredTab != 4) || sessionManager.isLoggedIn())
tabLayout.getTabAt(preferredTab).select(); tabLayout.getTabAt(preferredTab).select();
}
for (int i = 0; i < tabLayout.getTabCount(); i++) { for (int i = 0; i < tabLayout.getTabCount(); i++)
updateTabIcon(i); updateTabIcon(i);
}
setMainActivity(this); setMainActivity(this);
} }
@ -196,6 +195,7 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF
} }
void removeFragment(int position) { void removeFragment(int position) {
getSupportFragmentManager().beginTransaction().remove(fragmentList.get(position)).commit();
fragmentList.remove(position); fragmentList.remove(position);
fragmentTitleList.remove(position); fragmentTitleList.remove(position);
notifyDataSetChanged(); notifyDataSetChanged();
@ -228,26 +228,24 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF
public void updateTabIcon(int position) { public void updateTabIcon(int position) {
if (position >= tabLayout.getTabCount()) return; if (position >= tabLayout.getTabCount()) return;
if (position == 0) { if (position == 0)
tabLayout.getTabAt(0).setIcon(getResources().getDrawable(R.drawable.ic_access_time_white_24dp)); tabLayout.getTabAt(0).setIcon(getResources().getDrawable(R.drawable.ic_access_time_white_24dp));
} else if (position == 1) { else if (position == 1)
tabLayout.getTabAt(1).setIcon(getResources().getDrawable(R.drawable.ic_forum_white_24dp)); tabLayout.getTabAt(1).setIcon(getResources().getDrawable(R.drawable.ic_forum_white_24dp));
} else if (position == 2) { else if (position == 2)
tabLayout.getTabAt(2).setIcon(getResources().getDrawable(R.drawable.ic_fiber_new_white_24dp)); tabLayout.getTabAt(2).setIcon(getResources().getDrawable(R.drawable.ic_fiber_new_white_24dp));
} }
}
public void updateTabs() { public void updateTabs() {
if (!sessionManager.isLoggedIn() && sectionsPagerAdapter.getCount() == 3) { if (!sessionManager.isLoggedIn() && sectionsPagerAdapter.getCount() == 3)
sectionsPagerAdapter.removeFragment(2); sectionsPagerAdapter.removeFragment(2);
} else if (sessionManager.isLoggedIn() && sectionsPagerAdapter.getCount() == 2) { else if (sessionManager.isLoggedIn() && sectionsPagerAdapter.getCount() == 2)
sectionsPagerAdapter.addFragment(UnreadFragment.newInstance(3), "UNREAD"); sectionsPagerAdapter.addFragment(UnreadFragment.newInstance(3), "UNREAD");
}
for (int i = 0; i < tabLayout.getTabCount(); i++) { for (int i = 0; i < tabLayout.getTabCount(); i++)
updateTabIcon(i); updateTabIcon(i);
} }
}
//-------------------------------FragmentPagerAdapter END------------------------------------------- //-------------------------------FragmentPagerAdapter END-------------------------------------------
private void redirectToActivityFromIntent(Intent intent) { private void redirectToActivityFromIntent(Intent intent) {

6
app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java

@ -114,7 +114,7 @@ public class RecentFragment extends BaseFragment {
swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.primary); swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.primary);
swipeRefreshLayout.setColorSchemeResources(R.color.accent); swipeRefreshLayout.setColorSchemeResources(R.color.accent);
swipeRefreshLayout.setOnRefreshListener(() -> { swipeRefreshLayout.setOnRefreshListener(() -> {
if (recentTask != null && recentTask.getStatus() != AsyncTask.Status.RUNNING) { if (!recentTask.isRunning()) {
recentTask = new RecentTask(this::onRecentTaskStarted, this::onRecentTaskFinished); recentTask = new RecentTask(this::onRecentTaskStarted, this::onRecentTaskFinished);
recentTask.execute(SessionManager.indexUrl.toString()); recentTask.execute(SessionManager.indexUrl.toString());
} }
@ -128,7 +128,7 @@ public class RecentFragment extends BaseFragment {
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
if (recentTask != null && recentTask.getStatus() != AsyncTask.Status.RUNNING) if (recentTask.isRunning())
recentTask.cancel(true); recentTask.cancel(true);
} }
@ -160,7 +160,7 @@ public class RecentFragment extends BaseFragment {
//---------------------------------------ASYNC TASK----------------------------------- //---------------------------------------ASYNC TASK-----------------------------------
private class RecentTask extends NewParseTask<ArrayList<TopicSummary>> { private class RecentTask extends NewParseTask<ArrayList<TopicSummary>> {
public RecentTask(OnTaskStartedListener onTaskStartedListener, RecentTask(OnTaskStartedListener onTaskStartedListener,
OnNetworkTaskFinishedListener<ArrayList<TopicSummary>> onParseTaskFinishedListener) { OnNetworkTaskFinishedListener<ArrayList<TopicSummary>> onParseTaskFinishedListener) {
super(onTaskStartedListener, onParseTaskFinishedListener); super(onTaskStartedListener, onParseTaskFinishedListener);
} }

66
app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java

@ -1,3 +1,4 @@
package gr.thmmy.mthmmy.activities.main.unread; package gr.thmmy.mthmmy.activities.main.unread;
import android.os.AsyncTask; import android.os.AsyncTask;
@ -59,8 +60,7 @@ public class UnreadFragment extends BaseFragment {
private MarkReadTask markReadTask; private MarkReadTask markReadTask;
// Required empty public constructor // Required empty public constructor
public UnreadFragment() { public UnreadFragment() {}
}
/** /**
* Use ONLY this factory method to create a new instance of * Use ONLY this factory method to create a new instance of
@ -107,7 +107,7 @@ public class UnreadFragment extends BaseFragment {
progressBar = rootView.findViewById(R.id.progressBar); progressBar = rootView.findViewById(R.id.progressBar);
unreadAdapter = new UnreadAdapter(topicSummaries, unreadAdapter = new UnreadAdapter(topicSummaries,
fragmentInteractionListener, markReadLinkUrl -> { fragmentInteractionListener, markReadLinkUrl -> {
if (markReadTask != null && markReadTask.getStatus() != AsyncTask.Status.RUNNING) { if (!markReadTask.isRunning() && !unreadTask.isRunning()) {
markReadTask = new MarkReadTask(); markReadTask = new MarkReadTask();
markReadTask.execute(markReadLinkUrl); markReadTask.execute(markReadLinkUrl);
} }
@ -126,8 +126,7 @@ public class UnreadFragment extends BaseFragment {
swipeRefreshLayout.setColorSchemeResources(R.color.accent); swipeRefreshLayout.setColorSchemeResources(R.color.accent);
swipeRefreshLayout.setOnRefreshListener( swipeRefreshLayout.setOnRefreshListener(
() -> { () -> {
if (unreadTask != null && unreadTask.getStatus() != AsyncTask.Status.RUNNING) { if (!unreadTask.isRunning()) {
topicSummaries.clear();
numberOfPages = 0; numberOfPages = 0;
loadedPages = 0; loadedPages = 0;
unreadTask = new UnreadTask(this::onUnreadTaskStarted, this::onUnreadTaskFinished); unreadTask = new UnreadTask(this::onUnreadTaskStarted, this::onUnreadTaskFinished);
@ -144,10 +143,11 @@ public class UnreadFragment extends BaseFragment {
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
if (unreadTask != null && unreadTask.getStatus() != AsyncTask.Status.RUNNING) if (unreadTask.isRunning())
unreadTask.cancel(true); unreadTask.cancel(true);
if (markReadTask != null && markReadTask.getStatus() != AsyncTask.Status.RUNNING) if (markReadTask.isRunning())
markReadTask.cancel(true); markReadTask.cancel(true);
topicSummaries.clear();
} }
public interface UnreadFragmentInteractionListener extends FragmentInteractionListener { public interface UnreadFragmentInteractionListener extends FragmentInteractionListener {
@ -160,11 +160,15 @@ public class UnreadFragment extends BaseFragment {
progressBar.setVisibility(ProgressBar.VISIBLE); progressBar.setVisibility(ProgressBar.VISIBLE);
} }
private void onUnreadTaskFinished(int resultCode, Void data) { private void onUnreadTaskFinished(int resultCode, ArrayList<TopicSummary> fetchedUnread) {
if (resultCode == NetworkResultCodes.SUCCESSFUL) { if (resultCode == NetworkResultCodes.SUCCESSFUL) {
if(fetchedUnread!=null && !fetchedUnread.isEmpty()){
if(loadedPages==0)
topicSummaries.clear();
topicSummaries.addAll(fetchedUnread);
unreadAdapter.notifyDataSetChanged(); unreadAdapter.notifyDataSetChanged();
}
++loadedPages; loadedPages++;
if (loadedPages < numberOfPages) { if (loadedPages < numberOfPages) {
unreadTask = new UnreadTask(this::onUnreadTaskStarted, this::onUnreadTaskFinished); unreadTask = new UnreadTask(this::onUnreadTaskStarted, this::onUnreadTaskFinished);
assert SessionManager.unreadUrl != null; assert SessionManager.unreadUrl != null;
@ -186,15 +190,16 @@ public class UnreadFragment extends BaseFragment {
} }
} }
private class UnreadTask extends NewParseTask<Void> { private class UnreadTask extends NewParseTask<ArrayList<TopicSummary>> {
UnreadTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<Void> onParseTaskFinishedListener) { UnreadTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<ArrayList<TopicSummary>> onParseTaskFinishedListener) {
super(onTaskStartedListener, onParseTaskFinishedListener); super(onTaskStartedListener, onParseTaskFinishedListener);
} }
@Override @Override
protected Void parse(Document document, Response response) throws ParseException { protected ArrayList<TopicSummary> parse(Document document, Response response) throws ParseException {
Elements unread = document.select("table.bordercolor[cellspacing=1] tr:not(.titlebg)"); Elements unread = document.select("table.bordercolor[cellspacing=1] tr:not(.titlebg)");
ArrayList<TopicSummary> fetchedTopicSummaries = new ArrayList<>();
if (!unread.isEmpty()) { if (!unread.isEmpty()) {
//topicSummaries.clear(); //topicSummaries.clear();
for (Element row : unread) { for (Element row : unread) {
@ -210,16 +215,14 @@ public class UnreadFragment extends BaseFragment {
dateTime = dateTime.replace("<b>", ""); dateTime = dateTime.replace("<b>", "");
dateTime = dateTime.replace("</b>", ""); dateTime = dateTime.replace("</b>", "");
if (dateTime.contains(" am") || dateTime.contains(" pm") || if (dateTime.contains(" am") || dateTime.contains(" pm") ||
dateTime.contains(" πμ") || dateTime.contains(" μμ")) { dateTime.contains(" πμ") || dateTime.contains(" μμ"))
dateTime = dateTime.replaceAll(":[0-5][0-9] ", " "); dateTime = dateTime.replaceAll(":[0-5][0-9] ", " ");
} else { else
dateTime = dateTime.substring(0, dateTime.lastIndexOf(":")); dateTime = dateTime.substring(0, dateTime.lastIndexOf(":"));
} if (!dateTime.contains(","))
if (!dateTime.contains(",")) {
dateTime = dateTime.replaceAll(".+? ([0-9])", "$1"); dateTime = dateTime.replaceAll(".+? ([0-9])", "$1");
}
topicSummaries.add(new TopicSummary(link, title, lastUser, dateTime)); fetchedTopicSummaries.add(new TopicSummary(link, title, lastUser, dateTime));
} }
Element topBar = document.select("table:not(.bordercolor):not(#bodyarea):has(td.middletext)").first(); Element topBar = document.select("table:not(.bordercolor):not(#bodyarea):has(td.middletext)").first();
@ -233,31 +236,29 @@ public class UnreadFragment extends BaseFragment {
if (numberOfPages == 0 && pagesElement != null) { if (numberOfPages == 0 && pagesElement != null) {
Elements pages = pagesElement.select("a"); Elements pages = pagesElement.select("a");
if (!pages.isEmpty()) { if (!pages.isEmpty())
numberOfPages = Integer.parseInt(pages.last().text()); numberOfPages = Integer.parseInt(pages.last().text());
} else { else
numberOfPages = 1; numberOfPages = 1;
} }
}
if (markRead != null && loadedPages == numberOfPages - 1) if (markRead != null && loadedPages == numberOfPages - 1)
topicSummaries.add(new TopicSummary(markRead.attr("href"), markRead.text(), null, fetchedTopicSummaries.add(new TopicSummary(markRead.attr("href"), markRead.text(), null,
null)); null));
} else { } else {
topicSummaries.clear();
String message = document.select("table.bordercolor[cellspacing=1]").first().text(); String message = document.select("table.bordercolor[cellspacing=1]").first().text();
if (message.contains("No messages")) { //It's english if (message.contains("No messages")) { //It's english
message = "No unread posts!"; message = "No unread posts!";
} else { //It's greek } else { //It's greek
message = "Δεν υπάρχουν μη διαβασμένα μηνύματα!"; message = "Δεν υπάρχουν μη αναγνωσμένα μηνύματα!";
} }
topicSummaries.add(new TopicSummary(null, null, null, message)); fetchedTopicSummaries.add(new TopicSummary(null, null, null, message));
} }
return null; return fetchedTopicSummaries;
} }
@Override @Override
protected int getResultCode(Response response, Void data) { protected int getResultCode(Response response, ArrayList<TopicSummary> data) {
return NetworkResultCodes.SUCCESSFUL; return NetworkResultCodes.SUCCESSFUL;
} }
} }
@ -301,12 +302,19 @@ public class UnreadFragment extends BaseFragment {
Toast.makeText(getContext() Toast.makeText(getContext()
, "Fatal error!\n Task aborted...", Toast.LENGTH_LONG).show(); , "Fatal error!\n Task aborted...", Toast.LENGTH_LONG).show();
} else { } else {
if (unreadTask != null && unreadTask.getStatus() != AsyncTask.Status.RUNNING) { if (!unreadTask.isRunning()) {
numberOfPages = 0;
loadedPages = 0;
unreadTask = new UnreadTask(UnreadFragment.this::onUnreadTaskStarted, UnreadFragment.this::onUnreadTaskFinished); unreadTask = new UnreadTask(UnreadFragment.this::onUnreadTaskStarted, UnreadFragment.this::onUnreadTaskFinished);
assert SessionManager.unreadUrl != null; assert SessionManager.unreadUrl != null;
unreadTask.execute(SessionManager.unreadUrl.toString()); unreadTask.execute(SessionManager.unreadUrl.toString());
} }
} }
} }
//TODO: Maybe extend this task and use isRunning() from ExternalAsyncTask instead (?)
public boolean isRunning(){
return getStatus() == AsyncTask.Status.RUNNING;
}
} }
} }

4
app/src/main/java/gr/thmmy/mthmmy/utils/ExternalAsyncTask.java

@ -77,4 +77,8 @@ public abstract class ExternalAsyncTask<U, V> extends AsyncTask<U, Void, V> {
public interface OnTaskFinishedListener<V> { public interface OnTaskFinishedListener<V> {
void onTaskFinished(V result); void onTaskFinished(V result);
} }
public boolean isRunning(){
return getStatus() == AsyncTask.Status.RUNNING;
}
} }

Loading…
Cancel
Save