mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 02:34:21 +00:00
fix(native): first visits no longer wait behind the offline warm-up, day rail follows the swipe
The iOS app pulls every file of a build into WebKit's cache so it can start offline. It did so four at a time at normal priority, for all 1,900 files of every new build including all 27 translations, and a page opened meanwhile queued behind it: right after a server update, a first visit to Vacay took five to seven seconds. The warm-up now runs two at a time at low priority, takes only the user's language and English, and walks a build once. The day rail in the trip header lit the new day only after the pager's snap had landed and the plan had re-rendered, a second or two after the swipe. The pager now reports the day a swipe heads for as soon as its page is the nearer one, and the rail and its pill move with the finger; the plan still switches when the snap lands.
This commit is contained in:
@@ -82,6 +82,22 @@ describe('MDayPager', () => {
|
||||
expect(screen.getByRole('status')).toHaveTextContent('day 3 of 4');
|
||||
});
|
||||
|
||||
it('previews the day a swipe heads for while the strip still moves, and takes it back', () => {
|
||||
const onPreview = vi.fn();
|
||||
const { strip, onSelectDay } = setup(2, { onPreview });
|
||||
|
||||
place(strip, 3, true);
|
||||
fireEvent.scroll(strip);
|
||||
fireEvent.scroll(strip);
|
||||
expect(onPreview).toHaveBeenCalledTimes(1);
|
||||
expect(onPreview).toHaveBeenLastCalledWith(3);
|
||||
expect(onSelectDay).not.toHaveBeenCalled();
|
||||
|
||||
place(strip, 2, true);
|
||||
fireEvent.scroll(strip);
|
||||
expect(onPreview).toHaveBeenLastCalledWith(null);
|
||||
});
|
||||
|
||||
it('asks once, however many events report the same landing', () => {
|
||||
const { strip, onSelectDay } = setup(2);
|
||||
|
||||
|
||||
@@ -47,6 +47,12 @@ export interface MDayPagerProps {
|
||||
* (Vacay's month editor and year grid) rather than over the whole screen.
|
||||
*/
|
||||
inFlow?: boolean;
|
||||
/**
|
||||
* The day a swipe is heading for, as soon as its page is the nearer one, and
|
||||
* null when the swipe falls back: lets a day rail follow the finger before
|
||||
* the snap lands and onSelectDay fires.
|
||||
*/
|
||||
onPreview?: (dayId: number | null) => void;
|
||||
}
|
||||
|
||||
interface Page {
|
||||
@@ -84,6 +90,7 @@ export default function MDayPager({
|
||||
locked = false,
|
||||
describeDay,
|
||||
inFlow = false,
|
||||
onPreview,
|
||||
}: MDayPagerProps) {
|
||||
const stripRef = useRef<HTMLDivElement>(null);
|
||||
const [announcement, setAnnouncement] = useState('');
|
||||
@@ -91,8 +98,8 @@ export default function MDayPager({
|
||||
const windowKey = pages.map((p) => p.key).join(',');
|
||||
|
||||
// The handlers below are attached once, so they read the props through here.
|
||||
const latest = useRef({ days, onSelectDay, describeDay, selectedDayId });
|
||||
latest.current = { days, onSelectDay, describeDay, selectedDayId };
|
||||
const latest = useRef({ days, onSelectDay, describeDay, selectedDayId, onPreview });
|
||||
latest.current = { days, onSelectDay, describeDay, selectedDayId, onPreview };
|
||||
/** The day a landed swipe asked for, until the selection catches up. Stops a
|
||||
* late scroll event from selecting it twice. */
|
||||
const requested = useRef<number | null>(null);
|
||||
@@ -126,6 +133,31 @@ export default function MDayPager({
|
||||
if (!strip) return;
|
||||
let timer = 0;
|
||||
let touching = false;
|
||||
let previewed: number | null = null;
|
||||
|
||||
// The nearer page while the strip moves: a page past the middle is where the
|
||||
// swipe will land unless the finger turns back.
|
||||
const preview = () => {
|
||||
const notify = latest.current.onPreview;
|
||||
if (!notify) return;
|
||||
const left = strip.getBoundingClientRect().left;
|
||||
let nearest: HTMLElement | null = null;
|
||||
let distance = Infinity;
|
||||
for (const child of Array.from(strip.children) as HTMLElement[]) {
|
||||
const d = Math.abs(child.getBoundingClientRect().left - left);
|
||||
if (d < distance) {
|
||||
distance = d;
|
||||
nearest = child;
|
||||
}
|
||||
}
|
||||
if (!nearest) return;
|
||||
const raw = nearest.hasAttribute('data-page-active') ? '' : nearest.dataset.dayId;
|
||||
const dayId = raw ? Number(raw) : null;
|
||||
const next = dayId != null && Number.isFinite(dayId) ? dayId : null;
|
||||
if (next === previewed) return;
|
||||
previewed = next;
|
||||
notify(next);
|
||||
};
|
||||
|
||||
const settle = () => {
|
||||
if (touching) return;
|
||||
@@ -140,6 +172,7 @@ export default function MDayPager({
|
||||
const dayId = raw ? Number(raw) : NaN;
|
||||
if (!Number.isFinite(dayId) || requested.current === dayId) return;
|
||||
requested.current = dayId;
|
||||
previewed = null;
|
||||
const s = latest.current;
|
||||
tickHaptic();
|
||||
s.onSelectDay(dayId);
|
||||
@@ -162,14 +195,18 @@ export default function MDayPager({
|
||||
later();
|
||||
};
|
||||
|
||||
strip.addEventListener('scroll', later, { passive: true });
|
||||
const onScroll = () => {
|
||||
preview();
|
||||
later();
|
||||
};
|
||||
strip.addEventListener('scroll', onScroll, { passive: true });
|
||||
strip.addEventListener('scrollend', onScrollEnd);
|
||||
strip.addEventListener('touchstart', onTouchStart, { passive: true });
|
||||
strip.addEventListener('touchend', onTouchEnd, { passive: true });
|
||||
strip.addEventListener('touchcancel', onTouchEnd, { passive: true });
|
||||
return () => {
|
||||
window.clearTimeout(timer);
|
||||
strip.removeEventListener('scroll', later);
|
||||
strip.removeEventListener('scroll', onScroll);
|
||||
strip.removeEventListener('scrollend', onScrollEnd);
|
||||
strip.removeEventListener('touchstart', onTouchStart);
|
||||
strip.removeEventListener('touchend', onTouchEnd);
|
||||
|
||||
Reference in New Issue
Block a user