account_section_widget.dart 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_sodium/flutter_sodium.dart';
  4. import 'package:photos/services/local_authentication_service.dart';
  5. import 'package:photos/services/user_service.dart';
  6. import 'package:photos/theme/ente_theme.dart';
  7. import 'package:photos/ui/account/change_email_dialog.dart';
  8. import 'package:photos/ui/account/delete_account_page.dart';
  9. import 'package:photos/ui/account/password_entry_page.dart';
  10. import 'package:photos/ui/account/recovery_key_page.dart';
  11. import 'package:photos/ui/components/captioned_text_widget.dart';
  12. import 'package:photos/ui/components/expandable_menu_item_widget.dart';
  13. import 'package:photos/ui/components/menu_item_widget.dart';
  14. import 'package:photos/ui/settings/common_settings.dart';
  15. import 'package:photos/utils/dialog_util.dart';
  16. import 'package:photos/utils/navigation_util.dart';
  17. class AccountSectionWidget extends StatelessWidget {
  18. const AccountSectionWidget({Key? key}) : super(key: key);
  19. @override
  20. Widget build(BuildContext context) {
  21. return ExpandableMenuItemWidget(
  22. title: "Account",
  23. selectionOptionsWidget: _getSectionOptions(context),
  24. leadingIcon: Icons.account_circle_outlined,
  25. );
  26. }
  27. Column _getSectionOptions(BuildContext context) {
  28. return Column(
  29. children: [
  30. sectionOptionSpacing,
  31. MenuItemWidget(
  32. captionedTextWidget: const CaptionedTextWidget(
  33. title: "Recovery key",
  34. ),
  35. pressedColor: getEnteColorScheme(context).fillFaint,
  36. trailingIcon: Icons.chevron_right_outlined,
  37. trailingIconIsMuted: true,
  38. onTap: () async {
  39. final hasAuthenticated = await LocalAuthenticationService.instance
  40. .requestLocalAuthentication(
  41. context,
  42. "Please authenticate to view your recovery key",
  43. );
  44. if (hasAuthenticated) {
  45. String recoveryKey;
  46. try {
  47. recoveryKey = await _getOrCreateRecoveryKey(context);
  48. } catch (e) {
  49. await showGenericErrorDialog(context: context);
  50. return;
  51. }
  52. unawaited(
  53. routeToPage(
  54. context,
  55. RecoveryKeyPage(
  56. recoveryKey,
  57. "OK",
  58. showAppBar: true,
  59. onDone: () {},
  60. ),
  61. ),
  62. );
  63. }
  64. },
  65. ),
  66. sectionOptionSpacing,
  67. MenuItemWidget(
  68. captionedTextWidget: const CaptionedTextWidget(
  69. title: "Change email",
  70. ),
  71. pressedColor: getEnteColorScheme(context).fillFaint,
  72. trailingIcon: Icons.chevron_right_outlined,
  73. trailingIconIsMuted: true,
  74. onTap: () async {
  75. final hasAuthenticated = await LocalAuthenticationService.instance
  76. .requestLocalAuthentication(
  77. context,
  78. "Please authenticate to change your email",
  79. );
  80. if (hasAuthenticated) {
  81. showDialog(
  82. context: context,
  83. builder: (BuildContext context) {
  84. return const ChangeEmailDialog();
  85. },
  86. barrierColor: Colors.black.withOpacity(0.85),
  87. barrierDismissible: false,
  88. );
  89. }
  90. },
  91. ),
  92. sectionOptionSpacing,
  93. MenuItemWidget(
  94. captionedTextWidget: const CaptionedTextWidget(
  95. title: "Change password",
  96. ),
  97. pressedColor: getEnteColorScheme(context).fillFaint,
  98. trailingIcon: Icons.chevron_right_outlined,
  99. trailingIconIsMuted: true,
  100. onTap: () async {
  101. final hasAuthenticated = await LocalAuthenticationService.instance
  102. .requestLocalAuthentication(
  103. context,
  104. "Please authenticate to change your password",
  105. );
  106. if (hasAuthenticated) {
  107. Navigator.of(context).push(
  108. MaterialPageRoute(
  109. builder: (BuildContext context) {
  110. return const PasswordEntryPage(
  111. mode: PasswordEntryMode.update,
  112. );
  113. },
  114. ),
  115. );
  116. }
  117. },
  118. ),
  119. sectionOptionSpacing,
  120. MenuItemWidget(
  121. captionedTextWidget: const CaptionedTextWidget(
  122. title: "Logout",
  123. ),
  124. pressedColor: getEnteColorScheme(context).fillFaint,
  125. trailingIcon: Icons.chevron_right_outlined,
  126. trailingIconIsMuted: true,
  127. onTap: () {
  128. _onLogoutTapped(context);
  129. },
  130. ),
  131. sectionOptionSpacing,
  132. MenuItemWidget(
  133. captionedTextWidget: const CaptionedTextWidget(
  134. title: "Delete account",
  135. ),
  136. pressedColor: getEnteColorScheme(context).fillFaint,
  137. trailingIcon: Icons.chevron_right_outlined,
  138. trailingIconIsMuted: true,
  139. onTap: () {
  140. routeToPage(context, const DeleteAccountPage());
  141. },
  142. ),
  143. sectionOptionSpacing,
  144. ],
  145. );
  146. }
  147. Future<String> _getOrCreateRecoveryKey(BuildContext context) async {
  148. return Sodium.bin2hex(
  149. await UserService.instance.getOrCreateRecoveryKey(context),
  150. );
  151. }
  152. void _onLogoutTapped(BuildContext context) {
  153. showChoiceActionSheet(
  154. context,
  155. title: "Are you sure you want to logout?",
  156. firstButtonLabel: "Yes, logout",
  157. isCritical: true,
  158. firstButtonOnTap: () async {
  159. await UserService.instance.logout(context);
  160. },
  161. );
  162. }
  163. }