co.easimart.EasimartPushChannelsController Maven / Gradle / Ivy
package co.easimart;
import java.util.Collections;
import java.util.List;
import bolts.Continuation;
import bolts.Task;
/** package */ class EasimartPushChannelsController {
private static final String TAG = "co.easimart.EasimartPushChannelsController";
private static EasimartCurrentInstallationController getCurrentInstallationController() {
return EasimartCorePlugins.getInstance().getCurrentInstallationController();
}
public Task subscribeInBackground(final String channel) {
checkManifestAndLogErrorIfNecessary();
if (channel == null) {
throw new IllegalArgumentException("Can't subscribe to null channel.");
}
return getCurrentInstallationController().getAsync().onSuccessTask(new Continuation>() {
@Override
public Task then(Task task) throws Exception {
EasimartInstallation installation = task.getResult();
List channels = installation.getList(EasimartInstallation.KEY_CHANNELS);
if (channels == null
|| installation.isDirty(EasimartInstallation.KEY_CHANNELS)
|| !channels.contains(channel)) {
installation.addUnique(EasimartInstallation.KEY_CHANNELS, channel);
return installation.saveInBackground();
} else {
return Task.forResult(null);
}
}
});
}
public Task unsubscribeInBackground(final String channel) {
checkManifestAndLogErrorIfNecessary();
if (channel == null) {
throw new IllegalArgumentException("Can't unsubscribe from null channel.");
}
return getCurrentInstallationController().getAsync().onSuccessTask(new Continuation>() {
@Override
public Task then(Task task) throws Exception {
EasimartInstallation installation = task.getResult();
List channels = installation.getList(EasimartInstallation.KEY_CHANNELS);
if (channels != null && channels.contains(channel)) {
installation.removeAll(
EasimartInstallation.KEY_CHANNELS, Collections.singletonList(channel));
return installation.saveInBackground();
} else {
return Task.forResult(null);
}
}
});
}
private static boolean loggedManifestError = false;
private static void checkManifestAndLogErrorIfNecessary() {
if (!loggedManifestError && ManifestInfo.getPushType() == PushType.NONE) {
loggedManifestError = true;
EasimartLog.e(TAG, "Tried to subscribe or unsubscribe from a channel, but push is not enabled " +
"correctly. " + ManifestInfo.getNonePushTypeLogMessage());
}
}
}