Пример #1
0
  private void onRebroadcastResponse(
      boolean successful, Broadcast result, VolleyError error, RebroadcastState state) {

    if (successful) {

      if (!state.rebroadcast) {
        // Delete the rebroadcast broadcast by user. Must be done before we update the
        // broadcast so that we can retrieve rebroadcastId for the old one.
        // This will not finish this activity, because this activity displays the
        // rebroadcasted broadcast instead of the rebroadcast broadcast itself, and this is
        // the desired behavior since it won't surprise user, and user can have the chance
        // to undo it.
        Broadcast broadcast = mBroadcastAdapter.getBroadcast();
        if (broadcast != null && broadcast.rebroadcastId != null) {
          EventBus.getDefault().post(new BroadcastDeletedEvent(broadcast.rebroadcastId));
        }
      }
      EventBus.getDefault().post(new BroadcastUpdatedEvent(result));
      ToastUtils.show(
          state.rebroadcast
              ? R.string.broadcast_rebroadcast_successful
              : R.string.broadcast_unrebroadcast_successful,
          this);

    } else {

      LogUtils.e(error.toString());
      Broadcast broadcast = mBroadcastAdapter.getBroadcast();
      if (broadcast != null) {
        boolean notified = false;
        if (error instanceof ApiError) {
          // Correct our local state if needed.
          ApiError apiError = (ApiError) error;
          Boolean shouldBeRebroadcasted = null;
          if (apiError.code == Codes.RebroadcastBroadcast.ALREADY_REBROADCASTED) {
            shouldBeRebroadcasted = true;
          } else if (apiError.code == Codes.RebroadcastBroadcast.NOT_REBROADCASTED_YET) {
            shouldBeRebroadcasted = false;
          }
          if (shouldBeRebroadcasted != null) {
            broadcast.fixRebroacasted(shouldBeRebroadcasted);
            EventBus.getDefault().post(new BroadcastUpdatedEvent(broadcast));
            notified = true;
          }
        }
        if (!notified) {
          // Must notify changed to reset pending status so that off-screen
          // items will be invalidated.
          mBroadcastAdapter.notifyBroadcastChanged();
        }
      }
      ToastUtils.show(
          getString(
              state.rebroadcast
                  ? R.string.broadcast_rebroadcast_failed_format
                  : R.string.broadcast_unrebroadcast_failed_format,
              ApiError.getErrorString(error, this)),
          this);
    }
  }
Пример #2
0
  private void onLikeResponse(
      boolean successful, Broadcast result, VolleyError error, LikeState state) {

    if (successful) {

      EventBus.getDefault().post(new BroadcastUpdatedEvent(result));
      ToastUtils.show(
          state.like ? R.string.broadcast_like_successful : R.string.broadcast_unlike_successful,
          this);

    } else {

      LogUtils.e(error.toString());
      Broadcast broadcast = mBroadcastAdapter.getBroadcast();
      if (broadcast != null) {
        boolean notified = false;
        if (error instanceof ApiError) {
          // Correct our local state if needed.
          ApiError apiError = (ApiError) error;
          Boolean shouldBeLiked = null;
          if (apiError.code == Codes.LikeBroadcast.ALREADY_LIKED) {
            shouldBeLiked = true;
          } else if (apiError.code == Codes.LikeBroadcast.NOT_LIKED_YET) {
            shouldBeLiked = false;
          }
          if (shouldBeLiked != null) {
            broadcast.fixLiked(shouldBeLiked);
            EventBus.getDefault().post(new BroadcastUpdatedEvent(broadcast));
            notified = true;
          }
        }
        if (!notified) {
          // Must notify changed to reset pending status so that off-screen
          // items will be invalidated.
          mBroadcastAdapter.notifyBroadcastChanged();
        }
      }
      ToastUtils.show(
          getString(
              state.like
                  ? R.string.broadcast_like_failed_format
                  : R.string.broadcast_unlike_failed_format,
              ApiError.getErrorString(error, this)),
          this);
    }
  }