Refactor Battery Chart View State Controll

When users click the battery chart, the orignal behavior is that the view changes the state by itself.
This cl refactors the bahavior to that the view callbacks to the controller, and
the controller changes the view's state.
In this way, the controller is the only source of truth of the state.
This meets the controller-view model.

Test: manual
Bug: 239491373, 236101166
Change-Id: I754ded2dba20319f1571374dfdbef27f2420ed78
This commit is contained in:
Zaiyue Xue
2022-07-19 10:40:45 +08:00
committed by YK Hung
parent e18e92699e
commit d4f9588a3d
3 changed files with 42 additions and 45 deletions

View File

@@ -347,6 +347,7 @@ public class BatteryChartPreferenceControllerV2 extends AbstractPreferenceContro
trapezoidIndex, mBatteryIndexedMap.size(), isForce));
mTrapezoidIndex = trapezoidIndex;
mBatteryChartView.setSelectedIndex(mTrapezoidIndex);
mHandler.post(() -> {
final long start = System.currentTimeMillis();
removeAndCacheAllPrefs();

View File

@@ -110,9 +110,11 @@ public class BatteryChartViewV2 extends AppCompatImageView implements View.OnCli
@VisibleForTesting
Paint mTrapezoidCurvePaint = null;
private TrapezoidSlot[] mTrapezoidSlots;
@VisibleForTesting
TrapezoidSlot[] mTrapezoidSlots;
// Records the location to calculate selected index.
private float mTouchUpEventX = Float.MIN_VALUE;
@VisibleForTesting
float mTouchUpEventX = Float.MIN_VALUE;
private BatteryChartViewV2.OnSelectListener mOnSelectListener;
public BatteryChartViewV2(Context context) {
@@ -161,10 +163,6 @@ public class BatteryChartViewV2 extends AppCompatImageView implements View.OnCli
if (mSelectedIndex != index) {
mSelectedIndex = index;
invalidate();
// Callbacks to the listener if we have.
if (mOnSelectListener != null) {
mOnSelectListener.onSelect(mSelectedIndex);
}
}
}
@@ -301,11 +299,9 @@ public class BatteryChartViewV2 extends AppCompatImageView implements View.OnCli
|| !isValidToDraw(trapezoidIndex)) {
return;
}
// Selects all if users click the same trapezoid item two times.
if (trapezoidIndex == mSelectedIndex) {
setSelectedIndex(SELECTED_INDEX_ALL);
} else {
setSelectedIndex(trapezoidIndex);
if (mOnSelectListener != null) {
mOnSelectListener.onSelect(
trapezoidIndex == mSelectedIndex ? SELECTED_INDEX_ALL : trapezoidIndex);
}
view.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK);
}
@@ -614,7 +610,8 @@ public class BatteryChartViewV2 extends AppCompatImageView implements View.OnCli
}
// A container class for each trapezoid left and right location.
private static final class TrapezoidSlot {
@VisibleForTesting
static final class TrapezoidSlot {
public float mLeft;
public float mRight;