app.hacked.ProjectDetailsFragment.java Source code

Java tutorial

Introduction

Here is the source code for app.hacked.ProjectDetailsFragment.java

Source

/*
 * Copyright (C) 2013 - Gareth Llewellyn
 *
 * This file is part of the Unofficial HackedIO Android App https://github.com/NetworksAreMadeOfString/HackedIO-Android-App
 *
 * This app is unofficial. The "Hacked.io" logo is copyright (and maybe even a trademark of) Telefnica UK Limited.
 * The author of this app is not affiliated with Telefnica UK Limited, The Lab ( https://thelab.o2.com/ ) or
 * Geeks of London ( http://geeksoflondon.com/ ) although the author <strong>is</strong> an attendee.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>
 */
package app.hacked;

import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;

import java.util.ArrayList;

public class ProjectDetailsFragment extends Fragment {
    public static final String PROJECTID = "projectID";
    public static final String NAME = "Name";
    public static final String SYNOPSIS = "Synopsis";
    public static final String DESCRIPTION = "Description";
    public static final String LOCATION = "Location";
    public static final String TECHNOLOGIES = "Technologies";
    public static final String POPULARITY = "Popularity";
    public static final String TEAMMEMBERS = "TeamMembers";
    public static final String ARG_2PANE = "m2pane";

    private int projectID = 0;
    private String Name = "";
    private String Synopsis = "";
    private String Description = "";
    private String Location = "";
    private String Technologies = "";
    private int Popularity = 0;
    //private String TeamMembers = "";
    private Boolean m2pane = false;
    ArrayList<TeamMember> TeamMembers = null;
    TeamMemberGridAdapter adapter = null;
    RequestQueue queue = null;

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

        if (getArguments().containsKey(PROJECTID)) {
            projectID = getArguments().getInt(PROJECTID);
        }

        if (getArguments().containsKey(POPULARITY)) {
            Popularity = getArguments().getInt(POPULARITY);
        }

        if (getArguments().containsKey(NAME)) {
            Name = getArguments().getString(NAME);
        }

        if (getArguments().containsKey(SYNOPSIS)) {
            Synopsis = getArguments().getString(SYNOPSIS);
        }

        if (getArguments().containsKey(DESCRIPTION)) {
            Description = getArguments().getString(DESCRIPTION);
        }

        if (getArguments().containsKey(LOCATION)) {
            Location = getArguments().getString(LOCATION);
        }

        if (getArguments().containsKey(TECHNOLOGIES)) {
            Technologies = getArguments().getString(TECHNOLOGIES);
            if (null == Technologies || Technologies.equals("null")) {
                Technologies = "None specified";
            }
        } else {
            Technologies = "None specified";
        }

        if (getArguments().containsKey(TEAMMEMBERS)) {
            //TeamMembers = getArguments().getString(TEAMMEMBERS);
            TeamMembers = getArguments().getParcelableArrayList(TEAMMEMBERS);
            adapter = new TeamMemberGridAdapter(getActivity(), TeamMembers);
        }

        if (getArguments().containsKey(ARG_2PANE)) {
            m2pane = getArguments().getBoolean(ARG_2PANE);
        }

        queue = Volley.newRequestQueue(getActivity());
    }

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

        if (null != adapter && null != adapter.TeamMembers && adapter.TeamMembers.size() > 0) {
            ((GridView) getActivity().findViewById(R.id.gridViewteamMembers)).setAdapter(adapter);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.project_detail_fragment, container, false);

        ((TextView) rootView.findViewById(R.id.Title)).setText(Name);
        ((TextView) rootView.findViewById(R.id.Title))
                .setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));

        /*((TextView) rootView.findViewById(R.id.TeamMembers)).setText(TeamMembers);
        ((TextView) rootView.findViewById(R.id.TeamMembers)).setTypeface(Typeface.create("sans-serif-condensed", Typeface.NORMAL));*/

        ((TextView) rootView.findViewById(R.id.Tech)).setText(Technologies);
        ((TextView) rootView.findViewById(R.id.Tech))
                .setTypeface(Typeface.create("sans-serif-condensed", Typeface.NORMAL));

        ((TextView) rootView.findViewById(R.id.Description)).setText(Html.fromHtml(Description));
        ((TextView) rootView.findViewById(R.id.Description))
                .setTypeface(Typeface.create("sans-serif", Typeface.NORMAL));

        ((Button) rootView.findViewById(R.id.VoteUp)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doVote(true);
            }
        });

        ((Button) rootView.findViewById(R.id.VoteDown)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doVote(false);
            }
        });

        ((Button) rootView.findViewById(R.id.EditProject)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("https://www.hackerleague.org/hackathons/hacked/hacks")));
            }
        });

        return rootView;
    }

    /*@Override
    public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater)
    {
    inflater.inflate(R.menu.project_details, menu);
    super.onCreateOptionsMenu(menu, inflater);
    }
        
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    switch (item.getItemId())
    {
        case R.id.action_vote_down:
        {
            doVote(false);
            return true;
        }
        
        case R.id.action_vote_up:
        {
            doVote(true);
            return true;
        }
        
        case R.id.action_edit_project:
        {
            return true;
        }
        
        default:
            return false;
    }
    }*/

    public void doVote(boolean up) {
        String vote = "down";

        if (up)
            vote = "up";

        String url = "http://hackedioapp.networksaremadeofstring.co.uk/project_vote.php?direction=" + vote + "&uid="
                + Settings.Secure.getString(getActivity().getContentResolver(), Settings.Secure.ANDROID_ID)
                + "&project=" + Integer.toString(projectID);

        JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.e("response", response.toString());
                        try {
                            if (response.has("success") && response.getBoolean("success")) {
                                Toast.makeText(getActivity(), "Voted!", Toast.LENGTH_SHORT).show();
                            } else if (response.has("error")) {
                                Toast.makeText(getActivity(), response.getString("error"), Toast.LENGTH_SHORT)
                                        .show();
                            } else {
                                Toast.makeText(getActivity(), "An Error Was encountered", Toast.LENGTH_SHORT)
                                        .show();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(getActivity(), "An Error Was encountered: " + e.getMessage(),
                                    Toast.LENGTH_SHORT).show();
                        }

                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                        Toast.makeText(getActivity(), "An Error Was encountered", Toast.LENGTH_SHORT).show();
                        error.printStackTrace();
                    }
                });

        queue.add(jsObjRequest);
    }
}