Commit eb93a930 authored by Yusuke Iwaki's avatar Yusuke Iwaki

add Twitter/Github button and fix some logic of AbstractDDPDocEventSubscriber

parent 9214fc3e
package chat.rocket.android.fragment.server_config;
import android.view.View;
import chat.rocket.android.R;
import chat.rocket.android.model.MeteorLoginServiceConfiguration;
import io.realm.Realm;
import io.realm.RealmResults;
import java.util.List;
import jp.co.crowdworks.realm_java_helpers.RealmListObserver;
/**
* Login screen.
......@@ -10,7 +16,39 @@ public class LoginFragment extends AbstractServerConfigFragment {
return R.layout.fragment_login;
}
private RealmListObserver<MeteorLoginServiceConfiguration> authProvidersObserver =
new RealmListObserver<MeteorLoginServiceConfiguration>() {
@Override protected RealmResults<MeteorLoginServiceConfiguration> queryItems(Realm realm) {
return realm.where(MeteorLoginServiceConfiguration.class)
.equalTo("serverConfig.id", serverConfigId)
.findAll();
}
@Override protected void onCollectionChanged(List<MeteorLoginServiceConfiguration> list) {
onRenderAuthProviders(list);
}
};
@Override protected void onSetupView() {
}
private void onRenderAuthProviders(List<MeteorLoginServiceConfiguration> authProviders) {
final View btnTwitter = rootView.findViewById(R.id.btn_login_with_twitter);
final View btnGitHub = rootView.findViewById(R.id.btn_login_with_github);
boolean hasTwitter = false;
boolean hasGitHub = false;
for (MeteorLoginServiceConfiguration authProvider : authProviders) {
if (!hasTwitter && "twitter".equals(authProvider.getService())) {
hasTwitter = true;
}
if (!hasGitHub && "github".equals(authProvider.getService())) {
hasGitHub = true;
}
}
btnTwitter.setVisibility(hasTwitter ? View.VISIBLE : View.GONE);
btnGitHub.setVisibility(hasGitHub ? View.VISIBLE : View.GONE);
}
}
......@@ -15,4 +15,52 @@ public class MeteorLoginServiceConfiguration
private String consumerKey; //for Twitter
private String appId; //for Facebook
private String clientId; //for other auth providers
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public ServerConfig getServerConfig() {
return serverConfig;
}
public void setServerConfig(ServerConfig serverConfig) {
this.serverConfig = serverConfig;
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public String getConsumerKey() {
return consumerKey;
}
public void setConsumerKey(String consumerKey) {
this.consumerKey = consumerKey;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
}
......@@ -170,8 +170,8 @@ public class RocketChatWebSocketThread extends HandlerThread {
for (Class clazz : REGISTERABLE_CLASSES) {
try {
Constructor ctor = clazz.getConstructor(Context.class, RocketChatWebSocketAPI.class);
Object obj = ctor.newInstance(appContext, webSocketAPI);
Constructor ctor = clazz.getConstructor(Context.class, String.class, RocketChatWebSocketAPI.class);
Object obj = ctor.newInstance(appContext, serverConfigId, webSocketAPI);
if (obj instanceof Registerable) {
Registerable registerable = (Registerable) obj;
......
......@@ -17,12 +17,15 @@ import timber.log.Timber;
abstract class AbstractDDPDocEventSubscriber implements Registerable {
protected final Context context;
protected final String serverConfigId;
protected final RocketChatWebSocketAPI webSocketAPI;
private String subscriptionId;
private Subscription rxSubscription;
protected AbstractDDPDocEventSubscriber(Context context, RocketChatWebSocketAPI api) {
protected AbstractDDPDocEventSubscriber(Context context, String serverConfigId,
RocketChatWebSocketAPI api) {
this.context = context;
this.serverConfigId = serverConfigId;
this.webSocketAPI = api;
}
......@@ -90,6 +93,7 @@ abstract class AbstractDDPDocEventSubscriber implements Registerable {
private void onDocumentAdded(Realm realm, DDPSubscription.Added docEvent) throws JSONException {
//executed in RealmTransaction
JSONObject json = new JSONObject().put("id", docEvent.docID);
json.put("serverConfig", new JSONObject().put("id", serverConfigId));
mergeJson(json, docEvent.fields);
realm.createOrUpdateObjectFromJson(getModelClass(), customizeFieldJson(json));
}
......@@ -105,9 +109,10 @@ abstract class AbstractDDPDocEventSubscriber implements Registerable {
throws JSONException {
//executed in RealmTransaction
JSONObject json = new JSONObject().put("id", docEvent.docID);
json.put("serverConfig", new JSONObject().put("id", serverConfigId));
for (int i = 0; i < docEvent.cleared.length(); i++) {
String fieldToDelete = docEvent.cleared.getString(i);
json.remove(fieldToDelete);
json.put(fieldToDelete, JSONObject.NULL);
}
mergeJson(json, docEvent.fields);
realm.createOrUpdateObjectFromJson(getModelClass(), customizeFieldJson(json));
......
......@@ -9,8 +9,9 @@ import io.realm.RealmObject;
* meteor.loginServiceConfiguration subscriber
*/
public class LoginServiceConfigurationSubscriber extends AbstractDDPDocEventSubscriber {
public LoginServiceConfigurationSubscriber(Context context, RocketChatWebSocketAPI api) {
super(context, api);
public LoginServiceConfigurationSubscriber(Context context, String serverConfigId,
RocketChatWebSocketAPI api) {
super(context, serverConfigId, api);
}
@Override protected String getSubscriptionName() {
......
......@@ -10,10 +10,13 @@ abstract class AbstractModelObserver<T extends RealmObject> extends RealmListObs
implements Registerable {
protected final Context context;
protected final String serverConfigId;
protected final RocketChatWebSocketAPI webSocketAPI;
protected AbstractModelObserver(Context context, RocketChatWebSocketAPI api) {
protected AbstractModelObserver(Context context, String serverConfigId,
RocketChatWebSocketAPI api) {
this.context = context;
this.serverConfigId = serverConfigId;
webSocketAPI = api;
}
......
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