|
@ -10,6 +10,7 @@ import android.view.View; |
|
|
import android.view.ViewGroup; |
|
|
import android.view.ViewGroup; |
|
|
import android.widget.ProgressBar; |
|
|
import android.widget.ProgressBar; |
|
|
import android.widget.RelativeLayout; |
|
|
import android.widget.RelativeLayout; |
|
|
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
|
import com.bignerdranch.expandablerecyclerview.ExpandableRecyclerAdapter; |
|
|
import com.bignerdranch.expandablerecyclerview.ExpandableRecyclerAdapter; |
|
|
|
|
|
|
|
@ -17,21 +18,26 @@ 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.BaseActivity; |
|
|
import gr.thmmy.mthmmy.base.BaseActivity; |
|
|
|
|
|
import gr.thmmy.mthmmy.base.BaseApplication; |
|
|
import gr.thmmy.mthmmy.base.BaseFragment; |
|
|
import gr.thmmy.mthmmy.base.BaseFragment; |
|
|
import gr.thmmy.mthmmy.model.Board; |
|
|
import gr.thmmy.mthmmy.model.Board; |
|
|
import gr.thmmy.mthmmy.model.Category; |
|
|
import gr.thmmy.mthmmy.model.Category; |
|
|
import gr.thmmy.mthmmy.session.SessionManager; |
|
|
import gr.thmmy.mthmmy.session.SessionManager; |
|
|
import gr.thmmy.mthmmy.utils.CustomRecyclerView; |
|
|
import gr.thmmy.mthmmy.utils.CustomRecyclerView; |
|
|
|
|
|
import gr.thmmy.mthmmy.utils.parsing.NewParseTask; |
|
|
|
|
|
import gr.thmmy.mthmmy.utils.parsing.Parcel; |
|
|
import gr.thmmy.mthmmy.utils.parsing.ParseException; |
|
|
import gr.thmmy.mthmmy.utils.parsing.ParseException; |
|
|
import gr.thmmy.mthmmy.utils.parsing.ParseTask; |
|
|
|
|
|
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; |
|
|
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; |
|
|
import okhttp3.HttpUrl; |
|
|
import okhttp3.HttpUrl; |
|
|
|
|
|
import okhttp3.OkHttpClient; |
|
|
import okhttp3.Request; |
|
|
import okhttp3.Request; |
|
|
|
|
|
import okhttp3.Response; |
|
|
import timber.log.Timber; |
|
|
import timber.log.Timber; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@ -83,7 +89,7 @@ public class ForumFragment extends BaseFragment { |
|
|
public void onActivityCreated(Bundle savedInstanceState) { |
|
|
public void onActivityCreated(Bundle savedInstanceState) { |
|
|
super.onActivityCreated(savedInstanceState); |
|
|
super.onActivityCreated(savedInstanceState); |
|
|
if (categories.isEmpty()) { |
|
|
if (categories.isEmpty()) { |
|
|
forumTask = new ForumTask(); |
|
|
forumTask = new ForumTask(this::onForumTaskStarted, this::onForumTaskFinished); |
|
|
forumTask.execute(); |
|
|
forumTask.execute(); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
@ -106,7 +112,7 @@ public class ForumFragment extends BaseFragment { |
|
|
if (BaseActivity.getSessionManager().isLoggedIn()) { |
|
|
if (BaseActivity.getSessionManager().isLoggedIn()) { |
|
|
if (forumTask.getStatus() == AsyncTask.Status.RUNNING) |
|
|
if (forumTask.getStatus() == AsyncTask.Status.RUNNING) |
|
|
forumTask.cancel(true); |
|
|
forumTask.cancel(true); |
|
|
forumTask = new ForumTask(); |
|
|
forumTask = new ForumTask(ForumFragment.this::onForumTaskStarted, ForumFragment.this::onForumTaskFinished); |
|
|
forumTask.setUrl(categories.get(parentPosition).getCategoryURL()); |
|
|
forumTask.setUrl(categories.get(parentPosition).getCategoryURL()); |
|
|
forumTask.execute(); |
|
|
forumTask.execute(); |
|
|
} |
|
|
} |
|
@ -117,7 +123,7 @@ public class ForumFragment extends BaseFragment { |
|
|
if (BaseActivity.getSessionManager().isLoggedIn()) { |
|
|
if (BaseActivity.getSessionManager().isLoggedIn()) { |
|
|
if (forumTask.getStatus() == AsyncTask.Status.RUNNING) |
|
|
if (forumTask.getStatus() == AsyncTask.Status.RUNNING) |
|
|
forumTask.cancel(true); |
|
|
forumTask.cancel(true); |
|
|
forumTask = new ForumTask(); |
|
|
forumTask = new ForumTask(ForumFragment.this::onForumTaskStarted, ForumFragment.this::onForumTaskFinished); |
|
|
forumTask.setUrl(categories.get(parentPosition).getCategoryURL()); |
|
|
forumTask.setUrl(categories.get(parentPosition).getCategoryURL()); |
|
|
forumTask.execute(); |
|
|
forumTask.execute(); |
|
|
} |
|
|
} |
|
@ -135,17 +141,13 @@ public class ForumFragment extends BaseFragment { |
|
|
swipeRefreshLayout = rootView.findViewById(R.id.swiperefresh); |
|
|
swipeRefreshLayout = rootView.findViewById(R.id.swiperefresh); |
|
|
swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.primary); |
|
|
swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.primary); |
|
|
swipeRefreshLayout.setColorSchemeResources(R.color.accent); |
|
|
swipeRefreshLayout.setColorSchemeResources(R.color.accent); |
|
|
swipeRefreshLayout.setOnRefreshListener( |
|
|
swipeRefreshLayout.setOnRefreshListener(() -> { |
|
|
new SwipeRefreshLayout.OnRefreshListener() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void onRefresh() { |
|
|
|
|
|
if (forumTask != null && forumTask.getStatus() != AsyncTask.Status.RUNNING) { |
|
|
if (forumTask != null && forumTask.getStatus() != AsyncTask.Status.RUNNING) { |
|
|
forumTask = new ForumTask(); |
|
|
forumTask = new ForumTask(ForumFragment.this::onForumTaskStarted, ForumFragment.this::onForumTaskFinished); |
|
|
forumTask.execute(SessionManager.indexUrl.toString()); |
|
|
//forumTask.execute(SessionManager.indexUrl.toString());
|
|
|
|
|
|
forumTask.execute(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
@ -163,33 +165,38 @@ public class ForumFragment extends BaseFragment { |
|
|
void onForumFragmentInteraction(Board board); |
|
|
void onForumFragmentInteraction(Board board); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//---------------------------------------ASYNC TASK-----------------------------------
|
|
|
public void onForumTaskStarted() { |
|
|
|
|
|
progressBar.setVisibility(ProgressBar.VISIBLE); |
|
|
private class ForumTask extends ParseTask { |
|
|
|
|
|
private HttpUrl forumUrl = SessionManager.forumUrl; //may change upon collapse/expand
|
|
|
|
|
|
|
|
|
|
|
|
private final List<Category> fetchedCategories; |
|
|
|
|
|
|
|
|
|
|
|
ForumTask() { |
|
|
|
|
|
fetchedCategories = new ArrayList<>(); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected void onPreExecute() { |
|
|
public void onForumTaskFinished(int resultCode, ArrayList<Category> fetchedCategories) { |
|
|
progressBar.setVisibility(ProgressBar.VISIBLE); |
|
|
if (resultCode == Parcel.ResultCode.SUCCESSFUL) { |
|
|
|
|
|
categories.clear(); |
|
|
|
|
|
categories.addAll(fetchedCategories); |
|
|
|
|
|
forumAdapter.notifyParentDataSetChanged(false); |
|
|
|
|
|
} else if (resultCode == Parcel.ResultCode.NETWORK_ERROR) { |
|
|
|
|
|
Toast.makeText(BaseApplication.getInstance().getApplicationContext(), "Network error", Toast.LENGTH_SHORT).show(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
progressBar.setVisibility(ProgressBar.INVISIBLE); |
|
|
protected Request prepareRequest(String... params) { |
|
|
swipeRefreshLayout.setRefreshing(false); |
|
|
return new Request.Builder() |
|
|
|
|
|
.url(forumUrl) |
|
|
|
|
|
.build(); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------ASYNC TASK-----------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
private class ForumTask extends NewParseTask<ArrayList<Category>> { |
|
|
|
|
|
private HttpUrl forumUrl = SessionManager.forumUrl; //may change upon collapse/expand
|
|
|
|
|
|
|
|
|
|
|
|
public ForumTask(OnParseTaskStartedListener onParseTaskStartedListener, |
|
|
|
|
|
OnParseTaskFinishedListener<ArrayList<Category>> onParseTaskFinishedListener) { |
|
|
|
|
|
super(onParseTaskStartedListener, onParseTaskFinishedListener); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void parse(Document document) throws ParseException { |
|
|
protected ArrayList<Category> parse(Document document) throws ParseException { |
|
|
Elements categoryBlocks = document.select(".tborder:not([style])>table[cellpadding=5]"); |
|
|
Elements categoryBlocks = document.select(".tborder:not([style])>table[cellpadding=5]"); |
|
|
if (categoryBlocks.size() != 0) { |
|
|
if (categoryBlocks.size() != 0) { |
|
|
|
|
|
ArrayList<Category> fetchedCategories = new ArrayList<>(); |
|
|
for (Element categoryBlock : categoryBlocks) { |
|
|
for (Element categoryBlock : categoryBlocks) { |
|
|
Element categoryElement = categoryBlock.select("td[colspan=2]>[name]").first(); |
|
|
Element categoryElement = categoryBlock.select("td[colspan=2]>[name]").first(); |
|
|
String categoryUrl = categoryElement.attr("href"); |
|
|
String categoryUrl = categoryElement.attr("href"); |
|
@ -207,24 +214,26 @@ public class ForumFragment extends BaseFragment { |
|
|
|
|
|
|
|
|
fetchedCategories.add(category); |
|
|
fetchedCategories.add(category); |
|
|
} |
|
|
} |
|
|
categories.clear(); |
|
|
return fetchedCategories; |
|
|
categories.addAll(fetchedCategories); |
|
|
|
|
|
fetchedCategories.clear(); |
|
|
|
|
|
} else |
|
|
} else |
|
|
throw new ParseException("Parsing failed"); |
|
|
throw new ParseException("Parsing failed"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
protected void postExecution(ParseTask.ResultCode result) { |
|
|
protected Response sendRequest(OkHttpClient client, String... input) throws IOException { |
|
|
if (result == ResultCode.SUCCESS) |
|
|
Request request = new Request.Builder() |
|
|
forumAdapter.notifyParentDataSetChanged(false); |
|
|
.url(forumUrl) |
|
|
|
|
|
.build(); |
|
|
|
|
|
return client.newCall(request).execute(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
progressBar.setVisibility(ProgressBar.INVISIBLE); |
|
|
@Override |
|
|
swipeRefreshLayout.setRefreshing(false); |
|
|
protected int getResultCode(Response response, ArrayList<Category> data) { |
|
|
|
|
|
return Parcel.ResultCode.SUCCESSFUL; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void setUrl(String string) //TODO delete and simplify e.g. in prepareRequest possible?
|
|
|
//TODO delete and simplify e.g. in prepareRequest possible?
|
|
|
{ |
|
|
public void setUrl(String string) { |
|
|
forumUrl = HttpUrl.parse(string); |
|
|
forumUrl = HttpUrl.parse(string); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|