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

change MethodCallHelper to keepalive RocketChatService.

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