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