Skip to content

Commit

Permalink
Closes #676
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamantcheese committed Feb 19, 2020
1 parent fb6ea9f commit 14ebc10
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public class ThreadPresenter
private ChanThreadLoader chanLoader;
private boolean searchOpen;
private String searchQuery;
private boolean forcePageUpdate = true;
private boolean forcePageUpdate;
private PostsFilter.Order order = PostsFilter.Order.BUMP;
private boolean historyAdded;
private boolean addToLocalBackHistory;
Expand Down Expand Up @@ -1171,6 +1171,11 @@ public void onUnhidePostClick(Post post) {
threadPresenterCallback.unhideOrUnremovePost(post);
}

@Override
public void scrollToLastLocation() {
threadPresenterCallback.scrollToLastLocation();
}

public void deletePostConfirmed(Post post, boolean onlyImageDelete) {
threadPresenterCallback.showDeleting();

Expand Down Expand Up @@ -1276,7 +1281,8 @@ private void showPosts(boolean refreshAfterHideOrRemovePosts) {
if (chanLoader != null && chanLoader.getThread() != null) {
threadPresenterCallback.showPosts(chanLoader.getThread(),
new PostsFilter(order, searchQuery),
refreshAfterHideOrRemovePosts
refreshAfterHideOrRemovePosts,
forcePageUpdate
);
}
}
Expand Down Expand Up @@ -1373,7 +1379,7 @@ public void markAllPostsAsSeen() {
}

public interface ThreadPresenterCallback {
void showPosts(ChanThread thread, PostsFilter filter, boolean refreshAfterHideOrRemovePosts);
void showPosts(ChanThread thread, PostsFilter filter, boolean refreshAfterHideOrRemovePosts, boolean newReply);

void postClicked(Post post);

Expand Down Expand Up @@ -1415,6 +1421,8 @@ public interface ThreadPresenterCallback {

void smoothScrollNewPosts(int displayPosition);

void scrollToLastLocation();

void highlightPost(Post post);

void highlightPostId(String id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
boolean highlight =
post == highlightedPost || post.id.equals(highlightedPostId) || post.no == highlightedPostNo
|| post.tripcode.equals(highlightedPostTripcode);
((PostCellInterface) postViewHolder.itemView).setPost(loadable,
((PostCellInterface) postViewHolder.itemView).setPost(
loadable,
post,
postCellCallback,
false,
Expand Down Expand Up @@ -212,15 +213,18 @@ public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
}
}

public void setThread(Loadable threadLoadable, List<Post> posts, boolean refreshAfterHideOrRemovePosts) {
public void setThread(
Loadable threadLoadable, List<Post> posts, boolean refreshAfterHideOrRemovePosts, boolean newReply
) {
BackgroundUtils.ensureMainThread();
boolean changed = this.loadable != null && !this.loadable.equals(threadLoadable); //changed threads, update
boolean changed = (this.loadable != null && !this.loadable.equals(threadLoadable))
|| newReply; //changed threads or new reply, update

this.loadable = threadLoadable;
showError(null);

int lastLastSeenIndicator = lastSeenIndicatorPosition;
if (displayList.size() == posts.size()) {
if (!changed && displayList.size() == posts.size()) {
for (int i = 0; i < displayList.size(); i++) {
if (!displayList.get(i).equals(posts.get(i))) {
changed = true; //posts are different, or a post got deleted and needs to be updated
Expand Down Expand Up @@ -253,6 +257,11 @@ public void setThread(Loadable threadLoadable, List<Post> posts, boolean refresh
// so we need to refresh the UI
|| refreshAfterHideOrRemovePosts) {
notifyDataSetChanged();
if (ChanSettings.shiftPostFormat.get() && loadable.isThreadMode()) {
//sometimes the layout manager likes to just reset the scroll location with dynamic views
//this ensures that the thread ends up staying at the right location
postAdapterCallback.scrollToLastLocation();
}
}
}

Expand Down Expand Up @@ -383,5 +392,7 @@ public interface PostAdapterCallback {
Loadable getLoadable();

void onUnhidePostClick(Post post);

void scrollToLastLocation();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ public boolean threadBackPressed() {
}

@Override
public void showPosts(ChanThread thread, PostsFilter filter, boolean refreshAfterHideOrRemovePosts) {
public void showPosts(
ChanThread thread, PostsFilter filter, boolean refreshAfterHideOrRemovePosts, boolean newReply
) {
if (thread.getLoadable().isLocal()) {
if (replyButton.getVisibility() == VISIBLE) {
replyButton.hide();
Expand All @@ -269,7 +271,7 @@ public void showPosts(ChanThread thread, PostsFilter filter, boolean refreshAfte

getPresenter().updateLoadable(thread.getLoadable().loadableDownloadingState);

threadListLayout.showPosts(thread, filter, visible != Visible.THREAD, refreshAfterHideOrRemovePosts);
threadListLayout.showPosts(thread, filter, visible != Visible.THREAD, refreshAfterHideOrRemovePosts, newReply);

switchVisible(Visible.THREAD);
callback.onShowPosts();
Expand Down Expand Up @@ -433,6 +435,11 @@ public void smoothScrollNewPosts(int displayPosition) {
threadListLayout.smoothScrollNewPosts(displayPosition);
}

@Override
public void scrollToLastLocation() {
threadListLayout.scrollToLastLocation(presenter.getLoadable());
}

@Override
public void highlightPost(Post post) {
threadListLayout.highlightPost(post);
Expand Down Expand Up @@ -619,6 +626,11 @@ public void showNewPostsNotification(boolean show, int more) {
}
}

@Override
public Loadable getLoadable() {
return getPresenter().getLoadable();
}

@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.view.ViewTreeObserver;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.TextView;
Expand Down Expand Up @@ -274,7 +275,11 @@ public boolean requestChildRectangleOnScreen(
}

public void showPosts(
ChanThread thread, PostsFilter filter, boolean initial, boolean refreshAfterHideOrRemovePosts
ChanThread thread,
PostsFilter filter,
boolean initial,
boolean refreshAfterHideOrRemovePosts,
boolean newReply
) {
showingThread = thread;
if (initial) {
Expand Down Expand Up @@ -334,7 +339,7 @@ public void showPosts(
filteredPosts.removeAll(toRemove);
}

postAdapter.setThread(thread.getLoadable(), filteredPosts, refreshAfterHideOrRemovePosts);
postAdapter.setThread(thread.getLoadable(), filteredPosts, refreshAfterHideOrRemovePosts, newReply);
}

public boolean onBack() {
Expand Down Expand Up @@ -479,13 +484,13 @@ public void setSearchStatus(String query, boolean setEmptyText, boolean hideKeyb
}

public boolean canChildScrollUp() {
if (replyOpen) {
return true;
}
if (replyOpen) return true;

if (searchOpen) {
if (getTopAdapterPosition() == 0) {
View top = layoutManager.findViewByPosition(0);
if (getTopAdapterPosition() == 0) {
View top = layoutManager.findViewByPosition(0);
if (top == null) return true;

if (searchOpen) {
int searchExtraHeight = findViewById(R.id.search_status).getHeight();
if (postViewMode == LIST) {
return top.getTop() != searchExtraHeight;
Expand All @@ -498,27 +503,18 @@ public boolean canChildScrollUp() {
}
}
}
}

switch (postViewMode) {
case LIST:
if (getTopAdapterPosition() == 0) {
View top = layoutManager.findViewByPosition(0);
switch (postViewMode) {
case LIST:
return top.getTop() != toolbarHeight();
}
break;
case CARD:
if (getTopAdapterPosition() == 0) {
View top = layoutManager.findViewByPosition(0);

case CARD:
if (top instanceof PostStubCell) {
// PostStubCell does not have grid_card_margin
return top.getTop() != toolbarHeight() + dp(1);
} else {
return top.getTop() != getDimen(R.dimen.grid_card_margin) + dp(1) + toolbarHeight();
}
}
break;
}
}
return true;
}
Expand All @@ -539,6 +535,29 @@ public void smoothScrollNewPosts(int displayPosition) {
}
}

public void scrollToLastLocation(final Loadable loadable) {
final int index = loadable.listViewIndex;
final int top = loadable.listViewTop;
ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
recyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
Loadable checkingLoadable = threadListLayoutCallback.getLoadable();
if (checkingLoadable == loadable) {
//just to be sure that loadables haven't changed, so we don't scroll in a different thread than the one posted in
if (postViewMode == LIST) {
((LinearLayoutManager) layoutManager).scrollToPositionWithOffset(index, top);
} else {
throw new IllegalArgumentException(
"Should only be used for scrolling to the last location in a thread");
}
}
}
};
recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
postDelayed(() -> recyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(layoutListener), 2000);
}

public void cleanup() {
postAdapter.cleanup();
reply.cleanup();
Expand Down Expand Up @@ -840,5 +859,7 @@ public interface ThreadListLayoutCallback {
void showImageReencodingWindow(boolean supportsReencode);

boolean threadBackPressed();

Loadable getLoadable();
}
}

0 comments on commit 14ebc10

Please sign in to comment.