Search in sources :

Example 1 with RoomHistoryState

use of chat.rocket.core.models.RoomHistoryState 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)

Aggregations

Looper (android.os.Looper)1 Pair (android.support.v4.util.Pair)1 SortDirection (chat.rocket.core.SortDirection)1 Room (chat.rocket.core.models.Room)1 RoomHistoryState (chat.rocket.core.models.RoomHistoryState)1 RoomRepository (chat.rocket.core.repositories.RoomRepository)1 RealmStore (chat.rocket.persistence.realm.RealmStore)1 RealmRoom (chat.rocket.persistence.realm.models.ddp.RealmRoom)1 LoadMessageProcedure (chat.rocket.persistence.realm.models.internal.LoadMessageProcedure)1 Optional (com.fernandocejas.arrow.optional.Optional)1 RxJavaInterop (hu.akarnokd.rxjava.interop.RxJavaInterop)1 Flowable (io.reactivex.Flowable)1 Single (io.reactivex.Single)1 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)1 Case (io.realm.Case)1 Realm (io.realm.Realm)1 RealmResults (io.realm.RealmResults)1 Sort (io.realm.Sort)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1