Commit 5c6e524c authored by Yusuke Iwaki's avatar Yusuke Iwaki

change MethodCallHelper to keepalive RocketChatService.

parent b0497388
package chat.rocket.android.api; package chat.rocket.android.api;
import android.content.Context;
import android.util.Patterns; import android.util.Patterns;
import bolts.Continuation; import bolts.Continuation;
import bolts.Task; import bolts.Task;
...@@ -24,16 +25,33 @@ import org.json.JSONObject; ...@@ -24,16 +25,33 @@ import org.json.JSONObject;
*/ */
public class MethodCallHelper { public class MethodCallHelper {
private final Context context;
private final RealmHelper realmHelper; private final RealmHelper realmHelper;
private final DDPClientWraper ddpClient; private final DDPClientWraper ddpClient;
private static final long TIMEOUT_MS = 4000; private static final long TIMEOUT_MS = 4000;
@Deprecated
/**
* Deprecated. use MethodCall(Context, String) instead.
*/
public MethodCallHelper(String serverConfigId) { public MethodCallHelper(String serverConfigId) {
this(null, serverConfigId);
}
/**
* initialize with Context and ServerConfigId.
*/
public MethodCallHelper(Context context, String serverConfigId) {
this.context = context;
this.realmHelper = RealmStore.get(serverConfigId); this.realmHelper = RealmStore.get(serverConfigId);
ddpClient = null; ddpClient = null;
} }
/**
* initialize with RealmHelper and DDPClient.
*/
public MethodCallHelper(RealmHelper realmHelper, DDPClientWraper ddpClient) { public MethodCallHelper(RealmHelper realmHelper, DDPClientWraper ddpClient) {
this.context = null;
this.realmHelper = realmHelper; this.realmHelper = realmHelper;
this.ddpClient = ddpClient; this.ddpClient = ddpClient;
} }
...@@ -44,7 +62,7 @@ public class MethodCallHelper { ...@@ -44,7 +62,7 @@ public class MethodCallHelper {
return ddpClient.rpc(UUID.randomUUID().toString(), methodName, param, timeout) return ddpClient.rpc(UUID.randomUUID().toString(), methodName, param, timeout)
.onSuccessTask(task -> Task.forResult(task.getResult().result)); .onSuccessTask(task -> Task.forResult(task.getResult().result));
} else { } else {
return MethodCall.execute(realmHelper, methodName, param, timeout); return MethodCall.execute(context, realmHelper, methodName, param, timeout);
} }
} }
......
...@@ -151,7 +151,8 @@ public class GitHubOAuthFragment extends AbstractWebViewFragment { ...@@ -151,7 +151,8 @@ public class GitHubOAuthFragment extends AbstractWebViewFragment {
} }
private void handleOAuthCallback(final String credentialToken, final String credentialSecret) { private void handleOAuthCallback(final String credentialToken, final String credentialSecret) {
new MethodCallHelper(serverConfigId).loginWithGitHub(credentialToken, credentialSecret) new MethodCallHelper(getContext(), serverConfigId)
.loginWithGitHub(credentialToken, credentialSecret)
.continueWith(new LogcatIfError()); .continueWith(new LogcatIfError());
} }
......
...@@ -43,7 +43,8 @@ public class LoginFragment extends AbstractServerConfigFragment { ...@@ -43,7 +43,8 @@ public class LoginFragment extends AbstractServerConfigFragment {
} }
view.setEnabled(false); view.setEnabled(false);
new MethodCallHelper(serverConfigId).loginWithEmail(username.toString(), passwd.toString()) new MethodCallHelper(getContext(), serverConfigId)
.loginWithEmail(username.toString(), passwd.toString())
.continueWith(task -> { .continueWith(task -> {
if (task.isFaulted()) { if (task.isFaulted()) {
showError(task.getError().getMessage()); showError(task.getError().getMessage());
......
...@@ -46,7 +46,7 @@ public class RetryLoginFragment extends AbstractServerConfigFragment { ...@@ -46,7 +46,7 @@ public class RetryLoginFragment extends AbstractServerConfigFragment {
view.setEnabled(false); view.setEnabled(false);
waitingView.setVisibility(View.VISIBLE); waitingView.setVisibility(View.VISIBLE);
new MethodCallHelper(serverConfigId).loginWithToken(token) new MethodCallHelper(getContext(), serverConfigId).loginWithToken(token)
.continueWith(task -> { .continueWith(task -> {
if (task.isFaulted()) { if (task.isFaulted()) {
view.setEnabled(true); view.setEnabled(true);
......
...@@ -109,7 +109,7 @@ public class UserRegistrationDialogFragment extends DialogFragment { ...@@ -109,7 +109,7 @@ public class UserRegistrationDialogFragment extends DialogFragment {
email = txtEmail.getText().toString(); email = txtEmail.getText().toString();
password = txtPasswd.getText().toString(); password = txtPasswd.getText().toString();
MethodCallHelper methodCallHelper = new MethodCallHelper(serverConfigId); MethodCallHelper methodCallHelper = new MethodCallHelper(getContext(), serverConfigId);
methodCallHelper.registerUser(username, email, password, password) methodCallHelper.registerUser(username, email, password, password)
.onSuccessTask(task -> methodCallHelper.loginWithEmail(email, password)) .onSuccessTask(task -> methodCallHelper.loginWithEmail(email, password))
.onSuccessTask(task -> methodCallHelper.setUsername(username)) //TODO: should prompt! .onSuccessTask(task -> methodCallHelper.setUsername(username)) //TODO: should prompt!
......
...@@ -70,7 +70,7 @@ public class SidebarMainFragment extends AbstractFragment { ...@@ -70,7 +70,7 @@ public class SidebarMainFragment extends AbstractFragment {
.createObjectObserver(realm -> realm.where(User.class).isNotEmpty("emails")) .createObjectObserver(realm -> realm.where(User.class).isNotEmpty("emails"))
.setOnUpdateListener(this::onRenderCurrentUser); .setOnUpdateListener(this::onRenderCurrentUser);
methodCallHelper = new MethodCallHelper(serverConfigId); methodCallHelper = new MethodCallHelper(getContext(), serverConfigId);
} }
} }
} }
......
package chat.rocket.android.model.internal; package chat.rocket.android.model.internal;
import android.content.Context;
import android.support.annotation.Nullable;
import bolts.Task; import bolts.Task;
import bolts.TaskCompletionSource; import bolts.TaskCompletionSource;
import chat.rocket.android.helper.LogcatIfError; import chat.rocket.android.helper.LogcatIfError;
...@@ -7,6 +9,7 @@ import chat.rocket.android.helper.TextUtils; ...@@ -7,6 +9,7 @@ import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.SyncState; import chat.rocket.android.model.SyncState;
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.RealmObjectObserver;
import chat.rocket.android.service.RocketChatService;
import io.realm.RealmObject; import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey; import io.realm.annotations.PrimaryKey;
import java.util.UUID; import java.util.UUID;
...@@ -89,8 +92,8 @@ public class MethodCall extends RealmObject { ...@@ -89,8 +92,8 @@ public class MethodCall extends RealmObject {
/** /**
* insert a new record to request a method call. * insert a new record to request a method call.
*/ */
public static Task<String> execute(RealmHelper realmHelper, String name, String paramsJson, public static Task<String> execute(@Nullable final Context context,
long timeout) { RealmHelper realmHelper, String name, String paramsJson, long timeout) {
final String newId = UUID.randomUUID().toString(); final String newId = UUID.randomUUID().toString();
TaskCompletionSource<String> task = new TaskCompletionSource<>(); TaskCompletionSource<String> task = new TaskCompletionSource<>();
realmHelper.executeTransaction(realm -> { realmHelper.executeTransaction(realm -> {
...@@ -127,6 +130,10 @@ public class MethodCall extends RealmObject { ...@@ -127,6 +130,10 @@ public class MethodCall extends RealmObject {
} }
}); });
observer.sub(); observer.sub();
if (context != null) {
RocketChatService.keepalive(context);
}
} }
return null; return null;
}); });
......
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