Android Open Source - tpblogr Post Edition Fragment






From Project

Back to project page tpblogr.

License

The source code is released under:

MIT License

If you think the Android project tpblogr listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package blogr.vpm.fr.blogr.activity;
//  ww  w .  j  a  v  a 2  s.co  m
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import blogr.vpm.fr.blogr.R;
import blogr.vpm.fr.blogr.apis.flickr.FlickrJAndroidProvider;
import blogr.vpm.fr.blogr.apis.flickr.FlickrJAsyncTaskProvider;
import blogr.vpm.fr.blogr.apis.flickr.FlickrProvider;
import blogr.vpm.fr.blogr.bean.Blog;
import blogr.vpm.fr.blogr.bean.Post;
import blogr.vpm.fr.blogr.bean.TPBlog;
import blogr.vpm.fr.blogr.format.AlignCenterTagsProvider;
import blogr.vpm.fr.blogr.format.AlignLeftTagsProvider;
import blogr.vpm.fr.blogr.format.AlignRightTagsProvider;
import blogr.vpm.fr.blogr.insertion.DefaultInserter;
import blogr.vpm.fr.blogr.insertion.Inserter;
import blogr.vpm.fr.blogr.insertion.SurroundingTagsProvider;
import blogr.vpm.fr.blogr.location.AndroidLocationProvider;
import blogr.vpm.fr.blogr.location.LatLongTagProvider;
import blogr.vpm.fr.blogr.location.LocationProvider;
import blogr.vpm.fr.blogr.persistence.FilePostSaver;
import blogr.vpm.fr.blogr.persistence.PostSaver;
import blogr.vpm.fr.blogr.picture.PicturePickedListener;
import blogr.vpm.fr.blogr.publish.PostPublisher;
import blogr.vpm.fr.blogr.service.PostPublishingPreferencesProvider;
import blogr.vpm.fr.blogr.service.PostPublishingServiceProvider;

/**
 * Created by vincent on 07/10/14.
 */
public class PostEditionFragment extends Fragment implements PicturePickedListener {

  public static final int PICK_PIC_REQ_CODE = 32;
  public static final int MAX_NEW_POST_FILES = 100;

  private PostPublishingServiceProvider publisherProvider;

  private PostSaver saver;

  private LocationProvider locationProvider;

  private Blog currentBlog;

  private Post currentPost;

  private EditText contentField;

