ente/mobile/lib/ui/settings/account_section_widget.dart

228 lines
7.7 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:flutter/material.dart';
2023-04-06 04:42:07 +00:00
import "package:photos/generated/l10n.dart";
import 'package:photos/services/local_authentication_service.dart';
2021-07-28 15:41:45 +00:00
import 'package:photos/services/user_service.dart';
2022-10-21 13:37:57 +00:00
import 'package:photos/theme/ente_theme.dart';
import 'package:photos/ui/account/change_email_dialog.dart';
import 'package:photos/ui/account/delete_account_page.dart';
import 'package:photos/ui/account/password_entry_page.dart';
import "package:photos/ui/account/recovery_key_page.dart";
import 'package:photos/ui/components/captioned_text_widget.dart';
import 'package:photos/ui/components/expandable_menu_item_widget.dart';
2023-01-31 12:21:57 +00:00
import 'package:photos/ui/components/menu_item_widget/menu_item_widget.dart';
2023-03-25 05:52:38 +00:00
import "package:photos/ui/payment/subscription.dart";
import 'package:photos/ui/settings/common_settings.dart';
import "package:photos/utils/crypto_util.dart";
import 'package:photos/utils/dialog_util.dart';
import "package:photos/utils/navigation_util.dart";
import "package:url_launcher/url_launcher_string.dart";
class AccountSectionWidget extends StatelessWidget {
2022-12-27 18:01:23 +00:00
const AccountSectionWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return ExpandableMenuItemWidget(
2023-04-06 04:42:07 +00:00
title: S.of(context).account,
selectionOptionsWidget: _getSectionOptions(context),
leadingIcon: Icons.account_circle_outlined,
);
}
Column _getSectionOptions(BuildContext context) {
2021-07-27 14:51:45 +00:00
return Column(
children: [
sectionOptionSpacing,
MenuItemWidget(
2023-04-06 04:42:07 +00:00
captionedTextWidget: CaptionedTextWidget(
title: S.of(context).manageSubscription,
),
2022-10-21 13:37:57 +00:00
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
2021-07-27 14:51:45 +00:00
onTap: () async {
2023-03-25 05:52:38 +00:00
_onManageSubscriptionTapped(context);
2021-07-28 15:41:45 +00:00
},
),
sectionOptionSpacing,
MenuItemWidget(
2023-04-06 04:42:07 +00:00
captionedTextWidget: CaptionedTextWidget(
title: S.of(context).changeEmail,
),
2022-10-21 13:37:57 +00:00
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
showOnlyLoadingState: true,
2021-07-28 18:06:30 +00:00
onTap: () async {
2022-09-06 08:35:34 +00:00
final hasAuthenticated = await LocalAuthenticationService.instance
.requestLocalAuthentication(
context,
2023-04-06 04:42:07 +00:00
S.of(context).authToChangeYourEmail,
);
2022-09-06 08:35:34 +00:00
if (hasAuthenticated) {
2023-12-21 07:34:06 +00:00
// ignore: unawaited_futures
2022-09-03 03:02:42 +00:00
showDialog(
context: context,
builder: (BuildContext context) {
return const ChangeEmailDialog();
},
barrierColor: Colors.black.withOpacity(0.85),
barrierDismissible: false,
);
}
2021-07-28 18:06:30 +00:00
},
),
sectionOptionSpacing,
MenuItemWidget(
2023-04-06 04:42:07 +00:00
captionedTextWidget: CaptionedTextWidget(
title: S.of(context).changePassword,
),
2022-10-21 13:37:57 +00:00
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
showOnlyLoadingState: true,
2021-07-28 15:41:45 +00:00
onTap: () async {
2022-09-06 08:35:34 +00:00
final hasAuthenticated = await LocalAuthenticationService.instance
.requestLocalAuthentication(
context,
2023-04-06 04:42:07 +00:00
S.of(context).authToChangeYourPassword,
);
2022-09-06 08:35:34 +00:00
if (hasAuthenticated) {
2023-12-21 07:34:06 +00:00
// ignore: unawaited_futures
2022-09-03 03:02:42 +00:00
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return const PasswordEntryPage(
mode: PasswordEntryMode.update,
);
},
),
);
}
2021-07-28 15:41:45 +00:00
},
2021-07-27 14:51:45 +00:00
),
sectionOptionSpacing,
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
title: S.of(context).recoveryKey,
),
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
showOnlyLoadingState: true,
onTap: () async {
final hasAuthenticated = await LocalAuthenticationService.instance
.requestLocalAuthentication(
context,
S.of(context).authToViewYourRecoveryKey,
);
if (hasAuthenticated) {
String recoveryKey;
try {
recoveryKey = await _getOrCreateRecoveryKey(context);
} catch (e) {
await showGenericErrorDialog(context: context, error: e);
return;
}
unawaited(
routeToPage(
context,
RecoveryKeyPage(
recoveryKey,
S.of(context).ok,
showAppBar: true,
onDone: () {},
),
),
);
}
},
),
sectionOptionSpacing,
MenuItemWidget(
2023-04-06 04:42:07 +00:00
captionedTextWidget: CaptionedTextWidget(
title: S.of(context).exportYourData,
),
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
onTap: () async {
2023-12-21 07:34:06 +00:00
// ignore: unawaited_futures
launchUrlString("https://help.ente.io/photos/migration/export/");
},
),
sectionOptionSpacing,
MenuItemWidget(
2023-04-06 04:42:07 +00:00
captionedTextWidget: CaptionedTextWidget(
title: S.of(context).logout,
),
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
onTap: () async {
_onLogoutTapped(context);
},
),
sectionOptionSpacing,
MenuItemWidget(
2023-04-06 04:42:07 +00:00
captionedTextWidget: CaptionedTextWidget(
title: S.of(context).deleteAccount,
),
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
onTap: () async {
final hasAuthenticated = await LocalAuthenticationService.instance
.requestLocalAuthentication(
context,
2023-04-06 04:42:07 +00:00
S.of(context).authToInitiateAccountDeletion,
);
if (hasAuthenticated) {
unawaited(
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return const DeleteAccountPage();
},
),
),
);
}
},
),
sectionOptionSpacing,
2021-07-27 14:51:45 +00:00
],
);
}
Future<String> _getOrCreateRecoveryKey(BuildContext context) async {
return CryptoUtil.bin2hex(
await UserService.instance.getOrCreateRecoveryKey(context),
);
}
2023-01-27 13:00:32 +00:00
void _onLogoutTapped(BuildContext context) {
showChoiceActionSheet(
context,
2023-04-06 04:42:07 +00:00
title: S.of(context).areYouSureYouWantToLogout,
firstButtonLabel: S.of(context).yesLogout,
isCritical: true,
firstButtonOnTap: () async {
await UserService.instance.logout(context);
},
);
}
2023-03-25 05:52:38 +00:00
void _onManageSubscriptionTapped(BuildContext context) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return getSubscriptionPage();
},
),
);
}
}