|
|
@ -5,7 +5,6 @@ import android.os.Bundle; |
|
|
|
import android.view.LayoutInflater; |
|
|
|
import android.view.View; |
|
|
|
import android.view.ViewGroup; |
|
|
|
import android.widget.ProgressBar; |
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
|
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.utils.parsing.ParseException; |
|
|
|
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; |
|
|
|
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; |
|
|
|
import okhttp3.Request; |
|
|
|
import okhttp3.Response; |
|
|
|
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. |
|
|
|
*/ |
|
|
|
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. |
|
|
|
*/ |
|
|
@ -50,12 +48,13 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap |
|
|
|
private int pagesLoaded = 0; |
|
|
|
private String profileUrl; |
|
|
|
private LatestPostsTask profileLatestPostsTask; |
|
|
|
private MaterialProgressBar progressBar; |
|
|
|
private boolean isLoadingMore; |
|
|
|
private boolean userHasPosts = true; |
|
|
|
private static final int visibleThreshold = 5; |
|
|
|
private int lastVisibleItem, totalItemCount; |
|
|
|
|
|
|
|
private OnLoadingListener onLoadingListener; |
|
|
|
|
|
|
|
public LatestPostsFragment() { |
|
|
|
// Required empty public constructor
|
|
|
|
} |
|
|
@ -87,16 +86,16 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap |
|
|
|
Bundle savedInstanceState) { |
|
|
|
final View rootView = inflater.inflate(R.layout.fragment_profile_latest_posts, container, false); |
|
|
|
latestPostsAdapter = new LatestPostsAdapter(this.getContext(), fragmentInteractionListener, parsedTopicSummaries); |
|
|
|
RecyclerView mainContent = rootView.findViewById(R.id.profile_latest_posts_recycler); |
|
|
|
mainContent.setAdapter(latestPostsAdapter); |
|
|
|
RecyclerView recyclerView = rootView.findViewById(R.id.profile_latest_posts_recycler); |
|
|
|
recyclerView.setAdapter(latestPostsAdapter); |
|
|
|
final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); |
|
|
|
mainContent.setLayoutManager(layoutManager); |
|
|
|
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mainContent.getContext(), |
|
|
|
recyclerView.setLayoutManager(layoutManager); |
|
|
|
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), |
|
|
|
layoutManager.getOrientation()); |
|
|
|
mainContent.addItemDecoration(dividerItemDecoration); |
|
|
|
recyclerView.addItemDecoration(dividerItemDecoration); |
|
|
|
recyclerView.setItemViewCacheSize(15); |
|
|
|
|
|
|
|
//latestPostsAdapter.setOnLoadMoreListener();
|
|
|
|
mainContent.addOnScrollListener(new RecyclerView.OnScrollListener() { |
|
|
|
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { |
|
|
|
@Override |
|
|
|
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int 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; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onLoadMore() { |
|
|
|
if (pagesLoaded < numberOfPages) { |
|
|
|
parsedTopicSummaries.add(null); |
|
|
|
latestPostsAdapter.notifyItemInserted(parsedTopicSummaries.size() - 1); |
|
|
|
|
|
|
|
//Load data
|
|
|
|
profileLatestPostsTask = new LatestPostsTask(); |
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
|
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 |
|
|
|
* user's latest posts. |
|
|
@ -155,8 +157,16 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap |
|
|
|
* parameter!</p> |
|
|
|
*/ |
|
|
|
private class LatestPostsTask extends AsyncTask<String, Void, Boolean> { |
|
|
|
private ArrayList<PostSummary> fetchedParsedTopicSummaries; |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void onPreExecute() { |
|
|
|
if (!isLoadingMore) progressBar.setVisibility(ProgressBar.VISIBLE); |
|
|
|
onLoadingListener.onLoadingLatestPosts(true); |
|
|
|
} |
|
|
|
|
|
|
|
public LatestPostsTask() { |
|
|
|
super(); |
|
|
|
fetchedParsedTopicSummaries = new ArrayList<>(); |
|
|
|
} |
|
|
|
|
|
|
|
protected Boolean doInBackground(String... profileUrl) { |
|
|
@ -177,10 +187,12 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap |
|
|
|
protected void onPostExecute(Boolean 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)
|
|
|
|
|
|
|
|
progressBar.setVisibility(ProgressBar.INVISIBLE); |
|
|
|
int prevSize = parsedTopicSummaries.size(); |
|
|
|
parsedTopicSummaries.addAll(fetchedParsedTopicSummaries); |
|
|
|
latestPostsAdapter.notifyItemRangeInserted(prevSize, parsedTopicSummaries.size() - prevSize); |
|
|
|
latestPostsAdapter.notifyDataSetChanged(); |
|
|
|
isLoadingMore = false; |
|
|
|
onLoadingListener.onLoadingLatestPosts(false); |
|
|
|
} |
|
|
|
|
|
|
|
//TODO: better parse error handling (ParseException etc.)
|
|
|
@ -192,14 +204,10 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap |
|
|
|
latestPostsRows = latestPostsPage. |
|
|
|
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() || |
|
|
|
!latestPostsRows.select("td:contains(Δυστυχώς δεν βρέθηκε τίποτα)").isEmpty()){ |
|
|
|
userHasPosts = false; |
|
|
|
parsedTopicSummaries.add(null); |
|
|
|
fetchedParsedTopicSummaries.add(null); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
@ -228,7 +236,7 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap |
|
|
|
//style.css
|
|
|
|
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; |
|
|
|