Exemplo n.º 1
0
  private void saveBeacon(Beacon beacon) {
    if (DataUtils.isNetworkAvailable(getActivity())) {
      showProgressDialog();
      boolean hasStatusChanged = false;
      if (mPropertiesMode == Mode.UPDATE) hasStatusChanged = beacon.status != mBeacon.status;

      Observable<Beacon> beaconObservable =
          mPropertiesMode == Mode.UPDATE
              ? mDataManager.updateBeacon(
                  mBeacon.beaconName, beacon, hasStatusChanged, beacon.status)
              : mDataManager.registerBeacon(beacon);

      mSubscriptions.add(
          beaconObservable
              .observeOn(AndroidSchedulers.mainThread())
              .subscribeOn(mDataManager.getScheduler())
              .subscribe(
                  new Subscriber<Beacon>() {
                    @Override
                    public void onCompleted() {
                      mProgressDialog.dismiss();
                      WatchTowerApplication.get(getActivity())
                          .getComponent()
                          .eventBus()
                          .post(new BusEvent.BeaconListAmended());
                      getActivity().finish();
                    }

                    @Override
                    public void onError(Throwable error) {
                      mProgressDialog.dismiss();
                      Timber.d("There was an error saving the beacon : " + error.getMessage());
                      if (error instanceof RetrofitError) {
                        DialogFactory.createRetrofitErrorDialog(
                                getActivity(), (RetrofitError) error)
                            .show();
                      } else {
                        DialogFactory.createSimpleErrorDialog(getActivity()).show();
                      }
                    }

                    @Override
                    public void onNext(Beacon beacon) {
                      if (mPropertiesMode == Mode.UPDATE) {
                        WatchTowerApplication.get(getActivity())
                            .getComponent()
                            .eventBus()
                            .post(new BusEvent.BeaconUpdated(beacon));
                      }
                    }
                  }));
    } else {
      DialogFactory.createSimpleOkErrorDialog(
              getActivity(),
              getString(R.string.dialog_error_title),
              getString(R.string.dialog_error_no_connection))
          .show();
    }
  }
Exemplo n.º 2
0
 private void showProgressDialog() {
   mProgressDialog =
       DialogFactory.createProgressDialog(getActivity(), R.string.progress_dialog_saving_beacon);
   mProgressDialog.show();
 }