Merge pull request #2551 from crazyserver/MOBILE-3523

MOBILE-3469 course: Get course data when entering from search
main
Juan Leyva 2020-09-28 16:52:29 +02:00 committed by GitHub
commit ad2b5c4529
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 42 deletions

View File

@ -28,7 +28,7 @@ import { CoreCourseOfflineProvider } from './course-offline';
import { CoreSitePluginsProvider } from '@core/siteplugins/providers/siteplugins';
import { CoreCourseFormatDelegate } from './format-delegate';
import { CorePushNotificationsProvider } from '@core/pushnotifications/providers/pushnotifications';
import { CoreCoursesProvider } from '@core/courses/providers/courses';
import { CoreCoursesProvider, CoreCourses } from '@core/courses/providers/courses';
import { makeSingleton } from '@singletons/core.singletons';
/**
@ -978,49 +978,69 @@ export class CoreCourseProvider {
* @param params Other params to pass to the course page.
* @return Promise resolved when done.
*/
openCourse(navCtrl: NavController, course: any, params?: any): Promise<any> {
async openCourse(navCtrl: NavController, course: any, params?: any): Promise<void> {
const loading = this.domUtils.showModalLoading();
// Wait for site plugins to be fetched.
return this.sitePluginsProvider.waitFetchPlugins().then(() => {
if (this.sitePluginsProvider.sitePluginPromiseExists('format_' + course.format)) {
// This course uses a custom format plugin, wait for the format plugin to finish loading.
await this.sitePluginsProvider.waitFetchPlugins();
return this.sitePluginsProvider.sitePluginLoaded('format_' + course.format).then(() => {
// The format loaded successfully, but the handlers wont be registered until all site plugins have loaded.
if (this.sitePluginsProvider.sitePluginsFinishedLoading) {
return this.courseFormatDelegate.openCourse(navCtrl, course, params);
if (typeof course.format == 'undefined') {
const coursesProvider = CoreCourses.instance;
try {
course = await coursesProvider.getUserCourse(course.id, true);
} catch (error) {
// Not enrolled or an error happened. Try to use another WebService.
const available = coursesProvider.isGetCoursesByFieldAvailableInSite();
try {
if (available) {
course = await CoreCourses.instance.getCourseByField('id', course.id);
} else {
// Wait for plugins to be loaded.
const deferred = this.utils.promiseDefer(),
observer = this.eventsProvider.on(CoreEventsProvider.SITE_PLUGINS_LOADED, () => {
observer && observer.off();
this.courseFormatDelegate.openCourse(navCtrl, course, params).then((response) => {
deferred.resolve(response);
}).catch((error) => {
deferred.reject(error);
});
});
return deferred.promise;
course = await CoreCourses.instance.getCourse(course.id);
}
}).catch(() => {
// The site plugin failed to load. The user needs to restart the app to try loading it again.
const message = this.translate.instant('core.courses.errorloadplugins');
const reload = this.translate.instant('core.courses.reload');
const ignore = this.translate.instant('core.courses.ignore');
this.domUtils.showConfirm(message, '', reload, ignore).then(() => {
window.location.reload();
});
});
} else {
// No custom format plugin. We don't need to wait for anything.
} catch (error) {
// Ignore errors.
}
}
}
if (!this.sitePluginsProvider.sitePluginPromiseExists('format_' + course.format)) {
// No custom format plugin. We don't need to wait for anything.
await this.courseFormatDelegate.openCourse(navCtrl, course, params);
loading.dismiss();
return;
}
// This course uses a custom format plugin, wait for the format plugin to finish loading.
try {
await this.sitePluginsProvider.sitePluginLoaded('format_' + course.format);
// The format loaded successfully, but the handlers wont be registered until all site plugins have loaded.
if (this.sitePluginsProvider.sitePluginsFinishedLoading) {
return this.courseFormatDelegate.openCourse(navCtrl, course, params);
}
}).finally(() => {
loading.dismiss();
});
// Wait for plugins to be loaded.
const deferred = this.utils.promiseDefer(),
observer = this.eventsProvider.on(CoreEventsProvider.SITE_PLUGINS_LOADED, () => {
observer && observer.off();
this.courseFormatDelegate.openCourse(navCtrl, course, params).then((response) => {
deferred.resolve(response);
}).catch((error) => {
deferred.reject(error);
});
});
return deferred.promise;
} catch (error) {
// The site plugin failed to load. The user needs to restart the app to try loading it again.
const message = this.translate.instant('core.courses.errorloadplugins');
const reload = this.translate.instant('core.courses.reload');
const ignore = this.translate.instant('core.courses.ignore');
this.domUtils.showConfirm(message, '', reload, ignore).then(() => {
window.location.reload();
});
}
}
/**

View File

@ -172,7 +172,7 @@ export class CoreCourseFormatDefaultHandler implements CoreCourseFormatHandler {
* @param params Params to pass to the course page.
* @return Promise resolved when done.
*/
openCourse(navCtrl: NavController, course: any, params?: any): Promise<any> {
openCourse(navCtrl: NavController, course: any, params?: any): Promise<void> {
params = params || {};
Object.assign(params, { course: course });

View File

@ -102,7 +102,7 @@ export interface CoreCourseFormatHandler extends CoreDelegateHandler {
* @param params Params to pass to the course page.
* @return Promise resolved when done.
*/
openCourse?(navCtrl: NavController, course: any, params?: any): Promise<any>;
openCourse?(navCtrl: NavController, course: any, params?: any): Promise<void>;
/**
* Return the Component to use to display the course format instead of using the default one.
@ -351,14 +351,14 @@ export class CoreCourseFormatDelegate extends CoreDelegate {
}
/**
* Open a course.
* Open a course. Should not be called directly. Call CoreCourseHelper.openCourse instead.
*
* @param navCtrl The NavController instance to use.
* @param course The course to open. It should contain a "format" attribute.
* @param params Params to pass to the course page.
* @return Promise resolved when done.
*/
openCourse(navCtrl: NavController, course: any, params?: any): Promise<any> {
openCourse(navCtrl: NavController, course: any, params?: any): Promise<void> {
return this.executeFunctionOnEnabled(course.format, 'openCourse', [navCtrl, course, params]);
}

View File

@ -1601,7 +1601,7 @@ export class CoreCourseHelperProvider {
* @param siteId Site ID. If not defined, current site.
* @return Promise resolved when done.
*/
openCourse(navCtrl: NavController, course: any, params?: any, siteId?: string): Promise<any> {
openCourse(navCtrl: NavController, course: any, params?: any, siteId?: string): Promise<void> {
if (!siteId || siteId == this.sitesProvider.getCurrentSiteId()) {
// Current site, we can open the course.
return this.courseProvider.openCourse(navCtrl, course, params);

View File

@ -259,7 +259,7 @@ export class CoreCoursesCoursePreviewPage implements OnDestroy {
return;
}
this.courseFormatDelegate.openCourse(this.navCtrl, this.course);
this.courseHelper.openCourse(this.navCtrl, this.course);
}
/**