Skip to content
Snippets Groups Projects
Commit 4d3d919e authored by François Grand's avatar François Grand
Browse files

feat: add a service to handle user confirmation through GUI

refs #604
parent 00e67807
No related branches found
No related tags found
2 merge requests!225Release v4.17.0,!206Resolve "PWA: l'application ne se met pas à jour"
......@@ -127,6 +127,7 @@ import { SelectSectionDetailsComponent } from "./components/select-section-detai
import { ServiceWorkerModule } from '@angular/service-worker';
import { environment } from '../environments/environment';
import { ServiceWorkerUpdateService } from "./services/service-worker-update.service";
import { UserConfirmationService } from "./services/user-confirmation.service";
const appRoutes: Routes = [
{ path: "list/search", component: CalculatorListComponent },
......@@ -282,7 +283,8 @@ const appRoutes: Routes = [
provide: ErrorStateMatcher,
useClass: ImmediateErrorStateMatcher
},
ServiceWorkerUpdateService
ServiceWorkerUpdateService,
UserConfirmationService
],
schemas: [NO_ERRORS_SCHEMA],
bootstrap: [AppComponent]
......
import { Injectable } from "@angular/core";
import { BidirectionalSubject } from "app/util/bidir_subject";
import { Observer } from "rxjs";
/**
* This service enables any class (even another service) to display a confirmation dialog on GUI ans get the user answer
*/
@Injectable()
export class UserConfirmationService {
// used to communicate with UI component in charge of displaying confirmation dialog
// direction 0 : this
// direction 1 : UI
private _userConfirm: BidirectionalSubject<{}>;
public constructor() {
this._userConfirm = new BidirectionalSubject<{}>();
// we choose communication canal 0, UI will use 1
this._userConfirm.selectPostingChannel(this, 0);
}
/**
* add subscription from UI
* @param source
*/
public subscribe(source: any) {
this._userConfirm.selectPostingChannel(source, 1);
}
/**
* remove UI subscription
* @param source
*/
public unsubscribe(source: any) {
this._userConfirm.unselectPostingChannel(source);
}
/**
* add a handler (provided bu UI) to confirmation request
* @param source normally, UI component
* @param obs processing function
*/
public addHandler(source: any, obs: Observer<boolean>) {
this._userConfirm.addHandler(source, obs)
}
/**
* forward user confirmation from UI to requesting object
* @param confirm user confirmation status
*/
public postConfirmation(source: any, confirm: {}) {
this._userConfirm.post(source, confirm);
}
/**
* forward to UI a request from source to ask a user confirmation with a dialog
* @param source object requesting confirmation
* @param title confirmation dialog title
* @param text confirmation dialog body text
* @returns a Promise resolving to a boolean holding user confirmation status
*/
public askUserConfirmation(title: string, text: string): Promise<{}> {
const ret = this._userConfirm.getReceivePromise(this);
this._userConfirm.post(this, { title: title, body: text }); // false or true, we don't care
return ret;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment