diff --git a/src/core/courses/components/course-progress/course-progress.ts b/src/core/courses/components/course-progress/course-progress.ts index db705c889..2084e6113 100644 --- a/src/core/courses/components/course-progress/course-progress.ts +++ b/src/core/courses/components/course-progress/course-progress.ts @@ -12,10 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Component, Input, OnInit } from '@angular/core'; +import { Component, Input, OnInit, OnDestroy } from '@angular/core'; import { NavController } from 'ionic-angular'; import { TranslateService } from '@ngx-translate/core'; +import { CoreEventsProvider } from '../../../../providers/events'; +import { CoreSitesProvider } from '../../../../providers/sites'; +import { CoreDomUtilsProvider } from '../../../../providers/utils/dom'; import { CoreCourseFormatDelegate } from '../../../course/providers/format-delegate'; +import { CoreCourseProvider } from '../../../course/providers/course'; +import { CoreCourseHelperProvider } from '../../../course/providers/helper'; /** * This component is meant to display a course for a list of courses with progress. @@ -29,32 +34,52 @@ import { CoreCourseFormatDelegate } from '../../../course/providers/format-deleg selector: 'core-courses-course-progress', templateUrl: 'course-progress.html' }) -export class CoreCoursesCourseProgressComponent implements OnInit { +export class CoreCoursesCourseProgressComponent implements OnInit, OnDestroy { @Input() course: any; // The course to render. isDownloading: boolean; - - protected obsStatus; - protected downloadText; - protected downloadingText; - protected downloadButton = { - isDownload: true, - className: 'core-download-course', - priority: 1000 + prefetchCourseData = { + prefetchCourseIcon: 'spinner' }; - protected buttons; - constructor(private navCtrl: NavController, private translate: TranslateService, - private courseFormatDelegate: CoreCourseFormatDelegate) { - this.downloadText = this.translate.instant('core.course.downloadcourse'); - this.downloadingText = this.translate.instant('core.downloading'); + protected isDestroyed = false; + protected courseStatusObserver; + + constructor(private navCtrl: NavController, private translate: TranslateService, private courseHelper: CoreCourseHelperProvider, + private courseFormatDelegate: CoreCourseFormatDelegate, private domUtils: CoreDomUtilsProvider, + private courseProvider: CoreCourseProvider, eventsProvider: CoreEventsProvider, sitesProvider: CoreSitesProvider) { + // Listen for status change in course. + this.courseStatusObserver = eventsProvider.on(CoreEventsProvider.COURSE_STATUS_CHANGED, (data) => { + if (data.courseId == this.course.id) { + this.prefetchCourseData.prefetchCourseIcon = this.courseHelper.getCourseStatusIconFromStatus(data.status); + } + }, sitesProvider.getCurrentSiteId()); } /** * Component being initialized. */ ngOnInit() { - // @todo: Handle course prefetch. + // Determine course prefetch icon. + this.courseHelper.getCourseStatusIcon(this.course.id).then((icon) => { + this.prefetchCourseData.prefetchCourseIcon = icon; + + if (icon == 'spinner') { + // Course is being downloaded. Get the download promise. + const promise = this.courseHelper.getCourseDownloadPromise(this.course.id); + if (promise) { + // There is a download promise. If it fails, show an error. + promise.catch((error) => { + if (!this.isDestroyed) { + this.domUtils.showErrorModalDefault(error, 'core.course.errordownloadingcourse', true); + } + }); + } else { + // No download, this probably means that the app was closed while downloading. Set previous status. + this.courseProvider.setCoursePreviousStatus(this.course.id); + } + } + }); } /** @@ -64,4 +89,30 @@ export class CoreCoursesCourseProgressComponent implements OnInit { this.courseFormatDelegate.openCourse(this.navCtrl, course); } + /** + * Prefetch the course. + * + * @param {Event} e Click event. + */ + prefetchCourse(e: Event) { + e.preventDefault(); + e.stopPropagation(); + + this.courseHelper.confirmAndPrefetchCourse(this.prefetchCourseData, this.course).catch((error) => { + if (!this.isDestroyed) { + this.domUtils.showErrorModalDefault(error, 'core.course.errordownloadingcourse', true); + } + }) + } + + /** + * Component destroyed. + */ + ngOnDestroy() { + this.isDestroyed = true; + + if (this.courseStatusObserver) { + this.courseStatusObserver.off(); + } + } } diff --git a/src/core/courses/pages/course-preview/course-preview.html b/src/core/courses/pages/course-preview/course-preview.html index 58db99af7..452b2e79f 100644 --- a/src/core/courses/pages/course-preview/course-preview.html +++ b/src/core/courses/pages/course-preview/course-preview.html @@ -17,22 +17,22 @@
{{course.startdate * 1000 | coreFormatDate:"dfdaymonthyear"}} - {{course.enddate * 1000 | coreFormatDate:"dfdaymonthyear"}}
-{{ 'core.teachers' | translate }}
{{contact.fullname}}