Android Open Source - LinuxZaSve_mobile Comment Edit Fragment






From Project

Back to project page LinuxZaSve_mobile.

License

The source code is released under:

Apache License

If you think the Android project LinuxZaSve_mobile 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 com.linuxzasve.mobile.fragments;
/*from ww  w  .ja va2s.c  om*/
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
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.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.Toast;

import com.linuxzasve.mobile.R;
import com.linuxzasve.mobile.db.Comment;
import com.linuxzasve.mobile.rest.LzsRestGateway;
import com.linuxzasve.mobile.rest.model.LzsRestResponse;
import com.linuxzasve.mobile.rest.model.Post;

import java.util.HashSet;
import java.util.Set;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class CommentEditFragment extends Fragment {

    private CommentEditFragmentListener commentEditFragmentListener;
    private Post post;
    private String[] usedNames;
    private String[] usedEmails;

    private AutoCompleteTextView authorEmailAutocomplete;
    private AutoCompleteTextView authorNameAutocomplete;

    private Context context;

    @Override
    public void onResume() {
        super.onResume();

        commentEditFragmentListener.setCommentEditFragmentActionBarTitle();
    }

    @Override
    public void onAttach(final Activity activity) {
        super.onAttach(activity);

        context = activity;

        try {
            commentEditFragmentListener = (CommentEditFragmentListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement CommentEditFragmentListener");
        }
    }

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

        post = getArguments().getParcelable(ArticleDisplayFragment.BUNDLE_POST);

        usedNames = getUsedNames();
        usedEmails = getUsedEmails();

        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {

        return inflater.inflate(R.layout.comment_edit_fragment, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, usedNames);
        authorNameAutocomplete = (AutoCompleteTextView) getActivity().findViewById(
                R.id.comment_author_name);
        authorNameAutocomplete.setAdapter(adapter);

        ArrayAdapter<String> adapterEmails = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, usedEmails);
        authorEmailAutocomplete = (AutoCompleteTextView) getActivity().findViewById(
                R.id.comment_author_email);
        authorEmailAutocomplete.setAdapter(adapterEmails);
    }

    @Override
    public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
            inflater.inflate(R.menu.comment_edit_menu, menu);

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                commentEditFragmentListener.onCommentEditFragmentUpNavPressed();
                return true;

            case R.id.menu_send:

                final String name = authorNameAutocomplete.getText().toString();
                final String email = authorEmailAutocomplete.getText().toString();

                EditText et4 = (EditText) getActivity().findViewById(R.id.comment_text);
                String text = et4.getText().toString();

                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(email)) {
                    Toast toast = Toast.makeText(getActivity(), R.string.mandatory_fields_empty, Toast.LENGTH_SHORT);
                    toast.show();
                } else {
                    LzsRestGateway g = new LzsRestGateway();
                    g.submitComment(post.getId(), name, email, text,
                                    new Callback<LzsRestResponse>() {
                                        @Override
                                        public void success(LzsRestResponse lzsRestResponse, Response response) {
                                            if ("ok".equals(lzsRestResponse.getStatus())) {

                                                Toast toast = Toast.makeText(context, R.string.comment_sent, Toast.LENGTH_LONG);
                                                toast.show();

                                                Comment comment = new Comment(name, email);
                                                comment.save();
                                            } else {

                                                Toast toast = Toast.makeText(context, R.string.comment_send_failed, Toast.LENGTH_LONG);
                                                toast.show();
                                            }

                                        }

                                        @Override
                                        public void failure(RetrofitError error) {
                                            Toast toast = Toast.makeText(context, R.string.comment_send_failed, Toast.LENGTH_LONG);
                                            toast.show();

                                        }
                                    });
                    commentEditFragmentListener.onSendCommentButtonPressed();

                }

                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private String[] getUsedNames() {
        Set<String> allNames = new HashSet<String>();

        for (Comment c : Comment.all()) {
            allNames.add(c.name);
        }

        return allNames.toArray(new String[allNames.size()]);
    }

    private String[] getUsedEmails() {
        Set<String> allEmails = new HashSet<String>();

        for (Comment c : Comment.all()) {
            allEmails.add(c.email);
        }

        return allEmails.toArray(new String[allEmails.size()]);
    }

    public interface CommentEditFragmentListener {
        public void onCommentEditFragmentUpNavPressed();
        public void setCommentEditFragmentActionBarTitle();
        public void onSendCommentButtonPressed();
    }
}




Java Source Code List

com.linuxzasve.mobile.ActivityHelper.java
com.linuxzasve.mobile.MainActivity.java
com.linuxzasve.mobile.adapters.ArticleListArrayAdapter.java
com.linuxzasve.mobile.adapters.CommentListArrayAdapter.java
com.linuxzasve.mobile.db.Comment.java
com.linuxzasve.mobile.emote.EmoticonDrawables.java
com.linuxzasve.mobile.fragments.ArticleDisplayFragment.java
com.linuxzasve.mobile.fragments.ArticleListFragmentType.java
com.linuxzasve.mobile.fragments.ArticleListFragment.java
com.linuxzasve.mobile.fragments.CommentEditFragment.java
com.linuxzasve.mobile.fragments.CommentListFragment.java
com.linuxzasve.mobile.googl.GooGlService.java
com.linuxzasve.mobile.googl.GoogleUrlShortener.java
com.linuxzasve.mobile.googl.model.GooGlRequest.java
com.linuxzasve.mobile.googl.model.GooGlResponse.java
com.linuxzasve.mobile.rest.LzsRestGateway.java
com.linuxzasve.mobile.rest.LzsRestService.java
com.linuxzasve.mobile.rest.model.Attachment.java
com.linuxzasve.mobile.rest.model.Author.java
com.linuxzasve.mobile.rest.model.Category.java
com.linuxzasve.mobile.rest.model.Comment.java
com.linuxzasve.mobile.rest.model.CustomFields.java
com.linuxzasve.mobile.rest.model.DetailedImage.java
com.linuxzasve.mobile.rest.model.Image.java
com.linuxzasve.mobile.rest.model.LzsRestResponse.java
com.linuxzasve.mobile.rest.model.Post.java
com.linuxzasve.mobile.rest.model.Tag.java
com.linuxzasve.mobile.rest.model.Thumbnail.java
com.linuxzasve.mobile.timthumb.TimThumb.java