|
@ -5,7 +5,6 @@ import android.os.Bundle; |
|
|
import android.view.LayoutInflater; |
|
|
import android.view.LayoutInflater; |
|
|
import android.view.View; |
|
|
import android.view.View; |
|
|
import android.view.ViewGroup; |
|
|
import android.view.ViewGroup; |
|
|
import android.widget.ProgressBar; |
|
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
import androidx.annotation.NonNull; |
|
|
import androidx.recyclerview.widget.DividerItemDecoration; |
|
|
import androidx.recyclerview.widget.DividerItemDecoration; |
|
@ -27,7 +26,6 @@ import gr.thmmy.mthmmy.base.BaseFragment; |
|
|
import gr.thmmy.mthmmy.model.PostSummary; |
|
|
import gr.thmmy.mthmmy.model.PostSummary; |
|
|
import gr.thmmy.mthmmy.utils.parsing.ParseException; |
|
|
import gr.thmmy.mthmmy.utils.parsing.ParseException; |
|
|
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; |
|
|
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; |
|
|
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; |
|
|
|
|
|
import okhttp3.Request; |
|
|
import okhttp3.Request; |
|
|
import okhttp3.Response; |
|
|
import okhttp3.Response; |
|
|
import timber.log.Timber; |
|
|
import timber.log.Timber; |
|
@ -35,7 +33,7 @@ import timber.log.Timber; |
|
|
/** |
|
|
/** |
|
|
* Use the {@link LatestPostsFragment#newInstance} factory method to create an instance of this fragment. |
|
|
* Use the {@link LatestPostsFragment#newInstance} factory method to create an instance of this fragment. |
|
|
*/ |
|
|
*/ |
|
|
public class LatestPostsFragment extends BaseFragment implements LatestPostsAdapter.OnLoadMoreListener { |
|
|
public class LatestPostsFragment extends BaseFragment { |
|
|
/** |
|
|
/** |
|
|
* The key to use when putting profile's url String to {@link LatestPostsFragment}'s Bundle. |
|
|
* The key to use when putting profile's url String to {@link LatestPostsFragment}'s Bundle. |
|
|
*/ |
|
|
*/ |
|
@ -50,12 +48,13 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap |
|
|
private int pagesLoaded = 0; |
|
|
private int pagesLoaded = 0; |
|
|
private String profileUrl; |
|
|
private String profileUrl; |
|
|
private LatestPostsTask profileLatestPostsTask; |
|
|
private LatestPostsTask profileLatestPostsTask; |
|
|
private MaterialProgressBar progressBar; |
|
|
|
|
|
private boolean isLoadingMore; |
|
|
private boolean isLoadingMore; |
|
|
private boolean userHasPosts = true; |
|
|
private boolean userHasPosts = true; |
|
|
private static final int visibleThreshold = 5; |
|
|
private static final int visibleThreshold = 5; |
|
|
private int lastVisibleItem, totalItemCount; |
|
|
private int lastVisibleItem, totalItemCount; |
|
|
|
|
|
|
|
|
|
|
|
private OnLoadingListener onLoadingListener; |
|
|
|
|
|
|
|
|
public LatestPostsFragment() { |
|
|
public LatestPostsFragment() { |
|
|
// Required empty public constructor
|
|
|
// Required empty public constructor
|
|
|
} |
|
|
} |
|
@ -87,16 +86,16 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap |
|
|
Bundle savedInstanceState) { |
|
|
Bundle savedInstanceState) { |
|
|
final View rootView = inflater.inflate(R.layout.fragment_profile_latest_posts, container, false); |
|
|
final View rootView = inflater.inflate(R.layout.fragment_profile_latest_posts, container, false); |
|
|
latestPostsAdapter = new LatestPostsAdapter(this.getContext(), fragmentInteractionListener, parsedTopicSummaries); |
|
|
latestPostsAdapter = new LatestPostsAdapter(this.getContext(), fragmentInteractionListener, parsedTopicSummaries); |
|
|
RecyclerView mainContent = rootView.findViewById(R.id.profile_latest_posts_recycler); |
|
|
RecyclerView recyclerView = rootView.findViewById(R.id.profile_latest_posts_recycler); |
|
|
mainContent.setAdapter(latestPostsAdapter); |
|
|
recyclerView.setAdapter(latestPostsAdapter); |
|
|
final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); |
|
|
final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); |
|
|
mainContent.setLayoutManager(layoutManager); |
|
|
recyclerView.setLayoutManager(layoutManager); |
|
|
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mainContent.getContext(), |
|
|
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), |
|
|
layoutManager.getOrientation()); |
|
|
layoutManager.getOrientation()); |
|
|
mainContent.addItemDecoration(dividerItemDecoration); |
|
|
recyclerView.addItemDecoration(dividerItemDecoration); |
|
|
|
|
|
recyclerView.setItemViewCacheSize(15); |
|
|
|
|
|
|
|
|
//latestPostsAdapter.setOnLoadMoreListener();
|
|
|
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { |
|
|
mainContent.addOnScrollListener(new RecyclerView.OnScrollListener() { |
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { |
|
|
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { |
|
|
super.onScrolled(recyclerView, dx, dy); |
|
|
super.onScrolled(recyclerView, dx, dy); |
|
@ -110,20 +109,15 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
progressBar = rootView.findViewById(R.id.progressBar); |
|
|
|
|
|
return rootView; |
|
|
return rootView; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void onLoadMore() { |
|
|
public void onLoadMore() { |
|
|
if (pagesLoaded < numberOfPages) { |
|
|
if (pagesLoaded < numberOfPages) { |
|
|
parsedTopicSummaries.add(null); |
|
|
|
|
|
latestPostsAdapter.notifyItemInserted(parsedTopicSummaries.size() - 1); |
|
|
|
|
|
|
|
|
|
|
|
//Load data
|
|
|
//Load data
|
|
|
profileLatestPostsTask = new LatestPostsTask(); |
|
|
profileLatestPostsTask = new LatestPostsTask(); |
|
|
profileLatestPostsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, profileUrl + ";sa=showPosts;start=" + pagesLoaded * 15); |
|
|
profileLatestPostsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, profileUrl + ";sa=showPosts;start=" + pagesLoaded * 15); |
|
|
++pagesLoaded; |
|
|
pagesLoaded++; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -148,6 +142,14 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap |
|
|
void onLatestPostsFragmentInteraction(PostSummary postSummary); |
|
|
void onLatestPostsFragmentInteraction(PostSummary postSummary); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public interface OnLoadingListener { |
|
|
|
|
|
void onLoadingLatestPosts(boolean loading); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void setOnLoadingListener(OnLoadingListener onLoadingListener) { |
|
|
|
|
|
this.onLoadingListener = onLoadingListener; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 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 latest posts. |
|
|
* user's latest posts. |
|
@ -155,8 +157,16 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap |
|
|
* parameter!</p> |
|
|
* parameter!</p> |
|
|
*/ |
|
|
*/ |
|
|
private class LatestPostsTask extends AsyncTask<String, Void, Boolean> { |
|
|
private class LatestPostsTask extends AsyncTask<String, Void, Boolean> { |
|
|
|
|
|
private ArrayList<PostSummary> fetchedParsedTopicSummaries; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
protected void onPreExecute() { |
|
|
protected void onPreExecute() { |
|
|
if (!isLoadingMore) progressBar.setVisibility(ProgressBar.VISIBLE); |
|
|
onLoadingListener.onLoadingLatestPosts(true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public LatestPostsTask() { |
|
|
|
|
|
super(); |
|
|
|
|
|
fetchedParsedTopicSummaries = new ArrayList<>(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected Boolean doInBackground(String... profileUrl) { |
|
|
protected Boolean doInBackground(String... profileUrl) { |
|
@ -177,10 +187,12 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap |
|
|
protected void onPostExecute(Boolean result) { |
|
|
protected void onPostExecute(Boolean result) { |
|
|
if (Boolean.FALSE.equals(result)) |
|
|
if (Boolean.FALSE.equals(result)) |
|
|
Timber.e(new ParseException("Parsing failed (latest posts)"),"ParseException"); //TODO: This is inaccurate (e.g. can also have an I/O cause)
|
|
|
Timber.e(new ParseException("Parsing failed (latest posts)"),"ParseException"); //TODO: This is inaccurate (e.g. can also have an I/O cause)
|
|
|
|
|
|
int prevSize = parsedTopicSummaries.size(); |
|
|
progressBar.setVisibility(ProgressBar.INVISIBLE); |
|
|
parsedTopicSummaries.addAll(fetchedParsedTopicSummaries); |
|
|
|
|
|
latestPostsAdapter.notifyItemRangeInserted(prevSize, parsedTopicSummaries.size() - prevSize); |
|
|
latestPostsAdapter.notifyDataSetChanged(); |
|
|
latestPostsAdapter.notifyDataSetChanged(); |
|
|
isLoadingMore = false; |
|
|
isLoadingMore = false; |
|
|
|
|
|
onLoadingListener.onLoadingLatestPosts(false); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//TODO: better parse error handling (ParseException etc.)
|
|
|
//TODO: better parse error handling (ParseException etc.)
|
|
@ -192,14 +204,10 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap |
|
|
latestPostsRows = latestPostsPage. |
|
|
latestPostsRows = latestPostsPage. |
|
|
select("td:has(table:Contains(Εμφάνιση μηνυμάτων)):not([style]) > table"); |
|
|
select("td:has(table:Contains(Εμφάνιση μηνυμάτων)):not([style]) > table"); |
|
|
|
|
|
|
|
|
//Removes loading item
|
|
|
|
|
|
if (isLoadingMore) |
|
|
|
|
|
parsedTopicSummaries.remove(parsedTopicSummaries.size() - 1); |
|
|
|
|
|
|
|
|
|
|
|
if (!latestPostsRows.select("td:contains(Sorry, no matches were found)").isEmpty() || |
|
|
if (!latestPostsRows.select("td:contains(Sorry, no matches were found)").isEmpty() || |
|
|
!latestPostsRows.select("td:contains(Δυστυχώς δεν βρέθηκε τίποτα)").isEmpty()){ |
|
|
!latestPostsRows.select("td:contains(Δυστυχώς δεν βρέθηκε τίποτα)").isEmpty()){ |
|
|
userHasPosts = false; |
|
|
userHasPosts = false; |
|
|
parsedTopicSummaries.add(null); |
|
|
fetchedParsedTopicSummaries.add(null); |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -228,7 +236,7 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap |
|
|
//style.css
|
|
|
//style.css
|
|
|
pPost = ("<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" + pPost); |
|
|
pPost = ("<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" + pPost); |
|
|
|
|
|
|
|
|
parsedTopicSummaries.add(new PostSummary(pTopicUrl, pTopicTitle, pDateTime, pPost)); |
|
|
fetchedParsedTopicSummaries.add(new PostSummary(pTopicUrl, pTopicTitle, pDateTime, pPost)); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return true; |
|
|
return true; |
|
|