diff --git a/scripts/langindex.json b/scripts/langindex.json index ac392e4af..df29938fb 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -435,6 +435,7 @@ "addon.mod_book.toc": "book", "addon.mod_chat.beep": "chat", "addon.mod_chat.chatreport": "chat", + "addon.mod_chat.confirmloss": "local_moodlemobileapp", "addon.mod_chat.currentusers": "chat", "addon.mod_chat.enterchat": "chat", "addon.mod_chat.entermessage": "chat", diff --git a/src/addons/mod/chat/chat-lazy.module.ts b/src/addons/mod/chat/chat-lazy.module.ts index 3a667037e..0a41f9cbc 100644 --- a/src/addons/mod/chat/chat-lazy.module.ts +++ b/src/addons/mod/chat/chat-lazy.module.ts @@ -22,6 +22,7 @@ import { AddonModChatSessionMessagesPage } from './pages/session-messages/sessio import { CoreScreen } from '@services/screen'; import { conditionalRoutes } from '@/app/app-routing.module'; import { AddonModChatSessionsPage } from './pages/sessions/sessions'; +import { CanLeaveGuard } from '@guards/can-leave'; const commonRoutes: Routes = [ { @@ -31,6 +32,7 @@ const commonRoutes: Routes = [ { path: ':courseId/:cmId/chat', component: AddonModChatChatPage, + canDeactivate: [CanLeaveGuard], }, ]; diff --git a/src/addons/mod/chat/lang.json b/src/addons/mod/chat/lang.json index ad8f86ce8..7d5b644a0 100644 --- a/src/addons/mod/chat/lang.json +++ b/src/addons/mod/chat/lang.json @@ -1,6 +1,7 @@ { "beep": "Beep", "chatreport": "Chat sessions", + "confirmloss": "Are you sure? Chat history can be lost.", "currentusers": "Current users", "enterchat": "Click here to enter the chat now", "entermessage": "Enter your message", @@ -25,4 +26,4 @@ "showincompletesessions": "Show incomplete sessions", "talk": "Talk", "viewreport": "View past chat sessions" -} \ No newline at end of file +} diff --git a/src/addons/mod/chat/pages/chat/chat.ts b/src/addons/mod/chat/pages/chat/chat.ts index 073c372c1..732beb830 100644 --- a/src/addons/mod/chat/pages/chat/chat.ts +++ b/src/addons/mod/chat/pages/chat/chat.ts @@ -15,13 +15,14 @@ import { Component, ViewChild, OnInit, OnDestroy } from '@angular/core'; import { CoreAnimations } from '@components/animations'; import { CoreSendMessageFormComponent } from '@components/send-message-form/send-message-form'; +import { CanLeave } from '@guards/can-leave'; import { IonContent } from '@ionic/angular'; import { CoreApp } from '@services/app'; import { CoreNavigator } from '@services/navigator'; import { CoreSites } from '@services/sites'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreUtils } from '@services/utils/utils'; -import { Network, NgZone } from '@singletons'; +import { Network, NgZone, Translate } from '@singletons'; import { CoreEventObserver, CoreEvents } from '@singletons/events'; import { Subscription } from 'rxjs'; import { AddonModChatUsersModalComponent, AddonModChatUsersModalResult } from '../../components/users-modal/users-modal'; @@ -37,7 +38,7 @@ import { AddonModChatFormattedMessage, AddonModChatHelper } from '../../services animations: [CoreAnimations.SLIDE_IN_OUT], styleUrls: ['chat.scss', '../../../../messages/pages/discussion/discussion.scss'], }) -export class AddonModChatChatPage implements OnInit, OnDestroy { +export class AddonModChatChatPage implements OnInit, OnDestroy, CanLeave { @ViewChild(IonContent) content?: IonContent; @ViewChild(CoreSendMessageFormComponent) sendMessageForm?: CoreSendMessageFormComponent; @@ -381,6 +382,22 @@ export class AddonModChatChatPage implements OnInit, OnDestroy { // }); } + /** + * Check if we can leave the page or not. + * + * @return Resolved with true if we can leave it, rejected if not. + */ + async canLeave(): Promise { + if (! this.messages.some((message) => !message.special)) { + return true; + } + + // Modified, confirm user wants to go back. + await CoreDomUtils.showConfirm(Translate.instant('addon.mod_chat.confirmloss')); + + return true; + } + /** * @inheritdoc */