Commit 0580f1a6 authored by Yusuke Iwaki's avatar Yusuke Iwaki

fix infinite error looping.

parent 9c00b01c
...@@ -7,9 +7,11 @@ import android.support.v4.app.Fragment; ...@@ -7,9 +7,11 @@ import android.support.v4.app.Fragment;
import chat.rocket.android.R; import chat.rocket.android.R;
import chat.rocket.android.fragment.server_config.LoginFragment; import chat.rocket.android.fragment.server_config.LoginFragment;
import chat.rocket.android.fragment.server_config.RetryConnectFragment;
import chat.rocket.android.fragment.server_config.RetryLoginFragment; import chat.rocket.android.fragment.server_config.RetryLoginFragment;
import chat.rocket.android.fragment.server_config.WaitingFragment; import chat.rocket.android.fragment.server_config.WaitingFragment;
import chat.rocket.android.helper.TextUtils; import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.internal.Session; import chat.rocket.android.model.internal.Session;
import chat.rocket.android.realm_helper.RealmObjectObserver; import chat.rocket.android.realm_helper.RealmObjectObserver;
import chat.rocket.android.realm_helper.RealmStore; import chat.rocket.android.realm_helper.RealmStore;
...@@ -21,6 +23,7 @@ import chat.rocket.android.service.RocketChatService; ...@@ -21,6 +23,7 @@ import chat.rocket.android.service.RocketChatService;
public class ServerConfigActivity extends AbstractFragmentActivity { public class ServerConfigActivity extends AbstractFragmentActivity {
private String serverConfigId; private String serverConfigId;
private RealmObjectObserver<ServerConfig> serverConfigErrorObserver;
private RealmObjectObserver<Session> sessionObserver; private RealmObjectObserver<Session> sessionObserver;
@Override @Override
...@@ -44,6 +47,13 @@ public class ServerConfigActivity extends AbstractFragmentActivity { ...@@ -44,6 +47,13 @@ public class ServerConfigActivity extends AbstractFragmentActivity {
return; return;
} }
serverConfigErrorObserver = RealmStore.getDefault()
.createObjectObserver(realm ->
realm.where(ServerConfig.class)
.equalTo("serverConfigId", serverConfigId)
.equalTo("state", ServerConfig.STATE_CONNECTION_ERROR))
.setOnUpdateListener(this::onRenderServerConfigError);
sessionObserver = RealmStore.get(serverConfigId) sessionObserver = RealmStore.get(serverConfigId)
.createObjectObserver(Session::queryDefaultSession) .createObjectObserver(Session::queryDefaultSession)
.setOnUpdateListener(this::onRenderServerConfigSession); .setOnUpdateListener(this::onRenderServerConfigSession);
...@@ -56,15 +66,25 @@ public class ServerConfigActivity extends AbstractFragmentActivity { ...@@ -56,15 +66,25 @@ public class ServerConfigActivity extends AbstractFragmentActivity {
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
RocketChatService.keepalive(this); RocketChatService.keepalive(this);
sessionObserver.sub(); serverConfigErrorObserver.sub();
} }
@Override @Override
protected void onPause() { protected void onPause() {
sessionObserver.unsub(); sessionObserver.unsub();
serverConfigErrorObserver.unsub();
super.onPause(); super.onPause();
} }
private void onRenderServerConfigError(ServerConfig config) {
if (config != null) {
sessionObserver.unsub();
showFragment(new RetryConnectFragment());
} else {
sessionObserver.sub();
}
}
private void onRenderServerConfigSession(Session session) { private void onRenderServerConfigSession(Session session) {
if (session == null) { if (session == null) {
showFragment(new LoginFragment()); showFragment(new LoginFragment());
......
package chat.rocket.android.fragment.server_config;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;
import chat.rocket.android.R;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.realm_helper.RealmObjectObserver;
import chat.rocket.android.realm_helper.RealmStore;
/**
* Login screen.
*/
public class RetryConnectFragment extends AbstractServerConfigFragment {
private RealmObjectObserver<ServerConfig> serverConfigObserver;
@Override
protected int getLayout() {
return R.layout.fragment_retry_login;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
serverConfigObserver = RealmStore.getDefault()
.createObjectObserver(realm ->
realm.where(ServerConfig.class).equalTo("serverConfigId", serverConfigId))
.setOnUpdateListener(this::onRenderServerConfig);
}
@Override
protected void onSetupView() {
rootView.findViewById(R.id.waiting).setVisibility(View.GONE);
final View btnRetry = rootView.findViewById(R.id.btn_retry_login);
btnRetry.setOnClickListener(view -> {
RealmStore.getDefault()
.executeTransaction(realm -> {
ServerConfig config = realm.where(ServerConfig.class)
.equalTo("serverConfigId", serverConfigId).findFirst();
if (config != null && config.getState() == ServerConfig.STATE_CONNECTION_ERROR) {
config.setState(ServerConfig.STATE_READY);
}
return null;
}).continueWith(new LogcatIfError());
});
}
private void onRenderServerConfig(ServerConfig config) {
if (config == null) {
return;
}
final String error = config.getError();
final TextView txtError = (TextView) rootView.findViewById(R.id.txt_error_description);
if (!TextUtils.isEmpty(error)) {
txtError.setText(error);
}
final int state = config.getState();
if (state == ServerConfig.STATE_CONNECTED) {
finish();
}
rootView.findViewById(R.id.btn_retry_login)
.setEnabled(state == ServerConfig.STATE_CONNECTION_ERROR);
}
@Override
public void onResume() {
super.onResume();
serverConfigObserver.sub();
}
@Override
public void onPause() {
serverConfigObserver.unsub();
super.onPause();
}
}
...@@ -183,10 +183,10 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -183,10 +183,10 @@ public class RocketChatWebSocketThread extends HandlerThread {
return null; return null;
})).continueWith(new LogcatIfError()); })).continueWith(new LogcatIfError());
return task; return task;
}).onSuccess(new Continuation<DDPClientCallback.Connect, Object>() { }).onSuccess(new Continuation<DDPClientCallback.Connect, Void>() {
// TODO type detection doesn't work due to retrolambda's bug... // TODO type detection doesn't work due to retrolambda's bug...
@Override @Override
public Object then(Task<DDPClientCallback.Connect> task) public Void then(Task<DDPClientCallback.Connect> task)
throws Exception { throws Exception {
registerListeners(); registerListeners();
...@@ -194,20 +194,20 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -194,20 +194,20 @@ public class RocketChatWebSocketThread extends HandlerThread {
task.getResult().client.getOnCloseCallback().onSuccess(_task -> { task.getResult().client.getOnCloseCallback().onSuccess(_task -> {
quit(); quit();
return null; return null;
}).continueWith(_task -> { }).continueWithTask(_task -> {
if (_task.isFaulted()) { if (_task.isFaulted()) {
ServerConfig.logConnectionError(serverConfigId, _task.getError()); ServerConfig.logConnectionError(serverConfigId, _task.getError());
} }
return null; return _task;
}); });
return null; return null;
} }
}).continueWith(task -> { }).continueWithTask(task -> {
if (task.isFaulted()) { if (task.isFaulted()) {
ServerConfig.logConnectionError(serverConfigId, task.getError()); ServerConfig.logConnectionError(serverConfigId, task.getError());
} }
return null; return task;
}); });
} }
......
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