<commit_before>package com.example.spartahack.spartahack2016.Fragment;


import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.spartahack.spartahack2016.Activity.MainActivity;
import com.example.spartahack.spartahack2016.Adapters.SimpleSectionedRecyclerViewAdapter;
import com.example.spartahack.spartahack2016.Adapters.TicketAdapter;
import com.example.spartahack.spartahack2016.Model.Ticket;
import com.example.spartahack.spartahack2016.R;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HelpFragment extends BaseFragment {

    @Bind(R.id.recycler) RecyclerView ticketView;
    @Bind(R.id.no_user) LinearLayout noUser;
    @Bind(R.id.user) RelativeLayout userExists;
    @Bind(R.id.no_tix) TextView noTix;

    private TicketAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    private final String I_EXTRA_FROM = "from help";

    ParseUser user;
    List<ParseObject> categoryList;
    private ArrayList<Ticket> tickets;

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

        registerEventBus = true;

        tickets = new ArrayList<>();

        user = ParseUser.getCurrentUser();

        view = inflater.inflate(R.layout.fragment_help, container, false);

        ButterKnife.bind(this, view);


        if (user == null) {
            noUser.setVisibility(View.VISIBLE);
            userExists.setVisibility(View.GONE);
            noTix.setVisibility(View.GONE);
        } else {
            noUser.setVisibility(View.GONE);
            userExists.setVisibility(View.VISIBLE);
            noTix.setVisibility(View.GONE);


            //RecyclerView
            ticketView.setHasFixedSize(true);

            mLayoutManager = new LinearLayoutManager(getActivity());
            ticketView.setLayoutManager(mLayoutManager);

//            final ArrayList<Ticket> ticketList = new ArrayList<Ticket>();
            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("HelpDeskTickets");
            query.findInBackground(new FindCallback<ParseObject>() {
                @Override
                public void done(List<ParseObject> objects, ParseException e) {
                    if (e == null) {
                        String user_id = ParseUser.getCurrentUser().getObjectId();
                        for (ParseObject object : objects) {
                            String obj_id = ((ParseObject) object.get("user")).getObjectId().toString();

                            // pick the status
                            String status = "None";
                            if (object.containsKey("status"))
                                status = object.get("status").toString();

                            if (user_id.equals(obj_id) && !status.equals("Deleted")) {
                                // pick the title
                                String title = "No Subject";
                                if (object.containsKey("subject"))
                                    title = object.get("subject").toString();

                                try {
                                    tickets.add(new Ticket(title,
                                            ((ParseObject) object.get("category")).fetchIfNeeded().get("category").toString(),
                                            object.get("description").toString(), status, object.getObjectId()));
                                } catch (ParseException e1) {
                                    e1.printStackTrace();
                                }
                            }

                        }
                    }

                    // setup the recyclerview with the member var tickets
                    setRecyclerViewSections();
                }
        });

    }

    return view;
}

    public void onEvent(Ticket ticket) {
        // add ticket and update recyclerview
        tickets.add(0, ticket);
        setRecyclerViewSections();
    }

    @OnClick(R.id.fab)
    void showDialog() {

        // DialogFragment.show() will take care of adding the fragment
        // in a transaction.  We also want to remove any currently showing
        // dialog, so make our own transaction and take care of that here.

//        FragmentTransaction ft = getFragmentManager().beginTransaction();
//        Fragment prev = getFragmentManager().findFragmentByTag("dialog");
//        if (prev != null) {
//            ft.remove(prev);
//        }
//        ft.addToBackStack(null);
//
//        // Create and show the dialog.
//        DialogFragment newFragment = new CreateTicketDialogFragment();
//        newFragment.show(ft, "dialog");

        MainActivity activity = ((MainActivity) getActivity());
        CreateTicketDialogFragment fragment = new CreateTicketDialogFragment();
        activity.switchContent(R.id.container, fragment);
    }

    @OnClick(R.id.login)
    void onLogin() {
        MainActivity activity = ((MainActivity) getActivity());
        ProfileFragment fragment = new ProfileFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ProfileFragment.I_EXTRA_FROM, ProfileFragment.I_EXTRA_FROM);
        fragment.setArguments(bundle);
        activity.switchContent(R.id.container, fragment);
    }

    /**
     * Setup the recyclerview with the proper sections
     */
    private void setRecyclerViewSections(){
        if (tickets.isEmpty()){
            noTix.setVisibility(View.VISIBLE);
            return;
        }
        
        noTix.setVisibility(View.GONE);
        // sort tix first on expired or not, then by created date
        Collections.sort(tickets, new Comparator<Ticket>() {
            @Override
            public int compare(Ticket lhs, Ticket rhs) {
                if (lhs.getStatus().equals("Expired") && !rhs.getStatus().equals("Expired"))
                    return 1;
//                            else if (!lhs.getStatus().equals("Expired") && rhs.getStatus().equals("Expired"))
                else
                    return -1;
                // TODO: 1/27/16 Sort by time if neither
            }
        });

        mAdapter = new TicketAdapter(tickets);

        // add first section for either expired or current
        ArrayList<SimpleSectionedRecyclerViewAdapter.Section> sections = new ArrayList<>();
        sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0, tickets.get(0).getStatus().equals("Expired") ?  "Expired Tickets" : "Current Tickets"));<commit_msg>fix a bull shit crash<commit_after>package com.example.spartahack.spartahack2016.Fragment;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.spartahack.spartahack2016.Activity.MainActivity;
