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

Some refactoring

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