Search in sources :

Example 1 with LoadMessageProcedure

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);
    });
}
Also used : Realm(io.realm.Realm) Optional(com.fernandocejas.arrow.optional.Optional) RealmResults(io.realm.RealmResults) RoomRepository(chat.rocket.core.repositories.RoomRepository) RxJavaInterop(hu.akarnokd.rxjava.interop.RxJavaInterop) RoomHistoryState(chat.rocket.core.models.RoomHistoryState) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) LoadMessageProcedure(chat.rocket.persistence.realm.models.internal.LoadMessageProcedure) ArrayList(java.util.ArrayList) List(java.util.List) Flowable(io.reactivex.Flowable) Pair(android.support.v4.util.Pair) Sort(io.realm.Sort) Room(chat.rocket.core.models.Room) Looper(android.os.Looper) RealmRoom(chat.rocket.persistence.realm.models.ddp.RealmRoom) Case(io.realm.Case) SortDirection(chat.rocket.core.SortDirection) RealmStore(chat.rocket.persistence.realm.RealmStore) Looper(android.os.Looper) LoadMessageProcedure(chat.rocket.persistence.realm.models.internal.LoadMessageProcedure) Realm(io.realm.Realm)

Example 2 with LoadMessageProcedure

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);
        }
    });
}
Also used : Context(android.content.Context) Realm(io.realm.Realm) RealmResults(io.realm.RealmResults) RCLog(chat.rocket.android.log.RCLog) LoadMessageProcedure(chat.rocket.persistence.realm.models.internal.LoadMessageProcedure) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage) MethodCallHelper(chat.rocket.android.api.MethodCallHelper) List(java.util.List) JSONObject(org.json.JSONObject) Sort(io.realm.Sort) RealmHelper(chat.rocket.persistence.realm.RealmHelper) SyncState(chat.rocket.core.SyncState) DDPClientRef(chat.rocket.android.service.DDPClientRef) Task(bolts.Task) LoadMessageProcedure(chat.rocket.persistence.realm.models.internal.LoadMessageProcedure) JSONObject(org.json.JSONObject) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage)

Aggregations

LoadMessageProcedure (chat.rocket.persistence.realm.models.internal.LoadMessageProcedure)2 Realm (io.realm.Realm)2 RealmResults (io.realm.RealmResults)2 Sort (io.realm.Sort)2 List (java.util.List)2 Context (android.content.Context)1 Looper (android.os.Looper)1 Pair (android.support.v4.util.Pair)1 Task (bolts.Task)1 MethodCallHelper (chat.rocket.android.api.MethodCallHelper)1 RCLog (chat.rocket.android.log.RCLog)1 DDPClientRef (chat.rocket.android.service.DDPClientRef)1 SortDirection (chat.rocket.core.SortDirection)1 SyncState (chat.rocket.core.SyncState)1 Room (chat.rocket.core.models.Room)1 RoomHistoryState (chat.rocket.core.models.RoomHistoryState)1 RoomRepository (chat.rocket.core.repositories.RoomRepository)1 RealmHelper (chat.rocket.persistence.realm.RealmHelper)1 RealmStore (chat.rocket.persistence.realm.RealmStore)1 RealmMessage (chat.rocket.persistence.realm.models.ddp.RealmMessage)1