package uk.co.blatech.blaupload; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.GridView; import android.widget.Toast; import com.squareup.otto.Subscribe; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import uk.co.blatech.blaupload.data.ImageItem; import uk.co.blatech.blaupload.ui.GridViewAdapter; import uk.co.blatech.blaupload.util.BlaImageLoader; import uk.co.blatech.blaupload.util.EventBus; import uk.co.blatech.blaupload.util.ImageLoaderResultEvent; import uk.co.blatech.blaupload.util.JSONLoader; import uk.co.blatech.blaupload.util.JSONLoaderResultEvent; /** * A simple {@link Fragment} subclass. * This is the main part of the home screen. * Currently it displays the 9 most recent files from blaupload in a grid * */ public class HomeScreenFragment extends Fragment { private OnFragmentInteractionListener mListener; private ArrayList thumbnails; private GridViewAdapter gridAdapter; private GridView gridView; private ArrayList filenames; private JSONArray json; /** * Returns a new instance of this fragment */ public static HomeScreenFragment newInstance() { HomeScreenFragment fragment = new HomeScreenFragment(); Bundle args = new Bundle(); fragment.setArguments(args); return fragment; } public HomeScreenFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_home_screen, container, false); EventBus.getInstance().register(this); gridView = (GridView) rootView.findViewById(R.id.gridView); return rootView; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onCreate(savedInstanceState); thumbnails = new ArrayList(); gridAdapter = new GridViewAdapter(getActivity(), R.layout.grid_item, thumbnails); gridView.setAdapter(gridAdapter); filenames = new ArrayList(); //Open the full image in a pager when clicked gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(getActivity(), "Clicked image #"+position, Toast.LENGTH_SHORT).show(); Intent mIntent = new Intent(getActivity(), ImagePager.class); mIntent.putExtra("json", json.toString()); mIntent.putExtra("position", position); startActivity(mIntent); } }); } @Override public void onAttach(Activity activity) { super.onAttach(activity); //Start loading the JSON file list in a thread as soon as we start new JSONLoader().execute("http://www.blaupload.co.uk/?format=json&last=6"); } @Override public void onDetach() { super.onDetach(); mListener = null; } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. *

* See the Android Training lesson Communicating with Other Fragments for more information. */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name public void onFragmentInteraction(Uri uri); } /** * This function is called when the JSONLoader thread finishes * It processes the JSON and then starts loading the image thumbnails * @param event Event containing the JSONArray of files */ @Subscribe public void onAsyncTaskResult(JSONLoaderResultEvent event) { json = event.getResult(); if (json != null) { try { // Get the filename out of each JSON item for (int i = 0; i < json.length(); i++) { JSONObject item = json.getJSONObject(i); filenames.add(item.getString("filename")); } } catch (JSONException e) { //TODO: Error handling e.printStackTrace(); } //Load the thumbnail for each item on a thread int id = 0; for (String filename : filenames) { new BlaImageLoader(getResources()).execute("http://www.lc8n.com/bla100/", filename, String.valueOf(id)); id++; } } } /** * This function is called when an ImageLoader thread finishes * It puts the image into the grid * @param event Event containing an ImageItem with a bitmap and image details */ @Subscribe public void onAsyncTaskResult(ImageLoaderResultEvent event) { ImageItem image = event.getResult(); thumbnails.add(image.getId(), image); gridAdapter.notifyDataSetChanged(); } }