-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async media: Scroll to failed post from error notifications #6424
Merged
mzorz
merged 9 commits into
feature/async-media
from
feature/async-error-scroll-to-post
Jul 31, 2017
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e70fc2a
Pass the post when opening post list from error notification
aforcier 3bf6a80
Scroll list to failed post when opening error notification
aforcier d108595
Remove post list error dialog (triggered from error notifications)
aforcier 56d8d25
Scroll target post to the top of the list
aforcier 13de017
Merge branch 'issue/6386-async-design-tweaks' into feature/async-erro…
aforcier 08c6954
Smooth scroll to failed post
aforcier 711f21e
Offset scroll-to-post to show part of above post
aforcier 3e0b6f6
Rename target post id constant
aforcier fece2bf
Show toast when tapping error notification for unknown post
aforcier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,9 @@ | |
import android.support.design.widget.Snackbar; | ||
import android.support.v7.app.AlertDialog; | ||
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.LinearSmoothScroller; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.util.TypedValue; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
@@ -85,6 +87,7 @@ public class PostsListFragment extends Fragment | |
|
||
private boolean mCanLoadMorePosts = true; | ||
private boolean mIsPage; | ||
private PostModel mTargetPost; | ||
private boolean mIsFetchingPosts; | ||
private boolean mShouldCancelPendingDraftNotification = false; | ||
private int mPostIdForPostToBeDeleted = 0; | ||
|
@@ -97,11 +100,14 @@ public class PostsListFragment extends Fragment | |
@Inject PostStore mPostStore; | ||
@Inject Dispatcher mDispatcher; | ||
|
||
public static PostsListFragment newInstance(SiteModel site, boolean isPage) { | ||
public static PostsListFragment newInstance(SiteModel site, boolean isPage, @Nullable PostModel targetPost) { | ||
PostsListFragment fragment = new PostsListFragment(); | ||
Bundle bundle = new Bundle(); | ||
bundle.putSerializable(WordPress.SITE, site); | ||
bundle.putBoolean(PostsListActivity.EXTRA_VIEW_PAGES, isPage); | ||
if (targetPost != null) { | ||
bundle.putInt(PostsListActivity.EXTRA_TARGET_POST_LOCAL_ID, targetPost.getId()); | ||
} | ||
fragment.setArguments(bundle); | ||
return fragment; | ||
} | ||
|
@@ -130,18 +136,23 @@ private void updateSiteOrFinishActivity(Bundle savedInstanceState) { | |
if (getArguments() != null) { | ||
mSite = (SiteModel) getArguments().getSerializable(WordPress.SITE); | ||
mIsPage = getArguments().getBoolean(PostsListActivity.EXTRA_VIEW_PAGES); | ||
mTargetPost = mPostStore.getPostByLocalPostId( | ||
getArguments().getInt(PostsListActivity.EXTRA_TARGET_POST_LOCAL_ID)); | ||
} else { | ||
mSite = (SiteModel) getActivity().getIntent().getSerializableExtra(WordPress.SITE); | ||
mIsPage = getActivity().getIntent().getBooleanExtra(PostsListActivity.EXTRA_VIEW_PAGES, false); | ||
mTargetPost = mPostStore.getPostByLocalPostId( | ||
getActivity().getIntent().getIntExtra(PostsListActivity.EXTRA_TARGET_POST_LOCAL_ID, 0)); | ||
} | ||
} else { | ||
mSite = (SiteModel) savedInstanceState.getSerializable(WordPress.SITE); | ||
mIsPage = savedInstanceState.getBoolean(PostsListActivity.EXTRA_VIEW_PAGES); | ||
mTargetPost = mPostStore.getPostByLocalPostId( | ||
savedInstanceState.getInt(PostsListActivity.EXTRA_TARGET_POST_LOCAL_ID)); | ||
} | ||
|
||
if (mSite == null) { | ||
ToastUtils.showToast(getActivity(), R.string.blog_not_found, | ||
ToastUtils.Duration.SHORT); | ||
ToastUtils.showToast(getActivity(), R.string.blog_not_found, ToastUtils.Duration.SHORT); | ||
getActivity().finish(); | ||
} | ||
} | ||
|
@@ -494,6 +505,36 @@ public void onPostsLoaded(int postCount) { | |
} else if (postCount > 0) { | ||
hideEmptyView(); | ||
} | ||
|
||
// If the activity was given a target post, and this is the first time posts are loaded, scroll to that post | ||
if (mTargetPost != null) { | ||
if (mPostsListAdapter != null) { | ||
final int position = mPostsListAdapter.getPositionForPost(mTargetPost); | ||
if (position > -1) { | ||
RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(getActivity()) { | ||
private static final int SCROLL_OFFSET_DP = 23; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a comment here explaining why we are moving this offset? maybe something like |
||
|
||
@Override | ||
protected int getVerticalSnapPreference() { | ||
return LinearSmoothScroller.SNAP_TO_START; | ||
} | ||
|
||
@Override | ||
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, | ||
int snapPreference) { | ||
// Assume SNAP_TO_START, and offset the scroll, so the bottom of the above post shows | ||
int offsetPx = (int) TypedValue.applyDimension( | ||
TypedValue.COMPLEX_UNIT_DIP, SCROLL_OFFSET_DP, getResources().getDisplayMetrics()); | ||
return boxStart - viewStart + offsetPx; | ||
} | ||
}; | ||
|
||
smoothScroller.setTargetPosition(position); | ||
mRecyclerView.getLayoutManager().startSmoothScroll(smoothScroller); | ||
} | ||
} | ||
mTargetPost = null; | ||
} | ||
} | ||
|
||
/* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PostsListFragment.newInstance()
only usestargetPost
to obtain it'sid
and set a parcelable argument. Later on thatid
will be properly used to retrieve the actualPostModel
from the db when it is actually needed.Also, the only reason why
PostStore
is imported and injected here is to be able to get the PostModel.Why don't we just remove the use of
mPostStore
in PostsListActivity altogether, and pass the ID here as well?This would also be in line with the decision taken on trying to avoid this kind of
PostModel
passing around because of potential OOM problems, like in this PR here #5856There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case we're passing the
PostModel
directly to a method, it's not being parceled, so #5856 shouldn't apply.I decided to use the model for two reasons:
PostsListFragment
if the post has since been deleted (since it would have a non-0 id, but thePostModel
obtained from the store would benull
)PostsListFragment.newInstance()
taking an int id argument, which is error-prone and the main case for using models over idsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgive me, I wasn't clear: I was trying to acknowledge how the same treatment to this case is given within the fragment by using the ID instead of the PostModel. I should have said something like "it's great to see we're using this in the same way here".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having discussed these 2 points in Slack, and acknowledging point 1 is better kept as is as per the advantage of keeping fragments, and leaving point 2 for a longer discussion elsewhere, here's one minor thing users could benefit from:
If the user taps on an error notification for which the referred
PostModel
doesn’t exist anymore (i.e. has been deleted) we should do something else then, maybe show a Toast or something, indicating the Post wasn’t found.To test:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added toast in fece2bf.