import com.example.spartahack.spartahack2016.Adapters.SimpleSectionedRecyclerViewAdapter;
import com.example.spartahack.spartahack2016.Adapters.TicketAdapter;
import com.example.spartahack.spartahack2016.Model.Ticket;
import com.example.spartahack.spartahack2016.R;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HelpFragment extends BaseFragment {

    @Bind(R.id.recycler) RecyclerView ticketView;
    @Bind(R.id.no_user) LinearLayout noUser;
    @Bind(R.id.user) RelativeLayout userExists;
    @Bind(R.id.no_tix) TextView noTix;

    private TicketAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    private final String I_EXTRA_FROM = "from help";

    ParseUser user;
    List<ParseObject> categoryList;
    private ArrayList<Ticket> tickets;

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

        registerEventBus = true;

        tickets = new ArrayList<>();

        user = ParseUser.getCurrentUser();

        view = inflater.inflate(R.layout.fragment_help, container, false);

        ButterKnife.bind(this, view);


        if (user == null) {
            noUser.setVisibility(View.VISIBLE);
            userExists.setVisibility(View.GONE);
            noTix.setVisibility(View.GONE);
        } else {
            noUser.setVisibility(View.GONE);
            userExists.setVisibility(View.VISIBLE);
            noTix.setVisibility(View.GONE);


            //RecyclerView
            ticketView.setHasFixedSize(true);

            mLayoutManager = new LinearLayoutManager(getActivity());
            ticketView.setLayoutManager(mLayoutManager);

//            final ArrayList<Ticket> ticketList = new ArrayList<Ticket>();
            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("HelpDeskTickets");
            query.findInBackground(new FindCallback<ParseObject>() {
                @Override
                public void done(List<ParseObject> objects, ParseException e) {
                    if (e == null) {
                        String user_id = ParseUser.getCurrentUser().getObjectId();
                        for (ParseObject object : objects) {
                            String obj_id = ((ParseObject) object.get("user")).getObjectId().toString();

                            // pick the status
                            String status = "None";
                            if (object.containsKey("status"))
                                status = object.get("status").toString();

                            if (user_id.equals(obj_id) && !status.equals("Deleted")) {
                                // pick the title
                                String title = "No Subject";
                                if (object.containsKey("subject"))
                                    title = object.get("subject").toString();

                                try {
                                    tickets.add(new Ticket(title,
                                            ((ParseObject) object.get("category")).fetchIfNeeded().get("category").toString(),
                                            object.get("description").toString(), status, object.getObjectId()));
                                } catch (ParseException e1) {
                                    e1.printStackTrace();
                                }
                            }

                        }
                    }

                    // setup the recyclerview with the member var tickets
                    setRecyclerViewSections();
                }
        });

    }

    return view;
}

    public void onEvent(Ticket ticket) {
        // add ticket and update recyclerview
        tickets.add(0, ticket);
        setRecyclerViewSections();
    }

    @OnClick(R.id.fab)
    void showDialog() {

        // DialogFragment.show() will take care of adding the fragment
        // in a transaction.  We also want to remove any currently showing
        // dialog, so make our own transaction and take care of that here.

//        FragmentTransaction ft = getFragmentManager().beginTransaction();
//        Fragment prev = getFragmentManager().findFragmentByTag("dialog");
//        if (prev != null) {
//            ft.remove(prev);
//        }
//        ft.addToBackStack(null);
//
//        // Create and show the dialog.
//        DialogFragment newFragment = new CreateTicketDialogFragment();
//        newFragment.show(ft, "dialog");

        MainActivity activity = ((MainActivity) getActivity());
        CreateTicketDialogFragment fragment = new CreateTicketDialogFragment();
        activity.switchContent(R.id.container, fragment);
    }

    @OnClick(R.id.login)
    void onLogin() {
        MainActivity activity = ((MainActivity) getActivity());
        ProfileFragment fragment = new ProfileFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ProfileFragment.I_EXTRA_FROM, ProfileFragment.I_EXTRA_FROM);
        fragment.setArguments(bundle);
        activity.switchContent(R.id.container, fragment);
    }

    /**
     * Setup the recyclerview with the proper sections
     */
    private void setRecyclerViewSections(){
        if (tickets.isEmpty()){
            noTix.setVisibility(View.VISIBLE);
            return;
        }
        
        if (noTix!=null) noTix.setVisibility(View.GONE);
        // sort tix first on expired or not, then by created date
        Collections.sort(tickets, new Comparator<Ticket>() {
            @Override
            public int compare(Ticket lhs, Ticket rhs) {
                if (lhs.getStatus().equals("Expired") && !rhs.getStatus().equals("Expired"))
                    return 1;
//                            else if (!lhs.getStatus().equals("Expired") && rhs.getStatus().equals("Expired"))
                else
                    return -1;
                // TODO: 1/27/16 Sort by time if neither
            }
        });

        mAdapter = new TicketAdapter(tickets);

        // add first section for either expired or current
        ArrayList<SimpleSectionedRecyclerViewAdapter.Section> sections = new ArrayList<>();
        sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0, tickets.get(0).getStatus().equals("Expired") ?  "Expired Tickets" : "Current Tickets"));
{'fraction_non_alphanumeric': 0.10151398869741157, 'fraction_numerical': 0.0053024488941603295, 'mean_word_length': 2.0938916468810707, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 1, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2657290', 'n_tokens_mistral': 3807, 'n_tokens_neox': 3596, 'n_words': 779}