Commit e173fca0 authored by Yusuke Iwaki's avatar Yusuke Iwaki

fix logout

parent b83c2e63
...@@ -9,9 +9,9 @@ import chat.rocket.android.R; ...@@ -9,9 +9,9 @@ import chat.rocket.android.R;
import chat.rocket.android.fragment.chatroom.HomeFragment; import chat.rocket.android.fragment.chatroom.HomeFragment;
import chat.rocket.android.fragment.chatroom.RoomFragment; import chat.rocket.android.fragment.chatroom.RoomFragment;
import chat.rocket.android.fragment.sidebar.SidebarMainFragment; import chat.rocket.android.fragment.sidebar.SidebarMainFragment;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.internal.Session; import chat.rocket.android.model.internal.Session;
import chat.rocket.android.realm_helper.RealmHelper; import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmObjectObserver;
import chat.rocket.android.realm_helper.RealmStore; import chat.rocket.android.realm_helper.RealmStore;
import hugo.weaving.DebugLog; import hugo.weaving.DebugLog;
...@@ -19,11 +19,13 @@ import hugo.weaving.DebugLog; ...@@ -19,11 +19,13 @@ import hugo.weaving.DebugLog;
* Entry-point for Rocket.Chat.Android application. * Entry-point for Rocket.Chat.Android application.
*/ */
public class MainActivity extends AbstractAuthedActivity { public class MainActivity extends AbstractAuthedActivity {
private RealmObjectObserver<Session> sessionObserver;
@Override protected int getLayoutContainerForFragment() { @Override protected int getLayoutContainerForFragment() {
return R.id.activity_main_container; return R.id.activity_main_container;
} }
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
...@@ -62,18 +64,16 @@ public class MainActivity extends AbstractAuthedActivity { ...@@ -62,18 +64,16 @@ public class MainActivity extends AbstractAuthedActivity {
@DebugLog @DebugLog
@Override protected void onServerConfigIdUpdated() { @Override protected void onServerConfigIdUpdated() {
super.onServerConfigIdUpdated(); super.onServerConfigIdUpdated();
updateSessionObserver();
updateSidebarMainFragment(); updateSidebarMainFragment();
showServerConfigActivityIfNeeded();
} }
private void updateSidebarMainFragment() { private void updateSessionObserver() {
getSupportFragmentManager().beginTransaction() if (sessionObserver != null) {
.replace(R.id.sidebar_fragment_container, SidebarMainFragment.create(serverConfigId)) sessionObserver.unsub();
.commit(); sessionObserver = null;
} }
private void showServerConfigActivityIfNeeded() {
if (serverConfigId == null) { if (serverConfigId == null) {
return; return;
} }
...@@ -83,17 +83,25 @@ public class MainActivity extends AbstractAuthedActivity { ...@@ -83,17 +83,25 @@ public class MainActivity extends AbstractAuthedActivity {
return; return;
} }
Session session = realmHelper.executeTransactionForRead(realm -> sessionObserver = realmHelper
realm.where(Session.class).equalTo("sessionId", Session.DEFAULT_ID).findFirst()); .createObjectObserver(realm ->
realm.where(Session.class)
.equalTo("sessionId", Session.DEFAULT_ID)
.isNotNull("token")
.equalTo("tokenVerified", true)
.isNull("error"))
.setOnUpdateListener(session -> {
if (session == null) {
LaunchUtil.showServerConfigActivity(this, serverConfigId);
}
});
sessionObserver.sub();
}
if (session != null private void updateSidebarMainFragment() {
&& !TextUtils.isEmpty(session.getToken()) getSupportFragmentManager().beginTransaction()
&& session.isTokenVerified() .replace(R.id.sidebar_fragment_container, SidebarMainFragment.create(serverConfigId))
&& TextUtils.isEmpty(session.getError())) { .commit();
// session is OK.
} else {
LaunchUtil.showServerConfigActivity(this, serverConfigId);
}
} }
@Override protected void onRoomIdUpdated() { @Override protected void onRoomIdUpdated() {
...@@ -107,6 +115,21 @@ public class MainActivity extends AbstractAuthedActivity { ...@@ -107,6 +115,21 @@ public class MainActivity extends AbstractAuthedActivity {
} }
} }
@Override protected void onResume() {
super.onResume();
if (sessionObserver != null) {
sessionObserver.keepalive();
}
}
@Override protected void onDestroy() {
if (sessionObserver != null) {
sessionObserver.unsub();
sessionObserver = null;
}
super.onDestroy();
}
@Override protected boolean onBackPress() { @Override protected boolean onBackPress() {
return closeSidebarIfNeeded() || super.onBackPress(); return closeSidebarIfNeeded() || super.onBackPress();
} }
......
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