Skip to content

Commit 40df55e

Browse files
committed
Fallback, get room from local if failed to load room from server
1 parent 3521616 commit 40df55e

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

qiscus-sdk/src/main/java/com/qiscus/sdk/data/remote/QiscusApi.java

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -169,32 +169,41 @@ public Observable<QiscusChatRoom> getChatRoom(List<String> withEmails, String di
169169

170170
public Observable<QiscusChatRoom> getChatRoom(int roomId) {
171171
return api.getChatRoom(Qiscus.getToken(), roomId)
172+
.onErrorReturn(throwable -> null)
172173
.map(jsonElement -> {
173-
JsonObject jsonChatRoom = jsonElement.getAsJsonObject().get("results")
174-
.getAsJsonObject().get("room").getAsJsonObject();
175-
QiscusChatRoom qiscusChatRoom = new QiscusChatRoom();
176-
qiscusChatRoom.setId(jsonChatRoom.get("id").getAsInt());
177-
//TODO minta server ngasih tau distinctId biar bisa disimpen
178-
//qiscusChatRoom.setDistinctId("default");
179-
qiscusChatRoom.setLastCommentId(jsonChatRoom.get("last_comment_id").getAsInt());
180-
qiscusChatRoom.setLastCommentMessage(jsonChatRoom.get("last_comment_message").getAsString());
181-
qiscusChatRoom.setLastTopicId(jsonChatRoom.get("last_topic_id").getAsInt());
182-
qiscusChatRoom.setOptions(jsonChatRoom.get("options").isJsonNull() ? null
183-
: jsonChatRoom.get("options").getAsString());
184-
//TODO minta server ngasih tau member room siapa aja
185-
//qiscusChatRoom.setMember(withEmails);
186-
JsonArray comments = jsonElement.getAsJsonObject().get("results")
187-
.getAsJsonObject().get("comments").getAsJsonArray();
188-
189-
if (comments.size() > 0) {
190-
JsonObject lastComment = comments.get(0).getAsJsonObject();
191-
qiscusChatRoom.setLastCommentSender(lastComment.get("username").getAsString());
192-
qiscusChatRoom.setLastCommentSenderEmail(lastComment.get("email").getAsString());
193-
try {
194-
qiscusChatRoom.setLastCommentTime(dateFormat.parse(lastComment.get("timestamp").getAsString()));
195-
} catch (ParseException e) {
196-
e.printStackTrace();
174+
QiscusChatRoom qiscusChatRoom;
175+
if (jsonElement != null) {
176+
JsonObject jsonChatRoom = jsonElement.getAsJsonObject().get("results")
177+
.getAsJsonObject().get("room").getAsJsonObject();
178+
qiscusChatRoom = new QiscusChatRoom();
179+
qiscusChatRoom.setId(jsonChatRoom.get("id").getAsInt());
180+
//TODO minta server ngasih tau distinctId biar bisa disimpen
181+
//qiscusChatRoom.setDistinctId("default");
182+
qiscusChatRoom.setLastCommentId(jsonChatRoom.get("last_comment_id").getAsInt());
183+
qiscusChatRoom.setLastCommentMessage(jsonChatRoom.get("last_comment_message").getAsString());
184+
qiscusChatRoom.setLastTopicId(jsonChatRoom.get("last_topic_id").getAsInt());
185+
qiscusChatRoom.setOptions(jsonChatRoom.get("options").isJsonNull() ? null
186+
: jsonChatRoom.get("options").getAsString());
187+
//TODO minta server ngasih tau member room siapa aja
188+
//qiscusChatRoom.setMember(withEmails);
189+
JsonArray comments = jsonElement.getAsJsonObject().get("results")
190+
.getAsJsonObject().get("comments").getAsJsonArray();
191+
192+
if (comments.size() > 0) {
193+
JsonObject lastComment = comments.get(0).getAsJsonObject();
194+
qiscusChatRoom.setLastCommentSender(lastComment.get("username").getAsString());
195+
qiscusChatRoom.setLastCommentSenderEmail(lastComment.get("email").getAsString());
196+
try {
197+
qiscusChatRoom.setLastCommentTime(dateFormat.parse(lastComment.get("timestamp").getAsString()));
198+
} catch (ParseException e) {
199+
e.printStackTrace();
200+
}
197201
}
202+
return qiscusChatRoom;
203+
}
204+
qiscusChatRoom = Qiscus.getDataStore().getChatRoom(roomId);
205+
if (qiscusChatRoom == null) {
206+
throw new RuntimeException("Unable to connect with qiscus server!");
198207
}
199208
return qiscusChatRoom;
200209
});

0 commit comments

Comments
 (0)