@OnItemClick(R.id.list_view)
  public void onItemClick(int position) {
    if (Storage.isRuleEditing())
      findSensorId(mTransmitterAdapter.getItem(position), Storage.getRule().getSensorType());
    else {
      saveTransmitterData(mTransmitterAdapter.getItem(position));
      switchToEdit(FragmentName.SENSOR);
    }

    LogUtil.logMessage(
        Storage.isRuleEditing() ? LogUtil.EDIT_RULE_FINISH : LogUtil.CREATE_RULE_FINISH);
  }
  @Override
  public Observable<List<TMWRule>> loadRemoteRules() {
    return ruleApi
        .getAllRules(new DbSearch(Storage.loadUserId()))
        .subscribeOn(Schedulers.io())
        .map(
            new Func1<DbDocuments<DbRule>, List<TMWRule>>() {
              @Override
              public List<TMWRule> call(DbDocuments<DbRule> docs) {
                new Delete().from(TMWRule.class).execute();

                ActiveAndroid.beginTransaction();
                try {
                  for (DbRule dbRule : docs.getDocuments()) {
                    checkGcmNotification(dbRule);

                    TMWRule rule = DataMapper.toRule(dbRule);
                    rule.save();
                  }
                  ActiveAndroid.setTransactionSuccessful();
                } finally {
                  ActiveAndroid.endTransaction();
                }

                return new Select().from(TMWRule.class).execute();
              }
            });
  }
  @Override
  void onBackPressed() {
    LogUtil.logMessage(
        Storage.isRuleEditing() ? LogUtil.EDIT_RULE_CANCEL : LogUtil.CREATE_RULE_CANCEL);

    switchToEdit(FragmentName.MAIN);
  }
  @Override
  public void onResume() {
    super.onResume();

    mTransmitterAdapter.clear();
    mTransmitterAdapter.addAll(Storage.loadTransmitters());
    mTransmitterAdapter.notifyDataSetChanged();
  }
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    onCreateView(inflater, container, savedInstanceState, R.string.title_select_transmitter, true);

    View view = inflater.inflate(R.layout.transmitter_fragment, container, false);

    ButterKnife.inject(this, view);

    mTransmitterAdapter = new TransmitterAdapter(this.getActivity());
    mListView.setAdapter(mTransmitterAdapter);

    LogUtil.logMessage(
        Storage.isRuleEditing() ? LogUtil.EDIT_RULE_TRANSMITTER : LogUtil.CREATE_RULE_TRANSMITTER);

    return view;
  }
  @Override
  public Observable<Boolean> refreshRule(final String ruleId) {
    return ruleApi
        .getAllRules(new DbSearch(Storage.loadUserId()))
        .subscribeOn(Schedulers.io())
        .map(
            new Func1<DbDocuments<DbRule>, Boolean>() {
              @Override
              public Boolean call(DbDocuments<DbRule> docs) {
                for (DbRule dbRule : docs.getDocuments()) {
                  if (dbRule.getId().equals(ruleId)) {
                    new Delete().from(TMWRule.class).where("dbId = ?", ruleId).execute();

                    TMWRule rule = DataMapper.toRule(dbRule);
                    rule.save();
                    Storage.prepareRuleForEdit(rule);
                    return true;
                  }
                }

                return false;
              }
            });
  }
 public RuleServiceImpl() {
   TellMeWhenApplication.objectGraph.inject(this);
   mGcmRegId = Storage.loadGmsRegId();
   mOldGcmRegId = Storage.loadOldGmsRegId();
 }
 private void saveTransmitterData(Transmitter transmitter) {
   Storage.getRule().transmitterId = transmitter.id;
   Storage.getRule().transmitterName = transmitter.getName();
   Storage.getRule().transmitterType = "Relayr WunderBar";
 }