Synchronizing voting state between PostRecyclerViewAdapter and ViewPostDetailActivity using EventBus by greenrobot. Minor bugs fixed.

This commit is contained in:
Alex Ning 2019-04-18 23:31:41 +08:00
parent a7e9695717
commit cd4730c649
14 changed files with 149 additions and 63 deletions

Binary file not shown.

View File

@ -1,29 +0,0 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<Objective-C-extensions>
<file>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Import" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Macro" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Typedef" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Enum" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Constant" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Global" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Struct" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="FunctionPredecl" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Function" />
</file>
<class>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Property" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Synthesize" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InitMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="StaticMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InstanceMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="DeallocMethod" />
</class>
<extensions>
<pair source="cpp" header="h" fileNamingConvention="NONE" />
<pair source="c" header="h" fileNamingConvention="NONE" />
</extensions>
</Objective-C-extensions>
</code_scheme>
</component>

4
.idea/encodings.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>

View File

@ -41,8 +41,8 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0-alpha03'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha03'
implementation 'com.google.android.exoplayer:exoplayer:2.7.0'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.7.0'
implementation 'com.google.android.exoplayer:exoplayer:2.9.6'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.9.6'
implementation 'androidx.browser:browser:1.0.0'
implementation 'com.alexvasilkov:gesture-views:2.5.2'
implementation 'androidx.cardview:cardview:1.0.0'
@ -74,4 +74,5 @@ dependencies {
implementation "ru.noties:markwon-view:2.0.1"
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.16'
implementation 'com.github.Ferfalk:SimpleSearchView:0.1.3'
implementation 'org.greenrobot:eventbus:3.1.1'
}

View File

@ -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);

View File

@ -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",

View File

@ -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();
}
}

View File

@ -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());
});

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.android.tools.build:gradle:3.4.0'
// NOTE: Do not place your application dependencies here; they belong

View File

@ -1,6 +1,6 @@
#Tue Jan 22 01:13:16 HKT 2019
#Thu Apr 18 18:17:08 HKT 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip