PushSettingsObserver.java 1.19 KB
Newer Older
Tiago Cunha's avatar
Tiago Cunha committed
1 2 3 4 5 6 7
package chat.rocket.android.service.observer;

import android.content.Context;
import io.realm.Realm;
import io.realm.RealmResults;

import java.util.List;
8
import chat.rocket.android.helper.GcmPushSettingHelper;
Tiago Cunha's avatar
Tiago Cunha committed
9 10
import chat.rocket.persistence.realm.models.ddp.RealmPublicSetting;
import chat.rocket.persistence.realm.models.internal.GcmPushRegistration;
Tiago Cunha's avatar
Tiago Cunha committed
11
import chat.rocket.persistence.realm.RealmHelper;
12
import chat.rocket.android.service.DDPClientRef;
Tiago Cunha's avatar
Tiago Cunha committed
13

Tiago Cunha's avatar
Tiago Cunha committed
14
public class PushSettingsObserver extends AbstractModelObserver<RealmPublicSetting> {
Tiago Cunha's avatar
Tiago Cunha committed
15 16

  public PushSettingsObserver(Context context, String hostname,
17 18
                              RealmHelper realmHelper, DDPClientRef ddpClientRef) {
    super(context, hostname, realmHelper, ddpClientRef);
Tiago Cunha's avatar
Tiago Cunha committed
19 20 21
  }

  @Override
Tiago Cunha's avatar
Tiago Cunha committed
22
  public RealmResults<RealmPublicSetting> queryItems(Realm realm) {
23
    return GcmPushSettingHelper.queryForGcmPushEnabled(realm);
Tiago Cunha's avatar
Tiago Cunha committed
24 25
  }

26
  @Override
Tiago Cunha's avatar
Tiago Cunha committed
27
  public void onUpdateResults(List<RealmPublicSetting> results) {
28
    boolean gcmPushEnabled = GcmPushSettingHelper.isGcmPushEnabled(results);
Tiago Cunha's avatar
Tiago Cunha committed
29

30
    if (gcmPushEnabled) {
31 32
      realmHelper.executeTransaction(realm ->
          GcmPushRegistration.updateGcmPushEnabled(realm, gcmPushEnabled));
Tiago Cunha's avatar
Tiago Cunha committed
33 34 35
    }
  }
}