dev
dart 127 lines 4.31 KB
Raw
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/silent_payments_settings_view_model.dart';
10 import 'package:file_picker/file_picker.dart';
11 import 'package:flutter/material.dart';
12 import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
13 import 'package:path_provider/path_provider.dart';
14
15 class SilentPaymentsLogPage extends BasePage {
16 SilentPaymentsLogPage(this.silentPaymentsSettingsViewModelBase);
17
18 final SilentPaymentsSettingsViewModel silentPaymentsSettingsViewModelBase;
19
20 @override
21 String get title => S.current.silent_payments_logs;
22
23 @override
24 Widget body(BuildContext context) {
25 return Stack(
26 fit: StackFit.expand,
27 children: [
28 FutureBuilder<String>(
29 future: silentPaymentsSettingsViewModelBase.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 controller: ModalScrollController.of(context),
38 child: Padding(
39 padding: EdgeInsets.all(16.0),
40 child: Text(
41 snapshot.data!,
42 style:
43 Theme.of(context).textTheme.bodyMedium!.copyWith(fontFamily: 'Monospace'),
44 ),
45 ),
46 );
47 }
48 },
49 ),
50 Positioned(
51 child: LoadingPrimaryButton(
52 onPressed: () => onExportLogs(context),
53 text: S.of(context).export_logs,
54 color: Theme.of(context).colorScheme.primary,
55 textColor: Theme.of(context).colorScheme.onPrimary,
56 ),
57 bottom: 24,
58 left: 24,
59 right: 24,
60 )
61 ],
62 );
63 }
64
65 void onExportLogs(BuildContext context) {
66 if (Platform.isAndroid) {
67 onExportAndroid(context);
68 } else if (Platform.isIOS) {
69 share(context);
70 } else {
71 _saveFile();
72 }
73 }
74
75 void onExportAndroid(BuildContext context) {
76 showPopUp<void>(
77 context: context,
78 builder: (dialogContext) {
79 return AlertWithTwoActions(
80 alertTitle: S.of(context).export_backup,
81 alertContent: S.of(context).select_destination,
82 rightButtonText: S.of(context).save,
83 leftButtonText: S.of(context).cancel,
84 actionLeftButton: () async {
85 Navigator.of(dialogContext).pop();
86 },
87 actionRightButton: () async {
88 Navigator.of(dialogContext).pop();
89 try {
90 await share(context);
91 } catch (e, s) {
92 ExceptionHandler.onError(FlutterErrorDetails(
93 exception: e,
94 stack: s,
95 library: "Export Logs",
96 ));
97 }
98 });
99 });
100 }
101
102 Future<void> share(BuildContext context) async {
103 final inAppPath = "${(await getApplicationSupportDirectory()).path}/logs/debug.log";
104 await ShareUtil.shareFile(filePath: inAppPath, fileName: "debug.log", context: context);
105 }
106
107 Future<void> _saveFile() async {
108 String? outputFile = await FilePicker.platform.saveFile(
109 dialogTitle: 'Save Your File to desired location',
110 fileName: "debug.log",
111 lockParentWindow: true);
112
113 if (outputFile == null) return;
114
115 try {
116 final filePath = (await getApplicationSupportDirectory()).path + "/debug.log";
117 File debugLogFile = File(filePath);
118 await debugLogFile.copy(outputFile);
119 } catch (exception, stackTrace) {
120 ExceptionHandler.onError(FlutterErrorDetails(
121 exception: exception,
122 stack: stackTrace,
123 library: "Export Logs",
124 ));
125 }
126 }
127 }