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

Commit 7b58a7b

Browse files
committed
Narrow to last message read when narrow is performed from streams drawer.
1 parent f49f8b5 commit 7b58a7b

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

app/src/main/java/com/zulip/android/activities/RecyclerMessageAdapter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,8 @@ public void onClick(View view) {
422422
@Override
423423
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
424424
super.onViewRecycled(holder);
425-
if (holder.getItemViewType() == VIEWTYPE_MESSAGE && !startedFromFilter)
425+
if (holder.getItemViewType() == VIEWTYPE_MESSAGE)
426+
// mark fields as read in homeview and streams narrow
426427
markThisMessageAsRead((Message) getItem(holder.getAdapterPosition()));
427428
}
428429

@@ -434,7 +435,7 @@ public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
434435
private void markThisMessageAsRead(Message message) {
435436
try {
436437
int mID = message.getID();
437-
if (zulipApp.getPointer() < mID) {
438+
if (!startedFromFilter && zulipApp.getPointer() < mID) {
438439
zulipApp.syncPointer(mID);
439440
}
440441

app/src/main/java/com/zulip/android/activities/ZulipActivity.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,7 @@ public boolean onChildClick(ExpandableListView parent, View v, int groupPosition
12031203
public boolean onGroupClick(ExpandableListView expandableListView, View view, int position, long l) {
12041204
resetStreamSearch();
12051205
String streamName = ((TextView) view.findViewById(R.id.name)).getText().toString();
1206-
doNarrow(new NarrowFilterStream(streamName, null));
1206+
doNarrowToLastRead(streamName);
12071207
drawerLayout.openDrawer(GravityCompat.START);
12081208
if (previousClick != -1 && expandableListView.getCount() > previousClick) {
12091209
expandableListView.collapseGroup(previousClick);
@@ -1232,7 +1232,7 @@ public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
12321232
@Override
12331233
public void onClick(View v) {
12341234
resetStreamSearch();
1235-
onNarrow(new NarrowFilterStream(streamName, null));
1235+
doNarrowToLastRead(streamName);
12361236
onNarrowFillSendBoxStream(streamName, "", false);
12371237
}
12381238
});
@@ -1277,6 +1277,24 @@ public void onClick(View v) {
12771277
streamsDrawer.setAdapter(streamsDrawerAdapter);
12781278
}
12791279

1280+
/**
1281+
* Helper function used to call {@link ZulipActivity#onNarrow(NarrowFilter, int)} or
1282+
* {@link ZulipActivity#onNarrow(NarrowFilter)} based on last message read in {@param streamName} stream.
1283+
*
1284+
* @param streamName stream name
1285+
*/
1286+
private void doNarrowToLastRead(String streamName) {
1287+
// get last message read in stream
1288+
Message message = Stream.getLastMessageRead(app, streamName);
1289+
if (message != null) {
1290+
// fetch results around the last message read
1291+
onNarrow(new NarrowFilterStream(streamName, null), message.getId());
1292+
} else {
1293+
// new stream
1294+
onNarrow(new NarrowFilterStream(streamName, null));
1295+
}
1296+
}
1297+
12801298
/**
12811299
* Change streamsDrawerAdapter cursor to default
12821300
* Clear text of etSearchStream
@@ -1878,7 +1896,7 @@ public void onNarrowFillSendBoxStream(String stream, String subject, boolean ope
18781896
}
18791897

18801898
public void onNarrow(NarrowFilter narrowFilter) {
1881-
// TODO: check if already narrowed to this particular stream/subject
1899+
if (narrowedList == null || narrowFilter != narrowedList.filter)
18821900
doNarrow(narrowFilter);
18831901
}
18841902

app/src/main/java/com/zulip/android/models/Stream.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,5 +218,25 @@ public void setFetchColor(String fetchedColor) {
218218

219219
public void setInHomeView(boolean inHomeView) {
220220
this.inHomeView = inHomeView;
221+
222+
/**
223+
* This function returns the last message read in {@param streamName} stream.
224+
*
225+
* @param app {@link ZulipApp}
226+
* @param streamName name of stream
227+
* @return last message {@link Message} read
228+
*/
229+
public static Message getLastMessageRead(ZulipApp app, String streamName) {
230+
try {
231+
Dao<Message, Integer> messageDao = app.getDatabaseHelper().getDao(Message.class);
232+
233+
// query for message in given stream and orderby timestamp decreasingly
234+
return messageDao.queryBuilder().orderBy(Message.TIMESTAMP_FIELD, false)
235+
.where().eq(Message.RECIPIENTS_FIELD, new SelectArg(Message.RECIPIENTS_FIELD, streamName))
236+
.queryForFirst();
237+
} catch (SQLException e) {
238+
ZLog.logException(e);
239+
}
240+
return null;
221241
}
222242
}

0 commit comments

Comments
 (0)