Restored and updated api toggle

The settings screen where you can toggle each API per media type is now fully automatic and works for each media type that has more than 1 API.
This commit is contained in:
ltctceplrm 2025-03-04 23:01:44 +01:00
parent d54822444a
commit 4de707f29e
2 changed files with 540 additions and 559 deletions

View file

@ -19,13 +19,13 @@ export abstract class APIModel {
abstract getById(id: string): Promise<MediaTypeModel>;
hasType(type: MediaType): boolean {
// if (
// this.types.contains(type) &&
// (Boolean((this.plugin.settings.apiToggle as any)?.[this.apiName]?.[type]) === true || (this.plugin.settings.apiToggle as any)?.[this.apiName]?.[type] === undefined)
// ) {
// return true;
// }
return this.types.contains(type);
if (
this.types.contains(type) &&
(Boolean((this.plugin.settings.apiToggle as any)?.[this.apiName]?.[type]) === true || (this.plugin.settings.apiToggle as any)?.[this.apiName]?.[type] === undefined)
) {
return true;
}
return false;
}
hasTypeOverlap(types: MediaType[]): boolean {

View file

@ -4,7 +4,7 @@ import { mount } from 'svelte';
import type MediaDbPlugin from '../main';
import type { MediaTypeModel } from '../models/MediaTypeModel';
import { MEDIA_TYPES } from '../utils/MediaTypeManager';
import { fragWithHTML } from '../utils/Utils';
import { fragWithHTML, unCamelCase } from '../utils/Utils';
import { PropertyMapping, PropertyMappingModel, PropertyMappingOption } from './PropertyMapping';
import PropertyMappingModelsComponent from './PropertyMappingModelsComponent.svelte';
import { FileSuggest } from './suggesters/FileSuggest';
@ -21,24 +21,35 @@ export interface MediaDbPluginSettings {
openNoteInNewTab: boolean;
useDefaultFrontMatter: boolean;
enableTemplaterIntegration: boolean;
// TODO: disabled for now, as i currently don't have the time to fix this from the original PR that introduced it (#133)
// apiToggle: {
// OMDbAPI: {
// movie: boolean;
// series: boolean;
// game: boolean;
// };
// MALAPI: {
// movie: boolean;
// series: boolean;
// };
// SteamAPI: {
// game: boolean;
// };
// MobyGamesAPI: {
// game: boolean;
// };
// };
apiToggle: {
OMDbAPI: {
movie: boolean;
series: boolean;
game: boolean;
};
MALAPI: {
movie: boolean;
series: boolean;
};
MALAPIManga: {
comicManga: boolean;
};
ComicVineAPI: {
comicManga: boolean;
};
SteamAPI: {
game: boolean;
};
MobyGamesAPI: {
game: boolean;
};
GiantBombAPI: {
game: boolean;
};
VNDBAPI: {
game: boolean;
};
};
movieTemplate: string;
seriesTemplate: string;
mangaTemplate: string;
@ -89,23 +100,35 @@ const DEFAULT_SETTINGS: MediaDbPluginSettings = {
openNoteInNewTab: true,
useDefaultFrontMatter: true,
enableTemplaterIntegration: false,
// apiToggle: {
// OMDbAPI: {
// movie: true,
// series: true,
// game: true,
// },
// MALAPI: {
// movie: true,
// series: true,
// },
// SteamAPI: {
// game: true,
// },
// MobyGamesAPI: {
// game: true,
// },
// },
apiToggle: {
OMDbAPI: {
movie: true,
series: true,
game: true,
},
MALAPI: {
movie: true,
series: true,
},
MALAPIManga: {
comicManga: true
},
ComicVineAPI: {
comicManga: true
},
SteamAPI: {
game: true,
},
MobyGamesAPI: {
game: true,
},
GiantBombAPI: {
game: true,
},
VNDBAPI: {
game: true,
},
},
movieTemplate: '',
seriesTemplate: '',
mangaTemplate: '',
@ -310,82 +333,40 @@ export class MediaDbSettingTab extends PluginSettingTab {
});
});
// containerEl.createEl('h3', { text: 'APIs per media type' });
// containerEl.createEl('h5', { text: 'Movies' });
// new Setting(containerEl)
// .setName('OMDb API')
// .setDesc('Use OMDb API for movies.')
// .addToggle(cb => {
// cb.setValue(this.plugin.settings.apiToggle.OMDbAPI.movie).onChange(data => {
// this.plugin.settings.apiToggle.OMDbAPI.movie = data;
// void this.plugin.saveSettings();
// });
// });
// new Setting(containerEl)
// .setName('MAL API')
// .setDesc('Use MAL API for movies.')
// .addToggle(cb => {
// cb.setValue(this.plugin.settings.apiToggle.MALAPI.movie).onChange(data => {
// this.plugin.settings.apiToggle.MALAPI.movie = data;
// void this.plugin.saveSettings();
// });
// });
// containerEl.createEl('h5', { text: 'Series' });
// new Setting(containerEl)
// .setName('OMDb API')
// .setDesc('Use OMDb API for series.')
// .addToggle(cb => {
// cb.setValue(this.plugin.settings.apiToggle.OMDbAPI.series).onChange(data => {
// this.plugin.settings.apiToggle.OMDbAPI.series = data;
// void this.plugin.saveSettings();
// });
// });
// new Setting(containerEl)
// .setName('MAL API')
// .setDesc('Use MAL API for series.')
// .addToggle(cb => {
// cb.setValue(this.plugin.settings.apiToggle.MALAPI.series).onChange(data => {
// this.plugin.settings.apiToggle.MALAPI.series = data;
// void this.plugin.saveSettings();
// });
// });
// containerEl.createEl('h5', { text: 'Games' });
// new Setting(containerEl)
// .setName('OMDb API')
// .setDesc('Use OMDb API for games.')
// .addToggle(cb => {
// cb.setValue(this.plugin.settings.apiToggle.OMDbAPI.game).onChange(data => {
// this.plugin.settings.apiToggle.OMDbAPI.game = data;
// void this.plugin.saveSettings();
// });
// });
// new Setting(containerEl)
// .setName('Steam API')
// .setDesc('Use OMDb API for games.')
// .addToggle(cb => {
// cb.setValue(this.plugin.settings.apiToggle.SteamAPI.game).onChange(data => {
// this.plugin.settings.apiToggle.SteamAPI.game = data;
// void this.plugin.saveSettings();
// });
// });
// new Setting(containerEl)
// .setName('MobyGames API')
// .setDesc('Use MobyGames API for games.')
// .addToggle(cb => {
// cb.setValue(this.plugin.settings.apiToggle.MobyGamesAPI.game).onChange(data => {
// this.plugin.settings.apiToggle.MobyGamesAPI.game = data;
// void this.plugin.saveSettings();
// });
// });
// new Setting(containerEl)
// .setName('Giantbomb API')
// .setDesc('Use Giantbomb API for games.')
// .addToggle(cb => {
// cb.setValue(this.plugin.settings.apiToggle.GiantBombAPI.game).onChange(data => {
// this.plugin.settings.apiToggle.GiantBombAPI.game = data;
// void this.plugin.saveSettings();
// });
// });
// Create a map to store APIs for each media type
const mediaTypeApiMap = new Map<string, string[]>();
// Populate the map with APIs for each media type
Object.entries(this.plugin.settings.apiToggle).forEach(([apiName, api]) => {
Object.entries(api).forEach(([mediaType]) => {
if (!mediaTypeApiMap.has(mediaType)) {
mediaTypeApiMap.set(mediaType, []);
}
mediaTypeApiMap.get(mediaType)!.push(apiName);
});
});
// Filter out media types with only one API
const filteredMediaTypes = Array.from(mediaTypeApiMap.entries()).filter(([_, apis]) => apis.length > 1);
// Dynamically create settings based on the filtered media types and their APIs
filteredMediaTypes.forEach(([mediaType, apis]) => {
new Setting(containerEl).setName(`Select APIs for ${unCamelCase(mediaType)}`).setHeading();
apis.forEach(apiName => {
const apiToggle = this.plugin.settings.apiToggle[apiName as keyof typeof this.plugin.settings.apiToggle];
new Setting(containerEl)
.setName(apiName)
.setDesc(`Use ${apiName} API for ${unCamelCase(mediaType)}.`)
.addToggle(cb => {
cb.setValue((apiToggle as Record<string, boolean>)[mediaType])
.onChange(data => {
(apiToggle as Record<string, boolean>)[mediaType] = data;
void this.plugin.saveSettings();
});
});
});
});
new Setting(containerEl).setName('New file location').setHeading();
// region new file location
new Setting(containerEl)