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

Commit

Permalink
Restore narrow and scroll after theme switch.
Browse files Browse the repository at this point in the history
Fix:#406
  • Loading branch information
jainkuniya committed Mar 10, 2017
1 parent 7041b4d commit f5e0dea
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 5 deletions.
46 changes: 46 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,19 @@ public void setZulipActivity(ZulipActivity zulipActivity) {
this.zulipActivity = zulipActivity;
}

//store narrowFilter before switching theme
private NarrowFilter narrowFilter = null;

//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;

@Override
public void onCreate() {
super.onCreate();
Expand Down Expand Up @@ -602,4 +616,36 @@ 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;
}
}
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,38 @@ 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);
} 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);
}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);
}
} else {
selectMessage(getMessageById(anc));
}
}
}

Expand Down Expand Up @@ -849,6 +871,16 @@ private void selectMessage(final Message message) {
recyclerView.scrollToPosition(adapter.getItemIndex(message));
}

/**
* 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), 0);
}

private Message getMessageById(int id) {
return this.messageIndex.get(id);
}
Expand Down Expand Up @@ -889,4 +921,26 @@ 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.findFirstCompletelyVisibleItemPosition();
//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) {
return ((Message) topObject).getID();
}
}
}
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

0 comments on commit f5e0dea

Please sign in to comment.