ente/lib/ui/account/delete_account_page.dart

340 lines
11 KiB
Dart
Raw Normal View History

import 'dart:convert';
2023-03-15 16:29:11 +00:00
import "package:dropdown_button2/dropdown_button2.dart";
2022-07-08 07:27:04 +00:00
import 'package:flutter/material.dart';
import "package:logging/logging.dart";
import 'package:photos/core/configuration.dart';
import "package:photos/generated/l10n.dart";
import 'package:photos/models/delete_account.dart';
import 'package:photos/services/user_service.dart';
import 'package:photos/theme/ente_theme.dart';
import 'package:photos/ui/components/buttons/button_widget.dart';
import 'package:photos/ui/components/models/button_type.dart';
import 'package:photos/utils/crypto_util.dart';
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/email_util.dart';
import "package:photos/utils/toast_util.dart";
2023-04-08 04:17:04 +00:00
import "package:styled_text/styled_text.dart";
2022-07-08 07:27:04 +00:00
2023-03-15 12:21:39 +00:00
class DeleteAccountPage extends StatefulWidget {
2022-07-08 07:27:04 +00:00
const DeleteAccountPage({
2022-12-30 09:44:52 +00:00
Key? key,
2022-07-08 07:27:04 +00:00
}) : super(key: key);
2023-03-15 12:21:39 +00:00
@override
State<DeleteAccountPage> createState() => _DeleteAccountPageState();
}
class _DeleteAccountPageState extends State<DeleteAccountPage> {
bool _hasConfirmedDeletion = false;
final _feedbackTextCtrl = TextEditingController();
late String _defaultSelection = S.of(context).selectReason;
String? _dropdownValue;
late final List<String> _deletionReason = [
2023-03-15 12:21:39 +00:00
_defaultSelection,
S.of(context).deleteReason1,
S.of(context).deleteReason2,
S.of(context).deleteReason3,
S.of(context).deleteReason4,
2023-03-15 12:21:39 +00:00
];
2022-07-08 07:27:04 +00:00
@override
Widget build(BuildContext context) {
_defaultSelection = S.of(context).selectReason;
_dropdownValue ??= _defaultSelection;
2023-06-29 06:28:45 +00:00
final double dropDownTextSize = MediaQuery.of(context).size.width - 120;
final colorScheme = getEnteColorScheme(context);
2022-07-08 07:27:04 +00:00
return Scaffold(
appBar: AppBar(
elevation: 0,
title: Text(S.of(context).deleteAccount),
2022-07-08 07:27:04 +00:00
leading: IconButton(
icon: const Icon(Icons.arrow_back),
color: Theme.of(context).iconTheme.color,
onPressed: () {
2023-04-08 04:18:27 +00:00
Navigator.of(context).pop();
2022-07-08 07:27:04 +00:00
},
),
),
body: Padding(
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
2022-07-08 07:27:04 +00:00
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
2023-03-15 12:21:39 +00:00
mainAxisSize: MainAxisSize.max,
2022-07-08 07:27:04 +00:00
children: [
2023-03-15 12:21:39 +00:00
Text(
S.of(context).askDeleteReason,
2023-03-15 12:21:39 +00:00
style: getEnteTextTheme(context).body,
),
2023-07-21 09:37:48 +00:00
const SizedBox(height: 12),
2023-03-15 12:21:39 +00:00
Container(
width: double.infinity,
height: 48,
decoration: BoxDecoration(
color: colorScheme.fillFaint,
borderRadius: BorderRadius.circular(8),
),
2023-03-15 16:29:11 +00:00
child: DropdownButton2<String>(
2023-03-15 12:21:39 +00:00
alignment: AlignmentDirectional.topStart,
value: _dropdownValue,
2023-03-15 12:21:39 +00:00
onChanged: (String? newValue) {
setState(() {
_dropdownValue = newValue!;
2023-03-15 12:21:39 +00:00
});
},
underline: const SizedBox(),
items: _deletionReason
2023-03-15 12:21:39 +00:00
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
enabled: value != _defaultSelection,
alignment: Alignment.centerLeft,
2023-06-29 06:28:45 +00:00
child: SizedBox(
width: dropDownTextSize,
child: Text(
value,
style: value != _defaultSelection
? getEnteTextTheme(context).small
: getEnteTextTheme(context).smallMuted,
overflow: TextOverflow.visible,
),
2023-03-15 12:21:39 +00:00
),
);
}).toList(),
),
2022-07-08 07:27:04 +00:00
),
2023-03-15 12:21:39 +00:00
const SizedBox(height: 24),
Text(
S.of(context).deleteAccountFeedbackPrompt,
2023-03-15 12:21:39 +00:00
style: getEnteTextTheme(context).body,
2022-07-08 07:27:04 +00:00
),
2023-07-21 09:37:48 +00:00
const SizedBox(height: 12),
2023-03-15 12:21:39 +00:00
TextFormField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(color: colorScheme.strokeFaint, width: 1),
borderRadius: BorderRadius.circular(8),
),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: colorScheme.strokeFaint, width: 1),
borderRadius: BorderRadius.circular(8),
),
filled: true,
fillColor: Colors.transparent,
hintText: S.of(context).feedback,
2023-03-15 12:21:39 +00:00
contentPadding: const EdgeInsets.all(12),
2022-07-08 07:27:04 +00:00
),
2023-03-15 12:21:39 +00:00
controller: _feedbackTextCtrl,
autofocus: false,
autocorrect: false,
keyboardType: TextInputType.multiline,
minLines: 3,
maxLines: null,
onChanged: (_) {
setState(() {});
},
2022-07-08 07:27:04 +00:00
),
_shouldAskForFeedback()
? SizedBox(
height: 42,
child: Padding(
padding: const EdgeInsets.only(top: 4.0),
child: Text(
S.of(context).kindlyHelpUsWithThisInformation,
style: getEnteTextTheme(context)
.smallBold
.copyWith(color: colorScheme.warning700),
),
),
)
: const SizedBox(height: 42),
2023-03-15 16:22:09 +00:00
GestureDetector(
onTap: () {
setState(() {
_hasConfirmedDeletion = !_hasConfirmedDeletion;
});
},
child: Row(
children: [
Checkbox(
value: _hasConfirmedDeletion,
side: CheckboxTheme.of(context).side,
onChanged: (value) {
setState(() {
_hasConfirmedDeletion = value!;
});
},
2023-03-15 12:21:39 +00:00
),
2023-03-15 16:22:09 +00:00
Expanded(
child: Text(
S.of(context).confirmDeletePrompt,
2023-03-15 16:22:09 +00:00
style: getEnteTextTheme(context).bodyMuted,
textAlign: TextAlign.left,
),
2023-08-19 11:39:56 +00:00
),
2023-03-15 16:22:09 +00:00
],
),
2022-07-08 07:27:04 +00:00
),
2023-03-15 12:21:39 +00:00
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
ButtonWidget(
buttonType: ButtonType.critical,
labelText: S.of(context).confirmAccountDeletion,
isDisabled: _shouldBlockDeletion(),
onTap: () async {
await _initiateDelete(context);
},
2023-03-15 13:11:33 +00:00
shouldSurfaceExecutionStates: true,
2023-03-15 12:21:39 +00:00
),
const SizedBox(height: 8),
ButtonWidget(
buttonType: ButtonType.secondary,
labelText: S.of(context).cancel,
2023-03-15 12:21:39 +00:00
onTap: () async {
Navigator.of(context).pop();
},
2022-07-08 07:27:04 +00:00
),
2023-03-15 12:21:39 +00:00
const SafeArea(
child: SizedBox(
height: 12,
),
2022-07-08 07:27:04 +00:00
),
],
),
),
],
),
),
),
);
}
bool _shouldBlockDeletion() {
return !_hasConfirmedDeletion ||
_dropdownValue == _defaultSelection ||
_shouldAskForFeedback();
}
bool _shouldAskForFeedback() {
2023-03-31 08:54:31 +00:00
return _feedbackTextCtrl.text.trim().isEmpty;
}
Future<void> _initiateDelete(BuildContext context) async {
final choice = await showChoiceDialog(
context,
title: S.of(context).confirmAccountDeletion,
body: S.of(context).deleteConfirmDialogBody,
firstButtonLabel: S.of(context).deleteAccountPermanentlyButton,
firstButtonType: ButtonType.critical,
firstButtonOnTap: () async {
final deleteChallengeResponse =
await UserService.instance.getDeleteChallenge(context);
if (deleteChallengeResponse == null) {
return;
}
if (deleteChallengeResponse.allowDelete) {
await _delete(context, deleteChallengeResponse);
} else {
await _requestEmailForDeletion(context);
}
},
2023-03-15 17:36:01 +00:00
isDismissible: false,
);
if (choice!.action == ButtonAction.error) {
await showGenericErrorDialog(context: context);
}
}
Future<void> _delete(
BuildContext context,
DeleteChallengeResponse response,
) async {
try {
final decryptChallenge = CryptoUtil.openSealSync(
CryptoUtil.base642bin(response.encryptedChallenge),
CryptoUtil.base642bin(
Configuration.instance.getKeyAttributes()!.publicKey,
),
Configuration.instance.getSecretKey()!,
);
final challengeResponseStr = utf8.decode(decryptChallenge);
await UserService.instance.deleteAccount(
context,
challengeResponseStr,
reasonCategory: _dropdownValue!,
feedback: _feedbackTextCtrl.text.trim(),
);
Navigator.of(context).popUntil((route) => route.isFirst);
showShortToast(context, S.of(context).yourAccountHasBeenDeleted);
} catch (e, s) {
Logger("DeleteAccount").severe("failed to delete", e, s);
showGenericErrorDialog(context: context);
2022-07-08 07:27:04 +00:00
}
}
Future<void> _requestEmailForDeletion(BuildContext context) async {
2022-08-29 14:43:31 +00:00
final AlertDialog alert = AlertDialog(
title: Text(
S.of(context).deleteAccount,
style: const TextStyle(
color: Colors.red,
),
),
2023-04-08 04:17:04 +00:00
content: StyledText(
text:
"${S.of(context).deleteEmailRequest}\n\n${S.of(context).deleteRequestSLAText}",
tags: {
'warning': StyledTextTag(
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.orange[300],
),
),
2023-04-08 04:17:04 +00:00
},
),
actions: [
TextButton(
child: Text(
S.of(context).sendEmail,
style: const TextStyle(
color: Colors.red,
),
),
onPressed: () async {
Navigator.of(context, rootNavigator: true).pop('dialog');
await sendEmail(
context,
to: 'account-deletion@ente.io',
subject: '[${S.of(context).deleteAccount}]',
);
},
),
TextButton(
child: Text(
S.of(context).ok,
style: TextStyle(
color: Theme.of(context).colorScheme.onSurface,
),
),
onPressed: () {
Navigator.of(context, rootNavigator: true).pop('dialog');
},
),
],
);
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
2022-07-08 07:27:04 +00:00
}
}