account_section_widget.dart 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/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. showOnlyLoadingState: true,
  39. onTap: () async {
  40. final hasAuthenticated = await LocalAuthenticationService.instance
  41. .requestLocalAuthentication(
  42. context,
  43. "Please authenticate to view your recovery key",
  44. );
  45. if (hasAuthenticated) {
  46. String recoveryKey;
  47. try {
  48. recoveryKey = await _getOrCreateRecoveryKey(context);
  49. } catch (e) {
  50. await showGenericErrorDialog(context: context);
  51. return;
  52. }
  53. unawaited(
  54. routeToPage(
  55. context,
  56. RecoveryKeyPage(
  57. recoveryKey,
  58. "OK",
  59. showAppBar: true,
  60. onDone: () {},
  61. ),
  62. ),
  63. );
  64. }
  65. },
  66. ),
  67. sectionOptionSpacing,
  68. MenuItemWidget(
  69. captionedTextWidget: const CaptionedTextWidget(
  70. title: "Change email",
  71. ),
  72. pressedColor: getEnteColorScheme(context).fillFaint,
  73. trailingIcon: Icons.chevron_right_outlined,
  74. trailingIconIsMuted: true,
  75. showOnlyLoadingState: true,
  76. onTap: () async {
  77. final hasAuthenticated = await LocalAuthenticationService.instance
  78. .requestLocalAuthentication(
  79. context,
  80. "Please authenticate to change your email",
  81. );
  82. if (hasAuthenticated) {
  83. showDialog(
  84. context: context,
  85. builder: (BuildContext context) {
  86. return const ChangeEmailDialog();
  87. },
  88. barrierColor: Colors.black.withOpacity(0.85),
  89. barrierDismissible: false,
  90. );
  91. }
  92. },
  93. ),
  94. sectionOptionSpacing,
  95. MenuItemWidget(
  96. captionedTextWidget: const CaptionedTextWidget(
  97. title: "Change password",
  98. ),
  99. pressedColor: getEnteColorScheme(context).fillFaint,
  100. trailingIcon: Icons.chevron_right_outlined,
  101. trailingIconIsMuted: true,
  102. showOnlyLoadingState: true,
  103. onTap: () async {
  104. final hasAuthenticated = await LocalAuthenticationService.instance
  105. .requestLocalAuthentication(
  106. context,
  107. "Please authenticate to change your password",
  108. );
  109. if (hasAuthenticated) {
  110. Navigator.of(context).push(
  111. MaterialPageRoute(
  112. builder: (BuildContext context) {
  113. return const PasswordEntryPage(
  114. mode: PasswordEntryMode.update,
  115. );
  116. },
  117. ),
  118. );
  119. }
  120. },
  121. ),
  122. sectionOptionSpacing,
  123. MenuItemWidget(
  124. captionedTextWidget: const CaptionedTextWidget(
  125. title: "Logout",
  126. ),
  127. pressedColor: getEnteColorScheme(context).fillFaint,
  128. trailingIcon: Icons.chevron_right_outlined,
  129. trailingIconIsMuted: true,
  130. onTap: () async {
  131. _onLogoutTapped(context);
  132. },
  133. ),
  134. sectionOptionSpacing,
  135. MenuItemWidget(
  136. captionedTextWidget: const CaptionedTextWidget(
  137. title: "Delete account",
  138. ),
  139. pressedColor: getEnteColorScheme(context).fillFaint,
  140. trailingIcon: Icons.chevron_right_outlined,
  141. trailingIconIsMuted: true,
  142. onTap: () async {
  143. routeToPage(context, const DeleteAccountPage());
  144. },
  145. ),
  146. sectionOptionSpacing,
  147. ],
  148. );
  149. }
  150. Future<String> _getOrCreateRecoveryKey(BuildContext context) async {
  151. return Sodium.bin2hex(
  152. await UserService.instance.getOrCreateRecoveryKey(context),
  153. );
  154. }
  155. void _onLogoutTapped(BuildContext context) {
  156. showChoiceActionSheet(
  157. context,
  158. title: "Are you sure you want to logout?",
  159. firstButtonLabel: "Yes, logout",
  160. isCritical: true,
  161. firstButtonOnTap: () async {
  162. await UserService.instance.logout(context);
  163. },
  164. );
  165. }
  166. }