  private EditText titleField;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);

    Log.d("postF", "creating fragment");
    // init services
    publisherProvider = new PostPublishingPreferencesProvider();
    saver = new FilePostSaver(getActivity());
    locationProvider = new AndroidLocationProvider(getActivity());

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_post, container, false);
    contentField = (EditText) v.findViewById(R.id.postContent);
    refreshViewFromPost();
    return v;
  }

  /**
   * Notes on lifecycle
   * before onResume, the currentPost has the right content and title
   * between onResume and onPause, the view has the right content and title
   * between onPause and onResume, the currentPost has the right content and title but the view should not change
   * (e.g. when onActivityResult() is called)
   * after onPause, the currentPost has the right content and title but the view should not change
   */
  @Override
  public void onResume() {
    super.onResume();
    getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
    refreshViewFromPost();
    locationProvider.connect();
  }

  @Override
  public void onPause() {
    super.onPause();
    refreshPostFromView();
    saveCurrentPost();
    locationProvider.disconnect();
  }

  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.postedition, menu);
    titleField = (EditText) menu.findItem(R.id.action_title).getActionView();
    refreshViewFromPost();
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

    Inserter tagsInserter = new DefaultInserter(getActivity());
    switch (item.getItemId()) {
      case R.id.action_settings:
        startActivity(new Intent(getActivity(), AllPreferencesActivity.class));
        return true;
      case R.id.action_publish:
        PostPublisher publisher = publisherProvider.getPublisherService(getActivity());
        refreshPostFromView();
        publisher.publish(currentBlog, currentPost);
        return true;
      case R.id.action_insert_location:
        tagsInserter.insert(contentField, new LatLongTagProvider(getActivity(), locationProvider));
        return true;
      case R.id.action_insert_picture:
        Intent i = new Intent(Intent.ACTION_PICK);
        i.setType("image/*");
        startActivityForResult(i, PICK_PIC_REQ_CODE);
        return true;
      case R.id.action_insert_flickr:
        FlickrProvider flickrD = new FlickrJAndroidProvider(getActivity());
        FlickrProvider flickrP = new FlickrJAsyncTaskProvider(getActivity(), flickrD);
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String flickrUsername = prefs.getString("pref_flickr_username", "");
        int picNb = Integer.valueOf(prefs.getString("pref_flickr_number_pics", "20"));
        flickrP.getUserPhotos(flickrUsername, picNb);
        return true;
      case R.id.action_align_left:
        tagsInserter.insert(contentField, new AlignLeftTagsProvider());
        return true;
      case R.id.action_align_center:
        tagsInserter.insert(contentField, new AlignCenterTagsProvider());
        return true;
      case R.id.action_align_right:
        tagsInserter.insert(contentField, new AlignRightTagsProvider());
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  // called before onResume
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if ((PICK_PIC_REQ_CODE == requestCode) && (Activity.RESULT_OK == resultCode)) {
      Uri pictureUri = data.getData();
      currentPost.addPicture(pictureUri);
      onPicturePicked(pictureUri.toString());
    } else {
      super.onActivityResult(requestCode, resultCode, data);
    }
  }

  @Override
  public void onPicturePicked(String picUrl) {
    SurroundingTagsProvider pictureTagProvider = publisherProvider.getPictureTagsProvider(getActivity(), picUrl);
    String updatedPostContent = new DefaultInserter(getActivity()).insert(contentField, pictureTagProvider);
    // the currentPost must be updated because this may be called before onResume
    currentPost.setContent(updatedPostContent);
  }

  /**
   * Updates the current post with given instance and refreshes the view
   *
   * @param post The new post to edit
   */
  public void editPost(Post post) {
    currentPost = post;
    currentBlog = post.getBlog();
    refreshViewFromPost();
    getActivity().invalidateOptionsMenu();
  }

  /**
   * Refreshes the view with the current Post
   */
  private void refreshViewFromPost() {
    if (currentPost != null) {
      if (titleField != null) {
        titleField.setText(currentPost.getTitle());
      }
      if (contentField != null) {
        contentField.setText(currentPost.getContent());
      }
    }
  }

  /**
   * Refreshes the Post with the current view elements
   */
  private void refreshPostFromView() {
    if ((titleField != null) && (contentField != null)) {
      currentPost.setTitle(titleField.getText().toString());
      currentPost.setContent(contentField.getText().toString());
    }
  }

  /**
   * Saves the post built from the view
   */
  private void saveCurrentPost() {
    // save only if post has content or title
    if ((currentPost != null) && (!isPostTitleEmpty() || !isPostContentEmpty())) {
      if (isPostTitleEmpty()) {
        determineAvailablePostTitle();
      }
      saver.persist(currentPost);
    }
  }

  /**
   * Determines a title for the post that does not exist yet - in order not to override written post.
   */
  private void determineAvailablePostTitle() {
    String newPostTitle = getActivity().getResources().getString(R.string.newpost);
    currentPost.setTitle(newPostTitle);
    if (saver.exists(currentPost)) {
      for (int i = 1; saver.exists(currentPost) && (i < MAX_NEW_POST_FILES); i++) {
        currentPost.setTitle(newPostTitle + " " + i);
      }
    }
  }

  private boolean isPostContentEmpty() {
    return ("".equals(currentPost.getContent()));
  }

  private boolean isPostTitleEmpty() {
    return ("".equals(currentPost.getTitle()));
  }
}




Java Source Code List

