Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9c483781 authored by Robyn Coultas's avatar Robyn Coultas
Browse files

Do not invalidate listview just to update one time

Suspected mLapsAdapter.notifyDataSetChanged() was a key contributor to bug
7289174, but that proved not the case. This change does eliminate unneeded
view repaints in the listview.
Included some clean-ups.

Change-Id: I76e4698e17e3b8c6b1f988f09f96c7f7ec783d53
parent ce9b1067
Loading
Loading
Loading
Loading
+48 −26
Original line number Diff line number Diff line
@@ -72,6 +72,18 @@ public class StopwatchFragment extends DeskClockFragment
        }
        public long mLapTime;
        public long mTotalTime;

        public void updateView(Context context) {
            View lapInfo = mLapsList.findViewWithTag(this);
            if (lapInfo != null) {
                TextView lapTime = (TextView) lapInfo.findViewById(R.id.lap_time);
                TextView totalTime = (TextView) lapInfo.findViewById(R.id.lap_total);
                lapTime.setText(
                        Stopwatches.getTimeText(context, mLapTime, Stopwatches.NO_LAP_NUMBER));
                totalTime.setText(
                        Stopwatches.getTimeText(context, mTotalTime, Stopwatches.NO_LAP_NUMBER));
            }
        }
    }

    // Adapter for the ListView that shows the lap times.
@@ -112,22 +124,22 @@ public class StopwatchFragment extends DeskClockFragment
            if (mLaps.size() == 0 || position >= mLaps.size()) {
                return null;
            }
            Lap lap = getItem(position);
            View lapInfo;
            if (convertView != null) {
                lapInfo = convertView;
            } else {
                lapInfo = mInflater.inflate(R.layout.lap_view, parent, false);
                lapInfo.setBackgroundColor(mBackgroundColor);
            }
            lapInfo.setTag(lap);
            TextView count = (TextView)lapInfo.findViewById(R.id.lap_number);
            TextView lapTime = (TextView)lapInfo.findViewById(R.id.lap_time);
            TextView toalTime = (TextView)lapInfo.findViewById(R.id.lap_total);
            lapTime.setText(Stopwatches.formatTimeText(mLaps.get(position).mLapTime,
                    mFormats[mLapIndex]));
            toalTime.setText(Stopwatches.formatTimeText(mLaps.get(position).mTotalTime,
                    mFormats[mTotalIndex]));
            TextView totalTime = (TextView)lapInfo.findViewById(R.id.lap_total);
            lapTime.setText(Stopwatches.formatTimeText(lap.mLapTime, mFormats[mLapIndex]));
            totalTime.setText(Stopwatches.formatTimeText(lap.mTotalTime, mFormats[mTotalIndex]));
            count.setText(String.format(mLapFormat, mLaps.size() - position).toUpperCase());

            lapInfo.setBackgroundColor(mBackgroundColor);
            return lapInfo;
        }

@@ -137,7 +149,7 @@ public class StopwatchFragment extends DeskClockFragment
        }

        @Override
        public Object getItem(int position) {
        public Lap getItem(int position) {
            if (mLaps.size() == 0 || position >= mLaps.size()) {
                return null;
            }
@@ -153,6 +165,15 @@ public class StopwatchFragment extends DeskClockFragment
            mLapIndex = mTotalIndex = 0;
        }

        /**
         * A lap is printed into two columns: the total time and the lap time. To make this print
         * as pretty as possible, multiple formats were created which minimize the width of the
         * print. As the total or lap time exceed the limit of that format, this code updates
         * the format used for the total and/or lap times.
         *
         * @param lap to measure
         * @return true if this lap exceeded either threshold and a format was updated.
         */
        public boolean updateTimeFormats(Lap lap) {
            boolean formatChanged = false;
            while (mLapIndex + 1 < mThresholds.length && lap.mLapTime >= mThresholds[mLapIndex]) {
@@ -214,12 +235,6 @@ public class StopwatchFragment extends DeskClockFragment
        }
    }

    // Keys for data stored in the activity's bundle
    private static final String START_TIME_KEY = "start_time";
    private static final String ACCUM_TIME_KEY = "accum_time";
    private static final String STATE_KEY = "state";
    private static final String LAPS_KEY = "laps";

    LapsListAdapter mLapsAdapter;

    public StopwatchFragment() {
@@ -619,7 +634,7 @@ public class StopwatchFragment extends DeskClockFragment

    /***
     *
     * @param time - in hundredths of a second
     * @param time - in hundredth of a second
     */
    private void addLapTime(long time) {
        int size = mLapsAdapter.getCount();
@@ -632,13 +647,12 @@ public class StopwatchFragment extends DeskClockFragment
            mTime.setIntervalTime(curTime);
            mLapsAdapter.updateTimeFormats(firstLap);
        } else {
            long lapTime = curTime - ((Lap) mLapsAdapter.getItem(1)).mTotalTime;
            ((Lap)mLapsAdapter.getItem(0)).mLapTime = lapTime;
            ((Lap)mLapsAdapter.getItem(0)).mTotalTime = curTime;
            long lapTime = curTime - mLapsAdapter.getItem(1).mTotalTime;
            mLapsAdapter.getItem(0).mLapTime = lapTime;
            mLapsAdapter.getItem(0).mTotalTime = curTime;
            mLapsAdapter.addLap(new Lap(0, 0));
            mTime.setMarkerTime(lapTime);
            mLapsAdapter.updateLapFormat();
        //    mTime.setIntervalTime(lapTime * 10);
        }
        mLapsAdapter.notifyDataSetChanged();
        // Start lap animation starting from the second lap
@@ -649,11 +663,19 @@ public class StopwatchFragment extends DeskClockFragment
    }

    private void updateCurrentLap(long totalTime) {
        // There are either 0, 2 or more Laps in the list See {@link #addLapTime}
        if (mLapsAdapter.getCount() > 0) {
            Lap curLap = (Lap)mLapsAdapter.getItem(0);
            curLap.mLapTime = totalTime - ((Lap)mLapsAdapter.getItem(1)).mTotalTime;
            Lap curLap = mLapsAdapter.getItem(0);
            curLap.mLapTime = totalTime - mLapsAdapter.getItem(1).mTotalTime;
            curLap.mTotalTime = totalTime;
            // If this lap has caused a change in the format for total and/or lap time, all of
            // the rows need a fresh print. The simplest way to refresh all of the rows is
            // calling notifyDataSetChanged.
            if (mLapsAdapter.updateTimeFormats(curLap)) {
                mLapsAdapter.notifyDataSetChanged();
            } else {
                curLap.updateView(getActivity().getApplicationContext());
            }
        }
    }