Commit b6b6ef68 authored by Yusuke Iwaki's avatar Yusuke Iwaki

just refactor

parent 5f6b098d
package chat.rocket.android.push.gcm; package chat.rocket.android.push.gcm;
import android.content.Intent;
import com.google.android.gms.iid.InstanceIDListenerService; import com.google.android.gms.iid.InstanceIDListenerService;
import android.content.Intent;
import java.util.List; import java.util.List;
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;
...@@ -15,11 +15,9 @@ public class GcmInstanceIDListenerService extends InstanceIDListenerService { ...@@ -15,11 +15,9 @@ public class GcmInstanceIDListenerService extends InstanceIDListenerService {
@Override @Override
public void onTokenRefresh() { public void onTokenRefresh() {
List<ServerConfig> serverConfigs = getServerConfigs(); updateSyncPushTokenIfNeeded();
updateSyncPushToken(serverConfigs);
if (!shouldRefreshToken(serverConfigs)) { if (!shouldRefreshToken()) {
return; return;
} }
...@@ -27,36 +25,33 @@ public class GcmInstanceIDListenerService extends InstanceIDListenerService { ...@@ -27,36 +25,33 @@ public class GcmInstanceIDListenerService extends InstanceIDListenerService {
startService(intent); startService(intent);
} }
private List<ServerConfig> getServerConfigs() { private void updateSyncPushTokenIfNeeded() {
return RealmStore.getDefault().executeTransactionForReadResults(
realm -> realm.where(ServerConfig.class).findAll());
}
private void updateSyncPushToken(List<ServerConfig> serverConfigs) {
final RealmHelper realmHelper = RealmStore.getDefault(); final RealmHelper realmHelper = RealmStore.getDefault();
List<ServerConfig> serverConfigs = realmHelper.executeTransactionForReadResults(
realm -> realm.where(ServerConfig.class).findAll());
for (final ServerConfig serverConfig : serverConfigs) { for (final ServerConfig serverConfig : serverConfigs) {
final RealmHelper serverRealmHelper = RealmStore final RealmHelper serverRealmHelper = RealmStore.get(serverConfig.getServerConfigId());
.getOrCreate(serverConfig.getServerConfigId()); if (serverRealmHelper == null) {
continue;
}
boolean isPushEnable = PublicSetting boolean isPushEnable = PublicSetting
.getBoolean(serverRealmHelper, PublicSettingsConstants.Push.ENABLE, false); .getBoolean(serverRealmHelper, PublicSettingsConstants.Push.ENABLE, false);
String senderId = PublicSetting String senderId = PublicSetting
.getString(serverRealmHelper, PublicSettingsConstants.Push.GCM_PROJECT_NUMBER, "").trim(); .getString(serverRealmHelper, PublicSettingsConstants.Push.GCM_PROJECT_NUMBER, "").trim();
serverConfig.setSyncPushToken(isPushEnable && !"".equals(senderId)); boolean syncPushToken = isPushEnable && !"".equals(senderId);
if (serverConfig.shouldSyncPushToken() != syncPushToken) {
serverConfig.setSyncPushToken(syncPushToken);
realmHelper.executeTransaction(realm -> realm.copyToRealmOrUpdate(serverConfig)); realmHelper.executeTransaction(realm -> realm.copyToRealmOrUpdate(serverConfig));
} }
} }
private boolean shouldRefreshToken(List<ServerConfig> serverConfigs) {
for (ServerConfig serverConfig : serverConfigs) {
if (serverConfig.shouldSyncPushToken()) {
return true;
}
} }
return false; private boolean shouldRefreshToken() {
return RealmStore.getDefault().isObjectExists(realm ->
realm.where(ServerConfig.class).equalTo(ServerConfig.SYNC_PUSH_TOKEN, true));
} }
} }
\ No newline at end of file
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