private void updateSplits(long cumulativeTime, int cummulativeDistance, boolean reset) {
    MyLog.i("RaceTimerFragment", "updateSplits for Athlete " + mAthleteNumber);
    List<Split> splits = new ArrayList<>();

    if (reset) {
      tvAthletePace.setText(getActivity().getString(R.string.header_pace));
    } else {
      // gets the splits associated with the race Uuid
      splits = Split.getAllSplits(mRaceUuid, false);

      String paceAndDistance =
          CommonMethods.getAveragePaceString(
              cumulativeTime, cummulativeDistance, mPaceDistance, mPoolLengthUnits);

      if (mIsClockRunning) {
        paceAndDistance =
            paceAndDistance
                + CommonMethods.getEstimatedTotalDistance(
                    cumulativeTime, cummulativeDistance, mEventDurationMs, mPoolLengthUnits);
      }

      tvAthletePace.setText(paceAndDistance);
    }

    mSplitArrayAdapter.setData(splits);
    mSplitArrayAdapter.notifyDataSetChanged();
  }
 private void resetLapCounter() {
   MyLog.i("RaceTimerFragment", "resetLapCounter");
   enableAthlete();
   showStartButton();
   Split.deleteSplitsNotRaceComplete(mSelectedSwimEvent);
   resetSplits();
   tvAthletePace.setText(getActivity().getString(R.string.header_pace));
   mIsClockRunning = false;
 }
  private void createSplit(
      Athlete athlete,
      SwimEvent swimEvent,
      int lapNumber,
      int cummulativeDistance,
      long splitTime,
      long cumulativeTime) {
    try {
      Split newSplit = new Split();
      newSplit.newInstance(
          mRaceUuid, athlete, swimEvent, lapNumber, cummulativeDistance, splitTime, cumulativeTime);
      newSplit.pin();
      updateSplits(cumulativeTime, cummulativeDistance, false);
      mPreviousCummulativeTime = cumulativeTime;

    } catch (ParseException e) {
      MyLog.e(
          "RaceTimerFragment",
          "createSplit: ParseException: for Athlete " + mAthleteNumber + ": " + e.getMessage());
    }
  }
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (savedInstanceState != null) {
      MyLog.i("RaceTimerFragment", "onActivityCreated: savedInstanceState Not Null");
      if (savedInstanceState.containsKey(ARG_ATHLETE_NUMBER)) {
        mAthleteNumber = savedInstanceState.getInt(ARG_ATHLETE_NUMBER);
      }
      if (savedInstanceState.containsKey(ARG_RACE_UUID)) {
        mRaceUuid = savedInstanceState.getString(ARG_RACE_UUID);
      }
    } else {
      switch (mAthleteNumber) {
        case 1:
          mRaceUuid = MySettings.getAthlete1RaceUuid();
          break;
        case 2:
          mRaceUuid = MySettings.getAthlete2RaceUuid();
          break;
      }
    }

    MyLog.i(
        "RaceTimerFragment",
        "onActivityCreated: Athlete" + mAthleteNumber + "RaceUuid: " + mRaceUuid);
    if (mAthleteNumber == 1) {
      mLapDistance = MySettings.getLapDistance();
      mPaceDistance = MySettings.getPaceDistance();
      mPoolLength = MySettings.getPoolLength();
      mPoolLengthUnits = MySettings.getPoolLengthUnits();
      mEventDurationMs = MySettings.getEventDurationMinutes() * 60000;
      mSplitButtonDisableDurationMs = MySettings.getSplitButtonDisabledSeconds() * 1000;
    }

    int spnAthletesStartingPosition = 0;
    switch (mAthleteNumber) {
      case 1:
        if (!MySettings.getSelectedAthlete1ID().equals(MySettings.NOT_AVAILABLE)) {
          spnAthletesStartingPosition =
              mAthleteSpinnerAdapter.getItemPosition(MySettings.getSelectedAthlete1ID());
        }
        break;

      case 2:
        if (!MySettings.getSelectedAthlete2ID().equals(MySettings.NOT_AVAILABLE)) {
          spnAthletesStartingPosition =
              mAthleteSpinnerAdapter.getItemPosition(MySettings.getSelectedAthlete2ID());
        }
        break;
    }
    mOkToResetLapCounter = false;
    spnAthletes.setSelection(spnAthletesStartingPosition);
    mIsClockRunning = false;

    String pace = getActivity().getString(R.string.header_pace);
    if (mSplitsOfCompletedRace.size() > 0) {
      Split lastSplit = mSplitsOfCompletedRace.get(0);
      long cummulativeDurationMs = lastSplit.getAccumulativeTimeMs();
      int cummulativeDistance = lastSplit.getDistance();
      pace =
          CommonMethods.getAveragePaceString(
              cummulativeDurationMs, cummulativeDistance, mPaceDistance, mPoolLengthUnits);
    }
    tvAthletePace.setText(pace);
  }
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    MyLog.i("RaceTimerFragment", "onCreateView: Athlete" + mAthleteNumber);
    View view = null;

    if (savedInstanceState != null) {
      if (savedInstanceState.containsKey(ARG_ATHLETE_NUMBER)) {
        mAthleteNumber = savedInstanceState.getInt(ARG_ATHLETE_NUMBER);
      }
      if (savedInstanceState.containsKey(ARG_RACE_UUID)) {
        mRaceUuid = savedInstanceState.getString(ARG_RACE_UUID);
      }
    }

    switch (mAthleteNumber) {
      case 1:
        view = inflater.inflate(R.layout.frag_race_timer_athlete1, container, false);
        break;

      case 2:
        view = inflater.inflate(R.layout.frag_race_timer_athlete2, container, false);
        break;
    }

    if (view != null) {
      llAthlete = (LinearLayout) view.findViewById(R.id.llAthletes);
      spnAthletes = (Spinner) view.findViewById(R.id.spnAthletes);
      mAthleteSpinnerAdapter =
          new AthleteSpinnerAdapter(getActivity(), new ArrayList<Athlete>(), mAthleteNumber);
      spnAthletes.setAdapter(mAthleteSpinnerAdapter);
      updateAthletesSpinner();
      spnAthletes.setOnItemSelectedListener(
          new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
              mSelectedAthlete = mAthleteSpinnerAdapter.getItem(position);
              switch (mAthleteNumber) {
                case 1:
                  MySettings.setSelectedAthlete1ID(mSelectedAthlete.getAthleteID());
                  break;
                case 2:
                  MySettings.setSelectedAthlete2ID(mSelectedAthlete.getAthleteID());
                  break;
              }
              EventBus.getDefault().post(new MyEvents.updateAthletesSpinner(mAthleteNumber));
              if (mOkToResetLapCounter) {
                resetLapCounter();
              } else {
                mOkToResetLapCounter = true;
              }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
          });

      Button btnEditAthlete = (Button) view.findViewById(R.id.btnEditAthlete);

      tvAthletePace = (TextView) view.findViewById(R.id.tvAthletePace);
      ListView lvAthleteRaceSplits = (ListView) view.findViewById(R.id.lvAthleteRaceSplits);

      mSplitsOfCompletedRace = Split.getAllSplits(mRaceUuid, false);
      mSplitArrayAdapter =
          new SplitArrayAdapter(getActivity(), mSplitsOfCompletedRace, mAthleteNumber);
      lvAthleteRaceSplits.setAdapter(mSplitArrayAdapter);

      btnStart = (Button) view.findViewById(R.id.btnStart);
      btnSplit = (Button) view.findViewById(R.id.btnSplit);
      btnEmail = (Button) view.findViewById(R.id.btnEmail);

      btnEditAthlete.setOnClickListener(this);
      btnStart.setOnClickListener(this);
      btnSplit.setOnClickListener(this);
      btnEmail.setOnClickListener(this);
    }
    return view;
  }
 private void markSplitsRaceComplete() {
   List<Split> splits = Split.getAllSplits(mRaceUuid, false);
   for (Split split : splits) {
     split.setIsRaceComplete(true);
   }
 }