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

Show unread counts and add bankruptcy option. #430

Open
wants to merge 9 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import com.zulip.android.ZulipApp;
import com.zulip.android.models.Message;
import com.zulip.android.models.Stream;
import com.zulip.android.util.ZLog;

import java.sql.SQLException;
Expand All @@ -31,19 +30,22 @@ public ExpandableStreamDrawerAdapter(final Context context, Cursor cursor, int g

@Override
public Cursor getChildrenCursor(Cursor groupCursor) {
int pointer = zulipApp.getPointer();
List<String[]> results = new ArrayList<>();
try {
results = ZulipApp.get().getDao(Message.class).queryRaw("SELECT DISTINCT subject FROM messages " +
"JOIN streams ON streams.id=messages.stream " +
"WHERE streams.id=" + groupCursor.getInt(0) + " and streams." + Stream.SUBSCRIBED_FIELD + " = " + "1 group by subject").getResults();
results = zulipApp.getDao(Message.class).queryRaw("SELECT DISTINCT subject, " +
"count(case when messages.id > " + pointer + " and (messages." +
Message.MESSAGE_READ_FIELD + " = 0) then 1 end) as unreadcount FROM messages " +
"JOIN streams ON streams.name=messages.recipients " +
"WHERE streams.name LIKE ? group by subject", groupCursor.getString(1)).getResults();
} catch (SQLException e) {
ZLog.logException(e);
}
MatrixCursor matrixCursor = new MatrixCursor(new String[]{"subject", "_id"});
MatrixCursor matrixCursor = new MatrixCursor(new String[]{"subject", "_id", UNREAD_TABLE_NAME});

for (String[] result : results) {
try {
matrixCursor.addRow(new String[]{result[0], String.valueOf(groupCursor.getInt(0))});
matrixCursor.addRow(new String[]{result[0], String.valueOf(groupCursor.getInt(0)), result[1]});
} catch (Exception e) {
ZLog.logException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import android.widget.TextView;
import android.widget.Toast;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.UpdateBuilder;
import com.j256.ormlite.stmt.Where;
import com.zulip.android.R;
import com.zulip.android.ZulipApp;
Expand Down Expand Up @@ -65,6 +67,7 @@
* initiated and called by {@link ZulipActivity}
*/
public class MessageListFragment extends Fragment implements MessageListener {
public static final String LOG_TAG = MessageListFragment.class.getSimpleName();
private static final String PARAM_FILTER = "filter";
public NarrowFilter filter;
public ZulipApp app;
Expand Down Expand Up @@ -261,7 +264,7 @@ private void initializeNarrow() {
adapter.setFooterShowing(true);
}

public void onReadyToDisplay(boolean registered) {
public void onReadyToDisplay(boolean registered, boolean startup) {
if (initialized && !registered) {
// Already have state, and already processed any events that came in
// when resuming the existing queue.
Expand All @@ -270,9 +273,13 @@ public void onReadyToDisplay(boolean registered) {
Log.i("onReadyToDisplay", "just a resume");
return;
}

initializeNarrow();
fetch();
if (startup) {
// fetch 1000 messages after pointer on startup
fetch(app.getPointer(), 1000);
} else {
fetch(app.getPointer(), 100);
}
initialized = true;
}

Expand All @@ -293,26 +300,39 @@ public void onReadyToDisplay(boolean registered, int messageId) {
}

initializeNarrow();
fetch(messageId);
fetch(messageId, 100);
initialized = true;
}


private void showEmptyView() {
Log.d("ErrorRecieving", "No Messages found for current list" + ((filter != null) ? ":" + filter.getTitle() : ""));
recyclerView.setVisibility(View.GONE);
emptyTextView.setVisibility(View.VISIBLE);
}

private void fetch() {
/**
* Fetches all messages past current pointer and calls {@link #skipAllMessages()}
* to marks messages as read, update pointer and scroll to latest message.
*
* @param messagesFetched number of messages fetched in preceding call
*/
public void fetchAllMessages(final int messagesFetched, final CommonProgressDialog dialog) {
final AsyncGetOldMessages oldMessagesReq = new AsyncGetOldMessages(this);
oldMessagesReq.setCallback(new ZulipAsyncPushTask.AsyncTaskCompleteListener() {
@Override
public void onTaskComplete(String result, JSONObject jsonObject) {
loadingMessages = false;
adapter.setHeaderShowing(false);
if (result.equals("0")) {
showEmptyView();
skipAllMessages();
dialog.dismiss();
Toast.makeText(getContext(), R.string.bankruptcy_confirmation_msg, Toast.LENGTH_SHORT).show();
} else {
try {
fetchAllMessages(messagesFetched + Integer.parseInt(result), dialog);
} catch (NumberFormatException e) {
Log.e(LOG_TAG, "fetchAllMessages()", e);
}
}
}

Expand All @@ -322,8 +342,52 @@ public void onTaskFailure(String result) {
adapter.setHeaderShowing(false);
}
});
oldMessagesReq.execute(app.getPointer(), LoadPosition.INITIAL, 100,
100, filter);
oldMessagesReq.execute(app.getPointer() + messagesFetched, LoadPosition.INITIAL, 0,
200, filter);
}

/**
* Marks all messages as read, updates pointer and scrolls to last message.
*/
private void skipAllMessages() {
// mark all messages as read
try {
markAllMessagesAsRead();
} catch (SQLException e) {
Toast.makeText(getContext(), R.string.try_again_error_msg, Toast.LENGTH_SHORT).show();
ZLog.logException(e);
}

// sync pointer to latest message
int lastMessageId = app.getMaxMessageId();
app.syncPointer(lastMessageId);

// scroll to latest message
try {
RecyclerMessageAdapter adapter = getAdapter();
int lastMsgIndex = adapter.getItemIndex(lastMessageId);
if (lastMsgIndex != -1) {
recyclerView.scrollToPosition(lastMsgIndex);
} else {
// when the current narrow doesn't contain the latest message
// do nothing
}
} catch (NullPointerException e) {
Toast.makeText(getContext(), R.string.try_again_error_msg, Toast.LENGTH_SHORT).show();
ZLog.logException(e);
}
}

/**
* Marks all messages as read after current pointer.
* @throws SQLException
*/
private void markAllMessagesAsRead() throws SQLException {
Dao<Message, Integer> messageDao = app.getDao(Message.class);
UpdateBuilder builder = messageDao.updateBuilder();
builder.where().ge(Message.ID_FIELD, app.getPointer());
builder.updateColumnValue(Message.MESSAGE_READ_FIELD, true);
builder.update();
}

/**
Expand All @@ -332,8 +396,8 @@ public void onTaskFailure(String result) {
*
* @param messageID anchor message id
*/
private void fetch(int messageID) {
// set the achor for fetching messages as the message clicked
private void fetch(int messageID, int messages) {
// set the anchor for fetching messages as the message clicked
this.anchorId = messageID;

final AsyncGetOldMessages oldMessagesReq = new AsyncGetOldMessages(this);
Expand All @@ -354,7 +418,7 @@ public void onTaskFailure(String result) {
}
});
oldMessagesReq.execute(messageID, LoadPosition.INITIAL, 100,
100, filter);
messages, filter);
}

private void selectPointer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import com.zulip.android.viewholders.MessageHeaderParent;
import com.zulip.android.viewholders.MessageHolder;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -449,19 +448,8 @@ private void markThisMessageAsRead(Message message) {
if (!startedFromFilter && zulipApp.getPointer() < mID) {
zulipApp.syncPointer(mID);
}

boolean isMessageRead = false;
if (message.getMessageRead() != null) {
isMessageRead = message.getMessageRead();
}
if (!isMessageRead) {
try {
updateBuilder.where().eq(Message.ID_FIELD, message.getID());
updateBuilder.updateColumnValue(Message.MESSAGE_READ_FIELD, true);
updateBuilder.update();
} catch (SQLException e) {
ZLog.logException(e);
}
if (!message.getMessageRead()) {
// leaving message update to "update_message_flag" event
zulipApp.markMessageAsRead(message);
}
} catch (NullPointerException e) {
Expand Down
Loading