diff options
Diffstat (limited to 'app/src/main')
7 files changed, 139 insertions, 29 deletions
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<Post, RecyclerView.ViewHo Intent intent = new Intent(mContext, ViewPostDetailActivity.class); intent.putExtra(ViewPostDetailActivity.EXTRA_TITLE, title); intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, post); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_LIST_POSITION, position); mContext.startActivity(intent); } }); @@ -249,7 +253,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo ((DataViewHolder) holder).postTimeTextView.setText(postTime); ((DataViewHolder) holder).titleTextView.setText(title); - ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore())); + ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType())); if(gilded > 0) { ((DataViewHolder) holder).gildedImageView.setVisibility(View.VISIBLE); @@ -414,14 +418,19 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo ((DataViewHolder) holder).downvoteButton.clearColorFilter(); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType())); + + EventBus.getDefault().post(new VoteEventToDetailActivity(post.getId(), post.getVoteType())); } @Override public void onVoteThingFail(int position1) { Toast.makeText(mContext, R.string.vote_failed, Toast.LENGTH_SHORT).show(); + post.setVoteType(previousVoteType); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + previousVoteType)); ((DataViewHolder) holder).upvoteButton.setColorFilter(previousUpvoteButtonColorFilter); ((DataViewHolder) holder).downvoteButton.setColorFilter(previousDownvoteButtonColorFilter); + + EventBus.getDefault().post(new VoteEventToDetailActivity(post.getId(), post.getVoteType())); } }, id, newVoteType, holder.getAdapterPosition()); }); @@ -464,14 +473,19 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo ((DataViewHolder) holder).upvoteButton.clearColorFilter(); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType())); + + EventBus.getDefault().post(new VoteEventToDetailActivity(post.getId(), post.getVoteType())); } @Override public void onVoteThingFail(int position1) { Toast.makeText(mContext, R.string.vote_failed, Toast.LENGTH_SHORT).show(); + post.setVoteType(previousVoteType); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + previousVoteType)); ((DataViewHolder) holder).upvoteButton.setColorFilter(previousUpvoteButtonColorFilter); ((DataViewHolder) holder).downvoteButton.setColorFilter(previousDownvoteButtonColorFilter); + + EventBus.getDefault().post(new VoteEventToDetailActivity(post.getId(), post.getVoteType())); } }, id, newVoteType, holder.getAdapterPosition()); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 505d2fb1..56302115 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -17,6 +17,16 @@ import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.browser.customtabs.CustomTabsIntent; +import androidx.cardview.widget.CardView; +import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.core.content.ContextCompat; +import androidx.core.widget.NestedScrollView; +import androidx.recyclerview.widget.DividerItemDecoration; +import androidx.recyclerview.widget.LinearLayoutManager; + import com.bumptech.glide.Glide; import com.bumptech.glide.RequestBuilder; import com.bumptech.glide.RequestManager; @@ -31,6 +41,9 @@ import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; import com.multilevelview.MultiLevelRecyclerView; import com.santalu.aspectratioimageview.AspectRatioImageView; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + import java.util.ArrayList; import java.util.List; @@ -39,15 +52,6 @@ import javax.inject.Named; import CustomView.AspectRatioGifImageView; import SubredditDatabase.SubredditRoomDatabase; -import androidx.annotation.Nullable; -import androidx.appcompat.app.AppCompatActivity; -import androidx.browser.customtabs.CustomTabsIntent; -import androidx.cardview.widget.CardView; -import androidx.coordinatorlayout.widget.CoordinatorLayout; -import androidx.core.content.ContextCompat; -import androidx.core.widget.NestedScrollView; -import androidx.recyclerview.widget.DividerItemDecoration; -import androidx.recyclerview.widget.LinearLayoutManager; import butterknife.BindView; import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.BlurTransformation; @@ -60,6 +64,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { static final String EXTRA_TITLE = "ET"; static final String EXTRA_POST_DATA = "EPD"; + static final String EXTRA_POST_LIST_POSITION = "EPLI"; private RequestManager glide; @@ -67,6 +72,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { private String orientationState = "OS"; private Post mPost; + private int postListPosition = -1; private boolean isLoadingMoreChildren = false; private ArrayList<String> 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; + } +} |