Commit 4535d9ee authored by Leonardo Aramaki's avatar Leonardo Aramaki

Return a default empty PublicSetting if server does not provide any

parent b69d908b
......@@ -5,6 +5,9 @@ import android.support.v4.util.Pair;
import com.hadisatrio.optional.Optional;
import javax.annotation.Nullable;
import chat.rocket.core.PublicSettingsConstants;
import chat.rocket.core.models.PublicSetting;
import chat.rocket.core.repositories.PublicSettingRepository;
import chat.rocket.persistence.realm.RealmStore;
......@@ -15,32 +18,44 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
import io.realm.RealmResults;
public class RealmPublicSettingRepository extends RealmRepository
implements PublicSettingRepository {
private final String hostname;
public RealmPublicSettingRepository(String hostname) {
this.hostname = hostname;
}
@Override
public Single<Optional<PublicSetting>> getById(String id) {
return Single.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> {
if (pair.first == null) {
return Flowable.empty();
}
return pair.first.where(RealmPublicSetting.class)
.equalTo(RealmPublicSetting.ID, id)
.findAll()
.<RealmResults<RealmPublicSetting>>asFlowable();
},
pair -> close(pair.first, pair.second)
)
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
.filter(it -> it.isLoaded() && it.isValid() && it.size() > 0)
.map(it -> Optional.of(it.get(0).asPublicSetting()))
.first(Optional.absent()));
}
implements PublicSettingRepository {
private final String hostname;
public RealmPublicSettingRepository(String hostname) {
this.hostname = hostname;
}
@Override
public Single<Optional<PublicSetting>> getById(String id) {
return Single.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> {
if (pair.first == null) {
return Flowable.empty();
}
return pair.first.where(RealmPublicSetting.class)
.equalTo(RealmPublicSetting.ID, id)
.findAll()
.<RealmResults<RealmPublicSetting>>asFlowable();
},
pair -> close(pair.first, pair.second)
)
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
.filter(it -> it.isLoaded() && it.isValid())
.map(it -> {
if (it.size() > 0) {
return Optional.of(it.get(0).asPublicSetting());
}
PublicSetting defaultSetting = PublicSetting.builder()
.setId(id)
.setValue("")
.setUpdatedAt(0L)
.build();
return Optional.of(defaultSetting);
})
.first(Optional.absent()));
}
}
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