From 02bd2314b932e81c492369b37410f4ec4b776530 Mon Sep 17 00:00:00 2001 From: Alfonso Salces Date: Thu, 30 Jun 2022 08:17:25 +0200 Subject: [PATCH] MOBILE-4027 h5pactivity: Don't show modal if user finish activity 10 secs ago --- .../mod/h5pactivity/components/index/index.ts | 5 +- .../mod/h5pactivity/pages/index/index.html | 3 +- .../mod/h5pactivity/pages/index/index.ts | 47 ++++++++++++++++--- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/addons/mod/h5pactivity/components/index/index.ts b/src/addons/mod/h5pactivity/components/index/index.ts index 901f1b104..b1808e316 100644 --- a/src/addons/mod/h5pactivity/components/index/index.ts +++ b/src/addons/mod/h5pactivity/components/index/index.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Component, Optional, OnInit, OnDestroy } from '@angular/core'; +import { Component, Optional, OnInit, OnDestroy, Output, EventEmitter } from '@angular/core'; import { IonContent } from '@ionic/angular'; import { CoreConstants } from '@/core/constants'; @@ -55,6 +55,8 @@ import { AddonModH5PActivityModuleHandlerService } from '../../services/handlers }) export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActivityComponent implements OnInit, OnDestroy { + @Output() onActivityFinish = new EventEmitter(); + component = AddonModH5PActivityProvider.COMPONENT; moduleName = 'h5pactivity'; @@ -464,6 +466,7 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv // Check if the H5P has ended. Final statements don't include a subContentId. const hasEnded = data.statements.some(statement => !statement.object.id.includes('subContentId=')); if (hasEnded) { + this.onActivityFinish.emit(hasEnded); this.checkCompletion(); } } diff --git a/src/addons/mod/h5pactivity/pages/index/index.html b/src/addons/mod/h5pactivity/pages/index/index.html index 8c2890fee..f3633f9a3 100644 --- a/src/addons/mod/h5pactivity/pages/index/index.html +++ b/src/addons/mod/h5pactivity/pages/index/index.html @@ -20,6 +20,7 @@ - + diff --git a/src/addons/mod/h5pactivity/pages/index/index.ts b/src/addons/mod/h5pactivity/pages/index/index.ts index db3836c8a..5ce40fbfb 100644 --- a/src/addons/mod/h5pactivity/pages/index/index.ts +++ b/src/addons/mod/h5pactivity/pages/index/index.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Component, ViewChild } from '@angular/core'; +import { Component, OnDestroy, ViewChild } from '@angular/core'; import { CoreCourseModuleMainActivityPage } from '@features/course/classes/main-activity-page'; import { CanLeave } from '@guards/can-leave'; @@ -28,7 +28,10 @@ import { AddonModH5PActivityIndexComponent } from '../../components/index'; templateUrl: 'index.html', }) export class AddonModH5PActivityIndexPage extends CoreCourseModuleMainActivityPage - implements CanLeave { + implements CanLeave, OnDestroy { + + canLeaveSafely = false; + remainingTimeout?: ReturnType; @ViewChild(AddonModH5PActivityIndexComponent) activityComponent?: AddonModH5PActivityIndexComponent; @@ -40,12 +43,42 @@ export class AddonModH5PActivityIndexPage extends CoreCourseModuleMainActivityPa return true; } - try { - await CoreDomUtils.showConfirm(Translate.instant('core.confirmleaveunknownchanges')); + if (!this.canLeaveSafely) { + try { + await CoreDomUtils.showConfirm(Translate.instant('core.confirmleaveunknownchanges')); - return true; - } catch { - return false; + return true; + } catch { + return false; + } + } + + return true; + } + + /** + * Set if this activity can be leaved safely (withow showing warning modal) if activity is finished + * 10 seconds before. + * + * @param isDone the H5P activity is done. + */ + setCanleaveSafely(isDone: boolean): void { + this.canLeaveSafely = isDone; + if (this.remainingTimeout) { + clearTimeout(this.remainingTimeout); + } + // When user finish an activity, he have 10 seconds to leave safely (without show alert). + this.remainingTimeout = setTimeout(() => { + this.canLeaveSafely = false; + }, 10000); + } + + /** + * @inheritdoc + */ + ngOnDestroy(): void { + if (this.remainingTimeout) { + clearTimeout(this.remainingTimeout); } }