|
|
@ -2,9 +2,11 @@ package gr.thmmy.mthmmy.activities.main.unread; |
|
|
|
|
|
|
|
import android.os.AsyncTask; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.support.annotation.NonNull; |
|
|
|
import android.support.v4.widget.SwipeRefreshLayout; |
|
|
|
import android.support.v7.widget.DividerItemDecoration; |
|
|
|
import android.support.v7.widget.LinearLayoutManager; |
|
|
|
import android.util.Log; |
|
|
|
import android.view.LayoutInflater; |
|
|
|
import android.view.View; |
|
|
|
import android.view.ViewGroup; |
|
|
@ -25,7 +27,6 @@ import gr.thmmy.mthmmy.base.BaseFragment; |
|
|
|
import gr.thmmy.mthmmy.model.TopicSummary; |
|
|
|
import gr.thmmy.mthmmy.session.SessionManager; |
|
|
|
import gr.thmmy.mthmmy.utils.CustomRecyclerView; |
|
|
|
import gr.thmmy.mthmmy.utils.parsing.ParseException; |
|
|
|
import gr.thmmy.mthmmy.utils.parsing.ParseTask; |
|
|
|
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; |
|
|
|
import okhttp3.Request; |
|
|
@ -42,13 +43,14 @@ import timber.log.Timber; |
|
|
|
|
|
|
|
public class UnreadFragment extends BaseFragment { |
|
|
|
private static final String TAG = "UnreadFragment"; |
|
|
|
// Fragment initialization parameters, e.g. ARG_SECTION_NUMBER
|
|
|
|
|
|
|
|
private MaterialProgressBar progressBar; |
|
|
|
private SwipeRefreshLayout swipeRefreshLayout; |
|
|
|
private UnreadAdapter unreadAdapter; |
|
|
|
|
|
|
|
private List<TopicSummary> topicSummaries; |
|
|
|
private int numberOfPages = 0; |
|
|
|
private int loadedPages = 0; |
|
|
|
|
|
|
|
private UnreadTask unreadTask; |
|
|
|
private MarkReadTask markReadTask; |
|
|
@ -83,6 +85,7 @@ public class UnreadFragment extends BaseFragment { |
|
|
|
super.onActivityCreated(savedInstanceState); |
|
|
|
if (topicSummaries.isEmpty()) { |
|
|
|
unreadTask = new UnreadTask(); |
|
|
|
assert SessionManager.unreadUrl != null; |
|
|
|
unreadTask.execute(SessionManager.unreadUrl.toString()); |
|
|
|
} |
|
|
|
markReadTask = new MarkReadTask(); |
|
|
@ -91,7 +94,7 @@ public class UnreadFragment extends BaseFragment { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, |
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, |
|
|
|
Bundle savedInstanceState) { |
|
|
|
// Inflate the layout for this fragment
|
|
|
|
final View rootView = inflater.inflate(R.layout.fragment_unread, container, false); |
|
|
@ -99,7 +102,7 @@ public class UnreadFragment extends BaseFragment { |
|
|
|
// Set the adapter
|
|
|
|
if (rootView instanceof RelativeLayout) { |
|
|
|
progressBar = rootView.findViewById(R.id.progressBar); |
|
|
|
unreadAdapter = new UnreadAdapter(getActivity(), topicSummaries, |
|
|
|
unreadAdapter = new UnreadAdapter(topicSummaries, |
|
|
|
fragmentInteractionListener, new UnreadAdapter.MarkReadInteractionListener() { |
|
|
|
@Override |
|
|
|
public void onMarkReadInteraction(String markReadLinkUrl) { |
|
|
@ -126,7 +129,11 @@ public class UnreadFragment extends BaseFragment { |
|
|
|
@Override |
|
|
|
public void onRefresh() { |
|
|
|
if (unreadTask != null && unreadTask.getStatus() != AsyncTask.Status.RUNNING) { |
|
|
|
topicSummaries.clear(); |
|
|
|
numberOfPages = 0; |
|
|
|
loadedPages = 0; |
|
|
|
unreadTask = new UnreadTask(); |
|
|
|
assert SessionManager.unreadUrl != null; |
|
|
|
unreadTask.execute(SessionManager.unreadUrl.toString()); |
|
|
|
} |
|
|
|
} |
|
|
@ -158,10 +165,10 @@ public class UnreadFragment extends BaseFragment { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void parse(Document document) throws ParseException { |
|
|
|
public void parse(Document document) { |
|
|
|
Elements unread = document.select("table.bordercolor[cellspacing=1] tr:not(.titlebg)"); |
|
|
|
if (!unread.isEmpty()) { |
|
|
|
topicSummaries.clear(); |
|
|
|
//topicSummaries.clear();
|
|
|
|
for (Element row : unread) { |
|
|
|
Elements information = row.select("td"); |
|
|
|
String link = information.last().select("a").first().attr("href"); |
|
|
@ -178,7 +185,7 @@ public class UnreadFragment extends BaseFragment { |
|
|
|
dateTime.contains(" πμ") || dateTime.contains(" μμ")) { |
|
|
|
dateTime = dateTime.replaceAll(":[0-5][0-9] ", " "); |
|
|
|
} else { |
|
|
|
dateTime=dateTime.substring(0,dateTime.lastIndexOf(":")); |
|
|
|
dateTime = dateTime.substring(0, dateTime.lastIndexOf(":")); |
|
|
|
} |
|
|
|
if (!dateTime.contains(",")) { |
|
|
|
dateTime = dateTime.replaceAll(".+? ([0-9])", "$1"); |
|
|
@ -186,9 +193,26 @@ public class UnreadFragment extends BaseFragment { |
|
|
|
|
|
|
|
topicSummaries.add(new TopicSummary(link, title, lastUser, dateTime)); |
|
|
|
} |
|
|
|
Element markRead = document.select("table:not(.bordercolor):not([width])").select("a") |
|
|
|
Element topBar = document.select("table:not(.bordercolor):not(#bodyarea):has(td.middletext)").first(); |
|
|
|
|
|
|
|
Element pagesElement = null, markRead = null; |
|
|
|
if (topBar != null) { |
|
|
|
pagesElement = topBar.select("td.middletext").first(); |
|
|
|
|
|
|
|
markRead = document.select("table:not(.bordercolor):not([width])").select("a") |
|
|
|
.first(); |
|
|
|
if (markRead != null) |
|
|
|
} |
|
|
|
|
|
|
|
if (numberOfPages == 0 && pagesElement != null) { |
|
|
|
Elements pages = pagesElement.select("a"); |
|
|
|
if (!pages.isEmpty()) { |
|
|
|
numberOfPages = Integer.parseInt(pages.last().text()); |
|
|
|
} else { |
|
|
|
numberOfPages = 1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (markRead != null && loadedPages == numberOfPages - 1) |
|
|
|
topicSummaries.add(new TopicSummary(markRead.attr("href"), markRead.text(), null, |
|
|
|
null)); |
|
|
|
} else { |
|
|
@ -205,13 +229,21 @@ public class UnreadFragment extends BaseFragment { |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void postExecution(ParseTask.ResultCode result) { |
|
|
|
if (result == ResultCode.SUCCESS) |
|
|
|
if (result == ResultCode.SUCCESS) { |
|
|
|
unreadAdapter.notifyDataSetChanged(); |
|
|
|
|
|
|
|
++loadedPages; |
|
|
|
if (loadedPages < numberOfPages) { |
|
|
|
unreadTask = new UnreadTask(); |
|
|
|
assert SessionManager.unreadUrl != null; |
|
|
|
unreadTask.execute(SessionManager.unreadUrl.toString() + ";start=" + loadedPages * 20); |
|
|
|
} |
|
|
|
|
|
|
|
progressBar.setVisibility(ProgressBar.INVISIBLE); |
|
|
|
swipeRefreshLayout.setRefreshing(false); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private class MarkReadTask extends AsyncTask<String, Void, Integer> { |
|
|
|
private static final int SUCCESS = 0; |
|
|
@ -254,6 +286,7 @@ public class UnreadFragment extends BaseFragment { |
|
|
|
} else { |
|
|
|
if (unreadTask != null && unreadTask.getStatus() != AsyncTask.Status.RUNNING) { |
|
|
|
unreadTask = new UnreadTask(); |
|
|
|
assert SessionManager.unreadUrl != null; |
|
|
|
unreadTask.execute(SessionManager.unreadUrl.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|