Commit 0d0e4556 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Some refactoring

parent 692cd67f
package chat.rocket.android.service;
import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
......@@ -68,6 +69,7 @@ import io.reactivex.subjects.PublishSubject;
}
}
@SuppressLint("RxLeakedSubscription")
@DebugLog
@Override
public void ensureConnections() {
......@@ -84,6 +86,7 @@ import io.reactivex.subjects.PublishSubject;
});
}
@SuppressLint("RxLeakedSubscription")
@Override
public void addOrUpdateServer(String hostname, @Nullable String name, boolean insecure) {
RealmBasedServerInfo.addOrUpdate(hostname, name, insecure);
......@@ -95,6 +98,7 @@ import io.reactivex.subjects.PublishSubject;
}, RCLog::e);
}
@SuppressLint("RxLeakedSubscription")
@Override
public void removeServer(String hostname) {
RealmBasedServerInfo.remove(hostname);
......
......@@ -7,6 +7,7 @@ import com.hadisatrio.optional.Optional;
import chat.rocket.core.models.Session;
import chat.rocket.core.repositories.SessionRepository;
import chat.rocket.persistence.realm.RealmHelper;
import chat.rocket.persistence.realm.RealmStore;
import chat.rocket.persistence.realm.models.internal.RealmSession;
import hu.akarnokd.rxjava.interop.RxJavaInterop;
......@@ -35,7 +36,7 @@ public class RealmSessionRepository extends RealmRepository implements SessionRe
return RxJavaInterop.toV2Flowable(pair.first.where(RealmSession.class)
.equalTo(RealmSession.ID, id)
.findAll()
.<RealmSession>asObservable());
.<RealmSession>asObservable().first());
},
pair -> close(pair.first, pair.second)
)
......@@ -45,7 +46,7 @@ public class RealmSessionRepository extends RealmRepository implements SessionRe
if (realmSessions.size() == 0) {
return Optional.absent();
}
return Optional.of(realmSessions.get(0).asSession());
return Optional.of(realmSessions.get(0).asSession());
}));
}
......@@ -74,14 +75,9 @@ public class RealmSessionRepository extends RealmRepository implements SessionRe
realmSession.setTokenVerified(session.isTokenVerified());
realmSession.setError(session.getError());
realm.beginTransaction();
return RxJavaInterop.toV2Flowable(realm.copyToRealmOrUpdate(realmSession)
.asObservable())
return RealmHelper.copyToRealmOrUpdate(realm, realmSession)
.filter(it -> it != null && it.isLoaded() && it.isValid())
.firstElement()
.doOnSuccess(it -> realm.commitTransaction())
.doOnError(throwable -> realm.cancelTransaction())
.doOnEvent((realmObject, throwable) -> close(realm, looper))
.toSingle()
.map(realmObject -> true);
......
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