Android Open Source - spring-a-gram-android Gallery Add Fragment






From Project

Back to project page spring-a-gram-android.

License

The source code is released under:

Apache License

If you think the Android project spring-a-gram-android 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

/*
 * Copyright 2014 Roy Clarkson//from  w  ww  .java2s  .c o  m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.royclarkson.springagram;

import android.app.Activity;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.royclarkson.springagram.model.Gallery;

import org.springframework.web.client.RestTemplate;


/**
 * {@link Fragment} that displays a form to add a new {@link Gallery}
 *
 * @author Roy Clarkson
 */
public class GalleryAddFragment extends Fragment implements View.OnClickListener {

  private static final String TAG = GalleryAddFragment.class.getSimpleName();

  private static final String ARG_GALLERIES_LIST_URL = "galleries_url";

  private String galleriesUrl;

  private GalleryAddFragmentListener galleryAddFragmentListener;


  public GalleryAddFragment() {
    // Required empty public constructor
  }

  public static GalleryAddFragment newInstance(String galleriesUrl) {
    GalleryAddFragment fragment = new GalleryAddFragment();
    Bundle args = new Bundle();
    args.putString(ARG_GALLERIES_LIST_URL, galleriesUrl);
    fragment.setArguments(args);
    return fragment;
  }


  //***************************************
  // Fragment methods
  //***************************************

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
      this.galleriesUrl = getArguments().getString(ARG_GALLERIES_LIST_URL);
    }
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
               Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_gallery_add, container, false);
    Button button = (Button) view.findViewById(R.id.button_save);
    button.setOnClickListener(this);
    return view;
  }

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

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
      galleryAddFragmentListener = (GalleryAddFragmentListener) activity;
    } catch (ClassCastException e) {
      throw new ClassCastException(activity.toString()
          + " must implement GalleryAddFragmentListener");
    }
  }

  @Override
  public void onDetach() {
    super.onDetach();
    galleryAddFragmentListener = null;
  }


  //***************************************
  // onClickListener methods
  //***************************************

  public void onClick(View view) {
    if (view.getId() == R.id.button_save) {
      new AddGalleryTask().execute(this.galleriesUrl);
    }
  }

  private String getGalleryDescription() {
    EditText descriptionText = (EditText) this.getActivity().findViewById(R.id.text_description);
    return descriptionText.getText().toString();
  }


  //***************************************
  // Listener interface
  //***************************************

  public interface GalleryAddFragmentListener {

    public void onGalleryAddComplete();

  }


  //***************************************
  // Private classes
  //***************************************

  private class AddGalleryTask extends AsyncTask<String, Void, Boolean> {

    private Gallery gallery;

    @Override
    protected void onPreExecute() {
      this.gallery = new Gallery(getGalleryDescription());
    }

    @Override
    protected Boolean doInBackground(String... params) {
      try {
        final String url = params[0];
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.postForLocation(url, this.gallery);
        return true;
      } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
        return false;
      }
    }

    @Override
    protected void onPostExecute(Boolean success) {
      if (galleryAddFragmentListener != null) {
        galleryAddFragmentListener.onGalleryAddComplete();
      }
    }

  }

}




Java Source Code List

com.royclarkson.springagram.CheckableLinearLayout.java
com.royclarkson.springagram.GalleryAddFragment.java
com.royclarkson.springagram.GalleryListAdapter.java
com.royclarkson.springagram.GalleryListFragment.java
com.royclarkson.springagram.GalleryPhotoListFragment.java
com.royclarkson.springagram.HomeFragment.java
com.royclarkson.springagram.MainActivity.java
com.royclarkson.springagram.NavigationDrawerFragment.java
com.royclarkson.springagram.PhotoAddFragment.java
com.royclarkson.springagram.PhotoAddToGalleryFragment.java
com.royclarkson.springagram.PhotoAddToGalleryListAdapter.java
com.royclarkson.springagram.PhotoDetailFragment.java
com.royclarkson.springagram.PhotoListAdapter.java
com.royclarkson.springagram.PhotoListFragment.java
com.royclarkson.springagram.RestUtils.java
com.royclarkson.springagram.model.ApiResource.java
com.royclarkson.springagram.model.GalleryResource.java
com.royclarkson.springagram.model.Gallery.java
com.royclarkson.springagram.model.ItemResource.java
com.royclarkson.springagram.model.Item.java
org.springframework.hateoas.hal.ResourceMappingJackson2HttpMessageConverter.java