Android Open Source - RaceTimer In Race Fragment






From Project

Back to project page RaceTimer.

License

The source code is released under:

GNU General Public License

If you think the Android project RaceTimer 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

package com.liamfruzyna.android.racetimer.fragments;
//from w w w  . j a  va2  s. c om
import android.app.Fragment;
import android.app.FragmentManager;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ListView;
import android.widget.TextView;

import com.liamfruzyna.android.racetimer.AppData;
import com.liamfruzyna.android.racetimer.R;
import com.liamfruzyna.android.racetimer.objects.Event;
import com.liamfruzyna.android.racetimer.objects.Meet;
import com.liamfruzyna.android.racetimer.objects.Time;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by mail929 on 9/25/14.
 * A Fragment for timing races, includes race info and every runner with a checkpoint and finish button
 */
public class InRaceFragment extends Fragment
{

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

        //locally saves the current event
        final Event event = AppData.meets.get(getArguments().getInt("key")).events.get(getArguments().getInt("event"));

        //init view widgets
        final Chronometer chrono = (Chronometer) view.findViewById(R.id.time);
        ListView runnersL = (ListView) view.findViewById(R.id.runners);
        TextView team = (TextView) view.findViewById(R.id.teamShort);
        TextView meet = (TextView) view.findViewById(R.id.meetShort);
        TextView level = (TextView) view.findViewById(R.id.eventLevel);
        TextView distance = (TextView) view.findViewById(R.id.eventDistance);
        final Button start = (Button) view.findViewById(R.id.start);

        //sets the color of the start button to green
        start.getBackground().setColorFilter(getResources().getColor(R.color.go_green), PorterDuff.Mode.MULTIPLY);

        //sets textviews to their appropriate data
        for(int i = 0; i < AppData.teams.size(); i++)
        {
            for(int j = 0; j < AppData.teams.get(i).meets.size(); j++)
            {
                if(AppData.teams.get(i).meets.get(j) == getArguments().getInt("key"))
                {
                    team.setText(AppData.teams.get(i).shortName);
                }
            }
        }
        meet.setText(AppData.meets.get(getArguments().getInt("key")).shortName);
        level.setText(event.level);
        distance.setText(Double.toString(event.distance));

        //creates a list of all the runners names and adds the times
        final List<String> runners = new ArrayList<String>();
        for(int i = 0; i < event.runners.size(); i++)
        {
            runners.add(AppData.runners.get(event.runners.get(i)).name);
            AppData.runners.get(event.runners.get(i)).times.add(new Time(getArguments().getInt("key"), getArguments().getInt("event"), new ArrayList<String>()));
        }

        //listview of runners
        runnersL.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.runner_inrace, R.id.name, runners)
        {
            public View getView(final int position, View convertView, ViewGroup parent)
            {
                //basic setup crap, it checks if the view is null and updates it if it is because Android is to stupid to do so itself
                View view;
                if(convertView == null)
                {
                    LayoutInflater infl = (LayoutInflater) getActivity().getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
                    convertView = infl.inflate(R.layout.runner_inrace, parent, false);
                }
                view = super.getView(position, convertView, parent);

                //init view widgets and sets info
                TextView name = (TextView) view.findViewById(R.id.name);
                final Button checkpoint = (Button) view.findViewById(R.id.checkPoint);
                final Button finish = (Button) view.findViewById(R.id.finish);
                name.setText(runners.get(position));
                finish.getBackground().setColorFilter(getResources().getColor(R.color.stop_red), PorterDuff.Mode.MULTIPLY);

               //listens for checkpoint button to be pushed
                checkpoint.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View v)
                    {
                        //why the fuck isn't it saving the checkpoints here
                        //[fixed] -T-O-D-O- save the checkpoints because some retard removed the code
                        Time time = AppData.runners.get(event.runners.get(position)).times.get(AppData.runners.get(event.runners.get(position)).times.size()-1);
                        time.checkPoints.add(chrono.getText().toString());
                        System.out.println("Added: " + time.checkPoints.get(time.checkPoints.size()-1));

                        //checks if the runner has passed all the checkpoints, if he has it disables the button
                        if(event.checkPoints.size() == time.checkPoints.size())
                        {
                            checkpoint.setEnabled(false);
                            checkpoint.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
                        }
                    }
                });

                //listens for finish button to be pressed
                finish.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View v)
                    {
                        //saves the finishing time as a final checkpoint
                        AppData.runners.get(event.runners.get(position)).times.get(AppData.runners.get(event.runners.get(position)).times.size()-1).checkPoints.add(chrono.getText().toString());

                        //disables the buttons
                        checkpoint.setEnabled(false);
                        finish.setEnabled(false);
                        finish.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
                        checkpoint.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
                    }
                });
                return view;
            }
        });

        //listens for the start/finish button to be pressed
        //its conviently named start because who cares about finishing
        start.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                //if the start button doesn't say finish (I'm so good at code)
                if(start.getText().toString() != "Finish")
                {
                    //setup stopwatch and start it
                    chrono.setBase(SystemClock.elapsedRealtime());
                    chrono.start();

                    //use witchcraft to turn the start button into a finish button
                    start.setText("Finish");
                    start.getBackground().setColorFilter(getResources().getColor(R.color.stop_red), PorterDuff.Mode.MULTIPLY);
                }
                //if the "start" button says finish on it
                else
                {
                    //stop the chronometer
                    chrono.stop();

                    //save everything because who cares about saving the user time when you can save yourself time
                    AppData.save();

                    //go back to the meet page
                    Fragment tf = new MeetFragment();
                    Bundle args = new Bundle();
                    args.putInt("key", getArguments().getInt("key"));
                    tf.setArguments(args);
                    FragmentManager fragmentManager = getFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.container, tf).commit();
                }
            }
        });
        return view;
    }
}




Java Source Code List

com.liamfruzyna.android.racetimer.AppData.java
com.liamfruzyna.android.racetimer.ApplicationTest.java
com.liamfruzyna.android.racetimer.Fab.java
com.liamfruzyna.android.racetimer.RTActivity.java
com.liamfruzyna.android.racetimer.fragments.AddEventFragment.java
com.liamfruzyna.android.racetimer.fragments.AddMeetFragment.java
com.liamfruzyna.android.racetimer.fragments.AddTeamFragment.java
com.liamfruzyna.android.racetimer.fragments.EventFragment.java
com.liamfruzyna.android.racetimer.fragments.HomeFragment.java
com.liamfruzyna.android.racetimer.fragments.InRaceFragment.java
com.liamfruzyna.android.racetimer.fragments.MeetFragment.java
com.liamfruzyna.android.racetimer.fragments.RunnerFragment.java
com.liamfruzyna.android.racetimer.fragments.TeamFragment.java
com.liamfruzyna.android.racetimer.objects.Event.java
com.liamfruzyna.android.racetimer.objects.Meet.java
com.liamfruzyna.android.racetimer.objects.Runner.java
com.liamfruzyna.android.racetimer.objects.Team.java
com.liamfruzyna.android.racetimer.objects.Time.java