blogr.vpm.fr.blogr.activity.AllPreferencesActivity.java
blogr.vpm.fr.blogr.activity.BlogActivity.java
blogr.vpm.fr.blogr.activity.BlogListDialogFragment.java
blogr.vpm.fr.blogr.activity.DialogDismissedListener.java
blogr.vpm.fr.blogr.activity.EmailBlogEditionFragment.java
blogr.vpm.fr.blogr.activity.FlickrDialogFragment.java
blogr.vpm.fr.blogr.activity.GithubBlogEditionFragment.java
blogr.vpm.fr.blogr.activity.InvalidatedModelListener.java
blogr.vpm.fr.blogr.activity.PostEditionActivity.java
blogr.vpm.fr.blogr.activity.PostEditionFragment.java
blogr.vpm.fr.blogr.activity.PostListActivity.java
blogr.vpm.fr.blogr.activity.PostListChoiceModeListener.java
blogr.vpm.fr.blogr.activity.PostListFragment.java
blogr.vpm.fr.blogr.activity.PostSelectionListener.java
blogr.vpm.fr.blogr.activity.PreferenceCategoryFragment.java
blogr.vpm.fr.blogr.apis.flickr.FlickrJAndroidProvider.java
blogr.vpm.fr.blogr.apis.flickr.FlickrJAsyncTaskProvider.java
blogr.vpm.fr.blogr.apis.flickr.FlickrProvider.java
blogr.vpm.fr.blogr.apis.flickr.ParcelableFlickrPhoto.java
blogr.vpm.fr.blogr.bean.Blog.java
blogr.vpm.fr.blogr.bean.EmailBlog.java
blogr.vpm.fr.blogr.bean.GithubBlog.java
blogr.vpm.fr.blogr.bean.Post.java
blogr.vpm.fr.blogr.bean.TPBlog.java
blogr.vpm.fr.blogr.format.AlignCenterTagsProvider.java
blogr.vpm.fr.blogr.format.AlignLeftTagsProvider.java
blogr.vpm.fr.blogr.format.AlignRightTagsProvider.java
blogr.vpm.fr.blogr.insertion.DefaultInserter.java
blogr.vpm.fr.blogr.insertion.Inserter.java
blogr.vpm.fr.blogr.insertion.SingleTagProvider.java
blogr.vpm.fr.blogr.insertion.SurroundingTagsProvider.java
blogr.vpm.fr.blogr.location.AndroidLocationProvider.java
blogr.vpm.fr.blogr.location.LatLongTagProvider.java
blogr.vpm.fr.blogr.location.LocationProvider.java
blogr.vpm.fr.blogr.persistence.BlogRetriever.java
blogr.vpm.fr.blogr.persistence.BlogSaver.java
blogr.vpm.fr.blogr.persistence.FileBlogManager.java
blogr.vpm.fr.blogr.persistence.FilePostRetriever.java
blogr.vpm.fr.blogr.persistence.FilePostSaver.java
blogr.vpm.fr.blogr.persistence.PostRetriever.java
blogr.vpm.fr.blogr.persistence.PostSaver.java
blogr.vpm.fr.blogr.picture.AsyncPictureLoader.java
blogr.vpm.fr.blogr.picture.ImageViewLoader.java
blogr.vpm.fr.blogr.picture.PictureLoadedListener.java
blogr.vpm.fr.blogr.picture.PictureMdTagsProvider.java
blogr.vpm.fr.blogr.picture.PicturePickedListener.java
blogr.vpm.fr.blogr.picture.PictureTagProvider.java
blogr.vpm.fr.blogr.picture.PictureTpTagsProvider.java
blogr.vpm.fr.blogr.publish.Formatter.java
blogr.vpm.fr.blogr.publish.HtmlFormatter.java
blogr.vpm.fr.blogr.publish.IdentityFormatter.java
blogr.vpm.fr.blogr.publish.PostPublisher.java
blogr.vpm.fr.blogr.publish.StdEmailPostPublisher.java
blogr.vpm.fr.blogr.publish.TPJavaMailPostPublisher.java
blogr.vpm.fr.blogr.publish.TPPostPublisher.java
blogr.vpm.fr.blogr.service.PostPublishingPreferencesProvider.java
blogr.vpm.fr.blogr.service.PostPublishingServiceProvider.java