Commit d593760b authored by Yusuke Iwaki's avatar Yusuke Iwaki

implement hasNext for LoadMessageProcedure

parent df75aae5
......@@ -202,7 +202,9 @@ public class MethodCallHelper {
if (timestamp == 0) {
realm.where(Message.class).equalTo("rid", roomId).findAll().deleteAllFromRealm();
}
realm.createOrUpdateAllFromJson(Message.class, messages);
if (messages.length() > 0) {
realm.createOrUpdateAllFromJson(Message.class, messages);
}
return null;
});
});
......
package chat.rocket.android.model.ddp;
import chat.rocket.android.model.SyncState;
import io.realm.RealmList;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
......@@ -12,9 +13,12 @@ import org.json.JSONObject;
@SuppressWarnings({"PMD.ShortClassName", "PMD.ShortVariable",
"PMD.MethodNamingConventions", "PMD.VariableNamingConventions"})
public class Message extends RealmObject {
//ref: Rocket.Chat:packages/rocketchat-lib/lib/MessageTypes.coffee
@PrimaryKey private String _id;
private String t; //type:
private String rid; //roomId.
private int syncstate;
private long ts;
private String msg;
private User u;
......@@ -22,11 +26,90 @@ public class Message extends RealmObject {
private RealmList<MessageAttachment> attachments;
private RealmList<MessageUrl> urls;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getT() {
return t;
}
public void setT(String t) {
this.t = t;
}
public String getRid() {
return rid;
}
public void setRid(String rid) {
this.rid = rid;
}
public int getSyncstate() {
return syncstate;
}
public void setSyncstate(int syncstate) {
this.syncstate = syncstate;
}
public long getTs() {
return ts;
}
public void setTs(long ts) {
this.ts = ts;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public User getU() {
return u;
}
public void setU(User u) {
this.u = u;
}
public boolean isGroupable() {
return groupable;
}
public void setGroupable(boolean groupable) {
this.groupable = groupable;
}
public RealmList<MessageAttachment> getAttachments() {
return attachments;
}
public void setAttachments(RealmList<MessageAttachment> attachments) {
this.attachments = attachments;
}
public RealmList<MessageUrl> getUrls() {
return urls;
}
public void setUrls(RealmList<MessageUrl> urls) {
this.urls = urls;
}
public static JSONObject customizeJson(JSONObject messageJson) throws JSONException {
long ts = messageJson.getJSONObject("ts").getLong("$date");
messageJson.remove("ts");
messageJson.put("ts", ts);
messageJson.put("ts", ts).put("syncstate", SyncState.SYNCED);
return messageJson;
}
......
......@@ -5,10 +5,13 @@ import bolts.Task;
import chat.rocket.android.helper.MethodCallHelper;
import chat.rocket.android.model.LoadMessageProcedure;
import chat.rocket.android.model.SyncState;
import chat.rocket.android.model.ddp.Message;
import chat.rocket.android.ws.RocketChatWebSocketAPI;
import io.realm.Realm;
import io.realm.RealmResults;
import io.realm.Sort;
import java.util.List;
import jp.co.crowdworks.realm_java_helpers.RealmHelper;
import jp.co.crowdworks.realm_java_helpers_bolts.RealmHelperBolts;
import org.json.JSONObject;
import timber.log.Timber;
......@@ -51,11 +54,21 @@ public class LoadMessageProcedureObserver extends AbstractModelObserver<LoadMess
.put("syncstate", SyncState.SYNCING))
).onSuccessTask(task ->
methodCall.loadHistory(roomId, isReset ? 0 : timestamp, count, lastSeen)
.onSuccessTask(_task ->
RealmHelperBolts.executeTransaction(realm ->
realm.createOrUpdateObjectFromJson(LoadMessageProcedure.class, new JSONObject()
.put("roomId", roomId)
.put("syncstate", SyncState.SYNCED))))
.onSuccessTask(_task -> {
Message lastMessage = RealmHelper.executeTransactionForRead(realm ->
realm.where(Message.class)
.equalTo("rid", roomId)
.equalTo("syncstate", SyncState.SYNCED)
.findAllSorted("ts", Sort.ASCENDING).last(null));
long lastTs = lastMessage != null ? lastMessage.getTs() : 0;
return RealmHelperBolts.executeTransaction(realm ->
realm.createOrUpdateObjectFromJson(LoadMessageProcedure.class, new JSONObject()
.put("roomId", roomId)
.put("syncstate", SyncState.SYNCED)
.put("timestamp", lastTs)
.put("hasNext", lastTs > 0))
);
})
).continueWithTask(task -> {
if (task.isFaulted()) {
Timber.w(task.getError());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment