Fix wrong deck options being selected by default

We were sorting in getConfigList() without updating selectedIdx. For some
reason, this worked in the past, but something about #2084 stopped it
from working correctly. Resolved by sorting+updating selectedIdx after
adds/renames, instead of in getConfigList(). This required changes to
the unit tests, as the indexes are different now.

Fixes https://forums.ankiweb.net/t/anki-2-1-55-beta-3/24295/58
This commit is contained in:
Damien Elmes 2022-11-03 12:49:58 +10:00
parent e64784f093
commit 84935bee34
2 changed files with 22 additions and 10 deletions

View file

@ -109,13 +109,13 @@ test("deck list", () => {
expect(get(state.configList)).toStrictEqual([ expect(get(state.configList)).toStrictEqual([
{ {
current: true, current: true,
idx: 1, idx: 0,
name: "another one", name: "another one",
useCount: 1, useCount: 1,
}, },
{ {
current: false, current: false,
idx: 0, idx: 1,
name: "Default", name: "Default",
useCount: 1, useCount: 1,
}, },
@ -150,13 +150,13 @@ test("deck list", () => {
}, },
{ {
current: true, current: true,
idx: 2, idx: 1,
name: "hello", name: "hello",
useCount: 1, useCount: 1,
}, },
{ {
current: false, current: false,
idx: 1, idx: 2,
name: "zzz", name: "zzz",
useCount: 0, useCount: 0,
}, },
@ -174,13 +174,13 @@ test("deck list", () => {
}, },
{ {
current: false, current: false,
idx: 2, idx: 1,
name: "hello", name: "hello",
useCount: 0, useCount: 0,
}, },
{ {
current: false, current: false,
idx: 1, idx: 2,
name: "zzz", name: "zzz",
useCount: 0, useCount: 0,
}, },
@ -230,7 +230,7 @@ test("parent counts", () => {
expect(get(state.parentLimits)).toStrictEqual({ newCards: 10, reviews: 200 }); expect(get(state.parentLimits)).toStrictEqual({ newCards: 10, reviews: 200 });
// but adjusting the default config will, since the parent deck uses it // but adjusting the default config will, since the parent deck uses it
state.setCurrentIndex(0); state.setCurrentIndex(1);
state.currentConfig.update((c) => { state.currentConfig.update((c) => {
c.newPerDay = 123; c.newPerDay = 123;
return c; return c;
@ -269,7 +269,7 @@ test("saving", () => {
expect(out.removedConfigIds).toStrictEqual([]); expect(out.removedConfigIds).toStrictEqual([]);
// select the other non-default deck & remove // select the other non-default deck & remove
state.setCurrentIndex(1); state.setCurrentIndex(0);
state.removeCurrentConfig(); state.removeCurrentConfig();
// should be listed in removedConfigs, and modified should // should be listed in removedConfigs, and modified should
@ -287,7 +287,7 @@ test("aux data", () => {
}); });
// check default // check default
state.setCurrentIndex(0); state.setCurrentIndex(1);
expect(get(state.currentAuxData)).toStrictEqual({ expect(get(state.currentAuxData)).toStrictEqual({
new: { new: {
separate: true, separate: true,

View file

@ -65,6 +65,7 @@ export class DeckOptionsState {
0, 0,
this.configs.findIndex((c) => c.config.id === this.currentDeck.configId), this.configs.findIndex((c) => c.config.id === this.currentDeck.configId),
); );
this.sortConfigs();
this.v3Scheduler = data.v3Scheduler; this.v3Scheduler = data.v3Scheduler;
this.cardStateCustomizer = writable(data.cardStateCustomizer); this.cardStateCustomizer = writable(data.cardStateCustomizer);
this.deckLimits = writable(data.currentDeck?.limits ?? createLimits()); this.deckLimits = writable(data.currentDeck?.limits ?? createLimits());
@ -116,6 +117,7 @@ export class DeckOptionsState {
if (config.id) { if (config.id) {
this.modifiedConfigs.add(config.id); this.modifiedConfigs.add(config.id);
} }
this.sortConfigs();
this.updateConfigList(); this.updateConfigList();
} }
@ -140,6 +142,7 @@ export class DeckOptionsState {
const configWithCount = { config, useCount: 0 }; const configWithCount = { config, useCount: 0 };
this.configs.push(configWithCount); this.configs.push(configWithCount);
this.selectedIdx = this.configs.length - 1; this.selectedIdx = this.configs.length - 1;
this.sortConfigs();
this.updateCurrentConfig(); this.updateCurrentConfig();
this.updateConfigList(); this.updateConfigList();
} }
@ -254,6 +257,16 @@ export class DeckOptionsState {
return bytesToObject(conf.other); return bytesToObject(conf.other);
} }
private sortConfigs() {
const currentConfigName = this.configs[this.selectedIdx].config.name;
this.configs.sort((a, b) =>
localeCompare(a.config.name, b.config.name, { sensitivity: "base" }),
);
this.selectedIdx = this.configs.findIndex(
(c) => c.config.name == currentConfigName,
);
}
private getConfigList(): ConfigListEntry[] { private getConfigList(): ConfigListEntry[] {
const list: ConfigListEntry[] = this.configs.map((c, idx) => { const list: ConfigListEntry[] = this.configs.map((c, idx) => {
const useCount = c.useCount + (idx === this.selectedIdx ? 1 : 0); const useCount = c.useCount + (idx === this.selectedIdx ? 1 : 0);
@ -264,7 +277,6 @@ export class DeckOptionsState {
useCount, useCount,
}; };
}); });
list.sort((a, b) => localeCompare(a.name, b.name, { sensitivity: "base" }));
return list; return list;
} }