summaryrefslogtreecommitdiff
path: root/app/src/main/java/uk/co/blatech/blaupload/HomeScreenFragment.java
blob: ee51908872fed5ac6ebb27e53526cdc8427f8490 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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.EventBus;
import uk.co.blatech.blaupload.util.ImageLoader;
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<ImageItem> thumbnails;
    private GridViewAdapter gridAdapter;
    private GridView gridView;
    private ArrayList<String> 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<ImageItem>();
        gridAdapter =
                new GridViewAdapter(getActivity(), R.layout.grid_item, thumbnails);
        gridView.setAdapter(gridAdapter);

        filenames = new ArrayList<String>();
        //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.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> 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 ImageLoader(getResources()).execute("http://www.lc8n.com/blathumbs/", filename+".png", 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();

    }

}