Commit e427d271 authored by Yusuke Iwaki's avatar Yusuke Iwaki

just refactor

parent 4d709b6e
...@@ -9,6 +9,7 @@ import java.util.List; ...@@ -9,6 +9,7 @@ import java.util.List;
import chat.rocket.android.api.DDPClientWrapper; import chat.rocket.android.api.DDPClientWrapper;
import chat.rocket.android.helper.LogcatIfError; import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.helper.ServerPolicyHelper; import chat.rocket.android.helper.ServerPolicyHelper;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.ServerConfig; import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.ddp.PublicSetting; import chat.rocket.android.model.ddp.PublicSetting;
import chat.rocket.android.model.ddp.PublicSettingsConstants; import chat.rocket.android.model.ddp.PublicSettingsConstants;
...@@ -25,16 +26,16 @@ public class PushSettingsObserver extends AbstractModelObserver<PublicSetting> { ...@@ -25,16 +26,16 @@ public class PushSettingsObserver extends AbstractModelObserver<PublicSetting> {
@Override @Override
public void onUpdateResults(List<PublicSetting> results) { public void onUpdateResults(List<PublicSetting> results) {
RealmHelper realmHelper = RealmStore.getDefault(); final ServerConfig serverConfig = RealmStore.getDefault().executeTransactionForRead(realm ->
final ServerConfig serverConfig = realmHelper.executeTransactionForRead(realm ->
realm.where(ServerConfig.class) realm.where(ServerConfig.class)
.equalTo(ServerConfig.HOSTNAME, ServerPolicyHelper.enforceHostname(hostname)) .equalTo(ServerConfig.HOSTNAME, ServerPolicyHelper.enforceHostname(hostname))
.findFirst()); .findFirst());
serverConfig.setSyncPushToken(isPushEnabled(results)); boolean syncPushToken = shouldEnablePush(results);
if (serverConfig.shouldSyncPushToken() != syncPushToken) {
serverConfig.setSyncPushToken(syncPushToken);
realmHelper RealmStore.getDefault()
.executeTransaction(realm -> realm.copyToRealmOrUpdate(serverConfig)) .executeTransaction(realm -> realm.copyToRealmOrUpdate(serverConfig))
.continueWith(task -> { .continueWith(task -> {
if (serverConfig.shouldSyncPushToken()) { if (serverConfig.shouldSyncPushToken()) {
...@@ -47,6 +48,7 @@ public class PushSettingsObserver extends AbstractModelObserver<PublicSetting> { ...@@ -47,6 +48,7 @@ public class PushSettingsObserver extends AbstractModelObserver<PublicSetting> {
}) })
.continueWith(new LogcatIfError()); .continueWith(new LogcatIfError());
} }
}
@Override @Override
public RealmResults<PublicSetting> queryItems(Realm realm) { public RealmResults<PublicSetting> queryItems(Realm realm) {
...@@ -57,11 +59,11 @@ public class PushSettingsObserver extends AbstractModelObserver<PublicSetting> { ...@@ -57,11 +59,11 @@ public class PushSettingsObserver extends AbstractModelObserver<PublicSetting> {
.findAll(); .findAll();
} }
private boolean isPushEnabled(List<PublicSetting> results) { private boolean shouldEnablePush(List<PublicSetting> results) {
return isPushEnable(results) && isGcmValid(results); return isPushEnabled(results) && hasValidGcmConfig(results);
} }
private boolean isPushEnable(List<PublicSetting> results) { private boolean isPushEnabled(List<PublicSetting> results) {
for (PublicSetting setting : results) { for (PublicSetting setting : results) {
if (PublicSettingsConstants.Push.ENABLE.equals(setting.getId())) { if (PublicSettingsConstants.Push.ENABLE.equals(setting.getId())) {
return "true".equals(setting.getValue()); return "true".equals(setting.getValue());
...@@ -70,10 +72,10 @@ public class PushSettingsObserver extends AbstractModelObserver<PublicSetting> { ...@@ -70,10 +72,10 @@ public class PushSettingsObserver extends AbstractModelObserver<PublicSetting> {
return false; return false;
} }
private boolean isGcmValid(List<PublicSetting> results) { private boolean hasValidGcmConfig(List<PublicSetting> results) {
for (PublicSetting setting : results) { for (PublicSetting setting : results) {
if (PublicSettingsConstants.Push.GCM_PROJECT_NUMBER.equals(setting.getId())) { if (PublicSettingsConstants.Push.GCM_PROJECT_NUMBER.equals(setting.getId())) {
return setting.getValue() != null && !"".equals(setting.getValue()); return !TextUtils.isEmpty(setting.getValue());
} }
} }
return false; return false;
......
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