private void onFavoriteItemSelected(boolean favorited) {
    Observable<Boolean> obsAction;

    if (favorited) {
      obsAction =
          Observable.create(
              (Subscriber<? super Boolean> subscriber) -> {
                try {
                  FavoritesUtils.addToFavorites(getActivity(), FAVORITE_VIDEOS, mFeedItem);
                  mFeedItem = new FeedItem.Builder(mFeedItem).setFavorited(true).build();
                  subscriber.onNext(true);
                } catch (Exception e) {
                  Timber.w(e, "Unable to add FeedItem to favorites");
                  subscriber.onError(e);
                }
                subscriber.onCompleted();
              });
      obsAction = AndroidObservable.bindFragment(this, obsAction).subscribeOn(Schedulers.io());
      bindSubscription(
          obsAction.subscribe(
              result -> {
                if (result) {
                  Toast.makeText(getActivity(), R.string.added_to_favorites, Toast.LENGTH_SHORT)
                      .show();
                }
                configureOverflow(result);
              }));
    } else {
      obsAction =
          Observable.create(
              (Subscriber<? super Boolean> subscriber) -> {
                try {
                  FavoritesUtils.removeFromFavorites(getActivity(), FAVORITE_VIDEOS, mFeedItem);
                  mFeedItem = new FeedItem.Builder(mFeedItem).setFavorited(false).build();
                  subscriber.onNext(false);
                } catch (Exception e) {
                  Timber.w(e, "Unable to remove FeedItem from favorites");
                  subscriber.onError(e);
                }
                subscriber.onCompleted();
              });
      obsAction = AndroidObservable.bindFragment(this, obsAction).subscribeOn(Schedulers.io());
      bindSubscription(
          obsAction.subscribe(
              result -> {
                if (!result) {
                  Toast.makeText(getActivity(), R.string.removed_from_favorites, Toast.LENGTH_SHORT)
                      .show();
                }
                configureOverflow(result);
              }));
    }
  }
示例#2
0
    @Override
    public void updateGeoData(final IGeoData geo) {
      if (!nearestView.isClickable()) {
        nearestView.setFocusable(true);
        nearestView.setClickable(true);
        nearestView.setOnClickListener(
            new OnClickListener() {
              @Override
              public void onClick(final View v) {
                cgeoFindNearest(v);
              }
            });
        nearestView.setBackgroundResource(R.drawable.main_nearby);
      }

      navType.setText(res.getString(geo.getLocationProvider().resourceId));

      if (geo.getAccuracy() >= 0) {
        final int speed = Math.round(geo.getSpeed()) * 60 * 60 / 1000;
        navAccuracy.setText(
            "±"
                + Units.getDistanceFromMeters(geo.getAccuracy())
                + Formatter.SEPARATOR
                + Units.getSpeed(speed));
      } else {
        navAccuracy.setText(null);
      }

      if (Settings.isShowAddress()) {
        if (addCoords == null) {
          navLocation.setText(R.string.loc_no_addr);
        }
        if (addCoords == null || (geo.getCoords().distanceTo(addCoords) > 0.5)) {
          final Observable<String> address =
              Observable.create(
                  new OnSubscribe<String>() {
                    @Override
                    public void call(final Subscriber<? super String> subscriber) {
                      try {
                        addCoords = geo.getCoords();
                        final Geocoder geocoder =
                            new Geocoder(MainActivity.this, Locale.getDefault());
                        final Geopoint coords = app.currentGeo().getCoords();
                        final List<Address> addresses =
                            geocoder.getFromLocation(
                                coords.getLatitude(), coords.getLongitude(), 1);
                        if (!addresses.isEmpty()) {
                          subscriber.onNext(formatAddress(addresses.get(0)));
                        }
                        subscriber.onCompleted();
                      } catch (final Exception e) {
                        subscriber.onError(e);
                      }
                    }
                  });
          AndroidObservable.bindActivity(
                  MainActivity.this,
                  address.onErrorResumeNext(Observable.from(geo.getCoords().toString())))
              .subscribeOn(Schedulers.io())
              .subscribe(
                  new Action1<String>() {
                    @Override
                    public void call(final String address) {
                      navLocation.setText(address);
                    }
                  });
        }
      } else {
        navLocation.setText(geo.getCoords().toString());
      }
    }
示例#3
0
  @Override
  public View onCreateView(
      final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    final ViewGroup statusGroup = (ViewGroup) inflater.inflate(R.layout.status, container, false);
    final ImageView statusIcon = ButterKnife.findById(statusGroup, R.id.status_icon);
    final TextView statusMessage = ButterKnife.findById(statusGroup, R.id.status_message);
    statusSubscription =
        AndroidObservable.bindFragment(this, StatusUpdater.latestStatus)
            .subscribeOn(Schedulers.io())
            .subscribe(
                new Action1<Status>() {
                  @Override
                  public void call(final Status status) {
                    if (status == null) {
                      statusGroup.setVisibility(View.INVISIBLE);
                      return;
                    }

                    final Resources res = getResources();
                    final String packageName = getActivity().getPackageName();

                    if (status.icon != null) {
                      final int iconId = res.getIdentifier(status.icon, "drawable", packageName);
                      if (iconId != 0) {
                        statusIcon.setImageResource(iconId);
                        statusIcon.setVisibility(View.VISIBLE);
                      } else {
                        Log.w(
                            "StatusHandler: could not find icon corresponding to @drawable/"
                                + status.icon);
                        statusIcon.setVisibility(View.GONE);
                      }
                    } else {
                      statusIcon.setVisibility(View.GONE);
                    }

                    String message = status.message;
                    if (status.messageId != null) {
                      final int messageId =
                          res.getIdentifier(status.messageId, "string", packageName);
                      if (messageId != 0) {
                        message = res.getString(messageId);
                      }
                    }

                    statusMessage.setText(message);
                    statusGroup.setVisibility(View.VISIBLE);

                    if (status.url != null) {
                      statusGroup.setOnClickListener(
                          new OnClickListener() {
                            @Override
                            public void onClick(final View v) {
                              startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(status.url)));
                            }
                          });
                    } else {
                      statusGroup.setClickable(false);
                    }
                  }
                });
    return statusGroup;
  }