|
@ -1,4 +1,3 @@ |
|
|
|
|
|
|
|
|
package gr.thmmy.mthmmy.activities.main.unread; |
|
|
package gr.thmmy.mthmmy.activities.main.unread; |
|
|
|
|
|
|
|
|
import android.os.AsyncTask; |
|
|
import android.os.AsyncTask; |
|
@ -23,20 +22,20 @@ import org.jsoup.nodes.Document; |
|
|
import org.jsoup.nodes.Element; |
|
|
import org.jsoup.nodes.Element; |
|
|
import org.jsoup.select.Elements; |
|
|
import org.jsoup.select.Elements; |
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
import java.util.ArrayList; |
|
|
import java.util.ArrayList; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
import gr.thmmy.mthmmy.R; |
|
|
import gr.thmmy.mthmmy.R; |
|
|
|
|
|
import gr.thmmy.mthmmy.base.BaseApplication; |
|
|
import gr.thmmy.mthmmy.base.BaseFragment; |
|
|
import gr.thmmy.mthmmy.base.BaseFragment; |
|
|
import gr.thmmy.mthmmy.model.TopicSummary; |
|
|
import gr.thmmy.mthmmy.model.TopicSummary; |
|
|
import gr.thmmy.mthmmy.session.SessionManager; |
|
|
import gr.thmmy.mthmmy.session.SessionManager; |
|
|
|
|
|
import gr.thmmy.mthmmy.session.ValidateSessionTask; |
|
|
import gr.thmmy.mthmmy.utils.NetworkResultCodes; |
|
|
import gr.thmmy.mthmmy.utils.NetworkResultCodes; |
|
|
import gr.thmmy.mthmmy.utils.parsing.NewParseTask; |
|
|
import gr.thmmy.mthmmy.utils.parsing.NewParseTask; |
|
|
import gr.thmmy.mthmmy.utils.parsing.ParseException; |
|
|
import gr.thmmy.mthmmy.utils.parsing.ParseException; |
|
|
import gr.thmmy.mthmmy.views.CustomRecyclerView; |
|
|
import gr.thmmy.mthmmy.views.CustomRecyclerView; |
|
|
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; |
|
|
|
|
|
|
|
@ -60,12 +59,13 @@ public class UnreadFragment extends BaseFragment { |
|
|
private UnreadAdapter unreadAdapter; |
|
|
private UnreadAdapter unreadAdapter; |
|
|
|
|
|
|
|
|
private List<TopicSummary> topicSummaries; |
|
|
private List<TopicSummary> topicSummaries; |
|
|
private String markAsReadUrl = ""; |
|
|
private String markAsReadUrl; |
|
|
private int numberOfPages = 0; |
|
|
private int numberOfPages = 0; |
|
|
private int loadedPages = 0; |
|
|
private int loadedPages = 0; |
|
|
|
|
|
|
|
|
private UnreadTask unreadTask; |
|
|
private UnreadTask unreadTask; |
|
|
private MarkReadTask markReadTask; |
|
|
private MarkReadTask markReadTask; |
|
|
|
|
|
private ValidateSessionTask validateSessionTask; |
|
|
|
|
|
|
|
|
// Required empty public constructor
|
|
|
// Required empty public constructor
|
|
|
public UnreadFragment() {} |
|
|
public UnreadFragment() {} |
|
@ -89,18 +89,22 @@ public class UnreadFragment extends BaseFragment { |
|
|
public void onCreate(Bundle savedInstanceState) { |
|
|
public void onCreate(Bundle savedInstanceState) { |
|
|
super.onCreate(savedInstanceState); |
|
|
super.onCreate(savedInstanceState); |
|
|
topicSummaries = new ArrayList<>(); |
|
|
topicSummaries = new ArrayList<>(); |
|
|
|
|
|
markAsReadUrl = BaseApplication.getInstance().getSessionManager().getMarkAllAsReadLink(); |
|
|
|
|
|
if(markAsReadUrl==null){ |
|
|
|
|
|
Timber.i("MarkAsRead URL is null."); |
|
|
|
|
|
startValidateSessionTask(); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onActivityCreated(Bundle savedInstanceState) { |
|
|
public void onActivityCreated(Bundle savedInstanceState) { |
|
|
super.onActivityCreated(savedInstanceState); |
|
|
super.onActivityCreated(savedInstanceState); |
|
|
if (topicSummaries.isEmpty()){ |
|
|
if (topicSummaries.isEmpty()){ |
|
|
unreadTask = new UnreadTask(this::onUnreadTaskStarted, this::onUnreadTaskFinished); |
|
|
unreadTask = new UnreadTask(this::onUnreadTaskStarted, UnreadFragment.this::onUnreadTaskCancelled, this::onUnreadTaskFinished); |
|
|
assert SessionManager.unreadUrl != null; |
|
|
assert SessionManager.unreadUrl != null; |
|
|
unreadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, SessionManager.unreadUrl.toString()); |
|
|
unreadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, SessionManager.unreadUrl.toString()); |
|
|
} |
|
|
} |
|
|
markReadTask = new MarkReadTask(); |
|
|
markReadTask = new MarkReadTask(this::onMarkReadTaskStarted, this::onMarkReadTaskFinished); |
|
|
Timber.d("onActivityCreated"); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -139,15 +143,7 @@ public class UnreadFragment 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( |
|
|
() -> { |
|
|
this::startUnreadTask |
|
|
if (!unreadTask.isRunning()) { |
|
|
|
|
|
numberOfPages = 0; |
|
|
|
|
|
loadedPages = 0; |
|
|
|
|
|
unreadTask = new UnreadTask(this::onUnreadTaskStarted, this::onUnreadTaskFinished); |
|
|
|
|
|
assert SessionManager.unreadUrl != null; |
|
|
|
|
|
unreadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, SessionManager.unreadUrl.toString()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
); |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -157,17 +153,18 @@ public class UnreadFragment extends BaseFragment { |
|
|
@Override |
|
|
@Override |
|
|
public void onDestroy() { |
|
|
public void onDestroy() { |
|
|
super.onDestroy(); |
|
|
super.onDestroy(); |
|
|
if (unreadTask!=null){ |
|
|
cancelUnreadTaskIfRunning(); |
|
|
|
|
|
if (markReadTask!=null){ |
|
|
try{ |
|
|
try{ |
|
|
if(unreadTask.isRunning()) |
|
|
if(markReadTask.isRunning()) |
|
|
unreadTask.cancel(true); |
|
|
markReadTask.cancel(true); |
|
|
} // Yes, it happens even though we checked
|
|
|
} // Yes, it happens even though we checked
|
|
|
catch (NullPointerException ignored){ } |
|
|
catch (NullPointerException ignored){ } |
|
|
} |
|
|
} |
|
|
if (markReadTask!=null){ |
|
|
if (validateSessionTask!=null){ |
|
|
try{ |
|
|
try{ |
|
|
if(markReadTask.isRunning()) |
|
|
if(validateSessionTask.isRunning()) |
|
|
markReadTask.cancel(true); |
|
|
validateSessionTask.cancel(true); |
|
|
} // Yes, it happens even though we checked
|
|
|
} // Yes, it happens even though we checked
|
|
|
catch (NullPointerException ignored){ } |
|
|
catch (NullPointerException ignored){ } |
|
|
} |
|
|
} |
|
@ -175,15 +172,42 @@ public class UnreadFragment extends BaseFragment { |
|
|
topicSummaries.clear(); |
|
|
topicSummaries.clear(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void startUnreadTask(){ |
|
|
|
|
|
if (unreadTask!=null) { |
|
|
|
|
|
try{ |
|
|
|
|
|
if(!unreadTask.isRunning()){ |
|
|
|
|
|
numberOfPages = 0; |
|
|
|
|
|
loadedPages = 0; |
|
|
|
|
|
unreadTask = new UnreadTask(UnreadFragment.this::onUnreadTaskStarted, UnreadFragment.this::onUnreadTaskCancelled, UnreadFragment.this::onUnreadTaskFinished); |
|
|
|
|
|
assert SessionManager.unreadUrl != null; |
|
|
|
|
|
unreadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, SessionManager.unreadUrl.toString()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
catch (NullPointerException ignored){ } |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void startValidateSessionTask(){ |
|
|
|
|
|
validateSessionTask = new ValidateSessionTask(); |
|
|
|
|
|
validateSessionTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void cancelUnreadTaskIfRunning(){ |
|
|
|
|
|
if (unreadTask!=null){ |
|
|
|
|
|
try{ |
|
|
|
|
|
if(unreadTask.isRunning()) |
|
|
|
|
|
unreadTask.cancel(true); |
|
|
|
|
|
} // Yes, it happens even though we checked
|
|
|
|
|
|
catch (NullPointerException ignored){ } |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public interface UnreadFragmentInteractionListener extends FragmentInteractionListener { |
|
|
public interface UnreadFragmentInteractionListener extends FragmentInteractionListener { |
|
|
void onUnreadFragmentInteraction(TopicSummary topicSummary); |
|
|
void onUnreadFragmentInteraction(TopicSummary topicSummary); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void showMarkAsReadFAB() { |
|
|
private void showMarkAsReadFAB() { |
|
|
markAsReadFAB.setOnClickListener(v -> { |
|
|
markAsReadFAB.setOnClickListener(v -> showMarkAsReadConfirmationDialog()); |
|
|
if (!markReadTask.isRunning() && !unreadTask.isRunning()) |
|
|
|
|
|
showMarkAsReadConfirmationDialog(); |
|
|
|
|
|
}); |
|
|
|
|
|
markAsReadFAB.show(); |
|
|
markAsReadFAB.show(); |
|
|
markAsReadFAB.setTag(true); |
|
|
markAsReadFAB.setTag(true); |
|
|
} |
|
|
} |
|
@ -199,27 +223,36 @@ public class UnreadFragment extends BaseFragment { |
|
|
builder.setTitle("Mark all as read"); |
|
|
builder.setTitle("Mark all as read"); |
|
|
builder.setMessage("Are you sure that you want to mark ALL topics as read?"); |
|
|
builder.setMessage("Are you sure that you want to mark ALL topics as read?"); |
|
|
builder.setPositiveButton("Yep", (dialogInterface, i) -> { |
|
|
builder.setPositiveButton("Yep", (dialogInterface, i) -> { |
|
|
markReadTask = new MarkReadTask(); |
|
|
if (!markReadTask.isRunning() && markAsReadUrl!=null){ |
|
|
|
|
|
markReadTask = new MarkReadTask(this::onMarkReadTaskStarted, this::onMarkReadTaskFinished); |
|
|
markReadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, markAsReadUrl); |
|
|
markReadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, markAsReadUrl); |
|
|
|
|
|
} |
|
|
}); |
|
|
}); |
|
|
builder.setNegativeButton("Nope", (dialogInterface, i) -> {}); |
|
|
builder.setNegativeButton("Nope", (dialogInterface, i) -> {}); |
|
|
builder.create().show(); |
|
|
builder.create().show(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//---------------------------------------ASYNC TASK-----------------------------------
|
|
|
private void hideProgressUI(){ |
|
|
|
|
|
progressBar.setVisibility(ProgressBar.INVISIBLE); |
|
|
|
|
|
swipeRefreshLayout.setRefreshing(false); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------UNREAD TASK-----------------------------------
|
|
|
|
|
|
|
|
|
private void onUnreadTaskStarted() { |
|
|
private void onUnreadTaskStarted() { |
|
|
progressBar.setVisibility(ProgressBar.VISIBLE); |
|
|
progressBar.setVisibility(ProgressBar.VISIBLE); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void onUnreadTaskFinished(int resultCode, UnreadTaskData unreadTaskData) { |
|
|
private void onUnreadTaskCancelled() { |
|
|
|
|
|
swipeRefreshLayout.setRefreshing(false); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void onUnreadTaskFinished(int resultCode, ArrayList<TopicSummary> fetchedUnread) { |
|
|
if (resultCode == NetworkResultCodes.SUCCESSFUL) { |
|
|
if (resultCode == NetworkResultCodes.SUCCESSFUL) { |
|
|
ArrayList<TopicSummary> fetchedUnread = unreadTaskData.getTopicSummaries(); |
|
|
|
|
|
if(!fetchedUnread.isEmpty()){ |
|
|
if(!fetchedUnread.isEmpty()){ |
|
|
if(loadedPages==0) |
|
|
if(loadedPages==0) |
|
|
topicSummaries.clear(); |
|
|
topicSummaries.clear(); |
|
|
topicSummaries.addAll(fetchedUnread); |
|
|
topicSummaries.addAll(fetchedUnread); |
|
|
markAsReadUrl = unreadTaskData.getMarkAsReadUrl(); |
|
|
|
|
|
noUnreadTopicsTextView.setVisibility(View.INVISIBLE); |
|
|
noUnreadTopicsTextView.setVisibility(View.INVISIBLE); |
|
|
showMarkAsReadFAB(); |
|
|
showMarkAsReadFAB(); |
|
|
} |
|
|
} |
|
@ -231,18 +264,15 @@ public class UnreadFragment extends BaseFragment { |
|
|
unreadAdapter.notifyDataSetChanged(); |
|
|
unreadAdapter.notifyDataSetChanged(); |
|
|
loadedPages++; |
|
|
loadedPages++; |
|
|
if (loadedPages < numberOfPages) { |
|
|
if (loadedPages < numberOfPages) { |
|
|
unreadTask = new UnreadTask(this::onUnreadTaskStarted, this::onUnreadTaskFinished); |
|
|
unreadTask = new UnreadTask(this::onUnreadTaskStarted, UnreadFragment.this::onUnreadTaskCancelled, this::onUnreadTaskFinished); |
|
|
assert SessionManager.unreadUrl != null; |
|
|
assert SessionManager.unreadUrl != null; |
|
|
unreadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, SessionManager.unreadUrl.toString() + ";start=" + loadedPages * 20); |
|
|
unreadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, SessionManager.unreadUrl.toString() + ";start=" + loadedPages * 20); |
|
|
} |
|
|
} |
|
|
else { |
|
|
else |
|
|
progressBar.setVisibility(ProgressBar.INVISIBLE); |
|
|
hideProgressUI(); |
|
|
swipeRefreshLayout.setRefreshing(false); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
else{ |
|
|
else{ |
|
|
progressBar.setVisibility(ProgressBar.INVISIBLE); |
|
|
hideProgressUI(); |
|
|
swipeRefreshLayout.setRefreshing(false); |
|
|
|
|
|
if (resultCode == NetworkResultCodes.NETWORK_ERROR) |
|
|
if (resultCode == NetworkResultCodes.NETWORK_ERROR) |
|
|
Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show(); |
|
|
Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show(); |
|
|
else |
|
|
else |
|
@ -251,16 +281,15 @@ public class UnreadFragment extends BaseFragment { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private class UnreadTask extends NewParseTask<UnreadTaskData> { |
|
|
private class UnreadTask extends NewParseTask<ArrayList<TopicSummary>> { |
|
|
UnreadTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<UnreadTaskData> onParseTaskFinishedListener) { |
|
|
UnreadTask(OnTaskStartedListener onTaskStartedListener, OnTaskCancelledListener onTaskCancelledListener, OnNetworkTaskFinishedListener<ArrayList<TopicSummary>> onParseTaskFinishedListener) { |
|
|
super(onTaskStartedListener, onParseTaskFinishedListener); |
|
|
super(onTaskStartedListener, onTaskCancelledListener, onParseTaskFinishedListener); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
protected UnreadTaskData 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<>(); |
|
|
ArrayList<TopicSummary> fetchedTopicSummaries = new ArrayList<>(); |
|
|
String markAsReadUrl=""; |
|
|
|
|
|
if (!unread.isEmpty()) { |
|
|
if (!unread.isEmpty()) { |
|
|
for (Element row : unread) { |
|
|
for (Element row : unread) { |
|
|
Elements information = row.select("td"); |
|
|
Elements information = row.select("td"); |
|
@ -278,12 +307,9 @@ public class UnreadFragment extends BaseFragment { |
|
|
} |
|
|
} |
|
|
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(); |
|
|
|
|
|
|
|
|
Element pagesElement = null, markRead = null; |
|
|
Element pagesElement = null; |
|
|
if (topBar != null) { |
|
|
if (topBar != null) |
|
|
pagesElement = topBar.select("td.middletext").first(); |
|
|
pagesElement = topBar.select("td.middletext").first(); |
|
|
markRead = document.select("table:not(.bordercolor):not([width])").select("a") |
|
|
|
|
|
.first(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (numberOfPages == 0 && pagesElement != null) { |
|
|
if (numberOfPages == 0 && pagesElement != null) { |
|
|
Elements pages = pagesElement.select("a"); |
|
|
Elements pages = pagesElement.select("a"); |
|
@ -293,91 +319,60 @@ public class UnreadFragment extends BaseFragment { |
|
|
numberOfPages = 1; |
|
|
numberOfPages = 1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (markRead != null && loadedPages == numberOfPages - 1) |
|
|
return fetchedTopicSummaries; |
|
|
markAsReadUrl = markRead.attr("href"); |
|
|
|
|
|
|
|
|
|
|
|
return new UnreadTaskData(fetchedTopicSummaries, markAsReadUrl); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
return new ArrayList<>(); |
|
|
return new UnreadTaskData(new ArrayList<>(), markAsReadUrl); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
protected int getResultCode(Response response, UnreadTaskData data) { |
|
|
protected int getResultCode(Response response, ArrayList<TopicSummary> topicSummaries) { |
|
|
return NetworkResultCodes.SUCCESSFUL; |
|
|
return NetworkResultCodes.SUCCESSFUL; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private class UnreadTaskData { |
|
|
//---------------------------------------MARKREAD TASK------------------------------------------
|
|
|
ArrayList<TopicSummary> topicSummaries; |
|
|
private void onMarkReadTaskStarted() { |
|
|
String markAsReadUrl; |
|
|
cancelUnreadTaskIfRunning(); |
|
|
|
|
|
progressBar.setVisibility(ProgressBar.VISIBLE); |
|
|
UnreadTaskData(ArrayList<TopicSummary> topicSummaries, String markAsReadUrl){ |
|
|
|
|
|
this.topicSummaries = topicSummaries; |
|
|
|
|
|
this.markAsReadUrl = markAsReadUrl; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
ArrayList<TopicSummary> getTopicSummaries() { |
|
|
private void onMarkReadTaskFinished(int resultCode, Boolean isSessionVerified) { |
|
|
return topicSummaries; |
|
|
hideProgressUI(); |
|
|
|
|
|
if (resultCode == NetworkResultCodes.SUCCESSFUL) { |
|
|
|
|
|
if (!isSessionVerified){ |
|
|
|
|
|
Toast.makeText(getContext(), "Session verification failed", Toast.LENGTH_SHORT).show(); |
|
|
|
|
|
startValidateSessionTask(); |
|
|
} |
|
|
} |
|
|
|
|
|
else |
|
|
String getMarkAsReadUrl() { |
|
|
startUnreadTask(); |
|
|
return markAsReadUrl; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
else{ |
|
|
|
|
|
hideProgressUI(); |
|
|
|
|
|
if (resultCode == NetworkResultCodes.NETWORK_ERROR) |
|
|
|
|
|
Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show(); |
|
|
|
|
|
else |
|
|
|
|
|
Toast.makeText(getContext(), "Unexpected error," + |
|
|
|
|
|
" please contact the developers with the details", Toast.LENGTH_LONG).show(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private class MarkReadTask extends AsyncTask<String, Void, Integer> { |
|
|
|
|
|
private static final int SUCCESS = 0; |
|
|
|
|
|
private static final int NETWORK_ERROR = 1; |
|
|
|
|
|
private static final int OTHER_ERROR = 2; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
protected void onPreExecute() { |
|
|
|
|
|
progressBar.setVisibility(ProgressBar.VISIBLE); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
private class MarkReadTask extends NewParseTask<Boolean> { |
|
|
protected Integer doInBackground(String... strings) { |
|
|
MarkReadTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<Boolean> onParseTaskFinishedListener) { |
|
|
Request request = new Request.Builder() |
|
|
super(onTaskStartedListener, onParseTaskFinishedListener); |
|
|
.url(strings[0]) |
|
|
|
|
|
.build(); |
|
|
|
|
|
try { |
|
|
|
|
|
client.newCall(request).execute(); |
|
|
|
|
|
return SUCCESS; |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
Timber.i(e, "IO Exception"); |
|
|
|
|
|
return NETWORK_ERROR; |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
Timber.e(e, "Exception"); |
|
|
|
|
|
return OTHER_ERROR; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
protected void onPostExecute(Integer result) { |
|
|
protected Boolean parse(Document document, Response response) throws ParseException { |
|
|
progressBar.setVisibility(ProgressBar.GONE); |
|
|
Elements sessionVerificationFailed = document.select("td:containsOwn(Session " + |
|
|
|
|
|
"verification failed. Please try logging out and back in again, and then try " + |
|
|
if (result == NETWORK_ERROR) { |
|
|
"again.), td:containsOwn(Η επαλήθευση συνόδου απέτυχε. Παρακαλούμε κάντε " + |
|
|
Toast.makeText(getContext() |
|
|
"αποσύνδεση, επανασύνδεση και ξαναδοκιμάστε.)"); |
|
|
, "Task was unsuccessful!\n Please check your internet conneciton.", |
|
|
return sessionVerificationFailed.isEmpty(); |
|
|
Toast.LENGTH_LONG).show(); |
|
|
|
|
|
} else if (result == OTHER_ERROR) { |
|
|
|
|
|
Toast.makeText(getContext() |
|
|
|
|
|
, "Fatal error!\n Task aborted...", Toast.LENGTH_LONG).show(); |
|
|
|
|
|
} else { |
|
|
|
|
|
if (!unreadTask.isRunning()) { |
|
|
|
|
|
numberOfPages = 0; |
|
|
|
|
|
loadedPages = 0; |
|
|
|
|
|
unreadTask = new UnreadTask(UnreadFragment.this::onUnreadTaskStarted, UnreadFragment.this::onUnreadTaskFinished); |
|
|
|
|
|
assert SessionManager.unreadUrl != null; |
|
|
|
|
|
unreadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, SessionManager.unreadUrl.toString()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//TODO: Maybe extend this task and use isRunning() from ExternalAsyncTask instead (?)
|
|
|
@Override |
|
|
public boolean isRunning(){ |
|
|
protected int getResultCode(Response response, Boolean isSessionVerified) { |
|
|
return getStatus() == AsyncTask.Status.RUNNING; |
|
|
return NetworkResultCodes.SUCCESSFUL; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|