MOBILE-3817 core: Implement more observable funcs for myoverview

main
Dani Palou 2022-07-06 08:02:41 +02:00
parent c9a0b372a9
commit 88297ed400
2 changed files with 58 additions and 27 deletions

View File

@ -1890,16 +1890,28 @@ export class CoreSite {
getConfig(name?: undefined, ignoreCache?: boolean): Promise<CoreSiteConfig>;
getConfig(name: string, ignoreCache?: boolean): Promise<string>;
getConfig(name?: string, ignoreCache?: boolean): Promise<string | CoreSiteConfig> {
return firstValueFrom(
this.getConfigObservable(<string> name, ignoreCache ? CoreSitesReadingStrategy.ONLY_NETWORK : undefined),
);
}
/**
* Get the config of this site.
* It is recommended to use getStoredConfig instead since it's faster and doesn't use network.
*
* @param name Name of the setting to get. If not set or false, all settings will be returned.
* @param readingStrategy Reading strategy.
* @return Observable returning site config.
*/
getConfigObservable(name?: undefined, readingStrategy?: CoreSitesReadingStrategy): Observable<CoreSiteConfig>;
getConfigObservable(name: string, readingStrategy?: CoreSitesReadingStrategy): Observable<string>;
getConfigObservable(name?: string, readingStrategy?: CoreSitesReadingStrategy): Observable<string | CoreSiteConfig> {
const preSets: CoreSiteWSPreSets = {
cacheKey: this.getConfigCacheKey(),
...CoreSites.getReadingStrategyPreSets(readingStrategy),
};
if (ignoreCache) {
preSets.getFromCache = false;
preSets.emergencyCache = false;
}
return this.read('tool_mobile_get_config', {}, preSets).then((config: CoreSiteConfigResponse) => {
return this.readObservable<CoreSiteConfigResponse>('tool_mobile_get_config', {}, preSets).pipe(map(config => {
if (name) {
// Return the requested setting.
for (const x in config.settings) {
@ -1918,7 +1930,7 @@ export class CoreSite {
return settings;
}
});
}));
}
/**

View File

@ -585,7 +585,7 @@ export class CoreCoursesProvider {
}
/**
* Get courses matching the given custom field. Only works in online.
* Get courses matching the given custom field. By default it will try not to use cache.
*
* @param customFieldName Custom field name.
* @param customFieldValue Custom field value.
@ -593,30 +593,49 @@ export class CoreCoursesProvider {
* @return Promise resolved with the list of courses.
* @since 3.8
*/
async getEnrolledCoursesByCustomField(
getEnrolledCoursesByCustomField(
customFieldName: string,
customFieldValue: string,
siteId?: string,
): Promise<CoreCourseSummaryData[]> {
const site = await CoreSites.getSite(siteId);
const params: CoreCourseGetEnrolledCoursesByTimelineClassificationWSParams = {
classification: 'customfield',
customfieldname: customFieldName,
customfieldvalue: customFieldValue,
};
const preSets: CoreSiteWSPreSets = {
getFromCache: false,
};
const courses = await site.read<CoreCourseGetEnrolledCoursesByTimelineClassificationWSResponse>(
'core_course_get_enrolled_courses_by_timeline_classification',
params,
preSets,
);
if (courses.courses) {
return courses.courses;
}
return firstValueFrom(this.getEnrolledCoursesByCustomFieldObservable(customFieldName, customFieldValue, {
readingStrategy: CoreSitesReadingStrategy.PREFER_NETWORK,
siteId,
}));
}
throw Error('WS core_course_get_enrolled_courses_by_timeline_classification failed');
/**
* Get courses matching the given custom field.
*
* @param customFieldName Custom field name.
* @param customFieldValue Custom field value.
* @param options Common options.
* @return Promise resolved with the list of courses.
* @since 3.8
*/
getEnrolledCoursesByCustomFieldObservable(
customFieldName: string,
customFieldValue: string,
options: CoreSitesCommonWSOptions,
): Observable<CoreCourseSummaryData[]> {
return asyncObservable(async () => {
const site = await CoreSites.getSite(options. siteId);
const params: CoreCourseGetEnrolledCoursesByTimelineClassificationWSParams = {
classification: 'customfield',
customfieldname: customFieldName,
customfieldvalue: customFieldValue,
};
const preSets: CoreSiteWSPreSets = {
...CoreSites.getReadingStrategyPreSets(options.readingStrategy ?? CoreSitesReadingStrategy.PREFER_NETWORK),
};
return site.readObservable<CoreCourseGetEnrolledCoursesByTimelineClassificationWSResponse>(
'core_course_get_enrolled_courses_by_timeline_classification',
params,
preSets,
).pipe(map(response => response.courses));
});
}
/**