Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Restore narrow and scroll after theme switch. #414

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/src/main/java/com/zulip/android/ZulipApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ public void setZulipActivity(ZulipActivity zulipActivity) {
//on switch theme activity is recreated
private boolean isThemeSwitchedFromHome = true;

//account for header
//will be helpful when them is switched
private int homeViewHeaderCount = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default value for integer member variables will be 0, hence we can omit this initialisation.

private int narrowedViewHeaderCount = 0;

@Override
public void onCreate() {
super.onCreate();
Expand Down Expand Up @@ -648,4 +653,20 @@ public boolean isThemeSwitchedFromHome() {
public void setThemeSwitchedFromHome(boolean themeSwitchedFromHome) {
isThemeSwitchedFromHome = themeSwitchedFromHome;
}

public int getHomeViewHeaderCount() {
return homeViewHeaderCount;
}

public void setHomeViewHeaderCount(int homeViewHeaderCount) {
this.homeViewHeaderCount = homeViewHeaderCount;
}

public int getNarrowedViewHeaderCount() {
return narrowedViewHeaderCount;
}

public void setNarrowedViewHeaderCount(int narrowedViewHeaderCount) {
this.narrowedViewHeaderCount = narrowedViewHeaderCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ public void selectPointer() {
//user have scrolled above before switching theme
scrollWithZeroOffset(getMessageById(app.getTempNarrowedViewPointer()));
app.setTempNarrowedViewPointer(-1);
app.setNarrowedViewHeaderCount(0);
} else {
selectMessage(getMessageById(anchorId));
}
Expand All @@ -452,6 +453,7 @@ public void selectPointer() {
//user have scrolled above before switching theme
scrollWithZeroOffset(getMessageById(app.getTempNarrowedViewPointer()));
app.setTempNarrowedViewPointer(-1);
app.setNarrowedViewHeaderCount(0);
}else {
recyclerView.scrollToPosition(adapter.getItemIndex(closestMessage));
}
Expand All @@ -468,6 +470,7 @@ public void selectPointer() {
//will be useful when user switch theme from narrowed view and back trace to home
if (app.isThemeSwitchedFromHome()) {
app.setTempHomeViewPointer(-1);
app.setHomeViewHeaderCount(0);
}
} else {
selectMessage(getMessageById(anc));
Expand Down Expand Up @@ -868,7 +871,7 @@ private Boolean listHasMostRecent() {
}

private void selectMessage(final Message message) {
recyclerView.scrollToPosition(adapter.getItemIndex(message));
recyclerView.scrollToPosition(adapter.getItemIndex(message) - ((filter == null) ? app.getHomeViewHeaderCount() : app.getNarrowedViewHeaderCount()));
}

/**
Expand All @@ -878,7 +881,7 @@ private void selectMessage(final Message message) {
* @see {@link <a href="http://stackoverflow.com/questions/26875061/scroll-recyclerview-to-show-selected-item-on-top"/>}
*/
private void scrollWithZeroOffset(Message message) {
linearLayoutManager.scrollToPositionWithOffset(adapter.getItemIndex(message), 0);
linearLayoutManager.scrollToPositionWithOffset(adapter.getItemIndex(message) - ((filter == null) ? app.getHomeViewHeaderCount() : app.getNarrowedViewHeaderCount()), 0);
}

private Message getMessageById(int id) {
Expand Down Expand Up @@ -930,13 +933,23 @@ public interface Listener {
* @return messagedId of message which is at top
*/
public int getTopMessageId() {
int position = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
int position = linearLayoutManager.findFirstVisibleItemPosition();
int headerCount = 0;
//if there aren't any visible items position = -1
if (position >= 0) {
for (int i = position; i < adapter.getItemCount(); i++) {
Object topObject = adapter.getItem(i);
if (topObject instanceof Message) {
if (filter == null) {
//homeView
app.setHomeViewHeaderCount(headerCount);
} else {
//narrowed view
app.setNarrowedViewHeaderCount(headerCount);
}
return ((Message) topObject).getID();
} else {
headerCount++;
}
}
}
Expand Down