Skip to content

Commit daeefb9

Browse files
committed
Possible fix for the flat structured arrays
1 parent 7a0f8e0 commit daeefb9

File tree

2 files changed

+45
-66
lines changed

2 files changed

+45
-66
lines changed

src/api/APIModel.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export abstract class APIModel {
1010
plugin!: MediaDbPlugin;
1111

1212
/**
13-
* This function should query the api and return a list of matches. The matches should be caped at 20.
13+
* This function should query the api and return a list of matches. The matches should be capped at 20.
1414
*
1515
* @param title the title to query for
1616
*/
@@ -19,13 +19,8 @@ export abstract class APIModel {
1919
abstract getById(id: string): Promise<MediaTypeModel>;
2020

2121
hasType(type: MediaType): boolean {
22-
if (
23-
this.types.contains(type) &&
24-
(Boolean((this.plugin.settings.apiToggle as any)?.[this.apiName]?.[type]) === true || (this.plugin.settings.apiToggle as any)?.[this.apiName]?.[type] === undefined)
25-
) {
26-
return true;
27-
}
28-
return false;
22+
const disabledMediaTypes = this.plugin.settings[`${this.apiName}_disabledMediaTypes` as keyof typeof this.plugin.settings] as MediaType[];
23+
return this.types.includes(type) && !disabledMediaTypes.includes(type);
2924
}
3025

3126
hasTypeOverlap(types: MediaType[]): boolean {

src/settings/Settings.ts

Lines changed: 42 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,13 @@ export interface MediaDbPluginSettings {
2121
openNoteInNewTab: boolean;
2222
useDefaultFrontMatter: boolean;
2323
enableTemplaterIntegration: boolean;
24-
apiToggle: {
25-
OMDbAPI: {
26-
movie: boolean;
27-
series: boolean;
28-
game: boolean;
29-
};
30-
MALAPI: {
31-
movie: boolean;
32-
series: boolean;
33-
};
34-
MALAPIManga: {
35-
comicManga: boolean;
36-
};
37-
ComicVineAPI: {
38-
comicManga: boolean;
39-
};
40-
SteamAPI: {
41-
game: boolean;
42-
};
43-
MobyGamesAPI: {
44-
game: boolean;
45-
};
46-
GiantBombAPI: {
47-
game: boolean;
48-
};
49-
};
24+
OMDbAPI_disabledMediaTypes: string[];
25+
MALAPI_disabledMediaTypes: string[];
26+
MALAPIManga_disabledMediaTypes: string[];
27+
ComicVineAPI_disabledMediaTypes: string[];
28+
SteamAPI_disabledMediaTypes: string[];
29+
MobyGamesAPI_disabledMediaTypes: string[];
30+
GiantBombAPI_disabledMediaTypes: string[];
5031
movieTemplate: string;
5132
seriesTemplate: string;
5233
mangaTemplate: string;
@@ -97,32 +78,13 @@ const DEFAULT_SETTINGS: MediaDbPluginSettings = {
9778
openNoteInNewTab: true,
9879
useDefaultFrontMatter: true,
9980
enableTemplaterIntegration: false,
100-
apiToggle: {
101-
OMDbAPI: {
102-
movie: true,
103-
series: true,
104-
game: true,
105-
},
106-
MALAPI: {
107-
movie: true,
108-
series: true,
109-
},
110-
MALAPIManga: {
111-
comicManga: true,
112-
},
113-
ComicVineAPI: {
114-
comicManga: true,
115-
},
116-
SteamAPI: {
117-
game: true,
118-
},
119-
MobyGamesAPI: {
120-
game: true,
121-
},
122-
GiantBombAPI: {
123-
game: true,
124-
},
125-
},
81+
OMDbAPI_disabledMediaTypes: [],
82+
MALAPI_disabledMediaTypes: [],
83+
MALAPIManga_disabledMediaTypes: [],
84+
ComicVineAPI_disabledMediaTypes: [],
85+
SteamAPI_disabledMediaTypes: [],
86+
MobyGamesAPI_disabledMediaTypes: [],
87+
GiantBombAPI_disabledMediaTypes: [],
12688
movieTemplate: '',
12789
seriesTemplate: '',
12890
mangaTemplate: '',
@@ -329,31 +291,53 @@ export class MediaDbSettingTab extends PluginSettingTab {
329291

330292
// Create a map to store APIs for each media type
331293
const mediaTypeApiMap = new Map<string, string[]>();
294+
const apiMediaTypes = {
295+
OMDbAPI: ['movie', 'series', 'game'],
296+
MALAPI: ['movie', 'series'],
297+
MALAPIManga: ['manga'],
298+
ComicVineAPI: ['comic', 'manga'],
299+
SteamAPI: ['game'],
300+
MobyGamesAPI: ['game'],
301+
GiantBombAPI: ['game'],
302+
};
332303

333304
// Populate the map with APIs for each media type
334-
for (const [apiName, api] of Object.entries(this.plugin.settings.apiToggle)) {
335-
for (const mediaType of Object.keys(api)) {
305+
for (const [api, mediaTypes] of Object.entries(apiMediaTypes)) {
306+
for (const mediaType of mediaTypes) {
336307
if (!mediaTypeApiMap.has(mediaType)) {
337308
mediaTypeApiMap.set(mediaType, []);
338309
}
339-
mediaTypeApiMap.get(mediaType)!.push(apiName);
310+
mediaTypeApiMap.get(mediaType)!.push(api);
340311
}
341312
}
342313

314+
// Log the populated map to verify its contents
315+
console.log('mediaTypeApiMap:', mediaTypeApiMap);
316+
343317
// Filter out media types with only one API
344318
const filteredMediaTypes = Array.from(mediaTypeApiMap.entries()).filter(([_, apis]) => apis.length > 1);
345319

320+
// Log the filtered media types to verify the filtering
321+
console.log('filteredMediaTypes:', filteredMediaTypes);
322+
346323
// Dynamically create settings based on the filtered media types and their APIs
347324
for (const [mediaType, apis] of filteredMediaTypes) {
348325
new Setting(containerEl).setName(`Select APIs for ${unCamelCase(mediaType)}`).setHeading();
349326
for (const apiName of apis) {
350-
const apiToggle = this.plugin.settings.apiToggle[apiName as keyof typeof this.plugin.settings.apiToggle];
327+
const disabledMediaTypes = this.plugin.settings[`${apiName}_disabledMediaTypes` as keyof typeof this.plugin.settings] as string[];
351328
new Setting(containerEl)
352329
.setName(apiName)
353330
.setDesc(`Use ${apiName} API for ${unCamelCase(mediaType)}.`)
354331
.addToggle(cb => {
355-
cb.setValue((apiToggle as Record<string, boolean>)[mediaType]).onChange(data => {
356-
(apiToggle as Record<string, boolean>)[mediaType] = data;
332+
cb.setValue(!disabledMediaTypes.includes(mediaType)).onChange(data => {
333+
if (data) {
334+
const index = disabledMediaTypes.indexOf(mediaType);
335+
if (index > -1) {
336+
disabledMediaTypes.splice(index, 1);
337+
}
338+
} else {
339+
disabledMediaTypes.push(mediaType);
340+
}
357341
void this.plugin.saveSettings();
358342
});
359343
});

0 commit comments

Comments
 (0)