Skip to content

Commit

Permalink
feat: favorited options now take precedence on auto-select
Browse files Browse the repository at this point in the history
  • Loading branch information
Wassup789 committed Apr 23, 2024
1 parent 2fd36ce commit b1cc28d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/components/ExerciseSelectorPopup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
};

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion src/models/ExerciseOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default class ExerciseOption {
readonly text: string;
readonly textCleaned: string;

favorited: boolean;

elem!: ExerciseSelectorOption;
readonly optionElem: HTMLOptionElement;

Expand Down Expand Up @@ -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<MuscleGroupFilter>, bodyweightFilter: boolean | null, hasFavoritesFilter: boolean) {
Expand All @@ -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);
}

Expand Down

0 comments on commit b1cc28d

Please sign in to comment.