예제 #1
0
  public void onLinkAdded(String link) {
    Upload upload = new Upload(link, true);

    if (mAdapter == null) {
      mAdapter = new UploadPhotoAdapter(getActivity(), upload, this);
      mRecyclerView.setAdapter(mAdapter);
    } else {
      mAdapter.addItem(upload);
    }

    mMultiView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
    if (mListener != null) mListener.onPhotoAdded();
  }
예제 #2
0
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
      case RequestCodes.TAKE_PHOTO:
        if (resultCode == Activity.RESULT_OK) {
          if (!FileUtil.isFileValid(mTempFile)) {
            Snackbar.make(mMultiView, R.string.upload_camera_error, Snackbar.LENGTH_LONG).show();
            return;
          }

          FileUtil.scanFile(Uri.fromFile(mTempFile), getActivity());
          String fileLocation = mTempFile.getAbsolutePath();
          Upload upload = new Upload(fileLocation);

          if (mAdapter == null) {
            mAdapter = new UploadPhotoAdapter(getActivity(), upload, this);
            mRecyclerView.setAdapter(mAdapter);
          } else {
            mAdapter.addItem(upload);
          }

          mMultiView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
          mTempFile = null;
        }
        break;

      case RequestCodes.SELECT_PHOTO:
        if (resultCode == Activity.RESULT_OK && data != null) {
          ClipData clipData = data.getClipData();
          List<Uri> uris = null;

          // Check if we have multiple images
          if (clipData != null) {
            int size = clipData.getItemCount();
            uris = new ArrayList<>(size);

            for (int i = 0; i < size; i++) {
              uris.add(clipData.getItemAt(i).getUri());
            }
          } else if (data.getData() != null) {
            // If not multiple images, then only one was selected
            uris = new ArrayList<>(1);
            uris.add(data.getData());
          }

          if (uris != null && !uris.isEmpty()) {
            mMultiView.setViewState(MultiStateView.VIEW_STATE_LOADING);
            new DecodeImagesTask(this).execute(uris);
          } else {
            Snackbar.make(mMultiView, R.string.error_generic, Snackbar.LENGTH_LONG).show();
          }
        }
        break;

      case RequestCodes.UPLOAD_EDIT:
        if (resultCode == Activity.RESULT_OK && data != null) {
          final Upload upload;

          if (data.hasExtra(UploadEditActivity.KEY_UPDATED_UPLOAD)) {
            upload = data.getParcelableExtra(UploadEditActivity.KEY_UPDATED_UPLOAD);

            if (upload != null) {
              mAdapter.updateItem(upload);
            }
          } else if (data.hasExtra(UploadEditActivity.KEY_UPDATED_DELETED)) {
            upload = data.getParcelableExtra(UploadEditActivity.KEY_UPDATED_DELETED);

            if (upload != null) {
              final int itemIndex = mAdapter.indexOf(upload);

              if (itemIndex > -1) {
                mAdapter.removeItem(itemIndex);
                if (mListener != null) mListener.onPhotoRemoved(mAdapter.getItemCount());
                if (mAdapter.isEmpty()) mMultiView.setViewState(MultiStateView.VIEW_STATE_EMPTY);

                Snackbar.make(mMultiView, R.string.upload_photo_removed, Snackbar.LENGTH_LONG)
                    .setAction(
                        R.string.undo,
                        new View.OnClickListener() {
                          @Override
                          public void onClick(View v) {
                            mMultiView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                            mAdapter.addItem(upload, itemIndex);
                            if (mListener != null) mListener.onPhotoAdded();
                          }
                        })
                    .show();
              }
            }
          }
        }
        break;
    }

    super.onActivityResult(requestCode, resultCode, data);
  }