| 
									
										
										
										
											2022-05-20 15:55:27 +02:00
										 |  |  | import 'dart:io'; | 
					
						
							| 
									
										
										
										
											2023-09-04 17:28:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-10 22:38:25 +08:00
										 |  |  | import 'package:appflowy/workspace/application/export/document_exporter.dart'; | 
					
						
							| 
									
										
										
										
											2023-05-17 09:49:39 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-document2/entities.pb.dart'; | 
					
						
							| 
									
										
										
										
											2023-01-08 12:10:53 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart'; | 
					
						
							| 
									
										
										
										
											2023-09-04 17:28:19 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart'; | 
					
						
							| 
									
										
										
										
											2021-11-10 14:46:59 +08:00
										 |  |  | import 'package:dartz/dartz.dart'; | 
					
						
							| 
									
										
										
										
											2023-09-04 17:28:19 +08:00
										 |  |  | import 'package:flutter_bloc/flutter_bloc.dart'; | 
					
						
							|  |  |  | import 'package:freezed_annotation/freezed_annotation.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-10 14:46:59 +08:00
										 |  |  | part 'share_bloc.freezed.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DocShareBloc extends Bloc<DocShareEvent, DocShareState> { | 
					
						
							| 
									
										
										
										
											2023-06-10 22:38:25 +08:00
										 |  |  |   DocShareBloc({ | 
					
						
							|  |  |  |     required this.view, | 
					
						
							|  |  |  |   }) : super(const DocShareState.initial()) { | 
					
						
							|  |  |  |     on<ShareMarkdown>(_onShareMarkdown); | 
					
						
							| 
									
										
										
										
											2021-11-10 14:46:59 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-11-10 14:58:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-10 22:38:25 +08:00
										 |  |  |   final ViewPB view; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<void> _onShareMarkdown( | 
					
						
							|  |  |  |     ShareMarkdown event, | 
					
						
							|  |  |  |     Emitter<DocShareState> emit, | 
					
						
							|  |  |  |   ) async { | 
					
						
							|  |  |  |     emit(const DocShareState.loading()); | 
					
						
							| 
									
										
										
										
											2022-05-20 15:55:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-10 22:38:25 +08:00
										 |  |  |     final documentExporter = DocumentExporter(view); | 
					
						
							|  |  |  |     final result = await documentExporter.export(DocumentExportType.markdown); | 
					
						
							|  |  |  |     emit( | 
					
						
							|  |  |  |       DocShareState.finish( | 
					
						
							|  |  |  |         result.fold( | 
					
						
							|  |  |  |           (error) => right(error), | 
					
						
							|  |  |  |           (markdown) => left(_saveMarkdownToPath(markdown, event.path)), | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |       ), | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2022-05-20 15:55:27 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-06-10 22:38:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   ExportDataPB _saveMarkdownToPath(String markdown, String path) { | 
					
						
							|  |  |  |     File(path).writeAsStringSync(markdown); | 
					
						
							|  |  |  |     return ExportDataPB() | 
					
						
							|  |  |  |       ..data = markdown | 
					
						
							|  |  |  |       ..exportType = ExportType.Markdown; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-11-10 14:46:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @freezed | 
					
						
							|  |  |  | class DocShareEvent with _$DocShareEvent { | 
					
						
							| 
									
										
										
										
											2022-10-23 15:05:51 +08:00
										 |  |  |   const factory DocShareEvent.shareMarkdown(String path) = ShareMarkdown; | 
					
						
							| 
									
										
										
										
											2021-11-10 14:46:59 +08:00
										 |  |  |   const factory DocShareEvent.shareText() = ShareText; | 
					
						
							|  |  |  |   const factory DocShareEvent.shareLink() = ShareLink; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @freezed | 
					
						
							|  |  |  | class DocShareState with _$DocShareState { | 
					
						
							|  |  |  |   const factory DocShareState.initial() = _Initial; | 
					
						
							|  |  |  |   const factory DocShareState.loading() = _Loading; | 
					
						
							| 
									
										
										
										
											2022-08-09 10:35:27 +08:00
										 |  |  |   const factory DocShareState.finish( | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |     Either<ExportDataPB, FlowyError> successOrFail, | 
					
						
							|  |  |  |   ) = _Finish; | 
					
						
							| 
									
										
										
										
											2021-11-10 14:46:59 +08:00
										 |  |  | } |