Browse Source

LatestPostsFragment fixes and interaction listener

pull/24/head
Apostolos Fanakis 8 years ago
parent
commit
8111dc8ed9
  1. 8
      app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java
  2. 15
      app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java
  3. 36
      app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsAdapter.java
  4. 12
      app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java
  5. 10
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  6. 1
      app/src/main/res/layout/profile_fragment_latest_posts_row.xml

8
app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java

@ -22,8 +22,8 @@ import gr.thmmy.mthmmy.data.TopicSummary;
import static gr.thmmy.mthmmy.activities.board.BoardActivity.EXTRAS_BOARD_TITLE; import static gr.thmmy.mthmmy.activities.board.BoardActivity.EXTRAS_BOARD_TITLE;
import static gr.thmmy.mthmmy.activities.board.BoardActivity.EXTRAS_BOARD_URL; import static gr.thmmy.mthmmy.activities.board.BoardActivity.EXTRAS_BOARD_URL;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.EXTRAS_TOPIC_TITLE; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_TITLE;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.EXTRAS_TOPIC_URL; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL;
public class MainActivity extends BaseActivity implements RecentFragment.RecentFragmentInteractionListener, ForumFragment.ForumFragmentInteractionListener { public class MainActivity extends BaseActivity implements RecentFragment.RecentFragmentInteractionListener, ForumFragment.ForumFragmentInteractionListener {
@ -88,8 +88,8 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF
@Override @Override
public void onRecentFragmentInteraction(TopicSummary topicSummary) { public void onRecentFragmentInteraction(TopicSummary topicSummary) {
Intent i = new Intent(MainActivity.this, TopicActivity.class); Intent i = new Intent(MainActivity.this, TopicActivity.class);
i.putExtra(EXTRAS_TOPIC_URL, topicSummary.getTopicUrl()); i.putExtra(BUNDLE_TOPIC_URL, topicSummary.getTopicUrl());
i.putExtra(EXTRAS_TOPIC_TITLE, topicSummary.getTitle()); i.putExtra(BUNDLE_TOPIC_TITLE, topicSummary.getTitle());
startActivity(i); startActivity(i);
} }

15
app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java

@ -35,18 +35,23 @@ import gr.thmmy.mthmmy.activities.LoginActivity;
import gr.thmmy.mthmmy.activities.base.BaseActivity; import gr.thmmy.mthmmy.activities.base.BaseActivity;
import gr.thmmy.mthmmy.activities.profile.latestPosts.LatestPostsFragment; import gr.thmmy.mthmmy.activities.profile.latestPosts.LatestPostsFragment;
import gr.thmmy.mthmmy.activities.profile.summary.SummaryFragment; import gr.thmmy.mthmmy.activities.profile.summary.SummaryFragment;
import gr.thmmy.mthmmy.activities.topic.TopicActivity;
import gr.thmmy.mthmmy.data.TopicSummary;
import gr.thmmy.mthmmy.utils.CircleTransform; import gr.thmmy.mthmmy.utils.CircleTransform;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import mthmmy.utils.Report; import mthmmy.utils.Report;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_TITLE;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL;
/** /**
* Activity for user profile. When creating an Intent of this activity you need to bundle a <b>String</b> * Activity for user profile. When creating an Intent of this activity you need to bundle a <b>String</b>
* containing this user's profile url using the key {@link #BUNDLE_PROFILE_URL}, a <b>String</b> containing * containing this user's profile url using the key {@link #BUNDLE_PROFILE_URL}, a <b>String</b> containing
* this user's avatar url and a <b>String</b> containing the username. * this user's avatar url and a <b>String</b> containing the username.
*/ */
public class ProfileActivity extends BaseActivity { public class ProfileActivity extends BaseActivity implements LatestPostsFragment.LatestPostsFragmentInteractionListener{
//Graphics //Graphics
private TextView personalTextView; private TextView personalTextView;
private MaterialProgressBar progressBar; private MaterialProgressBar progressBar;
@ -159,6 +164,14 @@ public class ProfileActivity extends BaseActivity {
profileTask.cancel(true); profileTask.cancel(true);
} }
@Override
public void onLatestPostsFragmentInteraction(TopicSummary topicSummary) {
Intent i = new Intent(ProfileActivity.this, TopicActivity.class);
i.putExtra(BUNDLE_TOPIC_URL, topicSummary.getTopicUrl());
i.putExtra(BUNDLE_TOPIC_TITLE, topicSummary.getTitle());
startActivity(i);
}
/** /**
* 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 personal text. * user's personal text.

36
app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsAdapter.java

@ -7,11 +7,11 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.webkit.WebView; import android.webkit.WebView;
import android.webkit.WebViewClient; import android.widget.RelativeLayout;
import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.base.BaseFragment;
import gr.thmmy.mthmmy.data.TopicSummary; import gr.thmmy.mthmmy.data.TopicSummary;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
@ -21,14 +21,19 @@ class LatestPostsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final String TAG = "LatestPostsAdapter"; private static final String TAG = "LatestPostsAdapter";
private final int VIEW_TYPE_ITEM = 0; private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1; private final int VIEW_TYPE_LOADING = 1;
private OnLoadMoreListener mOnLoadMoreListener; //private OnLoadMoreListener mOnLoadMoreListener;
private LatestPostsFragment.LatestPostsFragmentInteractionListener interactionListener;
public interface OnLoadMoreListener { LatestPostsAdapter(BaseFragment.FragmentInteractionListener interactionListener){
this.interactionListener = (LatestPostsFragment.LatestPostsFragmentInteractionListener) interactionListener;
}
interface OnLoadMoreListener {
void onLoadMore(); void onLoadMore();
} }
public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) { void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
this.mOnLoadMoreListener = mOnLoadMoreListener; //this.mOnLoadMoreListener = mOnLoadMoreListener;
} }
@Override @Override
@ -43,7 +48,6 @@ class LatestPostsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
inflate(R.layout.profile_fragment_latest_posts_row, parent, false); inflate(R.layout.profile_fragment_latest_posts_row, parent, false);
return new LatestPostViewHolder(view); return new LatestPostViewHolder(view);
} else if (viewType == VIEW_TYPE_LOADING) { } else if (viewType == VIEW_TYPE_LOADING) {
Log.d(TAG, "GOT");
View view = LayoutInflater.from(parent.getContext()). View view = LayoutInflater.from(parent.getContext()).
inflate(R.layout.recycler_loading_item, parent, false); inflate(R.layout.recycler_loading_item, parent, false);
return new LoadingViewHolder(view); return new LoadingViewHolder(view);
@ -53,14 +57,28 @@ class LatestPostsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
@SuppressLint("SetJavaScriptEnabled") @SuppressLint("SetJavaScriptEnabled")
@Override @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
if (holder instanceof LatestPostViewHolder) { if (holder instanceof LatestPostViewHolder) {
TopicSummary topic = parsedTopicSummaries.get(position); TopicSummary topic = parsedTopicSummaries.get(position);
final LatestPostViewHolder latestPostViewHolder = (LatestPostViewHolder) holder; final LatestPostViewHolder latestPostViewHolder = (LatestPostViewHolder) holder;
latestPostViewHolder.postTitle.setText(topic.getTitle()); latestPostViewHolder.postTitle.setText(topic.getTitle());
latestPostViewHolder.postDate.setText(topic.getDateTimeModified()); latestPostViewHolder.postDate.setText(topic.getDateTimeModified());
latestPostViewHolder.post.loadDataWithBaseURL("file:///android_asset/" latestPostViewHolder.post.loadDataWithBaseURL("file:///android_asset/"
, topic.getPost(), "text/html", "UTF-8", null); , topic.getPost(), "text/html", "UTF-8", null);
latestPostViewHolder.latestPostsRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (interactionListener != null) {
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that a post has been selected.
interactionListener.onLatestPostsFragmentInteraction(
parsedTopicSummaries.get(holder.getAdapterPosition()));
}
}
});
} else if (holder instanceof LoadingViewHolder) { } else if (holder instanceof LoadingViewHolder) {
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder; LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
loadingViewHolder.progressBar.setIndeterminate(true); loadingViewHolder.progressBar.setIndeterminate(true);
@ -73,12 +91,14 @@ class LatestPostsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
} }
private static class LatestPostViewHolder extends RecyclerView.ViewHolder { private static class LatestPostViewHolder extends RecyclerView.ViewHolder {
RelativeLayout latestPostsRow;
TextView postTitle; TextView postTitle;
TextView postDate; TextView postDate;
WebView post; WebView post;
LatestPostViewHolder(View itemView) { LatestPostViewHolder(View itemView) {
super(itemView); super(itemView);
latestPostsRow = (RelativeLayout) itemView.findViewById(R.id.latest_posts_row);
postTitle = (TextView) itemView.findViewById(R.id.title); postTitle = (TextView) itemView.findViewById(R.id.title);
postDate = (TextView) itemView.findViewById(R.id.date); postDate = (TextView) itemView.findViewById(R.id.date);
post = (WebView) itemView.findViewById(R.id.post); post = (WebView) itemView.findViewById(R.id.post);

12
app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java

@ -2,7 +2,6 @@ package gr.thmmy.mthmmy.activities.profile.latestPosts;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
@ -24,6 +23,7 @@ import javax.net.ssl.SSLHandshakeException;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.base.BaseActivity; import gr.thmmy.mthmmy.activities.base.BaseActivity;
import gr.thmmy.mthmmy.activities.base.BaseFragment;
import gr.thmmy.mthmmy.data.TopicSummary; import gr.thmmy.mthmmy.data.TopicSummary;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import mthmmy.utils.Report; import mthmmy.utils.Report;
@ -33,7 +33,7 @@ import okhttp3.Response;
/** /**
* 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 Fragment { public class LatestPostsFragment extends BaseFragment {
/** /**
* Debug Tag for logging debug output to LogCat * Debug Tag for logging debug output to LogCat
*/ */
@ -48,7 +48,7 @@ public class LatestPostsFragment extends Fragment {
* are added in {@link LatestPostsFragment.ProfileLatestPostsTask}. * are added in {@link LatestPostsFragment.ProfileLatestPostsTask}.
*/ */
static ArrayList<TopicSummary> parsedTopicSummaries; static ArrayList<TopicSummary> parsedTopicSummaries;
private LatestPostsAdapter latestPostsAdapter = new LatestPostsAdapter(); private LatestPostsAdapter latestPostsAdapter;
private int numberOfPages = -1; private int numberOfPages = -1;
private int pagesLoaded = 0; private int pagesLoaded = 0;
private String profileUrl; private String profileUrl;
@ -89,6 +89,7 @@ public class LatestPostsFragment extends Fragment {
Bundle savedInstanceState) { Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.profile_fragment_latest_posts, container, false); final View rootView = inflater.inflate(R.layout.profile_fragment_latest_posts, container, false);
latestPostsAdapter = new LatestPostsAdapter(fragmentInteractionListener);
RecyclerView mainContent = (RecyclerView) rootView.findViewById(R.id.profile_latest_posts_recycler); RecyclerView mainContent = (RecyclerView) rootView.findViewById(R.id.profile_latest_posts_recycler);
mainContent.setAdapter(latestPostsAdapter); mainContent.setAdapter(latestPostsAdapter);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
@ -149,6 +150,10 @@ public class LatestPostsFragment extends Fragment {
profileLatestPostsTask.cancel(true); profileLatestPostsTask.cancel(true);
} }
public interface LatestPostsFragmentInteractionListener extends FragmentInteractionListener {
void onLatestPostsFragmentInteraction(TopicSummary topicSummary);
}
/** /**
* 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 personal text. * user's personal text.
@ -165,7 +170,6 @@ public class LatestPostsFragment extends Fragment {
protected void onPreExecute() { protected void onPreExecute() {
if (!isLoadingMore) { if (!isLoadingMore) {
Log.d(TAG, "false");
progressBar.setVisibility(ProgressBar.VISIBLE); progressBar.setVisibility(ProgressBar.VISIBLE);
} }
} }

10
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java

@ -37,8 +37,8 @@ import okhttp3.Response;
/** /**
* Activity for topics. When creating an Intent of this activity you need to bundle a <b>String</b> * Activity for topics. When creating an Intent of this activity you need to bundle a <b>String</b>
* containing this topics's url using the key {@link #EXTRAS_TOPIC_URL} and a <b>String</b> containing * containing this topics's url using the key {@link #BUNDLE_TOPIC_URL} and a <b>String</b> containing
* this topic's title using the key {@link #EXTRAS_TOPIC_TITLE}. * this topic's title using the key {@link #BUNDLE_TOPIC_TITLE}.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public class TopicActivity extends BaseActivity { public class TopicActivity extends BaseActivity {
@ -51,11 +51,11 @@ public class TopicActivity extends BaseActivity {
/** /**
* The key to use when putting topic's url String to {@link TopicActivity}'s Bundle. * The key to use when putting topic's url String to {@link TopicActivity}'s Bundle.
*/ */
public static final String EXTRAS_TOPIC_URL = "TOPIC_URL"; public static final String BUNDLE_TOPIC_URL = "TOPIC_URL";
/** /**
* The key to use when putting topic's title String to {@link TopicActivity}'s Bundle. * The key to use when putting topic's title String to {@link TopicActivity}'s Bundle.
*/ */
public static final String EXTRAS_TOPIC_TITLE = "TOPIC_TITLE"; public static final String BUNDLE_TOPIC_TITLE = "TOPIC_TITLE";
private static TopicTask topicTask; private static TopicTask topicTask;
//About posts //About posts
private List<Post> postsList; private List<Post> postsList;
@ -171,7 +171,7 @@ public class TopicActivity extends BaseActivity {
//Gets posts //Gets posts
topicTask = new TopicTask(); topicTask = new TopicTask();
topicTask.execute(extras.getString(EXTRAS_TOPIC_URL)); //Attempt data parsing topicTask.execute(extras.getString(BUNDLE_TOPIC_URL)); //Attempt data parsing
} }
@Override @Override

1
app/src/main/res/layout/profile_fragment_latest_posts_row.xml

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/latest_posts_row"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@color/card_background" android:background="@color/card_background"

Loading…
Cancel
Save