Commit 7c464ecc authored by Yusuke Iwaki's avatar Yusuke Iwaki

refactor querying default session.

parent 1a19e91d
......@@ -93,8 +93,7 @@ public class MainActivity extends AbstractAuthedActivity {
sessionObserver = realmHelper
.createObjectObserver(realm ->
realm.where(Session.class)
.equalTo("sessionId", Session.DEFAULT_ID)
Session.queryDefaultSession(realm)
.isNotNull("token")
.equalTo("tokenVerified", true)
.isNull("error"))
......
......@@ -42,8 +42,7 @@ public class ServerConfigActivity extends AbstractFragmentActivity {
}
sessionObserver = RealmStore.get(serverConfigId)
.createObjectObserver(realm ->
realm.where(Session.class).equalTo("sessionId", Session.DEFAULT_ID))
.createObjectObserver(Session::queryDefaultSession)
.setOnUpdateListener(this::onRenderServerConfigSession);
setContentView(R.layout.simple_screen);
......
......@@ -86,7 +86,7 @@ public class RoomFragment extends AbstractChatRoomFragment
userId = realmHelper.executeTransactionForRead(realm ->
User.queryCurrentUser(realm).findFirst()).get_id();
token = realmHelper.executeTransactionForRead(realm ->
realm.where(Session.class).equalTo("sessionId", Session.DEFAULT_ID).findFirst()).getToken();
Session.queryDefaultSession(realm).findFirst()).getToken();
roomObserver = realmHelper
.createObjectObserver(realm -> realm.where(RoomSubscription.class).equalTo("rid", roomId))
.setOnUpdateListener(this::onRenderRoom);
......
......@@ -24,8 +24,7 @@ public class RetryLoginFragment extends AbstractServerConfigFragment {
@Override public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sessionObserver = RealmStore.get(serverConfigId)
.createObjectObserver(realm ->
realm.where(Session.class).equalTo("sessionId", Session.DEFAULT_ID))
.createObjectObserver(Session::queryDefaultSession)
.setOnUpdateListener(this::onRenderServerConfigSession);
}
......
......@@ -4,7 +4,9 @@ import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.realm_helper.RealmHelper;
import hugo.weaving.DebugLog;
import io.realm.Realm;
import io.realm.RealmObject;
import io.realm.RealmQuery;
import io.realm.annotations.PrimaryKey;
import org.json.JSONObject;
......@@ -50,6 +52,10 @@ public class Session extends RealmObject {
this.error = error;
}
public static RealmQuery<Session> queryDefaultSession(Realm realm) {
return realm.where(Session.class).equalTo("sessionId", Session.DEFAULT_ID);
}
/**
* Log the server connection is lost due to soem exception.
*/
......
......@@ -91,8 +91,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
private void forceInvalidateTokens() {
serverConfigRealm.executeTransaction(realm -> {
Session session = realm.where(Session.class)
.equalTo("sessionId", Session.DEFAULT_ID).findFirst();
Session session = Session.queryDefaultSession(realm).findFirst();
if (session != null
&& !TextUtils.isEmpty(session.getToken())
&& (session.isTokenVerified() || !TextUtils.isEmpty(session.getError()))) {
......@@ -159,9 +158,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
.put("serverConfigId", serverConfigId)
.put("session", session))
).onSuccess(_task -> serverConfigRealm.executeTransaction(realm -> {
Session sessionObj = realm.where(Session.class)
.equalTo("sessionId", Session.DEFAULT_ID)
.findFirst();
Session sessionObj = Session.queryDefaultSession(realm).findFirst();
if (sessionObj == null) {
realm.createOrUpdateObjectFromJson(Session.class,
......
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