Skip to content

Commit 28a1389

Browse files
committed
fix: slow scroll & stop chart pagination on max scroll
1 parent 075a104 commit 28a1389

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

src/store/ChartAdapterStore.ts

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ export default class ChartAdapterStore {
5858

5959
isOverFlutterCharts = false;
6060
enableVerticalScrollTimer?: ReturnType<typeof setTimeout>;
61-
scrollChartParentOnTouchTimer?: ReturnType<typeof setTimeout>;
61+
clearTouchDeltasTimer?: ReturnType<typeof setTimeout>;
6262

6363
constructor(mainStore: MainStore) {
6464
makeObservable(this, {
65+
clearTouchDeltasTimer: observable,
6566
onMount: action.bound,
6667
onTickHistory: action.bound,
6768
onTouch: action.bound,
@@ -75,7 +76,6 @@ export default class ChartAdapterStore {
7576
enableVerticalScrollTimer: observable,
7677
scale: action.bound,
7778
scrollableChartParent: computed,
78-
scrollChartParentOnTouchTimer: observable,
7979
toggleDataFitMode: action.bound,
8080
touchValues: observable,
8181
onCrosshairMove: action.bound,
@@ -212,9 +212,9 @@ export default class ChartAdapterStore {
212212
}
213213

214214
window.flutterChartElement?.addEventListener('wheel', this.onWheel, { capture: true });
215-
window.addEventListener('touchstart', this.onTouch, { capture: true });
216-
window.addEventListener('touchmove', this.onTouch, { capture: true });
217-
window.addEventListener('touchend', this.onTouch, { capture: true });
215+
window.flutterChartElement?.addEventListener('touchstart', this.onTouch, { capture: true });
216+
window.flutterChartElement?.addEventListener('touchmove', this.onTouch, { capture: true });
217+
window.flutterChartElement?.addEventListener('touchend', this.onTouch, { capture: true });
218218
window.flutterChartElement?.addEventListener('dblclick', this.onDoubleClick, { capture: true });
219219
window.addEventListener('mousemove', this.onMouseMove, { capture: true });
220220
}
@@ -223,13 +223,13 @@ export default class ChartAdapterStore {
223223
window._flutter.initState.isMounted = false;
224224

225225
window.flutterChartElement?.removeEventListener('wheel', this.onWheel, { capture: true });
226-
window.removeEventListener('touchstart', this.onTouch, { capture: true });
227-
window.removeEventListener('touchmove', this.onTouch, { capture: true });
228-
window.removeEventListener('touchend', this.onTouch, { capture: true });
226+
window.flutterChartElement?.removeEventListener('touchstart', this.onTouch, { capture: true });
227+
window.flutterChartElement?.removeEventListener('touchmove', this.onTouch, { capture: true });
228+
window.flutterChartElement?.removeEventListener('touchend', this.onTouch, { capture: true });
229229
window.flutterChartElement?.removeEventListener('dblclick', this.onDoubleClick, { capture: true });
230230
window.removeEventListener('mousemove', this.onMouseMove, { capture: true });
231231
clearTimeout(this.enableVerticalScrollTimer);
232-
clearTimeout(this.scrollChartParentOnTouchTimer);
232+
clearTimeout(this.clearTouchDeltasTimer);
233233
}
234234

235235
onChartLoad() {
@@ -277,8 +277,6 @@ export default class ChartAdapterStore {
277277
const isForcedScrollArea = x < nonScrollableAreaWidth;
278278

279279
if (this.touchValues.x && this.touchValues.y) {
280-
const shouldForceMaxScroll =
281-
Math.abs(Number(this.touchValues.deltaYTotal)) > 10 && e.type === 'touchend';
282280
const xDiff = this.touchValues.x - pageX;
283281
const yDiff = this.touchValues.y - pageY;
284282
const deltaXTotal = (this.touchValues.deltaXTotal ?? 0) + xDiff;
@@ -289,27 +287,35 @@ export default class ChartAdapterStore {
289287
this.touchValues = { ...this.touchValues, deltaXTotal, deltaYTotal };
290288

291289
if (isForcedScrollArea && isVerticalScroll) {
292-
e.stopImmediatePropagation();
290+
const shouldForceMaxScroll =
291+
Math.abs(Number(this.touchValues.deltaYTotal)) > 10 && e.type === 'touchend';
292+
const stopChartPagination = () => {
293+
// calling _scrollAnimationController.stop() in flutter-chart, value 1 doesn't affect the scale:
294+
this.flutterChart?.app.scale(1);
295+
};
293296
if (shouldForceMaxScroll) {
294-
clearTimeout(this.scrollChartParentOnTouchTimer);
297+
// handling max scroll on quick swipe
298+
stopChartPagination();
295299
this.scrollableChartParent?.scrollTo({
296300
top:
297301
Number(this.touchValues.deltaYTotal) < 0
298302
? 0
299303
: this.scrollableChartParent.scrollHeight,
300304
behavior: 'smooth',
301305
});
302-
this.scrollChartParentOnTouchTimer = undefined;
303-
this.touchValues = { ...this.touchValues, deltaYTotal: 0, deltaXTotal: 0 };
304-
} else if (!this.scrollChartParentOnTouchTimer) {
305-
this.scrollChartParentOnTouchTimer = setTimeout(() => {
306-
this.scrollableChartParent?.scrollBy({
307-
top: this.touchValues.deltaYTotal,
308-
behavior: 'smooth',
309-
});
310-
this.scrollChartParentOnTouchTimer = undefined;
311-
this.touchValues = { ...this.touchValues, deltaYTotal: 0, deltaXTotal: 0 };
312-
}, 150);
306+
} else if (e.type === 'touchmove') {
307+
// handling slow scroll
308+
stopChartPagination();
309+
this.scrollableChartParent?.scrollBy({
310+
top: yDiff,
311+
});
312+
if (!this.clearTouchDeltasTimer) {
313+
this.clearTouchDeltasTimer = setTimeout(() => {
314+
// clearing total deltas to avoid triggering max scroll after the slow scroll
315+
this.touchValues = { ...this.touchValues, deltaYTotal: 0, deltaXTotal: 0 };
316+
this.clearTouchDeltasTimer = undefined;
317+
}, 100);
318+
}
313319
}
314320
}
315321
}

0 commit comments

Comments
 (0)