MOBILE-3833 core: Fix swipe race conditions
parent
7c33680159
commit
008453d5ab
|
@ -24,29 +24,8 @@ export class AddonModForumDiscussionsSwipeManager
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
async navigateToNextItem(): Promise<void> {
|
protected skipItemInSwipe(item: AddonModForumDiscussionItem): boolean {
|
||||||
let delta = -1;
|
return this.getSource().isNewDiscussionForm(item);
|
||||||
const item = await this.getItemBy(-1);
|
|
||||||
|
|
||||||
if (item && this.getSource().isNewDiscussionForm(item)) {
|
|
||||||
delta--;
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.navigateToItemBy(delta, 'back');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritdoc
|
|
||||||
*/
|
|
||||||
async navigateToPreviousItem(): Promise<void> {
|
|
||||||
let delta = 1;
|
|
||||||
const item = await this.getItemBy(1);
|
|
||||||
|
|
||||||
if (item && this.getSource().isNewDiscussionForm(item)) {
|
|
||||||
delta++;
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.navigateToItemBy(delta, 'forward');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,29 +24,8 @@ export abstract class AddonModGlossaryEntriesSwipeManager
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
async navigateToNextItem(): Promise<void> {
|
protected skipItemInSwipe(item: AddonModGlossaryEntryItem): boolean {
|
||||||
let delta = -1;
|
return this.getSource().isNewEntryForm(item);
|
||||||
const item = await this.getItemBy(-1);
|
|
||||||
|
|
||||||
if (item && this.getSource().isNewEntryForm(item)) {
|
|
||||||
delta--;
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.navigateToItemBy(delta, 'back');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritdoc
|
|
||||||
*/
|
|
||||||
async navigateToPreviousItem(): Promise<void> {
|
|
||||||
let delta = 1;
|
|
||||||
const item = await this.getItemBy(1);
|
|
||||||
|
|
||||||
if (item && this.getSource().isNewEntryForm(item)) {
|
|
||||||
delta++;
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.navigateToItemBy(delta, 'forward');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,7 +82,13 @@ export class CoreSwipeNavigationItemsManager<
|
||||||
* @param animationDirection Animation direction.
|
* @param animationDirection Animation direction.
|
||||||
*/
|
*/
|
||||||
protected async navigateToItemBy(delta: number, animationDirection: 'forward' | 'back'): Promise<void> {
|
protected async navigateToItemBy(delta: number, animationDirection: 'forward' | 'back'): Promise<void> {
|
||||||
const item = await this.getItemBy(delta);
|
let item: Item | null;
|
||||||
|
|
||||||
|
do {
|
||||||
|
item = await this.getItemBy(delta);
|
||||||
|
|
||||||
|
delta += delta > 0 ? 1 : -1;
|
||||||
|
} while (item && this.skipItemInSwipe(item));
|
||||||
|
|
||||||
if (!item) {
|
if (!item) {
|
||||||
return;
|
return;
|
||||||
|
@ -100,14 +106,15 @@ export class CoreSwipeNavigationItemsManager<
|
||||||
const items = this.getSource().getItems();
|
const items = this.getSource().getItems();
|
||||||
|
|
||||||
// Get selected item.
|
// Get selected item.
|
||||||
const index = (this.selectedItem && items?.indexOf(this.selectedItem)) ?? -1;
|
const selectedIndex = (this.selectedItem && items?.indexOf(this.selectedItem)) ?? -1;
|
||||||
|
const nextIndex = selectedIndex + delta;
|
||||||
|
|
||||||
if (index === -1) {
|
if (selectedIndex === -1 || nextIndex < 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get item by delta.
|
// Get item by delta.
|
||||||
const item = items?.[index + delta] ?? null;
|
const item = items?.[nextIndex] ?? null;
|
||||||
|
|
||||||
if (!item && !this.getSource().isCompleted()) {
|
if (!item && !this.getSource().isCompleted()) {
|
||||||
await this.getSource().load();
|
await this.getSource().load();
|
||||||
|
@ -118,4 +125,15 @@ export class CoreSwipeNavigationItemsManager<
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if an item should be skipped during swipe navigation.
|
||||||
|
*
|
||||||
|
* @param item Item.
|
||||||
|
* @returns Whether to skip this item during swipe navigation.
|
||||||
|
*/
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
protected skipItemInSwipe(item: Item): boolean {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue