From cd4730c6492eae869e74182383c35be16c70ae25 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 18 Apr 2019 23:31:41 +0800 Subject: Synchronizing voting state between PostRecyclerViewAdapter and ViewPostDetailActivity using EventBus by greenrobot. Minor bugs fixed. --- .../CommentMultiLevelRecyclerViewAdapter.java | 11 ++-- .../infinityforreddit/ParsePost.java | 2 + .../infinityforreddit/PostFragment.java | 33 +++++++++-- .../infinityforreddit/PostRecyclerViewAdapter.java | 32 +++++++--- .../infinityforreddit/ViewPostDetailActivity.java | 68 ++++++++++++++++++---- .../VoteEventToDetailActivity.java | 11 ++++ .../infinityforreddit/VoteEventToPostList.java | 11 ++++ 7 files changed, 139 insertions(+), 29 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/VoteEventToDetailActivity.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/VoteEventToPostList.java (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java index a649ed15..a8928495 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java @@ -13,6 +13,11 @@ import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; +import androidx.annotation.NonNull; +import androidx.browser.customtabs.CustomTabsIntent; +import androidx.core.content.ContextCompat; +import androidx.recyclerview.widget.RecyclerView; + import com.multilevelview.MultiLevelAdapter; import com.multilevelview.MultiLevelRecyclerView; import com.multilevelview.models.RecyclerViewItem; @@ -21,10 +26,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; -import androidx.annotation.NonNull; -import androidx.browser.customtabs.CustomTabsIntent; -import androidx.core.content.ContextCompat; -import androidx.recyclerview.widget.RecyclerView; import butterknife.BindView; import butterknife.ButterKnife; import retrofit2.Retrofit; @@ -184,6 +185,7 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { @Override public void onVoteThingFail(int position1) { Toast.makeText(mContext, R.string.vote_failed, Toast.LENGTH_SHORT).show(); + commentItem.setVoteType(previousVoteType); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + previousVoteType)); ((CommentViewHolder) holder).upvoteButton.setColorFilter(previousUpvoteButtonColorFilter); ((CommentViewHolder) holder).downvoteButton.setColorFilter(previousDownvoteButtonColorFilter); @@ -233,6 +235,7 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { @Override public void onVoteThingFail(int position1) { Toast.makeText(mContext, R.string.vote_failed, Toast.LENGTH_SHORT).show(); + commentItem.setVoteType(previousVoteType); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + previousVoteType)); ((CommentViewHolder) holder).upvoteButton.setColorFilter(previousUpvoteButtonColorFilter); ((CommentViewHolder) holder).downvoteButton.setColorFilter(previousDownvoteButtonColorFilter); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java index 4e0b574b..f3d92f70 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java @@ -81,7 +81,9 @@ class ParsePost { voteType = 0; } else { voteType = data.getBoolean(JSONUtils.LIKES_KEY) ? 1 : -1; + score -= voteType; } + Calendar postTimeCalendar = Calendar.getInstance(); postTimeCalendar.setTimeInMillis(postTime); String formattedPostTime = new SimpleDateFormat("MMM d, YYYY, HH:mm", diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 493eb616..de03f164 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -1,15 +1,9 @@ package ml.docilealligator.infinityforreddit; -import androidx.lifecycle.ViewModelProviders; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.coordinatorlayout.widget.CoordinatorLayout; -import androidx.fragment.app.Fragment; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -17,9 +11,19 @@ import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProviders; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + import com.bumptech.glide.Glide; import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + import javax.inject.Inject; import javax.inject.Named; @@ -83,6 +87,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { ButterKnife.bind(this, rootView); + EventBus.getDefault().register(this); + mLinearLayoutManager = new LinearLayoutManager(getActivity()); mPostRecyclerView.setLayoutManager(mLinearLayoutManager); /*FloatingActionButton fab = rootView.findViewById(R.id.fab_post_fragment); @@ -180,4 +186,19 @@ public class PostFragment extends Fragment implements FragmentCommunicator { Glide.with(this).load(R.drawable.load_post_error_indicator).into(mFetchPostInfoImageView); } } + + @Subscribe + public void onVoteEvent(VoteEventToPostList event) { + Post post = mAdapter.getCurrentList().get(event.positionInList); + if(post != null) { + post.setVoteType(event.voteType); + mAdapter.notifyItemChanged(event.positionInList); + } + } + + @Override + public void onDestroy() { + EventBus.getDefault().unregister(this); + super.onDestroy(); + } } \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 9aec077f..5b2fecb7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -18,6 +18,15 @@ import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.browser.customtabs.CustomTabsIntent; +import androidx.cardview.widget.CardView; +import androidx.core.content.ContextCompat; +import androidx.paging.PagedListAdapter; +import androidx.recyclerview.widget.DiffUtil; +import androidx.recyclerview.widget.RecyclerView; + import com.bumptech.glide.Glide; import com.bumptech.glide.RequestBuilder; import com.bumptech.glide.RequestManager; @@ -29,19 +38,13 @@ import com.bumptech.glide.request.target.Target; import com.google.android.material.card.MaterialCardView; import com.google.android.material.chip.Chip; +import org.greenrobot.eventbus.EventBus; + import CustomView.AspectRatioGifImageView; import SubredditDatabase.SubredditDao; import SubredditDatabase.SubredditRoomDatabase; import User.UserDao; import User.UserRoomDatabase; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.browser.customtabs.CustomTabsIntent; -import androidx.cardview.widget.CardView; -import androidx.core.content.ContextCompat; -import androidx.paging.PagedListAdapter; -import androidx.recyclerview.widget.DiffUtil; -import androidx.recyclerview.widget.RecyclerView; import butterknife.BindView; import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.BlurTransformation; @@ -144,6 +147,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter 0) { ((DataViewHolder) holder).gildedImageView.setVisibility(View.VISIBLE); @@ -414,14 +418,19 @@ class PostRecyclerViewAdapter extends PagedListAdapter children; @@ -121,8 +127,11 @@ public class ViewPostDetailActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_post_detail); + ButterKnife.bind(this); + EventBus.getDefault().register(this); + ((Infinity) getApplication()).getmNetworkComponent().inject(this); getSupportActionBar().setDisplayHomeAsUpEnabled(true); @@ -131,6 +140,10 @@ public class ViewPostDetailActivity extends AppCompatActivity { mPost = getIntent().getExtras().getParcelable(EXTRA_POST_DATA); + if(getIntent().hasExtra(EXTRA_POST_LIST_POSITION)) { + postListPosition = getIntent().getExtras().getInt(EXTRA_POST_LIST_POSITION); + } + TextView titleTextView = findViewById(R.id.title_text_view_view_post_detail); titleTextView.setText(mPost.getTitle()); @@ -214,7 +227,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { if(mPost.isNSFW()) { mNSFWChip.setVisibility(View.VISIBLE); } - mScoreTextView.setText(Integer.toString(mPost.getScore())); + mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType())); mShareButton.setOnClickListener(view -> { Intent intent = new Intent(Intent.ACTION_SEND); @@ -333,6 +346,10 @@ public class ViewPostDetailActivity extends AppCompatActivity { mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType())); + if(postListPosition != -1) { + EventBus.getDefault().post(new VoteEventToPostList(postListPosition, mPost.getVoteType())); + } + VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() { @Override public void onVoteThingSuccess() { @@ -346,14 +363,23 @@ public class ViewPostDetailActivity extends AppCompatActivity { mDownvoteButton.clearColorFilter(); mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType())); + + if(postListPosition != -1) { + EventBus.getDefault().post(new VoteEventToPostList(postListPosition, mPost.getVoteType())); + } } @Override public void onVoteThingFail() { Toast.makeText(ViewPostDetailActivity.this, R.string.vote_failed, Toast.LENGTH_SHORT).show(); + mPost.setVoteType(previousVoteType); mScoreTextView.setText(Integer.toString(mPost.getScore() + previousVoteType)); mUpvoteButton.setColorFilter(previousUpvoteButtonColorFilter); mDownvoteButton.setColorFilter(previousDownvoteButtonColorFilter); + + if(postListPosition != -1) { + EventBus.getDefault().post(new VoteEventToPostList(postListPosition, mPost.getVoteType())); + } } }, mPost.getFullName(), newVoteType); }); @@ -380,6 +406,10 @@ public class ViewPostDetailActivity extends AppCompatActivity { mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType())); + if(postListPosition != -1) { + EventBus.getDefault().post(new VoteEventToPostList(postListPosition, mPost.getVoteType())); + } + VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() { @Override public void onVoteThingSuccess() { @@ -393,14 +423,23 @@ public class ViewPostDetailActivity extends AppCompatActivity { mUpvoteButton.clearColorFilter(); mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType())); + + if(postListPosition != -1) { + EventBus.getDefault().post(new VoteEventToPostList(postListPosition, mPost.getVoteType())); + } } @Override public void onVoteThingFail() { Toast.makeText(ViewPostDetailActivity.this, R.string.vote_failed, Toast.LENGTH_SHORT).show(); + mPost.setVoteType(previousVoteType); mScoreTextView.setText(Integer.toString(mPost.getScore() + previousVoteType)); mUpvoteButton.setColorFilter(previousUpvoteButtonColorFilter); mDownvoteButton.setColorFilter(previousDownvoteButtonColorFilter); + + if(postListPosition != -1) { + EventBus.getDefault().post(new VoteEventToPostList(postListPosition, mPost.getVoteType())); + } } }, mPost.getFullName(), newVoteType); }); @@ -535,6 +574,14 @@ public class ViewPostDetailActivity extends AppCompatActivity { snackbar.show(); } + @Subscribe + public void onVoteEvent(VoteEventToDetailActivity event) { + if(mPost.getId() == event.postId) { + mPost.setVoteType(event.voteType); + mScoreTextView.setText(Integer.toString(mPost.getScore() + event.voteType)); + } + } + @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { @@ -568,6 +615,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { @Override protected void onDestroy() { + EventBus.getDefault().unregister(this); super.onDestroy(); if(mLoadSubredditIconAsyncTask != null) { mLoadSubredditIconAsyncTask.cancel(true); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/VoteEventToDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/VoteEventToDetailActivity.java new file mode 100644 index 00000000..d3e80ba4 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/VoteEventToDetailActivity.java @@ -0,0 +1,11 @@ +package ml.docilealligator.infinityforreddit; + +public class VoteEventToDetailActivity { + public final String postId; + public final int voteType; + + public VoteEventToDetailActivity(String postId, int voteType) { + this.postId = postId; + this.voteType = voteType; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/VoteEventToPostList.java b/app/src/main/java/ml/docilealligator/infinityforreddit/VoteEventToPostList.java new file mode 100644 index 00000000..5de4d799 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/VoteEventToPostList.java @@ -0,0 +1,11 @@ +package ml.docilealligator.infinityforreddit; + +public class VoteEventToPostList { + public final int positionInList; + public final int voteType; + + public VoteEventToPostList(int positionInList, int voteType) { + this.positionInList = positionInList; + this.voteType = voteType; + } +} -- cgit v1.2.3