account_section_widget.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_sodium/flutter_sodium.dart';
  4. import 'package:photos/ente_theme_data.dart';
  5. import 'package:photos/services/local_authentication_service.dart';
  6. import 'package:photos/services/user_service.dart';
  7. import 'package:photos/theme/ente_theme.dart';
  8. import 'package:photos/ui/account/change_email_dialog.dart';
  9. import 'package:photos/ui/account/delete_account_page.dart';
  10. import 'package:photos/ui/account/password_entry_page.dart';
  11. import 'package:photos/ui/account/recovery_key_page.dart';
  12. import 'package:photos/ui/components/captioned_text_widget.dart';
  13. import 'package:photos/ui/components/dialog_widget.dart';
  14. import 'package:photos/ui/components/expandable_menu_item_widget.dart';
  15. import 'package:photos/ui/components/menu_item_widget.dart';
  16. import 'package:photos/ui/settings/common_settings.dart';
  17. import 'package:photos/utils/navigation_util.dart';
  18. class AccountSectionWidget extends StatelessWidget {
  19. const AccountSectionWidget({Key? key}) : super(key: key);
  20. @override
  21. Widget build(BuildContext context) {
  22. return ExpandableMenuItemWidget(
  23. title: "Account",
  24. selectionOptionsWidget: _getSectionOptions(context),
  25. leadingIcon: Icons.account_circle_outlined,
  26. );
  27. }
  28. Column _getSectionOptions(BuildContext context) {
  29. return Column(
  30. children: [
  31. sectionOptionSpacing,
  32. MenuItemWidget(
  33. captionedTextWidget: const CaptionedTextWidget(
  34. title: "Recovery key",
  35. ),
  36. pressedColor: getEnteColorScheme(context).fillFaint,
  37. trailingIcon: Icons.chevron_right_outlined,
  38. trailingIconIsMuted: 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. onTap: () async {
  76. final hasAuthenticated = await LocalAuthenticationService.instance
  77. .requestLocalAuthentication(
  78. context,
  79. "Please authenticate to change your email",
  80. );
  81. if (hasAuthenticated) {
  82. showDialog(
  83. context: context,
  84. builder: (BuildContext context) {
  85. return const ChangeEmailDialog();
  86. },
  87. barrierColor: Colors.black.withOpacity(0.85),
  88. barrierDismissible: false,
  89. );
  90. }
  91. },
  92. ),
  93. sectionOptionSpacing,
  94. MenuItemWidget(
  95. captionedTextWidget: const CaptionedTextWidget(
  96. title: "Change password",
  97. ),
  98. pressedColor: getEnteColorScheme(context).fillFaint,
  99. trailingIcon: Icons.chevron_right_outlined,
  100. trailingIconIsMuted: true,
  101. onTap: () async {
  102. final hasAuthenticated = await LocalAuthenticationService.instance
  103. .requestLocalAuthentication(
  104. context,
  105. "Please authenticate to change your password",
  106. );
  107. if (hasAuthenticated) {
  108. Navigator.of(context).push(
  109. MaterialPageRoute(
  110. builder: (BuildContext context) {
  111. return const PasswordEntryPage(
  112. mode: PasswordEntryMode.update,
  113. );
  114. },
  115. ),
  116. );
  117. }
  118. },
  119. ),
  120. sectionOptionSpacing,
  121. MenuItemWidget(
  122. captionedTextWidget: const CaptionedTextWidget(
  123. title: "Logout",
  124. ),
  125. pressedColor: getEnteColorScheme(context).fillFaint,
  126. trailingIcon: Icons.chevron_right_outlined,
  127. trailingIconIsMuted: true,
  128. onTap: () {
  129. _onLogoutTapped(context);
  130. },
  131. ),
  132. sectionOptionSpacing,
  133. MenuItemWidget(
  134. captionedTextWidget: const CaptionedTextWidget(
  135. title: "Delete account",
  136. ),
  137. pressedColor: getEnteColorScheme(context).fillFaint,
  138. trailingIcon: Icons.chevron_right_outlined,
  139. trailingIconIsMuted: true,
  140. onTap: () {
  141. routeToPage(context, const DeleteAccountPage());
  142. },
  143. ),
  144. sectionOptionSpacing,
  145. ],
  146. );
  147. }
  148. Future<String> _getOrCreateRecoveryKey(BuildContext context) async {
  149. return Sodium.bin2hex(
  150. await UserService.instance.getOrCreateRecoveryKey(context),
  151. );
  152. }
  153. Future<void> _onLogoutTapped(BuildContext context) async {
  154. final AlertDialog alert = AlertDialog(
  155. title: const Text(
  156. "Logout",
  157. style: TextStyle(
  158. color: Colors.red,
  159. ),
  160. ),
  161. content: const Text("Are you sure you want to logout?"),
  162. actions: [
  163. TextButton(
  164. child: const Text(
  165. "Yes, logout",
  166. style: TextStyle(
  167. color: Colors.red,
  168. ),
  169. ),
  170. onPressed: () async {
  171. Navigator.of(context, rootNavigator: true).pop('dialog');
  172. await UserService.instance.logout(context);
  173. },
  174. ),
  175. TextButton(
  176. child: Text(
  177. "No",
  178. style: TextStyle(
  179. color: Theme.of(context).colorScheme.greenAlternative,
  180. ),
  181. ),
  182. onPressed: () {
  183. Navigator.of(context, rootNavigator: true).pop('dialog');
  184. },
  185. ),
  186. ],
  187. );
  188. await showDialog(
  189. context: context,
  190. builder: (BuildContext context) {
  191. return alert;
  192. },
  193. );
  194. }
  195. }