refactor: AlertService

This commit is contained in:
Baptiste Grob
2020-07-09 15:43:58 +02:00
parent a9aba87033
commit 5656edb385
4 changed files with 70 additions and 101 deletions

View File

@@ -296,21 +296,16 @@ class AccountMenuCtrl extends PureViewCtrl {
}
}
mergeLocalChanged() {
async mergeLocalChanged() {
if (!this.getState().formData.mergeLocal) {
this.application!.alertService!.confirm(
STRING_ACCOUNT_MENU_UNCHECK_MERGE,
undefined,
undefined,
undefined,
undefined,
() => {
this.setFormDataState({
mergeLocal: true
});
},
true,
);
if (await confirmDialog({
text: STRING_ACCOUNT_MENU_UNCHECK_MERGE,
confirmButtonStyle: 'danger'
})) {
this.setFormDataState({
mergeLocal: true
});
}
}
}
@@ -553,17 +548,12 @@ class AccountMenuCtrl extends PureViewCtrl {
if (!signedIn) {
message += STRING_REMOVE_PASSCODE_OFFLINE_ADDENDUM;
}
this.application!.alertService!.confirm(
message,
undefined,
undefined,
undefined,
() => {
this.application!.removePasscode();
},
undefined,
true,
);
if (await confirmDialog({
text: message,
confirmButtonStyle: 'danger'
})) {
this.application!.removePasscode();
}
};
const needsPrivilege = await this.application!.privilegesService!.actionRequiresPrivilege(
ProtectedAction.ManagePasscode

View File

@@ -1,5 +1,5 @@
/* eslint-disable prefer-promise-reject-errors */
import { SNAlertService } from 'snjs';
import { SNAlertService, ButtonType, DismissBlockingDialog } from 'snjs';
import { SKAlert } from 'sn-stylekit';
/** @returns a promise resolving to true if the user confirmed, false if they canceled */
@@ -41,55 +41,47 @@ export function confirmDialog({
});
}
export class AlertService extends SNAlertService {
async alert(
export class AlertService implements SNAlertService {
alert(
text: string,
title: string,
closeButtonText = 'OK',
onClose: () => void
) {
return new Promise((resolve) => {
const buttons = [
{
return new Promise<void>((resolve) => {
const alert = new SKAlert({
title,
text,
buttons: [{
text: closeButtonText,
style: 'neutral',
action: async () => {
if (onClose) {
this.deviceInterface!.timeout(onClose);
}
resolve(true);
},
},
];
const alert = new SKAlert({ title, text, buttons });
action: resolve,
}],
});
alert.present();
});
}
/** @deprecated use confirmDialog instead */
async confirm(
confirm(
text: string,
title: string,
confirmButtonText = 'Confirm',
cancelButtonText = 'Cancel',
onConfirm?: () => void,
onCancel?: () => void,
destructive = false
) {
title?: string,
confirmButtonText?: string,
confirmButtonType?: ButtonType,
cancelButtonText?: string
): Promise<boolean> {
return confirmDialog({
text,
title,
confirmButtonText,
cancelButtonText,
confirmButtonStyle: destructive ? 'danger' : 'info'
}).then(confirmed => new Promise((resolve, reject) => {
if (confirmed) {
onConfirm?.();
resolve(true);
} else {
onCancel?.();
reject(false);
}
}))
confirmButtonStyle: confirmButtonType === ButtonType.Danger ? 'danger' : 'info'
});
}
blockingDialog(text: string) {
const alert = new SKAlert({ text });
alert.present();
return () => {
alert.dismiss();
};
}
}

View File

@@ -63,13 +63,9 @@ export class WebApplication extends SNApplication {
platformFromString(getPlatformString()),
deviceInterface,
new SNWebCrypto(),
new AlertService(),
namespace,
[
{
swap: SNAlertService,
with: AlertService
}
],
undefined,
undefined,
);
this.$compile = $compile;

View File

@@ -32,6 +32,7 @@ import {
StringDeleteNote,
StringEmptyTrash
} from '@/strings';
import { confirmDialog } from '@/services/alertService';
const NOTE_PREVIEW_CHAR_LIMIT = 80;
const MINIMUM_STATUS_DURATION = 400;
@@ -685,7 +686,7 @@ class EditorViewCtrl extends PureViewCtrl<{}, EditorState> {
);
return;
}
const run = () => {
const run = async () => {
if (this.note.locked) {
this.application.alertService!.alert(
STRING_DELETE_LOCKED_ATTEMPT
@@ -699,28 +700,23 @@ class EditorViewCtrl extends PureViewCtrl<{}, EditorState> {
title,
permanently
);
this.application.alertService!.confirm(
if (await confirmDialog({
text,
undefined,
undefined,
undefined,
() => {
if (permanently) {
this.performNoteDeletion(this.note);
} else {
this.saveNote(
true,
false,
true,
(mutator) => {
mutator.trashed = true;
}
);
}
},
undefined,
true,
);
confirmButtonStyle: 'danger'
})) {
if (permanently) {
this.performNoteDeletion(this.note);
} else {
this.saveNote(
true,
false,
true,
(mutator) => {
mutator.trashed = true;
}
);
}
};
};
const requiresPrivilege = await this.application.privilegesService!.actionRequiresPrivilege(
ProtectedAction.DeleteNote
@@ -761,20 +757,15 @@ class EditorViewCtrl extends PureViewCtrl<{}, EditorState> {
return this.application.getTrashedItems().length;
}
emptyTrash() {
async emptyTrash() {
const count = this.getTrashCount();
this.application.alertService!.confirm(
StringEmptyTrash(count),
undefined,
undefined,
undefined,
() => {
this.application.emptyTrash();
this.application.sync();
},
undefined,
true,
);
if (await confirmDialog({
text: StringEmptyTrash(count),
confirmButtonStyle: 'danger'
})) {
this.application.emptyTrash();
this.application.sync();
}
}
togglePin() {