From b1cc28d22366f128406e538f950301af301f05de Mon Sep 17 00:00:00 2001 From: Wassup789 Date: Tue, 23 Apr 2024 00:08:45 -0700 Subject: [PATCH] feat: favorited options now take precedence on auto-select --- src/components/ExerciseSelectorPopup.ts | 19 +++++++++++++++++-- src/models/ExerciseOption.ts | 5 ++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/components/ExerciseSelectorPopup.ts b/src/components/ExerciseSelectorPopup.ts index a95c891..f047567 100644 --- a/src/components/ExerciseSelectorPopup.ts +++ b/src/components/ExerciseSelectorPopup.ts @@ -12,6 +12,8 @@ import ExerciseSelector from "./ExerciseSelector"; import { ApplyMode } from "./ExerciseSelectorFilterApplies"; import { styleMap } from "lit/directives/style-map.js"; +type BestOptionDetails = { option: ExerciseOption | null; startsWith: boolean; rank: number }; + @customElement(ExerciseSelectorPopup.NAME) export default class ExerciseSelectorPopup extends LitElement { static readonly NAME = "exercise-selector-popup"; @@ -261,6 +263,8 @@ export default class ExerciseSelectorPopup extends LitElement { this.onUserSelectOption(event.detail.option, event.detail.metaKey ? "*" : undefined); }; private onOptionFavoriteUpdate = (event: ExerciseSelectorOptionFavoriteUpdateEvent) => { + event.detail.option.favorited = event.detail.isFavorited; + this.updateFilterVisibilityForOption(event.detail.option); }; @@ -364,7 +368,8 @@ export default class ExerciseSelectorPopup extends LitElement { return; } - const bestOptionDetails: { option: ExerciseOption | null; startsWith: boolean; rank: number } = { option: null, startsWith: false, rank: 0 }; + const bestOptionDetails: BestOptionDetails = { option: null, startsWith: false, rank: 0 }, + bestFavoritedOptionDetails: BestOptionDetails = { option: null, startsWith: false, rank: 0 }; let firstVisibleOption: ExerciseOption | null = null; const searchWords = value.split(/\s+/); for (const group of groups) { @@ -394,6 +399,13 @@ export default class ExerciseSelectorPopup extends LitElement { bestOptionDetails.startsWith = fullStartsWith; bestOptionDetails.rank = relativeRank; } + + const hasBetterFavoriteRank = relativeRank > bestFavoritedOptionDetails.rank; + if (option.favorited && hasBetterFavoriteRank && optionStartsWith) { + bestFavoritedOptionDetails.option = option; + bestFavoritedOptionDetails.startsWith = fullStartsWith; + bestFavoritedOptionDetails.rank = relativeRank; + } } else { if (!matchesGroupName) { visible = false; @@ -421,8 +433,11 @@ export default class ExerciseSelectorPopup extends LitElement { (async () => { await this.requestUpdate(); - if (bestOptionDetails.option) { + // Select this best option unless a favorite option exists while the best option does not start with the query + if (bestOptionDetails.option && (!bestFavoritedOptionDetails.option || (bestOptionDetails.startsWith && !bestFavoritedOptionDetails.startsWith))) { this.setSelectedOption(bestOptionDetails.option); + } else if (bestFavoritedOptionDetails.option) { + this.setSelectedOption(bestFavoritedOptionDetails.option); } else if (firstVisibleOption) { this.setSelectedOption(firstVisibleOption); } else { diff --git a/src/models/ExerciseOption.ts b/src/models/ExerciseOption.ts index d010635..02de9ee 100644 --- a/src/models/ExerciseOption.ts +++ b/src/models/ExerciseOption.ts @@ -15,6 +15,8 @@ export default class ExerciseOption { readonly text: string; readonly textCleaned: string; + favorited: boolean; + elem!: ExerciseSelectorOption; readonly optionElem: HTMLOptionElement; @@ -54,6 +56,7 @@ export default class ExerciseOption { this.textCleaned = SearchHelper.clean(this.text); this.optionElem = optionElem; this.suggested = optionElem.parentElement!.matches("[label='Suggested']"); + this.favorited = FavoritesService.INSTANCE.hasFavorite(this.categoryValue, this.value); } updateFilterVisibility(activeMuscleGroupFilters: ReadonlySet, bodyweightFilter: boolean | null, hasFavoritesFilter: boolean) { @@ -65,7 +68,7 @@ export default class ExerciseOption { return setFilterVisibility(true); } - if (hasFavoritesFilter && !FavoritesService.INSTANCE.hasFavorite(this.categoryValue, this.value)) { + if (hasFavoritesFilter && !this.favorited) { return setFilterVisibility(false); }