| 
									
										
										
										
											2024-01-31 10:07:50 +08:00
										 |  |  | import 'dart:io'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-06 14:36:48 +08:00
										 |  |  | import 'package:appflowy/core/helpers/url_launcher.dart'; | 
					
						
							| 
									
										
										
										
											2024-01-31 10:07:50 +08:00
										 |  |  | import 'package:appflowy/generated/locale_keys.g.dart'; | 
					
						
							| 
									
										
										
										
											2024-09-09 17:09:54 +08:00
										 |  |  | import 'package:appflowy/workspace/presentation/widgets/dialogs.dart'; | 
					
						
							| 
									
										
										
										
											2024-01-31 10:07:50 +08:00
										 |  |  | import 'package:archive/archive_io.dart'; | 
					
						
							|  |  |  | import 'package:easy_localization/easy_localization.dart'; | 
					
						
							|  |  |  | import 'package:flutter/material.dart'; | 
					
						
							|  |  |  | import 'package:path/path.dart' as p; | 
					
						
							|  |  |  | import 'package:path_provider/path_provider.dart'; | 
					
						
							|  |  |  | import 'package:share_plus/share_plus.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Future<void> shareLogFiles(BuildContext? context) async { | 
					
						
							|  |  |  |   final dir = await getApplicationSupportDirectory(); | 
					
						
							|  |  |  |   final zipEncoder = ZipEncoder(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   final archiveLogFiles = dir | 
					
						
							|  |  |  |       .listSync(recursive: true) | 
					
						
							|  |  |  |       .where((e) => p.basename(e.path).startsWith('log.')) | 
					
						
							|  |  |  |       .map((e) { | 
					
						
							|  |  |  |     final bytes = File(e.path).readAsBytesSync(); | 
					
						
							|  |  |  |     return ArchiveFile(p.basename(e.path), bytes.length, bytes); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (archiveLogFiles.isEmpty) { | 
					
						
							|  |  |  |     if (context != null && context.mounted) { | 
					
						
							| 
									
										
										
										
											2024-09-09 17:09:54 +08:00
										 |  |  |       showToastNotification( | 
					
						
							| 
									
										
										
										
											2024-01-31 10:07:50 +08:00
										 |  |  |         context, | 
					
						
							| 
									
										
										
										
											2024-09-09 17:09:54 +08:00
										 |  |  |         message: LocaleKeys.noLogFiles.tr(), | 
					
						
							|  |  |  |         type: ToastificationType.error, | 
					
						
							| 
									
										
										
										
											2024-01-31 10:07:50 +08:00
										 |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   final archive = Archive(); | 
					
						
							|  |  |  |   for (final file in archiveLogFiles) { | 
					
						
							|  |  |  |     archive.addFile(file); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   final zip = zipEncoder.encode(archive); | 
					
						
							|  |  |  |   if (zip == null) { | 
					
						
							| 
									
										
										
										
											2024-09-09 17:09:54 +08:00
										 |  |  |     if (context != null && context.mounted) { | 
					
						
							|  |  |  |       showToastNotification( | 
					
						
							|  |  |  |         context, | 
					
						
							|  |  |  |         message: LocaleKeys.noLogFiles.tr(), | 
					
						
							|  |  |  |         type: ToastificationType.error, | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-01-31 10:07:50 +08:00
										 |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // create a zipped appflowy logs file
 | 
					
						
							| 
									
										
										
										
											2024-09-09 17:09:54 +08:00
										 |  |  |   try { | 
					
						
							|  |  |  |     final tempDirectory = await getTemporaryDirectory(); | 
					
						
							|  |  |  |     final path = Platform.isAndroid ? tempDirectory.path : dir.path; | 
					
						
							|  |  |  |     final zipFile = | 
					
						
							|  |  |  |         await File(p.join(path, 'appflowy_logs.zip')).writeAsBytes(zip); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (Platform.isIOS) { | 
					
						
							|  |  |  |       await Share.shareUri(zipFile.uri); | 
					
						
							| 
									
										
										
										
											2024-09-18 18:03:03 +08:00
										 |  |  |       // delete the zipped appflowy logs file
 | 
					
						
							|  |  |  |       await zipFile.delete(); | 
					
						
							|  |  |  |     } else if (Platform.isAndroid) { | 
					
						
							| 
									
										
										
										
											2024-09-09 17:09:54 +08:00
										 |  |  |       await Share.shareXFiles([XFile(zipFile.path)]); | 
					
						
							| 
									
										
										
										
											2024-09-18 18:03:03 +08:00
										 |  |  |       // delete the zipped appflowy logs file
 | 
					
						
							|  |  |  |       await zipFile.delete(); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       // open the directory
 | 
					
						
							| 
									
										
										
										
											2024-12-06 14:36:48 +08:00
										 |  |  |       await afLaunchUri(zipFile.uri); | 
					
						
							| 
									
										
										
										
											2024-09-09 17:09:54 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							|  |  |  |     if (context != null && context.mounted) { | 
					
						
							|  |  |  |       showToastNotification( | 
					
						
							|  |  |  |         context, | 
					
						
							|  |  |  |         message: e.toString(), | 
					
						
							|  |  |  |         type: ToastificationType.error, | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-01-31 10:07:50 +08:00
										 |  |  | } |