Commit 1e2ee218 authored by Lucio Maciel's avatar Lucio Maciel

Make RealmUserRepository more readable.

parent 1483458f
...@@ -3,9 +3,11 @@ package chat.rocket.persistence.realm.repositories; ...@@ -3,9 +3,11 @@ package chat.rocket.persistence.realm.repositories;
import android.os.Looper; import android.os.Looper;
import android.support.v4.util.Pair; import android.support.v4.util.Pair;
import com.hadisatrio.optional.Optional; import com.hadisatrio.optional.Optional;
import io.reactivex.Flowable; import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.realm.Case; import io.realm.Case;
import io.realm.Realm;
import io.realm.RealmResults; import io.realm.RealmResults;
import io.realm.Sort; import io.realm.Sort;
...@@ -26,38 +28,56 @@ public class RealmUserRepository extends RealmRepository implements UserReposito ...@@ -26,38 +28,56 @@ public class RealmUserRepository extends RealmRepository implements UserReposito
@Override @Override
public Flowable<Optional<User>> getCurrent() { public Flowable<Optional<User>> getCurrent() {
return Flowable.defer(() -> Flowable.using( return Flowable.defer(this::realmGetCurrent)
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable(
pair.first.where(RealmUser.class)
.isNotEmpty(RealmUser.EMAILS)
.findAll()
.<RealmResults<RealmUser>>asObservable()),
pair -> close(pair.first, pair.second)
)
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper())) .unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
.filter(it -> it != null && it.isLoaded() .filter(it -> it != null && it.isLoaded() && it.isValid())
&& it.isValid())
.map(realmUsers -> { .map(realmUsers -> {
if (realmUsers.size() > 0) { if (realmUsers.size() > 0) {
return Optional.of(realmUsers.get(0).asUser()); return Optional.of(realmUsers.get(0).asUser());
} }
return Optional.<User>absent(); return Optional.<User>absent();
})); });
}
private Flowable<RealmResults<RealmUser>> realmGetCurrent() {
return Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable(
pair.first.where(RealmUser.class)
.isNotEmpty(RealmUser.EMAILS)
.findAll()
.<RealmResults<RealmUser>>asObservable()),
pair -> close(pair.first, pair.second));
} }
@Override @Override
public Flowable<Optional<User>> getByUsername(String username) { public Flowable<Optional<User>> getByUsername(String username) {
return Flowable.defer(() -> Flowable.using( return Flowable.defer(() -> realmGetByUsername(username))
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
.map(optional -> {
if (optional.isPresent()) {
return Optional.of(optional.get().asUser());
}
return Optional.absent();
});
}
private Flowable<Optional<RealmUser>> realmGetByUsername(String username) {
return Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> { pair -> realmQueryUsername(pair.first, username),
RealmUser realmUser = pair.first.where(RealmUser.class) pair -> close(pair.first, pair.second));
}
private Flowable<Optional<RealmUser>> realmQueryUsername(Realm realm, String username) {
RealmUser realmUser = realm.where(RealmUser.class)
.equalTo(RealmUser.USERNAME, username) .equalTo(RealmUser.USERNAME, username)
.findFirst(); .findFirst();
if (realmUser == null) { if (realmUser == null) {
return Flowable.just(Optional.<RealmUser>absent()); return Flowable.just(Optional.absent());
} }
return RxJavaInterop.toV2Flowable( return RxJavaInterop.toV2Flowable(
...@@ -65,34 +85,26 @@ public class RealmUserRepository extends RealmRepository implements UserReposito ...@@ -65,34 +85,26 @@ public class RealmUserRepository extends RealmRepository implements UserReposito
.<RealmUser>asObservable() .<RealmUser>asObservable()
.filter(user -> user.isLoaded() && user.isValid()) .filter(user -> user.isLoaded() && user.isValid())
.map(Optional::of)); .map(Optional::of));
},
pair -> close(pair.first, pair.second)
)
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
.map(optional -> {
if (optional.isPresent()) {
return Optional.of(optional.get().asUser());
}
return Optional.absent();
}));
} }
@Override @Override
public Flowable<List<User>> getSortedLikeName(String name, int limit) { public Flowable<List<User>> getSortedLikeName(String name, int limit) {
return Flowable.defer(() -> Flowable.using( return Flowable.defer(() -> realmGetSortedLikeName(name))
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
.filter(realmUsers -> realmUsers != null && realmUsers.isLoaded()
&& realmUsers.isValid())
.map(realmUsers -> toList(safeSubList(realmUsers, 0, limit)));
}
private Flowable<RealmResults<RealmUser>> realmGetSortedLikeName(String name) {
return Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable( pair -> RxJavaInterop.toV2Flowable(
pair.first.where(RealmUser.class) pair.first.where(RealmUser.class)
.like(RealmUser.USERNAME, "*" + name + "*", Case.INSENSITIVE) .like(RealmUser.USERNAME, "*" + name + "*", Case.INSENSITIVE)
.findAllSorted(RealmUser.USERNAME, Sort.DESCENDING) .findAllSorted(RealmUser.USERNAME, Sort.DESCENDING)
.asObservable()), .asObservable()),
pair -> close(pair.first, pair.second) pair -> close(pair.first, pair.second));
)
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
.filter(realmUsers -> realmUsers != null && realmUsers.isLoaded()
&& realmUsers.isValid())
.map(realmUsers -> toList(safeSubList(realmUsers, 0, limit))));
} }
private List<User> toList(List<RealmUser> realmUsers) { private List<User> toList(List<RealmUser> realmUsers) {
......
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