From 36d24523b68e4ebecab02e7afff044b1625b80c9 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 10 Jul 2019 20:46:54 +0800 Subject: Select subreddit in PostTextActivity. --- .../FollowedUsersListingFragment.java | 2 +- .../FollowedUsersRecyclerViewAdapter.java | 89 ++++++++++++++++++++++ .../infinityforreddit/PostTextActivity.java | 29 ++++++- .../SubredditSelectionActivity.java | 28 ++++--- .../SubscribedSubredditsListingFragment.java | 14 +++- .../SubscribedSubredditsRecyclerViewAdapter.java | 21 ++++- .../SubscribedThingListingActivity.java | 11 +++ .../SubscribedUserRecyclerViewAdapter.java | 89 ---------------------- 8 files changed, 177 insertions(+), 106 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersRecyclerViewAdapter.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRecyclerViewAdapter.java (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java index 9e64ae04..3db27463 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java @@ -54,7 +54,7 @@ public class FollowedUsersListingFragment extends Fragment { mGlide = Glide.with(this); mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity)); - SubscribedUserRecyclerViewAdapter adapter = new SubscribedUserRecyclerViewAdapter(mActivity); + FollowedUsersRecyclerViewAdapter adapter = new FollowedUsersRecyclerViewAdapter(mActivity); mRecyclerView.setAdapter(adapter); mSubscribedUserViewModel = ViewModelProviders.of(this).get(SubscribedUserViewModel.class); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersRecyclerViewAdapter.java new file mode 100644 index 00000000..6f83527b --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersRecyclerViewAdapter.java @@ -0,0 +1,89 @@ +package ml.docilealligator.infinityforreddit; + +import android.content.Context; +import android.content.Intent; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestManager; +import com.bumptech.glide.request.RequestOptions; + +import java.util.List; + +import SubscribedUserDatabase.SubscribedUserData; +import jp.wasabeef.glide.transformations.RoundedCornersTransformation; +import pl.droidsonroids.gif.GifImageView; + +public class FollowedUsersRecyclerViewAdapter extends RecyclerView.Adapter { + private List mSubscribedUserData; + private Context mContext; + private RequestManager glide; + + FollowedUsersRecyclerViewAdapter(Context context) { + mContext = context; + glide = Glide.with(context.getApplicationContext()); + } + + @NonNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { + return new UserViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_subscribed_thing, viewGroup, false)); + } + + @Override + public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, int i) { + viewHolder.itemView.setOnClickListener(view -> { + Intent intent = new Intent(mContext, ViewUserDetailActivity.class); + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mSubscribedUserData.get(viewHolder.getAdapterPosition()).getName()); + mContext.startActivity(intent); + }); + if(!mSubscribedUserData.get(i).getIconUrl().equals("")) { + glide.load(mSubscribedUserData.get(i).getIconUrl()) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .error(glide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) + .into(((UserViewHolder) viewHolder).iconGifImageView); + } else { + glide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(((UserViewHolder) viewHolder).iconGifImageView); + } + ((UserViewHolder) viewHolder).subredditNameTextView.setText(mSubscribedUserData.get(i).getName()); + } + + @Override + public int getItemCount() { + if(mSubscribedUserData != null) { + return mSubscribedUserData.size(); + } + return 0; + } + + @Override + public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { + glide.clear(((UserViewHolder) holder).iconGifImageView); + } + + void setSubscribedUsers(List subscribedUsers){ + mSubscribedUserData = subscribedUsers; + notifyDataSetChanged(); + } + + + private class UserViewHolder extends RecyclerView.ViewHolder { + private final GifImageView iconGifImageView; + private final TextView subredditNameTextView; + + public UserViewHolder(View itemView) { + super(itemView); + iconGifImageView = itemView.findViewById(R.id.subreddit_icon_gif_image_view_item_subscribed_subreddit); + subredditNameTextView = itemView.findViewById(R.id.subreddit_name_text_view_item_subscribed_subreddit); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java index d499dae4..26ece355 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java @@ -9,6 +9,7 @@ import android.widget.EditText; import android.widget.TextView; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; @@ -25,6 +26,8 @@ public class PostTextActivity extends AppCompatActivity { static final String EXTRA_SUBREDDIT_NAME = "ESN"; + private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; + @BindView(R.id.subreddit_icon_gif_image_view_post_text_activity) GifImageView iconGifImageView; @BindView(R.id.subreddit_name_text_view_post_text_activity) TextView subreditNameTextView; @BindView(R.id.rules_button_post_text_activity) Button rulesButton; @@ -56,7 +59,7 @@ public class PostTextActivity extends AppCompatActivity { subreditNameTextView.setOnClickListener(view -> { Intent intent = new Intent(this, SubredditSelectionActivity.class); - startActivity(intent); + startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); }); } @@ -70,4 +73,28 @@ public class PostTextActivity extends AppCompatActivity { return false; } + + @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if(requestCode == SUBREDDIT_SELECTION_REQUEST_CODE) { + if(resultCode == RESULT_OK) { + subreditNameTextView.setTextColor(getResources().getColor(R.color.primaryTextColor)); + subreditNameTextView.setText(data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_NAME_KEY)); + + String iconUrl = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY); + if(!iconUrl.equals("")) { + mGlide.load(iconUrl) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .error(mGlide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) + .into(iconGifImageView); + } else { + mGlide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(iconGifImageView); + } + } + } + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java index 25569eb5..3d565f3c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java @@ -1,5 +1,7 @@ package ml.docilealligator.infinityforreddit; +import android.app.Activity; +import android.content.Intent; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.MenuItem; @@ -7,17 +9,14 @@ import android.view.MenuItem; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; -import androidx.lifecycle.ViewModelProviders; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; import SubscribedSubredditDatabase.SubscribedSubredditViewModel; -import butterknife.BindView; import butterknife.ButterKnife; public class SubredditSelectionActivity extends AppCompatActivity { - @BindView(R.id.recycler_view_subreddit_selection_activity) RecyclerView recyclerView; + static final String EXTRA_RETURN_SUBREDDIT_NAME_KEY = "ERSNK"; + static final String EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY = "ERSIUK"; private SubscribedSubredditViewModel mSubscribedSubredditViewModel; @@ -32,12 +31,11 @@ public class SubredditSelectionActivity extends AppCompatActivity { Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp); actionBar.setHomeAsUpIndicator(upArrow); - recyclerView.setLayoutManager(new LinearLayoutManager(this)); - SubscribedSubredditsRecyclerViewAdapter adapter = new SubscribedSubredditsRecyclerViewAdapter(this); - recyclerView.setAdapter(adapter); - - mSubscribedSubredditViewModel = ViewModelProviders.of(this).get(SubscribedSubredditViewModel.class); - mSubscribedSubredditViewModel.getAllSubscribedSubreddits().observe(this, adapter::setSubscribedSubreddits); + SubscribedSubredditsListingFragment fragment = new SubscribedSubredditsListingFragment(); + Bundle bundle = new Bundle(); + bundle.putInt(PostFragment.POST_TYPE_KEY, PostDataSource.TYPE_FRONT_PAGE); + fragment.setArguments(bundle); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, fragment).commit(); } @Override @@ -50,4 +48,12 @@ public class SubredditSelectionActivity extends AppCompatActivity { return false; } + + void getSelectedSubreddit(String name, String iconUrl) { + Intent returnIntent = new Intent(); + returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_NAME_KEY, name); + returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY, iconUrl); + setResult(Activity.RESULT_OK, returnIntent); + finish(); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java index 6dd8d4fa..476782cb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java @@ -27,6 +27,8 @@ import butterknife.ButterKnife; */ public class SubscribedSubredditsListingFragment extends Fragment { + static final String EXTRA_IS_SUBREDDIT_SELECTION = "EISSK"; + @BindView(R.id.recycler_view_subscribed_subreddits_listing_fragment) RecyclerView mRecyclerView; @BindView(R.id.no_subscriptions_linear_layout_subreddits_listing_fragment) LinearLayout mLinearLayout; @BindView(R.id.no_subscriptions_image_view_subreddits_listing_fragment) ImageView mImageView; @@ -55,7 +57,17 @@ public class SubscribedSubredditsListingFragment extends Fragment { mGlide = Glide.with(this); mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity)); - SubscribedSubredditsRecyclerViewAdapter adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity); + + SubscribedSubredditsRecyclerViewAdapter adapter; + if(getArguments().getBoolean(EXTRA_IS_SUBREDDIT_SELECTION)) { + adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity); + } else { + adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity, (name, iconUrl) -> { + ((SubredditSelectionActivity) mActivity).getSelectedSubreddit(name, iconUrl); + }); + } + + mRecyclerView.setAdapter(adapter); mSubscribedSubredditViewModel = ViewModelProviders.of(this).get(SubscribedSubredditViewModel.class); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java index f3bdb63b..d9f37e73 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java @@ -24,12 +24,23 @@ class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapter mSubscribedSubredditData; private RequestManager glide; + private ItemClickListener itemClickListener; + + interface ItemClickListener { + void onClick(String name, String iconUrl); + } SubscribedSubredditsRecyclerViewAdapter(Context context) { mContext = context; glide = Glide.with(context.getApplicationContext()); } + SubscribedSubredditsRecyclerViewAdapter(Context context, ItemClickListener itemClickListener) { + mContext = context; + glide = Glide.with(context.getApplicationContext()); + this.itemClickListener = itemClickListener; + } + @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { @@ -40,9 +51,13 @@ class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapter { if(viewHolder.getAdapterPosition() >= 0) { - Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class); - intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getName()); - mContext.startActivity(intent); + if(itemClickListener != null) { + itemClickListener.onClick(mSubscribedSubredditData.get(i).getName(), mSubscribedSubredditData.get(i).getIconUrl()); + } else { + Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class); + intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getName()); + mContext.startActivity(intent); + } } }); if(!mSubscribedSubredditData.get(i).getIconUrl().equals("")) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java index 1ae1327e..ce3a8b72 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java @@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit; import android.content.SharedPreferences; import android.os.Bundle; +import android.view.MenuItem; import android.view.ViewGroup; import androidx.annotation.NonNull; @@ -73,6 +74,16 @@ public class SubscribedThingListingActivity extends AppCompatActivity { loadSubscriptions(); } + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + if(item.getItemId() == android.R.id.home) { + finish(); + return true; + } + + return false; + } + @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRecyclerViewAdapter.java deleted file mode 100644 index 68c040b0..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRecyclerViewAdapter.java +++ /dev/null @@ -1,89 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.content.Context; -import android.content.Intent; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.recyclerview.widget.RecyclerView; - -import com.bumptech.glide.Glide; -import com.bumptech.glide.RequestManager; -import com.bumptech.glide.request.RequestOptions; - -import java.util.List; - -import SubscribedUserDatabase.SubscribedUserData; -import jp.wasabeef.glide.transformations.RoundedCornersTransformation; -import pl.droidsonroids.gif.GifImageView; - -public class SubscribedUserRecyclerViewAdapter extends RecyclerView.Adapter { - private List mSubscribedUserData; - private Context mContext; - private RequestManager glide; - - SubscribedUserRecyclerViewAdapter(Context context) { - mContext = context; - glide = Glide.with(context.getApplicationContext()); - } - - @NonNull - @Override - public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { - return new UserViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_subscribed_thing, viewGroup, false)); - } - - @Override - public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, int i) { - viewHolder.itemView.setOnClickListener(view -> { - Intent intent = new Intent(mContext, ViewUserDetailActivity.class); - intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mSubscribedUserData.get(viewHolder.getAdapterPosition()).getName()); - mContext.startActivity(intent); - }); - if(!mSubscribedUserData.get(i).getIconUrl().equals("")) { - glide.load(mSubscribedUserData.get(i).getIconUrl()) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) - .error(glide.load(R.drawable.subreddit_default_icon) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) - .into(((UserViewHolder) viewHolder).iconGifImageView); - } else { - glide.load(R.drawable.subreddit_default_icon) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) - .into(((UserViewHolder) viewHolder).iconGifImageView); - } - ((UserViewHolder) viewHolder).subredditNameTextView.setText(mSubscribedUserData.get(i).getName()); - } - - @Override - public int getItemCount() { - if(mSubscribedUserData != null) { - return mSubscribedUserData.size(); - } - return 0; - } - - @Override - public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { - glide.clear(((UserViewHolder) holder).iconGifImageView); - } - - void setSubscribedUsers(List subscribedUsers){ - mSubscribedUserData = subscribedUsers; - notifyDataSetChanged(); - } - - - private class UserViewHolder extends RecyclerView.ViewHolder { - private final GifImageView iconGifImageView; - private final TextView subredditNameTextView; - - public UserViewHolder(View itemView) { - super(itemView); - iconGifImageView = itemView.findViewById(R.id.subreddit_icon_gif_image_view_item_subscribed_subreddit); - subredditNameTextView = itemView.findViewById(R.id.subreddit_name_text_view_item_subscribed_subreddit); - } - } -} -- cgit v1.2.3