Fixed bug that updating wrong post when PostFragment receives PostUpdateEventToPostList.

This commit is contained in:
Alex Ning 2019-08-22 17:16:31 +08:00
parent d4fc671605
commit d864ec6d7f
2 changed files with 14 additions and 12 deletions

View File

@ -25,6 +25,7 @@ import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.paging.PagedList;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller; import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -437,16 +438,19 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
@Subscribe @Subscribe
public void onPostUpdateEvent(PostUpdateEventToPostList event) { public void onPostUpdateEvent(PostUpdateEventToPostList event) {
Post post = mAdapter.getCurrentList().get(event.positionInList); PagedList<Post> posts = mAdapter.getCurrentList();
if(post != null) { if (posts != null && event.positionInList >= 0 && event.positionInList < posts.size()) {
post.setTitle(event.post.getTitle()); Post post = posts.get(event.positionInList);
post.setVoteType(event.post.getVoteType()); if (post != null && post.getFullName().equals(event.post.getFullName())) {
post.setScore(event.post.getScore()); post.setTitle(event.post.getTitle());
post.setNSFW(event.post.isNSFW()); post.setVoteType(event.post.getVoteType());
post.setSpoiler(event.post.isSpoiler()); post.setScore(event.post.getScore());
post.setFlair(event.post.getFlair()); post.setNSFW(event.post.isNSFW());
post.setSaved(event.post.isSaved()); post.setSpoiler(event.post.isSpoiler());
mAdapter.notifyItemChanged(event.positionInList); post.setFlair(event.post.getFlair());
post.setSaved(event.post.isSaved());
mAdapter.notifyItemChanged(event.positionInList);
}
} }
} }

View File

@ -6,7 +6,6 @@ import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -300,7 +299,6 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
mPost = getIntent().getParcelableExtra(EXTRA_POST_DATA); mPost = getIntent().getParcelableExtra(EXTRA_POST_DATA);
} }
Log.i("asdfasdfadfadf", "" + mPost.isSaved());
if(mPost == null) { if(mPost == null) {
fetchPostAndCommentsById(getIntent().getStringExtra(EXTRA_POST_ID)); fetchPostAndCommentsById(getIntent().getStringExtra(EXTRA_POST_ID));
} else { } else {