| 1 | import 'dart:io'; |
| 2 | import 'package:cake_wallet/src/screens/base_page.dart'; |
| 3 | import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart'; |
| 4 | import 'package:cake_wallet/src/widgets/primary_button.dart'; |
| 5 | import 'package:cake_wallet/generated/i18n.dart'; |
| 6 | import 'package:cake_wallet/utils/exception_handler.dart'; |
| 7 | import 'package:cake_wallet/utils/share_util.dart'; |
| 8 | import 'package:cake_wallet/utils/show_pop_up.dart'; |
| 9 | import 'package:cake_wallet/view_model/settings/mweb_settings_view_model.dart'; |
| 10 | import 'package:cw_core/root_dir.dart'; |
| 11 | import 'package:file_picker/file_picker.dart'; |
| 12 | import 'package:flutter/material.dart'; |
| 13 | import 'package:path_provider/path_provider.dart'; |
| 14 | |
| 15 | class MwebLogsPage extends BasePage { |
| 16 | MwebLogsPage(this.mwebSettingsViewModelBase); |
| 17 | |
| 18 | final MwebSettingsViewModelBase mwebSettingsViewModelBase; |
| 19 | |
| 20 | @override |
| 21 | String get title => S.current.litecoin_mweb_logs; |
| 22 | |
| 23 | @override |
| 24 | Widget body(BuildContext context) { |
| 25 | return Stack( |
| 26 | fit: StackFit.expand, |
| 27 | children: [ |
| 28 | FutureBuilder<String>( |
| 29 | future: mwebSettingsViewModelBase.getAbbreviatedLogs(), |
| 30 | builder: (context, snapshot) { |
| 31 | if (snapshot.connectionState == ConnectionState.waiting) { |
| 32 | return Center(child: CircularProgressIndicator()); |
| 33 | } else if (snapshot.hasError || !snapshot.hasData || snapshot.data!.isEmpty) { |
| 34 | return Center(child: Text('No logs found')); |
| 35 | } else { |
| 36 | return SingleChildScrollView( |
| 37 | child: Padding( |
| 38 | padding: EdgeInsets.all(16.0), |
| 39 | child: Text( |
| 40 | snapshot.data!, |
| 41 | style: |
| 42 | Theme.of(context).textTheme.bodyMedium!.copyWith(fontFamily: 'Monospace'), |
| 43 | ), |
| 44 | ), |
| 45 | ); |
| 46 | } |
| 47 | }, |
| 48 | ), |
| 49 | Positioned( |
| 50 | child: LoadingPrimaryButton( |
| 51 | onPressed: () => onExportLogs(context), |
| 52 | text: S.of(context).export_logs, |
| 53 | color: Theme.of(context).colorScheme.primary, |
| 54 | textColor: Theme.of(context).colorScheme.onPrimary, |
| 55 | ), |
| 56 | bottom: 24, |
| 57 | left: 24, |
| 58 | right: 24, |
| 59 | ) |
| 60 | ], |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | void onExportLogs(BuildContext context) { |
| 65 | if (Platform.isAndroid) { |
| 66 | onExportAndroid(context); |
| 67 | } else if (Platform.isIOS) { |
| 68 | share(context); |
| 69 | } else { |
| 70 | _saveFile(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void onExportAndroid(BuildContext context) { |
| 75 | showPopUp<void>( |
| 76 | context: context, |
| 77 | builder: (dialogContext) { |
| 78 | return AlertWithTwoActions( |
| 79 | alertTitle: S.of(context).export_backup, |
| 80 | alertContent: S.of(context).select_destination, |
| 81 | rightButtonText: S.of(context).save, |
| 82 | leftButtonText: S.of(context).cancel, |
| 83 | actionLeftButton: () async { |
| 84 | Navigator.of(dialogContext).pop(); |
| 85 | }, |
| 86 | actionRightButton: () async { |
| 87 | Navigator.of(dialogContext).pop(); |
| 88 | try { |
| 89 | await share(context); |
| 90 | } catch (e, s) { |
| 91 | ExceptionHandler.onError(FlutterErrorDetails( |
| 92 | exception: e, |
| 93 | stack: s, |
| 94 | library: "Export Logs", |
| 95 | )); |
| 96 | } |
| 97 | }); |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | Future<void> share(BuildContext context) async { |
| 102 | final inAppPath = "${(await getApplicationSupportDirectory()).path}/logs/debug.log"; |
| 103 | await ShareUtil.shareFile(filePath: inAppPath, fileName: "debug.log", context: context); |
| 104 | } |
| 105 | |
| 106 | Future<void> _saveFile() async { |
| 107 | String? outputFile = await FilePicker.platform.saveFile( |
| 108 | dialogTitle: 'Save Your File to desired location', |
| 109 | fileName: "debug.log", |
| 110 | lockParentWindow: true); |
| 111 | |
| 112 | if (outputFile == null) return; |
| 113 | |
| 114 | try { |
| 115 | final filePath = (await getApplicationSupportDirectory()).path + "/debug.log"; |
| 116 | File debugLogFile = File(filePath); |
| 117 | await debugLogFile.copy(outputFile); |
| 118 | } catch (exception, stackTrace) { |
| 119 | ExceptionHandler.onError(FlutterErrorDetails( |
| 120 | exception: exception, |
| 121 | stack: stackTrace, |
| 122 | library: "Export Logs", |
| 123 | )); |
| 124 | } |
| 125 | } |
| 126 | } |