export_widget.dart 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import 'dart:io';
  2. import 'package:ente_auth/core/configuration.dart';
  3. import 'package:ente_auth/l10n/l10n.dart';
  4. import 'package:ente_auth/services/local_authentication_service.dart';
  5. import 'package:ente_auth/store/code_store.dart';
  6. import 'package:ente_auth/ui/components/buttons/button_widget.dart';
  7. import 'package:ente_auth/ui/components/dialog_widget.dart';
  8. import 'package:ente_auth/ui/components/models/button_type.dart';
  9. import 'package:ente_auth/utils/dialog_util.dart';
  10. import 'package:ente_auth/utils/toast_util.dart';
  11. import 'package:flutter/cupertino.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:share_plus/share_plus.dart';
  14. Future<void> handleExportClick(BuildContext context) async {
  15. final result = await showDialogWidget(
  16. context: context,
  17. title: "Select export format",
  18. body: "Encrypted exports will be protected by a password of your choice.",
  19. buttons: [
  20. ButtonWidget(
  21. buttonType: ButtonType.primary,
  22. labelText: "Encrypted",
  23. isInAlert: true,
  24. buttonSize: ButtonSize.large,
  25. buttonAction: ButtonAction.first,
  26. onTap: () async {
  27. showShortToast(context, "Encrypted export");
  28. },
  29. // shouldShowSuccessConfirmation: true,
  30. ),
  31. const ButtonWidget(
  32. buttonType: ButtonType.secondary,
  33. labelText: "Plain text",
  34. buttonSize: ButtonSize.large,
  35. isInAlert: true,
  36. buttonAction: ButtonAction.second,
  37. ),
  38. ],
  39. );
  40. if (result?.action != null && result!.action != ButtonAction.cancel) {
  41. await _showExportWarningDialog(context);
  42. }
  43. }
  44. Future<void> _showExportWarningDialog(BuildContext context) async {
  45. await showChoiceActionSheet(
  46. context,
  47. title: context.l10n.warning,
  48. body: context.l10n.exportWarningDesc,
  49. isCritical: true,
  50. firstButtonOnTap: () async {
  51. _exportCodes(context);
  52. },
  53. secondButtonLabel: context.l10n.cancel,
  54. firstButtonLabel: context.l10n.iUnderStand,
  55. );
  56. }
  57. Future<void> _exportCodes(BuildContext context) async {
  58. final _codeFile = File(
  59. Configuration.instance.getTempDirectory() + "ente-authenticator-codes.txt",
  60. );
  61. final hasAuthenticated = await LocalAuthenticationService.instance
  62. .requestLocalAuthentication(context, context.l10n.authToExportCodes);
  63. if (!hasAuthenticated) {
  64. return;
  65. }
  66. if (_codeFile.existsSync()) {
  67. await _codeFile.delete();
  68. }
  69. final codes = await CodeStore.instance.getAllCodes();
  70. String data = "";
  71. for (final code in codes) {
  72. data += code.rawData + "\n";
  73. }
  74. _codeFile.writeAsStringSync(data);
  75. await Share.shareFiles([_codeFile.path]);
  76. Future.delayed(const Duration(seconds: 15), () async {
  77. if (_codeFile.existsSync()) {
  78. _codeFile.deleteSync();
  79. }
  80. });
  81. }