use of chat.rocket.persistence.realm.models.internal.LoadMessageProcedure in project Rocket.Chat.Android by RocketChat.
the class RealmRoomRepository method setHistoryState.
@Override
public Single<Boolean> setHistoryState(RoomHistoryState roomHistoryState) {
return Single.defer(() -> {
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null || looper == null) {
return Single.just(false);
}
LoadMessageProcedure loadMessage = new LoadMessageProcedure();
loadMessage.setRoomId(roomHistoryState.getRoomId());
loadMessage.setSyncState(roomHistoryState.getSyncState());
loadMessage.setCount(roomHistoryState.getCount());
loadMessage.setReset(roomHistoryState.isReset());
loadMessage.setHasNext(!roomHistoryState.isComplete());
loadMessage.setTimestamp(roomHistoryState.getTimestamp());
realm.beginTransaction();
return RxJavaInterop.toV2Flowable(realm.copyToRealmOrUpdate(loadMessage).asObservable()).filter(realmObject -> realmObject.isLoaded() && realmObject.isValid()).firstElement().doOnSuccess(it -> realm.commitTransaction()).doOnError(throwable -> realm.cancelTransaction()).doOnEvent((realmObject, throwable) -> close(realm, looper)).toSingle().map(realmObject -> true);
});
}
use of chat.rocket.persistence.realm.models.internal.LoadMessageProcedure in project Rocket.Chat.Android by RocketChat.
the class LoadMessageProcedureObserver method onUpdateResults.
@Override
public void onUpdateResults(List<LoadMessageProcedure> results) {
if (results == null || results.isEmpty()) {
return;
}
LoadMessageProcedure procedure = results.get(0);
final String roomId = procedure.getRoomId();
final boolean isReset = procedure.isReset();
final long timestamp = procedure.getTimestamp();
final int count = procedure.getCount();
// TODO: Not implemented yet.
final long lastSeen = 0;
realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(LoadMessageProcedure.class, new JSONObject().put(LoadMessageProcedure.ID, roomId).put(LoadMessageProcedure.SYNC_STATE, SyncState.SYNCING))).onSuccessTask(task -> methodCall.loadHistory(roomId, isReset ? 0 : timestamp, count, lastSeen).onSuccessTask(_task -> {
RealmMessage lastMessage = realmHelper.executeTransactionForRead(realm -> realm.where(RealmMessage.class).equalTo(RealmMessage.ROOM_ID, roomId).equalTo(RealmMessage.SYNC_STATE, SyncState.SYNCED).findAllSorted(RealmMessage.TIMESTAMP, Sort.ASCENDING).first(null));
long lastTs = lastMessage != null ? lastMessage.getTimestamp() : 0;
int messageCount = _task.getResult().length();
return realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(LoadMessageProcedure.class, new JSONObject().put(LoadMessageProcedure.ID, roomId).put(LoadMessageProcedure.SYNC_STATE, SyncState.SYNCED).put(LoadMessageProcedure.TIMESTAMP, lastTs).put(LoadMessageProcedure.RESET, false).put(LoadMessageProcedure.HAS_NEXT, messageCount == count)));
})).continueWithTask(task -> {
if (task.isFaulted()) {
RCLog.w(task.getError());
return realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(LoadMessageProcedure.class, new JSONObject().put(LoadMessageProcedure.ID, roomId).put(LoadMessageProcedure.SYNC_STATE, SyncState.FAILED)));
} else {
return Task.forResult(null);
}
});
}
Aggregations