Commit d8e2bef9 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Change new hostname reselection logic on logout

parent 2bf9e8c8
...@@ -60,7 +60,7 @@ public class RocketChatCache { ...@@ -60,7 +60,7 @@ public class RocketChatCache {
json = new JSONObject(hostnameList); json = new JSONObject(hostnameList);
} }
JSONObject serverInfoJson = new JSONObject(); JSONObject serverInfoJson = new JSONObject();
serverInfoJson.put("hostname", hostnameAvatarUri); serverInfoJson.put("avatar", hostnameAvatarUri);
serverInfoJson.put("sitename", siteName); serverInfoJson.put("sitename", siteName);
// Replace server avatar uri if exists. // Replace server avatar uri if exists.
json.put(hostname, hostnameAvatarUri == null ? JSONObject.NULL : serverInfoJson); json.put(hostname, hostnameAvatarUri == null ? JSONObject.NULL : serverInfoJson);
...@@ -82,7 +82,7 @@ public class RocketChatCache { ...@@ -82,7 +82,7 @@ public class RocketChatCache {
String hostname = iter.next(); String hostname = iter.next();
JSONObject serverInfoJson = jsonObj.getJSONObject(hostname); JSONObject serverInfoJson = jsonObj.getJSONObject(hostname);
serverList.add(new Pair<>(hostname, new Pair<>( serverList.add(new Pair<>(hostname, new Pair<>(
"http://" + hostname + "/" + serverInfoJson.getString("hostname"), "http://" + hostname + "/" + serverInfoJson.getString("avatar"),
serverInfoJson.getString("sitename")))); serverInfoJson.getString("sitename"))));
} }
return serverList; return serverList;
...@@ -100,12 +100,30 @@ public class RocketChatCache { ...@@ -100,12 +100,30 @@ public class RocketChatCache {
try { try {
JSONObject jsonObj = new JSONObject(json); JSONObject jsonObj = new JSONObject(json);
jsonObj.remove(hostname); jsonObj.remove(hostname);
setString(KEY_HOSTNAME_LIST, jsonObj.toString()); String result = jsonObj.length() == 0 ? null : jsonObj.toString();
setString(KEY_HOSTNAME_LIST, result);
} catch (JSONException e) { } catch (JSONException e) {
RCLog.e(e); RCLog.e(e);
} }
} }
@Nullable
public String getFirstLoggedHostnameIfAny() {
String json = getString(KEY_HOSTNAME_LIST, null);
if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
if (jsonObj.length() > 0 && jsonObj.keys().hasNext()) {
// Returns the first hostname on the list.
return jsonObj.keys().next();
}
} catch (JSONException e) {
RCLog.e(e);
}
}
return null;
}
public String getSelectedRoomId() { public String getSelectedRoomId() {
try { try {
JSONObject jsonObject = getSelectedRoomIdJsonObject(); JSONObject jsonObject = getSelectedRoomIdJsonObject();
...@@ -194,7 +212,9 @@ public class RocketChatCache { ...@@ -194,7 +212,9 @@ public class RocketChatCache {
try { try {
JSONObject selectedRoomIdJsonObject = getSelectedRoomIdJsonObject(); JSONObject selectedRoomIdJsonObject = getSelectedRoomIdJsonObject();
selectedRoomIdJsonObject.remove(currentHostname); selectedRoomIdJsonObject.remove(currentHostname);
setString(KEY_SELECTED_ROOM_ID, selectedRoomIdJsonObject.toString()); String result = selectedRoomIdJsonObject.length() == 0 ?
null : selectedRoomIdJsonObject.toString();
setString(KEY_SELECTED_ROOM_ID, result);
} catch (JSONException e) { } catch (JSONException e) {
Logger.report(e); Logger.report(e);
RCLog.e(e); RCLog.e(e);
......
...@@ -329,7 +329,7 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -329,7 +329,7 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
} }
@DebugLog @DebugLog
public void cleanUpBeforeLogout() { public void beforeLogoutCleanUp() {
presenter.beforeLogoutCleanUp(); presenter.beforeLogoutCleanUp();
} }
......
...@@ -45,8 +45,8 @@ public interface SidebarMainContract { ...@@ -45,8 +45,8 @@ public interface SidebarMainContract {
void onUserOffline(); void onUserOffline();
void onLogout(Continuation continuation); void onLogout(Continuation<Void, Object> continuation);
void cleanUpBeforeLogout(); void beforeLogoutCleanUp();
} }
} }
\ No newline at end of file
...@@ -339,14 +339,14 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -339,14 +339,14 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
private void setupLogoutButton() { private void setupLogoutButton() {
rootView.findViewById(R.id.btn_logout).setOnClickListener(view -> { rootView.findViewById(R.id.btn_logout).setOnClickListener(view -> {
closeUserActionContainer(); closeUserActionContainer();
// Clear relative data and set new hostname if any.
presenter.beforeLogoutCleanUp();
final Activity activity = getActivity(); final Activity activity = getActivity();
if (activity != null && activity instanceof MainActivity) { if (activity != null && activity instanceof MainActivity) {
((MainActivity) activity).showLogoutMessage(); ((MainActivity) activity).showLogoutMessage();
// Clear subscriptions. // Clear subscriptions on MainPresenter.
((MainActivity) activity).cleanUpBeforeLogout(); ((MainActivity) activity).beforeLogoutCleanUp();
} }
// Clear relative data and set new hostname if any.
presenter.cleanUpBeforeLogout();
}); });
} }
......
...@@ -22,7 +22,6 @@ import chat.rocket.android.shared.BasePresenter; ...@@ -22,7 +22,6 @@ import chat.rocket.android.shared.BasePresenter;
import chat.rocket.core.interactors.RoomInteractor; import chat.rocket.core.interactors.RoomInteractor;
import chat.rocket.core.models.Room; import chat.rocket.core.models.Room;
import chat.rocket.core.models.RoomSidebar; import chat.rocket.core.models.RoomSidebar;
import chat.rocket.core.models.ServerInfo;
import chat.rocket.core.models.Spotlight; import chat.rocket.core.models.Spotlight;
import chat.rocket.core.models.User; import chat.rocket.core.models.User;
import chat.rocket.core.repositories.SpotlightRepository; import chat.rocket.core.repositories.SpotlightRepository;
...@@ -139,7 +138,7 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View ...@@ -139,7 +138,7 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
} }
@Override @Override
public void onLogout(Continuation continuation) { public void onLogout(Continuation<Void, Object> continuation) {
methodCallHelper.logout().continueWith(task -> { methodCallHelper.logout().continueWith(task -> {
if (task.isFaulted()) { if (task.isFaulted()) {
Logger.report(task.getError()); Logger.report(task.getError());
...@@ -150,21 +149,17 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View ...@@ -150,21 +149,17 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
} }
@Override @Override
public void cleanUpBeforeLogout() { public void beforeLogoutCleanUp() {
clearSubscriptions();
String currentHostname = rocketChatCache.getSelectedServerHostname(); String currentHostname = rocketChatCache.getSelectedServerHostname();
RealmHelper realmHelper = RealmStore.getOrCreate(currentHostname); RealmHelper realmHelper = RealmStore.getOrCreate(currentHostname);
realmHelper.executeTransaction(realm -> { realmHelper.executeTransaction(realm -> {
realm.deleteAll(); realm.deleteAll();
ConnectivityManagerApi connectivityManagerApi = ConnectivityManager.getInstance(RocketChatApplication.getInstance()); ConnectivityManagerApi connectivityManagerApi = ConnectivityManager.getInstance(RocketChatApplication.getInstance());
connectivityManagerApi.removeServer(currentHostname); connectivityManagerApi.removeServer(currentHostname);
List<ServerInfo> serverList = connectivityManagerApi.getServerList();
String newHostname = null;
if (serverList != null && serverList.size() > 0) {
newHostname = serverList.get(0).getHostname();
}
rocketChatCache.removeHostname(currentHostname); rocketChatCache.removeHostname(currentHostname);
rocketChatCache.removeSelectedRoomId(currentHostname); rocketChatCache.removeSelectedRoomId(currentHostname);
rocketChatCache.setSelectedServerHostname(newHostname); rocketChatCache.setSelectedServerHostname(rocketChatCache.getFirstLoggedHostnameIfAny());
view.onLogoutCleanUp(); view.onLogoutCleanUp();
return null; return null;
}); });
......
...@@ -29,11 +29,17 @@ public class RealmLoginServiceConfigurationRepository extends RealmRepository ...@@ -29,11 +29,17 @@ public class RealmLoginServiceConfigurationRepository extends RealmRepository
public Single<Optional<LoginServiceConfiguration>> getByName(String serviceName) { public Single<Optional<LoginServiceConfiguration>> getByName(String serviceName) {
return Single.defer(() -> Flowable.using( return Single.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable( pair -> {
pair.first.where(RealmMeteorLoginServiceConfiguration.class) if (pair.first == null) {
.equalTo(RealmMeteorLoginServiceConfiguration.SERVICE, serviceName) return Flowable.empty();
.findAll() }
.<RealmResults<RealmMeteorLoginServiceConfiguration>>asObservable()),
return RxJavaInterop.toV2Flowable(
pair.first.where(RealmMeteorLoginServiceConfiguration.class)
.equalTo(RealmMeteorLoginServiceConfiguration.SERVICE, serviceName)
.findAll()
.<RealmResults<RealmMeteorLoginServiceConfiguration>>asObservable());
},
pair -> close(pair.first, pair.second) pair -> close(pair.first, pair.second)
) )
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper())) .unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
...@@ -46,10 +52,16 @@ public class RealmLoginServiceConfigurationRepository extends RealmRepository ...@@ -46,10 +52,16 @@ public class RealmLoginServiceConfigurationRepository extends RealmRepository
public Flowable<List<LoginServiceConfiguration>> getAll() { public Flowable<List<LoginServiceConfiguration>> getAll() {
return Flowable.defer(() -> Flowable.using( return Flowable.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop pair -> {
.toV2Flowable(pair.first.where(RealmMeteorLoginServiceConfiguration.class) if (pair.first == null) {
.findAll() return Flowable.empty();
.asObservable()), }
return RxJavaInterop
.toV2Flowable(pair.first.where(RealmMeteorLoginServiceConfiguration.class)
.findAll()
.asObservable());
},
pair -> close(pair.first, pair.second) pair -> close(pair.first, pair.second)
) )
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper())) .unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
......
...@@ -36,11 +36,17 @@ public class RealmMessageRepository extends RealmRepository implements MessageRe ...@@ -36,11 +36,17 @@ public class RealmMessageRepository extends RealmRepository implements MessageRe
public Single<Optional<Message>> getById(String messageId) { public Single<Optional<Message>> getById(String messageId) {
return Single.defer(() -> Flowable.using( return Single.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable( pair -> {
pair.first.where(RealmMessage.class) if (pair.first == null) {
.equalTo(RealmMessage.ID, messageId) return Flowable.empty();
.findAll() }
.<RealmResults<RealmMessage>>asObservable()),
return RxJavaInterop.toV2Flowable(
pair.first.where(RealmMessage.class)
.equalTo(RealmMessage.ID, messageId)
.findAll()
.<RealmResults<RealmMessage>>asObservable());
},
pair -> close(pair.first, pair.second) pair -> close(pair.first, pair.second)
) )
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper())) .unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
...@@ -133,11 +139,17 @@ public class RealmMessageRepository extends RealmRepository implements MessageRe ...@@ -133,11 +139,17 @@ public class RealmMessageRepository extends RealmRepository implements MessageRe
public Flowable<List<Message>> getAllFrom(Room room) { public Flowable<List<Message>> getAllFrom(Room room) {
return Flowable.defer(() -> Flowable.using( return Flowable.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable(pair.first.where(RealmMessage.class) pair -> {
.equalTo(RealmMessage.ROOM_ID, room.getRoomId()) if (pair.first == null) {
.isNotNull(RealmMessage.USER) return Flowable.empty();
.findAllSorted(RealmMessage.TIMESTAMP, Sort.DESCENDING) }
.asObservable()),
return RxJavaInterop.toV2Flowable(pair.first.where(RealmMessage.class)
.equalTo(RealmMessage.ROOM_ID, room.getRoomId())
.isNotNull(RealmMessage.USER)
.findAllSorted(RealmMessage.TIMESTAMP, Sort.DESCENDING)
.asObservable());
},
pair -> close(pair.first, pair.second) pair -> close(pair.first, pair.second)
) )
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper())) .unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
...@@ -150,12 +162,18 @@ public class RealmMessageRepository extends RealmRepository implements MessageRe ...@@ -150,12 +162,18 @@ public class RealmMessageRepository extends RealmRepository implements MessageRe
public Single<Integer> unreadCountFor(Room room, User user) { public Single<Integer> unreadCountFor(Room room, User user) {
return Single.defer(() -> Flowable.using( return Single.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable(pair.first.where(RealmMessage.class) pair -> {
.equalTo(RealmMessage.ROOM_ID, room.getId()) if (pair.first == null) {
.greaterThanOrEqualTo(RealmMessage.TIMESTAMP, room.getLastSeen()) return Flowable.empty();
.notEqualTo(RealmMessage.USER_ID, user.getId()) }
.findAll()
.asObservable()), return RxJavaInterop.toV2Flowable(pair.first.where(RealmMessage.class)
.equalTo(RealmMessage.ROOM_ID, room.getId())
.greaterThanOrEqualTo(RealmMessage.TIMESTAMP, room.getLastSeen())
.notEqualTo(RealmMessage.USER_ID, user.getId())
.findAll()
.asObservable());
},
pair -> close(pair.first, pair.second) pair -> close(pair.first, pair.second)
) )
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper())) .unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
......
...@@ -26,11 +26,16 @@ public class RealmPermissionRepository extends RealmRepository implements Permis ...@@ -26,11 +26,16 @@ public class RealmPermissionRepository extends RealmRepository implements Permis
public Single<Optional<Permission>> getById(String id) { public Single<Optional<Permission>> getById(String id) {
return Single.defer(() -> Flowable.using( return Single.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable( pair -> {
pair.first.where(RealmPermission.class) if (pair.first == null) {
.equalTo(RealmPermission.Columns.ID, id) return Flowable.empty();
.findAll() }
.<RealmResults<RealmPermission>>asObservable()), return RxJavaInterop.toV2Flowable(
pair.first.where(RealmPermission.class)
.equalTo(RealmPermission.Columns.ID, id)
.findAll()
.<RealmResults<RealmPermission>>asObservable());
},
pair -> close(pair.first, pair.second) pair -> close(pair.first, pair.second)
) )
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper())) .unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
......
...@@ -27,11 +27,16 @@ public class RealmPublicSettingRepository extends RealmRepository ...@@ -27,11 +27,16 @@ public class RealmPublicSettingRepository extends RealmRepository
public Single<Optional<PublicSetting>> getById(String id) { public Single<Optional<PublicSetting>> getById(String id) {
return Single.defer(() -> Flowable.using( return Single.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable( pair -> {
pair.first.where(RealmPublicSetting.class) if (pair.first == null) {
.equalTo(RealmPublicSetting.ID, id) return Flowable.empty();
.findAll() }
.<RealmResults<RealmPublicSetting>>asObservable()), return RxJavaInterop.toV2Flowable(
pair.first.where(RealmPublicSetting.class)
.equalTo(RealmPublicSetting.ID, id)
.findAll()
.<RealmResults<RealmPublicSetting>>asObservable());
},
pair -> close(pair.first, pair.second) pair -> close(pair.first, pair.second)
) )
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper())) .unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
......
...@@ -34,10 +34,16 @@ public class RealmRoomRepository extends RealmRepository implements RoomReposito ...@@ -34,10 +34,16 @@ public class RealmRoomRepository extends RealmRepository implements RoomReposito
public Flowable<List<Room>> getAll() { public Flowable<List<Room>> getAll() {
return Flowable.defer(() -> Flowable.using( return Flowable.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable( pair -> {
pair.first.where(RealmRoom.class) if (pair.first == null) {
.findAll() return Flowable.empty();
.asObservable()), }
return RxJavaInterop.toV2Flowable(
pair.first.where(RealmRoom.class)
.findAll()
.asObservable());
},
pair -> close(pair.first, pair.second) pair -> close(pair.first, pair.second)
) )
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper())) .unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
...@@ -51,6 +57,10 @@ public class RealmRoomRepository extends RealmRepository implements RoomReposito ...@@ -51,6 +57,10 @@ public class RealmRoomRepository extends RealmRepository implements RoomReposito
return Flowable.defer(() -> Flowable.using( return Flowable.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> { pair -> {
if (pair.first == null) {
return Flowable.empty();
}
RealmRoom realmRoom = pair.first.where(RealmRoom.class) RealmRoom realmRoom = pair.first.where(RealmRoom.class)
.equalTo(RealmRoom.ROOM_ID, roomId) .equalTo(RealmRoom.ROOM_ID, roomId)
.findFirst(); .findFirst();
...@@ -150,17 +160,22 @@ public class RealmRoomRepository extends RealmRepository implements RoomReposito ...@@ -150,17 +160,22 @@ public class RealmRoomRepository extends RealmRepository implements RoomReposito
public Flowable<List<Room>> getSortedLikeName(String name, SortDirection direction, int limit) { public Flowable<List<Room>> getSortedLikeName(String name, SortDirection direction, int limit) {
return Flowable.defer(() -> Flowable.using( return Flowable.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable( pair -> {
pair.first.where(RealmRoom.class) if (pair.first == null) {
.like(RealmRoom.NAME, "*" + name + "*", Case.INSENSITIVE) return Flowable.empty();
.beginGroup() }
.equalTo(RealmRoom.TYPE, RealmRoom.TYPE_CHANNEL) return RxJavaInterop.toV2Flowable(
.or() pair.first.where(RealmRoom.class)
.equalTo(RealmRoom.TYPE, RealmRoom.TYPE_PRIVATE) .like(RealmRoom.NAME, "*" + name + "*", Case.INSENSITIVE)
.endGroup() .beginGroup()
.findAllSorted(RealmRoom.NAME, .equalTo(RealmRoom.TYPE, RealmRoom.TYPE_CHANNEL)
direction.equals(SortDirection.ASC) ? Sort.ASCENDING : Sort.DESCENDING) .or()
.asObservable()), .equalTo(RealmRoom.TYPE, RealmRoom.TYPE_PRIVATE)
.endGroup()
.findAllSorted(RealmRoom.NAME,
direction.equals(SortDirection.ASC) ? Sort.ASCENDING : Sort.DESCENDING)
.asObservable());
},
pair -> close(pair.first, pair.second) pair -> close(pair.first, pair.second)
) )
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper())) .unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
...@@ -173,15 +188,20 @@ public class RealmRoomRepository extends RealmRepository implements RoomReposito ...@@ -173,15 +188,20 @@ public class RealmRoomRepository extends RealmRepository implements RoomReposito
public Flowable<List<Room>> getLatestSeen(int limit) { public Flowable<List<Room>> getLatestSeen(int limit) {
return Flowable.defer(() -> Flowable.using( return Flowable.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable( pair -> {
pair.first.where(RealmRoom.class) if (pair.first == null) {
.beginGroup() return Flowable.empty();
.equalTo(RealmRoom.TYPE, RealmRoom.TYPE_CHANNEL) }
.or() return RxJavaInterop.toV2Flowable(
.equalTo(RealmRoom.TYPE, RealmRoom.TYPE_PRIVATE) pair.first.where(RealmRoom.class)
.endGroup() .beginGroup()
.findAllSorted(RealmRoom.LAST_SEEN, Sort.ASCENDING) .equalTo(RealmRoom.TYPE, RealmRoom.TYPE_CHANNEL)
.asObservable()), .or()
.equalTo(RealmRoom.TYPE, RealmRoom.TYPE_PRIVATE)
.endGroup()
.findAllSorted(RealmRoom.LAST_SEEN, Sort.ASCENDING)
.asObservable());
},
pair -> close(pair.first, pair.second) pair -> close(pair.first, pair.second)
) )
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper())) .unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
......
...@@ -29,12 +29,17 @@ public class RealmRoomRoleRepository extends RealmRepository implements RoomRole ...@@ -29,12 +29,17 @@ public class RealmRoomRoleRepository extends RealmRepository implements RoomRole
public Single<Optional<RoomRole>> getFor(Room room, User user) { public Single<Optional<RoomRole>> getFor(Room room, User user) {
return Single.defer(() -> Flowable.using( return Single.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable( pair -> {
pair.first.where(RealmRoomRole.class) if (pair.first == null) {
.equalTo(RealmRoomRole.Columns.ROOM_ID, room.getId()) return Flowable.empty();
.equalTo(RealmRoomRole.Columns.USER + "." + RealmUser.ID, user.getId()) }
.findAll() return RxJavaInterop.toV2Flowable(
.<RealmResults<RealmRoomRole>>asObservable()), pair.first.where(RealmRoomRole.class)
.equalTo(RealmRoomRole.Columns.ROOM_ID, room.getId())
.equalTo(RealmRoomRole.Columns.USER + "." + RealmUser.ID, user.getId())
.findAll()
.<RealmResults<RealmRoomRole>>asObservable());
},
pair -> close(pair.first, pair.second) pair -> close(pair.first, pair.second)
) )
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper())) .unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
......
...@@ -26,11 +26,17 @@ public class RealmSessionRepository extends RealmRepository implements SessionRe ...@@ -26,11 +26,17 @@ public class RealmSessionRepository extends RealmRepository implements SessionRe
public Flowable<Optional<Session>> getById(int id) { public Flowable<Optional<Session>> getById(int id) {
return Flowable.defer(() -> Flowable.using( return Flowable.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable( pair -> {
pair.first.where(RealmSession.class) if (pair.first == null) {
.equalTo(RealmSession.ID, id) return Flowable.empty();
.findAll() }
.<RealmSession>asObservable()),
return RxJavaInterop.toV2Flowable(
pair.first.where(RealmSession.class)
.equalTo(RealmSession.ID, id)
.findAll()
.<RealmSession>asObservable());
},
pair -> close(pair.first, pair.second) pair -> close(pair.first, pair.second)
) )
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper())) .unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
......
...@@ -20,7 +20,12 @@ class RealmSpotlightRepository(private val hostname: String) : RealmRepository() ...@@ -20,7 +20,12 @@ class RealmSpotlightRepository(private val hostname: String) : RealmRepository()
override fun getSuggestionsFor(term: String, limit: Int): Flowable<List<Spotlight>> { override fun getSuggestionsFor(term: String, limit: Int): Flowable<List<Spotlight>> {
return Flowable.defer { Flowable.using<RealmResults<RealmSpotlight>, Pair<Realm, Looper>>({ return Flowable.defer { Flowable.using<RealmResults<RealmSpotlight>, Pair<Realm, Looper>>({
Pair(RealmStore.getRealm(hostname), Looper.myLooper()) Pair(RealmStore.getRealm(hostname), Looper.myLooper())
}, { pair -> RxJavaInterop.toV2Flowable<RealmResults<RealmSpotlight>>(pair.first.where(RealmSpotlight::class.java) }, { pair ->
if (pair.first == null) {
return@using Flowable.empty()
}
return@using RxJavaInterop.toV2Flowable<RealmResults<RealmSpotlight>>(pair.first.where(RealmSpotlight::class.java)
.findAllSorted(Columns.TYPE, Sort.DESCENDING) .findAllSorted(Columns.TYPE, Sort.DESCENDING)
.asObservable()) .asObservable())
}) { pair -> close(pair.first, pair.second) } }) { pair -> close(pair.first, pair.second) }
......
...@@ -29,16 +29,22 @@ public class RealmSpotlightRoomRepository extends RealmRepository implements Spo ...@@ -29,16 +29,22 @@ public class RealmSpotlightRoomRepository extends RealmRepository implements Spo
public Flowable<List<SpotlightRoom>> getSuggestionsFor(String name, SortDirection direction, int limit) { public Flowable<List<SpotlightRoom>> getSuggestionsFor(String name, SortDirection direction, int limit) {
return Flowable.defer(() -> Flowable.using( return Flowable.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable( pair -> {
pair.first.where(RealmSpotlightRoom.class) if (pair.first == null) {
.like(RealmSpotlightRoom.Columns.NAME, "*" + name + "*", Case.INSENSITIVE) return Flowable.empty();
.beginGroup() }
.equalTo(RealmSpotlightRoom.Columns.TYPE, RealmRoom.TYPE_CHANNEL)
.or() return RxJavaInterop.toV2Flowable(
.equalTo(RealmSpotlightRoom.Columns.TYPE, RealmRoom.TYPE_PRIVATE) pair.first.where(RealmSpotlightRoom.class)
.endGroup() .like(RealmSpotlightRoom.Columns.NAME, "*" + name + "*", Case.INSENSITIVE)
.findAllSorted(RealmSpotlightRoom.Columns.NAME, direction.equals(SortDirection.ASC) ? Sort.ASCENDING : Sort.DESCENDING) .beginGroup()
.asObservable()), .equalTo(RealmSpotlightRoom.Columns.TYPE, RealmRoom.TYPE_CHANNEL)
.or()
.equalTo(RealmSpotlightRoom.Columns.TYPE, RealmRoom.TYPE_PRIVATE)
.endGroup()
.findAllSorted(RealmSpotlightRoom.Columns.NAME, direction.equals(SortDirection.ASC) ? Sort.ASCENDING : Sort.DESCENDING)
.asObservable());
},
pair -> close(pair.first, pair.second) pair -> close(pair.first, pair.second)
) )
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper())) .unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
......
...@@ -28,20 +28,26 @@ public class RealmSpotlightUserRepository extends RealmRepository implements Spo ...@@ -28,20 +28,26 @@ public class RealmSpotlightUserRepository extends RealmRepository implements Spo
public Flowable<List<SpotlightUser>> getSuggestionsFor(String name, SortDirection direction, int limit) { public Flowable<List<SpotlightUser>> getSuggestionsFor(String name, SortDirection direction, int limit) {
return Flowable.defer(() -> Flowable.using( return Flowable.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable( pair -> {
pair.first.where(RealmSpotlightUser.class) if (pair.first == null) {
.beginGroup() return Flowable.empty();
.like(RealmSpotlightUser.Columns.USERNAME, "*" + name + "*", Case.INSENSITIVE) }
.isNull(RealmSpotlightUser.Columns.NAME)
.endGroup() return RxJavaInterop.toV2Flowable(
.or() pair.first.where(RealmSpotlightUser.class)
.beginGroup() .beginGroup()
.like(RealmSpotlightUser.Columns.NAME, "*" + name + "*", Case.INSENSITIVE) .like(RealmSpotlightUser.Columns.USERNAME, "*" + name + "*", Case.INSENSITIVE)
.isNotNull(RealmSpotlightUser.Columns.USERNAME) .isNull(RealmSpotlightUser.Columns.NAME)
.endGroup() .endGroup()
.findAllSorted(RealmSpotlightUser.Columns.USERNAME, .or()
direction.equals(SortDirection.ASC) ? Sort.ASCENDING : Sort.DESCENDING) .beginGroup()
.asObservable()), .like(RealmSpotlightUser.Columns.NAME, "*" + name + "*", Case.INSENSITIVE)
.isNotNull(RealmSpotlightUser.Columns.USERNAME)
.endGroup()
.findAllSorted(RealmSpotlightUser.Columns.USERNAME,
direction.equals(SortDirection.ASC) ? Sort.ASCENDING : Sort.DESCENDING)
.asObservable());
},
pair -> close(pair.first, pair.second) pair -> close(pair.first, pair.second)
) )
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper())) .unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
......
...@@ -31,10 +31,16 @@ public class RealmUserRepository extends RealmRepository implements UserReposito ...@@ -31,10 +31,16 @@ public class RealmUserRepository extends RealmRepository implements UserReposito
public Flowable<List<User>> getAll() { public Flowable<List<User>> getAll() {
return Flowable.defer(() -> Flowable.using( return Flowable.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable( pair -> {
pair.first.where(RealmUser.class) if (pair.first == null) {
.findAll() return Flowable.empty();
.asObservable()), }
return RxJavaInterop.toV2Flowable(
pair.first.where(RealmUser.class)
.findAll()
.asObservable());
},
pair -> close(pair.first, pair.second)) pair -> close(pair.first, pair.second))
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper())) .unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
.filter(roomSubscriptions -> roomSubscriptions != null && roomSubscriptions.isLoaded() .filter(roomSubscriptions -> roomSubscriptions != null && roomSubscriptions.isLoaded()
...@@ -64,6 +70,7 @@ public class RealmUserRepository extends RealmRepository implements UserReposito ...@@ -64,6 +70,7 @@ public class RealmUserRepository extends RealmRepository implements UserReposito
if (pair.first == null) { if (pair.first == null) {
return Flowable.empty(); return Flowable.empty();
} }
return RxJavaInterop.toV2Flowable( return RxJavaInterop.toV2Flowable(
pair.first.where(RealmUser.class) pair.first.where(RealmUser.class)
.isNotEmpty(RealmUser.EMAILS) .isNotEmpty(RealmUser.EMAILS)
...@@ -95,6 +102,10 @@ public class RealmUserRepository extends RealmRepository implements UserReposito ...@@ -95,6 +102,10 @@ public class RealmUserRepository extends RealmRepository implements UserReposito
} }
private Flowable<Optional<RealmUser>> realmQueryUsername(Realm realm, String username) { private Flowable<Optional<RealmUser>> realmQueryUsername(Realm realm, String username) {
if (realm == null) {
return Flowable.empty();
}
RealmUser realmUser = realm.where(RealmUser.class) RealmUser realmUser = realm.where(RealmUser.class)
.equalTo(RealmUser.USERNAME, username) .equalTo(RealmUser.USERNAME, username)
.findFirst(); .findFirst();
...@@ -123,11 +134,17 @@ public class RealmUserRepository extends RealmRepository implements UserReposito ...@@ -123,11 +134,17 @@ public class RealmUserRepository extends RealmRepository implements UserReposito
private Flowable<RealmResults<RealmUser>> realmGetSortedLikeName(String name) { private Flowable<RealmResults<RealmUser>> realmGetSortedLikeName(String name) {
return Flowable.using( return Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable( pair -> {
pair.first.where(RealmUser.class) if (pair.first == null) {
.like(RealmUser.USERNAME, "*" + name + "*", Case.INSENSITIVE) return Flowable.empty();
.findAllSorted(RealmUser.USERNAME, Sort.DESCENDING) }
.asObservable()),
return RxJavaInterop.toV2Flowable(
pair.first.where(RealmUser.class)
.like(RealmUser.USERNAME, "*" + name + "*", Case.INSENSITIVE)
.findAllSorted(RealmUser.USERNAME, Sort.DESCENDING)
.asObservable());
},
pair -> close(pair.first, pair.second)); pair -> close(pair.first, pair.second));
} }
......
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