From 66402e661be86823cb11bc544cd0af482c3ec8ad Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 27 Oct 2018 16:46:30 +0800 Subject: [PATCH] Close the navigation drawer after clicking the item in it. Fix some logic related to post parsing. --- .../infinityforreddit/MainActivity.java | 19 +++++++++-- .../infinityforreddit/ParsePost.java | 32 +++++++++++++------ .../infinityforreddit/PostData.java | 1 - .../PostRecyclerViewAdapter.java | 7 ++-- ...ubscribedSubredditRecyclerViewAdapter.java | 13 ++++++-- .../SubscribedUserRecyclerViewAdapter.java | 20 +++++++++--- .../ViewPostDetailActivity.java | 19 ++++------- 7 files changed, 76 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 309d7a0f..5f559aa1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -91,7 +91,7 @@ public class MainActivity extends AppCompatActivity { Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); - DrawerLayout drawer = findViewById(R.id.drawer_layout); + final DrawerLayout drawer = findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); @@ -142,8 +142,15 @@ public class MainActivity extends AppCompatActivity { glide.load(mBannerImageUrl).into(mBannerImageView); } - final SubscribedSubredditRecyclerViewAdapter subredditadapter = new SubscribedSubredditRecyclerViewAdapter(this); + final SubscribedSubredditRecyclerViewAdapter subredditadapter = new SubscribedSubredditRecyclerViewAdapter(this, + new SubscribedSubredditRecyclerViewAdapter.OnItemClickListener() { + @Override + public void onClick() { + drawer.closeDrawers(); + } + }); subscribedSubredditRecyclerView.setAdapter(subredditadapter); + mSubscribedSubredditViewModel = ViewModelProviders.of(this).get(SubscribedSubredditViewModel.class); mSubscribedSubredditViewModel.getAllSubscribedSubreddits().observe(this, new Observer>() { @Override @@ -158,7 +165,13 @@ public class MainActivity extends AppCompatActivity { } }); - final SubscribedUserRecyclerViewAdapter userAdapter = new SubscribedUserRecyclerViewAdapter(this); + final SubscribedUserRecyclerViewAdapter userAdapter = new SubscribedUserRecyclerViewAdapter(this, + new SubscribedUserRecyclerViewAdapter.OnItemClickListener() { + @Override + public void onClick() { + drawer.closeDrawers(); + } + }); subscribedUserRecyclerView.setAdapter(userAdapter); mSubscribedUserViewModel = ViewModelProviders.of(this).get(SubscribedUserViewModel.class); mSubscribedUserViewModel.getAllSubscribedUsers().observe(this, new Observer>() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java index 4330afe3..2f8a4091 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java @@ -200,17 +200,31 @@ class ParsePost { bestPostData.add(new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, title, url, url, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost)); } else { - //Link post - Log.i("link", Integer.toString(i)); - int postType = PostData.LINK_TYPE; - PostData linkPostData = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, - title, previewUrl, url, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost); - if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) { - linkPostData.setSelfText(""); + if (url.contains(permalink)) { + //Text post but with a preview + Log.i("text with image", Integer.toString(i)); + int postType = PostData.TEXT_TYPE; + PostData textWithImagePostData = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, + title, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost); + if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) { + textWithImagePostData.setSelfText(""); + } else { + textWithImagePostData.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim()); + } + bestPostData.add(textWithImagePostData); } else { - linkPostData.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim()); + //Link post + Log.i("link", Integer.toString(i)); + int postType = PostData.LINK_TYPE; + PostData linkPostData = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, + title, previewUrl, url, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost); + if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) { + linkPostData.setSelfText(""); + } else { + linkPostData.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim()); + } + bestPostData.add(linkPostData); } - bestPostData.add(linkPostData); } } } else { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostData.java index 289ca083..0ece0b00 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostData.java @@ -36,7 +36,6 @@ class PostData implements Parcelable { private boolean isCrosspost; private boolean isDashVideo; private boolean isDownloadableGifOrVideo; - private PostData crosspostParentPostData; PostData(String id, String fullName, String subredditNamePrefixed, String postTime, String title, String previewUrl, String permalink, int score, int postType, int voteType, int gilded, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 8a80821e..86b1254a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -69,8 +69,8 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter mSubscribedSubredditData; private RequestManager glide; + private OnItemClickListener mOnItemClickListener; - SubscribedSubredditRecyclerViewAdapter(Context context) { - mContext = context; - glide = Glide.with(context); + interface OnItemClickListener { + void onClick(); + } + + SubscribedSubredditRecyclerViewAdapter(Context context, OnItemClickListener onItemClickListener) { + mContext = context.getApplicationContext(); + glide = Glide.with(context.getApplicationContext()); + mOnItemClickListener = onItemClickListener; } @NonNull @@ -38,6 +44,7 @@ class SubscribedSubredditRecyclerViewAdapter extends RecyclerView.Adapter= 0) { + mOnItemClickListener.onClick(); Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getName()); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_VALUE_KEY, mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getId()); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRecyclerViewAdapter.java index cb779a2c..44089043 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRecyclerViewAdapter.java @@ -16,13 +16,17 @@ import java.util.List; import de.hdodenhof.circleimageview.CircleImageView; class SubscribedUserRecyclerViewAdapter extends RecyclerView.Adapter { - private Context mContext; private List mSubscribedUserData; private RequestManager glide; + private OnItemClickListener mOnItemClickListener; - SubscribedUserRecyclerViewAdapter(Context context) { - mContext = context; - glide = Glide.with(context); + interface OnItemClickListener { + void onClick(); + } + + SubscribedUserRecyclerViewAdapter(Context context, OnItemClickListener onItemClickListener) { + glide = Glide.with(context.getApplicationContext()); + mOnItemClickListener = onItemClickListener; } @NonNull @@ -33,6 +37,14 @@ class SubscribedUserRecyclerViewAdapter extends RecyclerView.Adapter