From 8111dc8ed925c9a522ece37190cf2990a1b86210 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Wed, 4 Jan 2017 14:52:04 +0200 Subject: [PATCH] LatestPostsFragment fixes and interaction listener --- .../mthmmy/activities/main/MainActivity.java | 8 ++--- .../activities/profile/ProfileActivity.java | 15 +++++++- .../latestPosts/LatestPostsAdapter.java | 36 ++++++++++++++----- .../latestPosts/LatestPostsFragment.java | 12 ++++--- .../activities/topic/TopicActivity.java | 10 +++--- .../profile_fragment_latest_posts_row.xml | 1 + 6 files changed, 60 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java index 9a1e756d..8fb76090 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java +++ b/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_URL; -import static gr.thmmy.mthmmy.activities.topic.TopicActivity.EXTRAS_TOPIC_TITLE; -import static gr.thmmy.mthmmy.activities.topic.TopicActivity.EXTRAS_TOPIC_URL; +import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_TITLE; +import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL; public class MainActivity extends BaseActivity implements RecentFragment.RecentFragmentInteractionListener, ForumFragment.ForumFragmentInteractionListener { @@ -88,8 +88,8 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF @Override public void onRecentFragmentInteraction(TopicSummary topicSummary) { Intent i = new Intent(MainActivity.this, TopicActivity.class); - i.putExtra(EXTRAS_TOPIC_URL, topicSummary.getTopicUrl()); - i.putExtra(EXTRAS_TOPIC_TITLE, topicSummary.getTitle()); + i.putExtra(BUNDLE_TOPIC_URL, topicSummary.getTopicUrl()); + i.putExtra(BUNDLE_TOPIC_TITLE, topicSummary.getTitle()); startActivity(i); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java index cf0f651e..beec6b08 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java +++ b/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.profile.latestPosts.LatestPostsFragment; 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 me.zhanghai.android.materialprogressbar.MaterialProgressBar; import mthmmy.utils.Report; import okhttp3.Request; 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 String * containing this user's profile url using the key {@link #BUNDLE_PROFILE_URL}, a String containing * this user's avatar url and a String containing the username. */ -public class ProfileActivity extends BaseActivity { +public class ProfileActivity extends BaseActivity implements LatestPostsFragment.LatestPostsFragmentInteractionListener{ //Graphics private TextView personalTextView; private MaterialProgressBar progressBar; @@ -159,6 +164,14 @@ public class ProfileActivity extends BaseActivity { 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 * user's personal text. diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsAdapter.java index 401298c2..273ed1b4 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsAdapter.java +++ b/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.ViewGroup; import android.webkit.WebView; -import android.webkit.WebViewClient; -import android.widget.ProgressBar; +import android.widget.RelativeLayout; import android.widget.TextView; import gr.thmmy.mthmmy.R; +import gr.thmmy.mthmmy.activities.base.BaseFragment; import gr.thmmy.mthmmy.data.TopicSummary; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; @@ -21,14 +21,19 @@ class LatestPostsAdapter extends RecyclerView.Adapter { private static final String TAG = "LatestPostsAdapter"; private final int VIEW_TYPE_ITEM = 0; 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(); } - public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) { - this.mOnLoadMoreListener = mOnLoadMoreListener; + void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) { + //this.mOnLoadMoreListener = mOnLoadMoreListener; } @Override @@ -43,7 +48,6 @@ class LatestPostsAdapter extends RecyclerView.Adapter { inflate(R.layout.profile_fragment_latest_posts_row, parent, false); return new LatestPostViewHolder(view); } else if (viewType == VIEW_TYPE_LOADING) { - Log.d(TAG, "GOT"); View view = LayoutInflater.from(parent.getContext()). inflate(R.layout.recycler_loading_item, parent, false); return new LoadingViewHolder(view); @@ -53,14 +57,28 @@ class LatestPostsAdapter extends RecyclerView.Adapter { @SuppressLint("SetJavaScriptEnabled") @Override - public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { + public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) { if (holder instanceof LatestPostViewHolder) { TopicSummary topic = parsedTopicSummaries.get(position); final LatestPostViewHolder latestPostViewHolder = (LatestPostViewHolder) holder; + latestPostViewHolder.postTitle.setText(topic.getTitle()); latestPostViewHolder.postDate.setText(topic.getDateTimeModified()); latestPostViewHolder.post.loadDataWithBaseURL("file:///android_asset/" , 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) { LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder; loadingViewHolder.progressBar.setIndeterminate(true); @@ -73,12 +91,14 @@ class LatestPostsAdapter extends RecyclerView.Adapter { } private static class LatestPostViewHolder extends RecyclerView.ViewHolder { + RelativeLayout latestPostsRow; TextView postTitle; TextView postDate; WebView post; LatestPostViewHolder(View itemView) { super(itemView); + latestPostsRow = (RelativeLayout) itemView.findViewById(R.id.latest_posts_row); postTitle = (TextView) itemView.findViewById(R.id.title); postDate = (TextView) itemView.findViewById(R.id.date); post = (WebView) itemView.findViewById(R.id.post); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java index 5cd81712..336ab0c9 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java +++ b/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.Bundle; -import android.support.v4.app.Fragment; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; @@ -24,6 +23,7 @@ import javax.net.ssl.SSLHandshakeException; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.activities.base.BaseActivity; +import gr.thmmy.mthmmy.activities.base.BaseFragment; import gr.thmmy.mthmmy.data.TopicSummary; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; 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. */ -public class LatestPostsFragment extends Fragment { +public class LatestPostsFragment extends BaseFragment { /** * Debug Tag for logging debug output to LogCat */ @@ -48,7 +48,7 @@ public class LatestPostsFragment extends Fragment { * are added in {@link LatestPostsFragment.ProfileLatestPostsTask}. */ static ArrayList parsedTopicSummaries; - private LatestPostsAdapter latestPostsAdapter = new LatestPostsAdapter(); + private LatestPostsAdapter latestPostsAdapter; private int numberOfPages = -1; private int pagesLoaded = 0; private String profileUrl; @@ -89,6 +89,7 @@ public class LatestPostsFragment extends Fragment { Bundle savedInstanceState) { 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); mainContent.setAdapter(latestPostsAdapter); final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); @@ -149,6 +150,10 @@ public class LatestPostsFragment extends Fragment { 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 * user's personal text. @@ -165,7 +170,6 @@ public class LatestPostsFragment extends Fragment { protected void onPreExecute() { if (!isLoadingMore) { - Log.d(TAG, "false"); progressBar.setVisibility(ProgressBar.VISIBLE); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java index 4da904ab..9db884f3 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java +++ b/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 String - * containing this topics's url using the key {@link #EXTRAS_TOPIC_URL} and a String containing - * this topic's title using the key {@link #EXTRAS_TOPIC_TITLE}. + * containing this topics's url using the key {@link #BUNDLE_TOPIC_URL} and a String containing + * this topic's title using the key {@link #BUNDLE_TOPIC_TITLE}. */ @SuppressWarnings("unchecked") 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. */ - 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. */ - public static final String EXTRAS_TOPIC_TITLE = "TOPIC_TITLE"; + public static final String BUNDLE_TOPIC_TITLE = "TOPIC_TITLE"; private static TopicTask topicTask; //About posts private List postsList; @@ -171,7 +171,7 @@ public class TopicActivity extends BaseActivity { //Gets posts topicTask = new TopicTask(); - topicTask.execute(extras.getString(EXTRAS_TOPIC_URL)); //Attempt data parsing + topicTask.execute(extras.getString(BUNDLE_TOPIC_URL)); //Attempt data parsing } @Override diff --git a/app/src/main/res/layout/profile_fragment_latest_posts_row.xml b/app/src/main/res/layout/profile_fragment_latest_posts_row.xml index 5623df43..82d5b60d 100644 --- a/app/src/main/res/layout/profile_fragment_latest_posts_row.xml +++ b/app/src/main/res/layout/profile_fragment_latest_posts_row.xml @@ -1,6 +1,7 @@