Skip to content

Commit

Permalink
better handling of some errors, default ctrl.params
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Mar 3, 2019
1 parent 2eb5cb4 commit 5f6d510
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 41 deletions.
16 changes: 15 additions & 1 deletion app/src/main/java/co/tinode/tindroid/ChatListFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,11 @@ private void showDeleteTopicsConfirmationDialog(final int[] positions) {
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
PromisedReply<ServerMessage> reply = null;
for (int pos : positions) {
ComTopic<VxCard> t = (ComTopic<VxCard>) mAdapter.getItem(pos);
try {
t.delete().thenCatch(new PromisedReply.FailureListener<ServerMessage>() {
reply = t.delete().thenCatch(new PromisedReply.FailureListener<ServerMessage>() {
@Override
public PromisedReply<ServerMessage> onFailure(final Exception err) {
activity.runOnUiThread(new Runnable() {
Expand All @@ -342,11 +343,24 @@ public void run() {
Log.d(TAG, "Delete failed", err);
}
}
// Wait for the last reply to resolve then update dataset.
if (reply != null) {
reply.thenApply(new PromisedReply.SuccessListener<ServerMessage>() {
@Override
public PromisedReply<ServerMessage> onSuccess(ServerMessage result) throws Exception {
datasetChanged();
return null;
}
});
}
}
});
confirmBuilder.show();
}

/**
* Wraps mAdapter.notifyDataSetChanged() into runOnUiThread()
*/
void datasetChanged() {
mAdapter.resetContent(mIsArchive);

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/message_left_avatar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="6dp"
android:paddingEnd="6dp"
android:paddingLeft="32dp"
android:paddingStart="32dp">

<LinearLayout
Expand All @@ -30,9 +28,11 @@
android:layout_height="wrap_content"
android:maxWidth="270dp"
android:minWidth="130dp"
android:paddingStart="12dp"
android:textColor="@color/colorTextPrimary"
android:clickable="false"
android:focusable="false"
tools:ignore="RtlSymmetry"
tools:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."/>

<TextView
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/message_left_single.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
android:layout_height="wrap_content"
android:maxWidth="280dp"
android:minWidth="140dp"
android:paddingStart="12dp"
android:textColor="@color/colorTextPrimary"
tools:ignore="RtlSymmetry"
tools:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."/>

<TextView
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/message_left_single_avatar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@
android:layout_height="wrap_content"
android:maxWidth="270dp"
android:minWidth="130dp"
android:paddingStart="12dp"
android:textColor="@color/colorTextPrimary"
android:clickable="false"
android:focusable="false"
tools:ignore="RtlSymmetry"
tools:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."/>


Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/layout/message_right.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
android:layout_height="wrap_content"
android:maxWidth="280dp"
android:minWidth="140dp"
android:paddingStart="12dp"
android:textColor="@color/colorTextPrimary"
tools:ignore="RtlSymmetry"
tools:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
android:textColor="@color/colorTextPrimary"/>
/>

<include layout="@layout/fragment_progress"
android:id="@+id/progressInclide"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/message_right_single.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
android:layout_height="wrap_content"
android:maxWidth="280dp"
android:minWidth="140dp"
android:paddingStart="12dp"
android:textColor="@color/colorTextPrimary"
tools:ignore="RtlSymmetry"
tools:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."/>

<include layout="@layout/fragment_progress"
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/layout/meta_message.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
android:layout_height="wrap_content"
android:maxWidth="200dp"
android:minWidth="100dp"
android:paddingStart="12dp"
android:textColor="@color/colorTextSecondary"
style="@style/textMetaBubble"
tools:text="Lorem ipsum"/>
tools:text="Lorem ipsum"
tools:ignore="RtlSymmetry"/>

</LinearLayout>
</LinearLayout>
21 changes: 11 additions & 10 deletions tinodesdk/src/main/java/co/tinode/tinodesdk/Tinode.java
Original file line number Diff line number Diff line change
Expand Up @@ -491,20 +491,20 @@ private void dispatchPacket(String message) throws Exception {
r.resolve(pkt);
} else {
r.reject(new ServerResponseException(pkt.ctrl.code, pkt.ctrl.text,
pkt.ctrl.getStringParam("what")));
pkt.ctrl.getStringParam("what", null)));
}
}
}
if (pkt.ctrl.code == 205 && "evicted".equals(pkt.ctrl.text)) {
Topic topic = getTopic(pkt.ctrl.topic);
if (topic != null) {
Boolean unsub = pkt.ctrl.getBoolParam("unsub");
topic.topicLeft(unsub != null ? unsub : false, pkt.ctrl.code, pkt.ctrl.text);
boolean unsub = pkt.ctrl.getBoolParam("unsub", false);
topic.topicLeft(unsub, pkt.ctrl.code, pkt.ctrl.text);
}
} else if ("data".equals(pkt.ctrl.getStringParam("what"))) {
} else if ("data".equals(pkt.ctrl.getStringParam("what", null))) {
Topic topic = getTopic(pkt.ctrl.topic);
if (topic != null) {
topic.allMessagesReceived(pkt.ctrl.getIntParam("count"));
topic.allMessagesReceived(pkt.ctrl.getIntParam("count", 0));
}
}
} else if (pkt.meta != null) {
Expand Down Expand Up @@ -1000,18 +1000,19 @@ protected PromisedReply<ServerMessage> login(String combined) throws Exception {
throw new IllegalArgumentException();
}

private void loginSuccessful(final MsgServerCtrl ctrl) throws InvalidObjectException, ParseException {
private void loginSuccessful(final MsgServerCtrl ctrl) throws IllegalStateException,
InvalidObjectException, ParseException {
if (ctrl == null) {
throw new InvalidObjectException("Unexpected type of reply packet");
}

String newUid = ctrl.getStringParam("user");
String newUid = ctrl.getStringParam("user", null);
if (mMyUid != null && !mMyUid.equals(newUid)) {
logout();
if (mListener != null) {
mListener.onLogin(400, "UID mismatch");
}
return;
throw new IllegalStateException("UID mismatch: received '" + newUid + "', expected '" + mMyUid + "'");
}

mMyUid = newUid;
Expand All @@ -1021,8 +1022,8 @@ private void loginSuccessful(final MsgServerCtrl ctrl) throws InvalidObjectExcep
}
// If topics were not loaded earlier, load them now.
loadTopics();
mAuthToken = ctrl.getStringParam("token");
mAuthTokenExpires = sDateFormat.parse(ctrl.getStringParam("expires"));
mAuthToken = ctrl.getStringParam("token", null);
mAuthTokenExpires = sDateFormat.parse(ctrl.getStringParam("expires", ""));
if (ctrl.code < 300) {
mConnAuth = true;
if (mListener != null) {
Expand Down
48 changes: 28 additions & 20 deletions tinodesdk/src/main/java/co/tinode/tinodesdk/Topic.java
Original file line number Diff line number Diff line change
Expand Up @@ -742,18 +742,20 @@ public PromisedReply<ServerMessage> leave() {

private void processDelivery(final MsgServerCtrl ctrl, final long id) {
if (ctrl != null) {
int seq = ctrl.getIntParam("seq");
setSeq(seq);
if (id > 0 && mStore != null) {
if (mStore.msgDelivered(Topic.this, id, ctrl.ts, seq)) {
int seq = ctrl.getIntParam("seq", 0);
if (seq > 0) {
setSeq(seq);
if (id > 0 && mStore != null) {
if (mStore.msgDelivered(Topic.this, id, ctrl.ts, seq)) {
setRecv(seq);
}
} else {
setRecv(seq);
}
} else {
setRecv(seq);
}
setRead(seq);
if (mStore != null) {
mStore.setRead(Topic.this, seq);
setRead(seq);
if (mStore != null) {
mStore.setRead(Topic.this, seq);
}
}
}
}
Expand Down Expand Up @@ -839,8 +841,10 @@ public <ML extends Iterator<Storage.Message> & Closeable> PromisedReply<ServerMe
.thenApply(new PromisedReply.SuccessListener<ServerMessage>() {
@Override
public PromisedReply<ServerMessage> onSuccess(ServerMessage result) {
Integer delId = result.ctrl.getIntParam("del");
mStore.msgDelete(Topic.this, delId, toSoftDelete);
int delId = result.ctrl.getIntParam("del", 0);
if (mStore != null && delId > 0) {
mStore.msgDelete(Topic.this, delId, toSoftDelete);
}
return null;
}
});
Expand All @@ -853,8 +857,10 @@ public PromisedReply<ServerMessage> onSuccess(ServerMessage result) {
.thenApply(new PromisedReply.SuccessListener<ServerMessage>() {
@Override
public PromisedReply<ServerMessage> onSuccess(ServerMessage result) {
Integer delId = result.ctrl.getIntParam("del");
mStore.msgDelete(Topic.this, delId, toHardDelete);
int delId = result.ctrl.getIntParam("del", 0);
if (mStore != null && delId > 0) {
mStore.msgDelete(Topic.this, delId, toHardDelete);
}
return null;
}
});
Expand Down Expand Up @@ -920,8 +926,10 @@ public PromisedReply<ServerMessage> syncOne(long msgDatabaseId) {
.thenApply(new PromisedReply.SuccessListener<ServerMessage>() {
@Override
public PromisedReply<ServerMessage> onSuccess(ServerMessage result) {
Integer delId = result.ctrl.getIntParam("del");
mStore.msgDelete(Topic.this, delId, m.getSeqId(), m.getSeqId() + 1);
int delId = result.ctrl.getIntParam("del", 0);
if (mStore != null && delId > 0) {
mStore.msgDelete(Topic.this, delId, m.getSeqId(), m.getSeqId() + 1);
}
return null;
}
});
Expand Down Expand Up @@ -1206,8 +1214,8 @@ public PromisedReply<ServerMessage> delMessages(final int fromId, final int toId
return mTinode.delMessage(getName(), fromId, toId, hard).thenApply(new PromisedReply.SuccessListener<ServerMessage>() {
@Override
public PromisedReply<ServerMessage> onSuccess(ServerMessage result) {
Integer delId = result.ctrl.getIntParam("del");
if (mStore != null && delId != null) {
int delId = result.ctrl.getIntParam("del", 0);
if (mStore != null && delId > 0) {
mStore.msgDelete(Topic.this, delId, fromId, toId);
}
return null;
Expand Down Expand Up @@ -1240,8 +1248,8 @@ public PromisedReply<ServerMessage> delMessages(final List<Integer> list, final
return mTinode.delMessage(getName(), list, hard).thenApply(new PromisedReply.SuccessListener<ServerMessage>() {
@Override
public PromisedReply<ServerMessage> onSuccess(ServerMessage result) {
Integer delId = result.ctrl.getIntParam("del");
if (mStore != null && delId != null) {
int delId = result.ctrl.getIntParam("del", 0);
if (mStore != null && delId > 0) {
mStore.msgDelete(Topic.this, delId, list);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,27 @@ public MsgServerCtrl() {
}

@JsonIgnore
public Integer getIntParam(String key) {
return params != null ? (Integer) params.get(key) : null;
private Object getParam(String key, Object def) {
if (params == null) {
return def;
}
Object result = params.get(key);
return result != null ? result : def;
}

@JsonIgnore
public String getStringParam(String key) {
return params != null ? (String) params.get(key) : null;
public Integer getIntParam(String key, Integer def) {
return (Integer) getParam(key, def);
}

@JsonIgnore
public Boolean getBoolParam(String key) {
return params != null ? (Boolean) params.get(key) : null;
public String getStringParam(String key, String def) {
return (String) getParam(key, def);
}

@JsonIgnore
public Boolean getBoolParam(String key, Boolean def) {
return (Boolean) getParam(key, def);
}

@JsonIgnore
Expand Down

0 comments on commit 5f6d510

Please sign in to comment.