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 all commits
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
67 changes: 67 additions & 0 deletions app/src/main/java/com/zulip/android/ZulipApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.j256.ormlite.misc.TransactionManager;
import com.zulip.android.activities.ZulipActivity;
import com.zulip.android.database.DatabaseHelper;
import com.zulip.android.filters.NarrowFilter;
import com.zulip.android.models.Emoji;
import com.zulip.android.models.Message;
import com.zulip.android.models.MessageType;
Expand Down Expand Up @@ -133,6 +134,24 @@ public void setZulipActivity(ZulipActivity zulipActivity) {
this.zulipActivity = zulipActivity;
}

//store narrowFilter before switching theme
private NarrowFilter narrowFilter = null;
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 will be null

Copy link
Member Author

Choose a reason for hiding this comment

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

yup, its null private NarrowFilter narrowFilter = null;

Copy link
Member

Choose a reason for hiding this comment

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

So we won't need to assign null additionally
private NarrowFilter narrowFilter;


//set it after restore
//for narrowed view
private int tempNarrowedViewPointer = -1;
//for home view
private int tempHomeViewPointer = -1;

//useful when switch takes place from narrowed view
//on switch theme activity is recreated
private boolean isThemeSwitchedFromHome = true;

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

@Override
public void onCreate() {
super.onCreate();
Expand Down Expand Up @@ -602,4 +621,52 @@ public void setMessageContentEditParams(int seconds, boolean param) {
}
}
}

public NarrowFilter getNarrowFilter() {
return narrowFilter;
}

public void setNarrowFilter(NarrowFilter narrowFilter) {
this.narrowFilter = narrowFilter;
}

public void setTempNarrowedViewPointer(int mID) {
this.tempNarrowedViewPointer = mID;
}

public int getTempNarrowedViewPointer() {
return tempNarrowedViewPointer;
}

public int getTempHomeViewPointer() {
return tempHomeViewPointer;
}

public void setTempHomeViewPointer(int tempHomeViewPointer) {
this.tempHomeViewPointer = tempHomeViewPointer;
}

public boolean isThemeSwitchedFromHome() {
return 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 @@ -422,7 +422,7 @@ public void onTaskFailure(String result) {
100, filter);
}

private void selectPointer() {
public void selectPointer() {
if (filter != null) {
Where<Message, Object> filteredWhere;
try {
Expand All @@ -440,16 +440,41 @@ private void selectPointer() {

// use anchor message id if message was narrowed
if (anchorId != -1) {
selectMessage(getMessageById(anchorId));
if (app.getTempNarrowedViewPointer() != -1 && app.getTempNarrowedViewPointer() < anchorId) {
//user have scrolled above before switching theme
scrollWithZeroOffset(getMessageById(app.getTempNarrowedViewPointer()));
app.setTempNarrowedViewPointer(-1);
app.setNarrowedViewHeaderCount(0);
} else {
selectMessage(getMessageById(anchorId));
}
} else {
recyclerView.scrollToPosition(adapter.getItemIndex(closestMessage));
if (app.getTempNarrowedViewPointer() != -1 && adapter.getItemIndex(getMessageById(app.getTempNarrowedViewPointer())) < adapter.getItemIndex(closestMessage)) {
//user have scrolled above before switching theme
scrollWithZeroOffset(getMessageById(app.getTempNarrowedViewPointer()));
app.setTempNarrowedViewPointer(-1);
app.setNarrowedViewHeaderCount(0);
}else {
recyclerView.scrollToPosition(adapter.getItemIndex(closestMessage));
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
} else {
int anc = app.getPointer();
selectMessage(getMessageById(anc));
int tempPointer = app.getTempHomeViewPointer();
if (tempPointer != -1 && tempPointer < anc) {
//user have scrolled above before switching theme
scrollWithZeroOffset(getMessageById(tempPointer));
//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 @@ -846,7 +871,17 @@ 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()));
}

/**
* scroll such that message comes to top of the view
*
* @param message that should come to top
* @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) - ((filter == null) ? app.getHomeViewHeaderCount() : app.getNarrowedViewHeaderCount()), 0);
}

private Message getMessageById(int id) {
Expand Down Expand Up @@ -889,4 +924,36 @@ public interface Listener {
void setLayoutBehaviour(LinearLayoutManager linearLayoutManager, RecyclerMessageAdapter adapter);

}

/**
* get messageId of FirstCompletelyVisibleItem
* loop until we get message
* skip message header
*
* @return messagedId of message which is at top
*/
public int getTopMessageId() {
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++;
}
}
}
return -1;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -642,10 +642,14 @@ public void onBackStackChanged() {
if (menu == null)
return;
if (narrowedList == null) {
//will be useful when user switch theme from narrowed view and back trace to home
app.setThemeSwitchedFromHome(true);
calendar = Calendar.getInstance();
menu.getItem(2).getSubMenu().getItem(0).setTitle(R.string.menu_today);
switchToStream();
checkForChatBoxFocusRequest();
//on narrowed view is restored after switching and coming back to homeView set pointer accordingly
homeList.selectPointer();
} else if (narrowedList.filter instanceof NarrowFilterByDate) {
menu.getItem(2).getSubMenu().getItem(0).setTitle(R.string.menu_one_day_before);
}
Expand Down Expand Up @@ -933,6 +937,17 @@ protected void onNewIntent(Intent intent) {
}
}

@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
//check if it was narrowed before switching theme
if (app.getNarrowFilter() != null) {
//restore narrow
doNarrow(app.getNarrowFilter());
app.setNarrowFilter(null);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Expand Down Expand Up @@ -2061,7 +2076,12 @@ private void pushListFragment(MessageListFragment list, String back) {
transaction.replace(R.id.list_fragment_container, list);
if (back != null) {
transaction.addToBackStack(back);
clearSearch();
//searchView is in menu
//menu is inflated in onCreateOptionsMenu
//onCreateOptionsMenu is called somewhere in between onCreate is executing
if (searchView != null) {
clearSearch();
}
}
transaction.commit();
getSupportFragmentManager().executePendingTransactions();
Expand Down Expand Up @@ -2355,6 +2375,16 @@ public void onClick(
}
break;
case R.id.daynight:
//for narrowed view
if (narrowedList != null) {
app.setNarrowFilter(narrowedList.filter);
app.setTempNarrowedViewPointer(narrowedList.getTopMessageId());
app.setThemeSwitchedFromHome(false);
}
//for home view
if (app.getTempHomeViewPointer() == -1 || app.isThemeSwitchedFromHome()) {
app.setTempHomeViewPointer(homeList.getTopMessageId());
}
switch (AppCompatDelegate.getDefaultNightMode()) {
case -1:
case AppCompatDelegate.MODE_NIGHT_NO:
Expand Down