@Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
   if (requestCode == RESULT_LOAD_IMAGE && resultCode == Activity.RESULT_OK && null != data) {
     mImagesPanel.addImage(data.getData(), data.getType());
   }
 }
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_new_post, container, false);
    mPostText = (MultiAutoCompleteTextView) rootView.findViewById(R.id.postText);
    mUsersListAdapter = new UserCompletionAdapter(getActivity());
    mPostText.setAdapter(mUsersListAdapter);
    mPostText.setTokenizer(new SymbolTokenizer('@'));
    mIsPrivate = (Switch) rootView.findViewById(R.id.isPrivate);
    mTagsListAdapter =
        new ArrayAdapter<>(getActivity(), android.R.layout.simple_dropdown_item_1line);
    mPostTags = (MultiAutoCompleteTextView) rootView.findViewById(R.id.postTags);
    mPostTags.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    mPostTags.setAdapter(mTagsListAdapter);
    mImagesPanel = (ImageUploadingPanel) rootView.findViewById(R.id.imagesPanel);
    setHasOptionsMenu(true);
    if (savedInstanceState == null) {
      Bundle args = getArguments();
      if (args != null) {
        mPostId = args.getString(ARG_ID);
        mPostText.setText(args.getString(ARG_TEXT, ""));
        String[] tags = args.getStringArray(ARG_TAGS);
        mIsPrivate.setChecked(args.getBoolean(ARG_PRIVATE));
        if (tags != null) {
          mPostTags.setText(TextUtils.join(", ", tags));
        }
        ArrayList<Uri> images = args.getParcelableArrayList(ARG_IMAGES);
        if (images != null)
          for (Uri image : images) {
            mImagesPanel.addImage(image, mMime);
          }
        mMime = args.getString(ARG_MIME);
      }
    }
    if (mPostId == null) {
      mIsPrivate.setVisibility(View.VISIBLE);
    } else {
      mIsPrivate.setVisibility(View.GONE);
    }
    mProgressDialog =
        new MaterialDialog.Builder(getActivity())
            .cancelable(false)
            .customView(R.layout.dialog_progress, false)
            .build();

    TagsRequest request = new TagsRequest(PointConnectionManager.getInstance().loginResult.login);
    getSpiceManager()
        .getFromCacheAndLoadFromNetworkIfExpired(
            request, request.getCacheName(), DurationInMillis.ONE_DAY, mTagsRequestListener);
    UserSubscriptionsRequest request2 =
        new UserSubscriptionsRequest(PointConnectionManager.getInstance().loginResult.login);
    getSpiceManager()
        .getFromCacheAndLoadFromNetworkIfExpired(
            request2, request2.getCacheName(), DurationInMillis.ONE_DAY, mUsersRequestListener);
    return rootView;
  }
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
   int id = item.getItemId();
   if (id == R.id.send) {
     if (!mImagesPanel.isUploadFinished()) {
       Toast.makeText(getActivity(), "Wait or check for errors!", Toast.LENGTH_SHORT).show();
       return true;
     }
     final String text = mPostText.getText().toString();
     final String[] tags = mPostTags.getText().toString().split("\\s*,\\s*");
     StringBuilder sb = new StringBuilder(text);
     for (String l : mImagesPanel.getLinks()) {
       sb.append("\n").append(l);
     }
     mProgressDialog.show();
     if (TextUtils.isEmpty(mPostId)) {
       if (mIsPrivate.isChecked())
         PointConnectionManager.getInstance()
             .pointIm
             .createPrivatePost(
                 sb.toString().trim(), tags, mIsPrivate.isChecked(), mNewPostCallback);
       else
         PointConnectionManager.getInstance()
             .pointIm
             .createPost(sb.toString().trim(), tags, mNewPostCallback);
     } else {
       PointConnectionManager.getInstance()
           .pointIm
           .editPost(mPostId, sb.toString().trim(), tags, mNewPostCallback);
     }
     return true;
   } else if (id == R.id.attach) {
     Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
     startActivityForResult(i, RESULT_LOAD_IMAGE);
   }
   return super.onOptionsItemSelected(item);
 }