| 
									
										
										
										
											2023-12-11 11:19:20 +08:00
										 |  |  | import 'dart:typed_data'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-08 12:10:53 +08:00
										 |  |  | import 'package:appflowy_backend/dispatch/dispatch.dart'; | 
					
						
							| 
									
										
										
										
											2023-12-11 11:19:20 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-database2/protobuf.dart'; | 
					
						
							| 
									
										
										
										
											2023-12-02 20:39:03 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart'; | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  | import 'package:appflowy_result/appflowy_result.dart'; | 
					
						
							| 
									
										
										
										
											2022-03-24 17:09:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  | /// FieldService provides many field-related interfaces event functions. Check out
 | 
					
						
							|  |  |  | /// `rust-lib/flowy-database/event_map.rs` for a list of events and their
 | 
					
						
							|  |  |  | /// implementations.
 | 
					
						
							| 
									
										
										
										
											2023-02-26 16:27:17 +08:00
										 |  |  | class FieldBackendService { | 
					
						
							| 
									
										
										
										
											2024-01-25 16:37:36 +01:00
										 |  |  |   FieldBackendService({required this.viewId, required this.fieldId}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-21 15:47:51 +08:00
										 |  |  |   final String viewId; | 
					
						
							| 
									
										
										
										
											2022-05-09 14:59:26 +08:00
										 |  |  |   final String fieldId; | 
					
						
							| 
									
										
										
										
											2022-03-24 17:09:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |   /// Create a field in a database view. The position will only be applicable
 | 
					
						
							|  |  |  |   /// in this view; for other views it will be appended to the end
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   static Future<FlowyResult<FieldPB, FlowyError>> createField({ | 
					
						
							| 
									
										
										
										
											2023-12-11 11:19:20 +08:00
										 |  |  |     required String viewId, | 
					
						
							|  |  |  |     FieldType fieldType = FieldType.RichText, | 
					
						
							|  |  |  |     String? fieldName, | 
					
						
							|  |  |  |     Uint8List? typeOptionData, | 
					
						
							|  |  |  |     OrderObjectPositionPB? position, | 
					
						
							|  |  |  |   }) { | 
					
						
							|  |  |  |     final payload = CreateFieldPayloadPB( | 
					
						
							|  |  |  |       viewId: viewId, | 
					
						
							|  |  |  |       fieldType: fieldType, | 
					
						
							|  |  |  |       fieldName: fieldName, | 
					
						
							|  |  |  |       typeOptionData: typeOptionData, | 
					
						
							|  |  |  |       fieldPosition: position, | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return DatabaseEventCreateField(payload).send(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-03-24 17:09:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |   /// Reorder a field within a database view
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   static Future<FlowyResult<void, FlowyError>> moveField({ | 
					
						
							| 
									
										
										
										
											2023-12-11 11:19:20 +08:00
										 |  |  |     required String viewId, | 
					
						
							|  |  |  |     required String fromFieldId, | 
					
						
							|  |  |  |     required String toFieldId, | 
					
						
							|  |  |  |   }) { | 
					
						
							|  |  |  |     final payload = MoveFieldPayloadPB( | 
					
						
							|  |  |  |       viewId: viewId, | 
					
						
							|  |  |  |       fromFieldId: fromFieldId, | 
					
						
							|  |  |  |       toFieldId: toFieldId, | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2022-04-15 19:56:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-31 08:28:31 +08:00
										 |  |  |     return DatabaseEventMoveField(payload).send(); | 
					
						
							| 
									
										
										
										
											2022-04-15 19:56:44 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |   /// Delete a field
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   static Future<FlowyResult<void, FlowyError>> deleteField({ | 
					
						
							| 
									
										
										
										
											2023-12-11 11:19:20 +08:00
										 |  |  |     required String viewId, | 
					
						
							|  |  |  |     required String fieldId, | 
					
						
							|  |  |  |   }) { | 
					
						
							|  |  |  |     final payload = DeleteFieldPayloadPB( | 
					
						
							|  |  |  |       viewId: viewId, | 
					
						
							|  |  |  |       fieldId: fieldId, | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return DatabaseEventDeleteField(payload).send(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 17:40:23 +01:00
										 |  |  |   // Clear all data of all cells in a Field
 | 
					
						
							|  |  |  |   static Future<FlowyResult<void, FlowyError>> clearField({ | 
					
						
							|  |  |  |     required String viewId, | 
					
						
							|  |  |  |     required String fieldId, | 
					
						
							|  |  |  |   }) { | 
					
						
							|  |  |  |     final payload = ClearFieldPayloadPB( | 
					
						
							|  |  |  |       viewId: viewId, | 
					
						
							|  |  |  |       fieldId: fieldId, | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return DatabaseEventClearField(payload).send(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |   /// Duplicate a field
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   static Future<FlowyResult<void, FlowyError>> duplicateField({ | 
					
						
							| 
									
										
										
										
											2023-12-11 11:19:20 +08:00
										 |  |  |     required String viewId, | 
					
						
							|  |  |  |     required String fieldId, | 
					
						
							|  |  |  |   }) { | 
					
						
							| 
									
										
										
										
											2024-02-27 14:42:16 +01:00
										 |  |  |     final payload = DuplicateFieldPayloadPB(viewId: viewId, fieldId: fieldId); | 
					
						
							| 
									
										
										
										
											2023-12-11 11:19:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return DatabaseEventDuplicateField(payload).send(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |   /// Update a field's properties
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   Future<FlowyResult<void, FlowyError>> updateField({ | 
					
						
							| 
									
										
										
										
											2022-03-27 09:35:10 +08:00
										 |  |  |     String? name, | 
					
						
							|  |  |  |     bool? frozen, | 
					
						
							|  |  |  |   }) { | 
					
						
							| 
									
										
										
										
											2023-06-07 13:55:37 +05:30
										 |  |  |     final payload = FieldChangesetPB.create() | 
					
						
							| 
									
										
										
										
											2023-02-26 16:27:17 +08:00
										 |  |  |       ..viewId = viewId | 
					
						
							| 
									
										
										
										
											2022-03-27 11:14:21 +08:00
										 |  |  |       ..fieldId = fieldId; | 
					
						
							| 
									
										
										
										
											2022-03-27 09:35:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (name != null) { | 
					
						
							|  |  |  |       payload.name = name; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (frozen != null) { | 
					
						
							|  |  |  |       payload.frozen = frozen; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-31 08:28:31 +08:00
										 |  |  |     return DatabaseEventUpdateField(payload).send(); | 
					
						
							| 
									
										
										
										
											2022-03-27 09:35:10 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |   /// Change a field's type
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   static Future<FlowyResult<void, FlowyError>> updateFieldType({ | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |     required String viewId, | 
					
						
							|  |  |  |     required String fieldId, | 
					
						
							|  |  |  |     required FieldType fieldType, | 
					
						
							|  |  |  |   }) { | 
					
						
							|  |  |  |     final payload = UpdateFieldTypePayloadPB() | 
					
						
							|  |  |  |       ..viewId = viewId | 
					
						
							|  |  |  |       ..fieldId = fieldId | 
					
						
							|  |  |  |       ..fieldType = fieldType; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return DatabaseEventUpdateFieldType(payload).send(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Update a field's type option data
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   static Future<FlowyResult<void, FlowyError>> updateFieldTypeOption({ | 
					
						
							| 
									
										
										
										
											2023-02-21 15:47:51 +08:00
										 |  |  |     required String viewId, | 
					
						
							| 
									
										
										
										
											2022-05-10 09:33:34 +08:00
										 |  |  |     required String fieldId, | 
					
						
							|  |  |  |     required List<int> typeOptionData, | 
					
						
							|  |  |  |   }) { | 
					
						
							| 
									
										
										
										
											2023-06-07 13:55:37 +05:30
										 |  |  |     final payload = TypeOptionChangesetPB.create() | 
					
						
							| 
									
										
										
										
											2023-02-21 15:47:51 +08:00
										 |  |  |       ..viewId = viewId | 
					
						
							| 
									
										
										
										
											2022-05-10 09:33:34 +08:00
										 |  |  |       ..fieldId = fieldId | 
					
						
							|  |  |  |       ..typeOptionData = typeOptionData; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-31 08:28:31 +08:00
										 |  |  |     return DatabaseEventUpdateFieldTypeOption(payload).send(); | 
					
						
							| 
									
										
										
										
											2022-05-10 09:33:34 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |   /// Returns the primary field of the view.
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   static Future<FlowyResult<FieldPB, FlowyError>> getPrimaryField({ | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |     required String viewId, | 
					
						
							| 
									
										
										
										
											2023-12-02 20:39:03 +08:00
										 |  |  |   }) { | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |     final payload = DatabaseViewIdPB.create()..value = viewId; | 
					
						
							|  |  |  |     return DatabaseEventGetPrimaryField(payload).send(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-12-02 20:39:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   Future<FlowyResult<FieldPB, FlowyError>> createBefore({ | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |     FieldType fieldType = FieldType.RichText, | 
					
						
							|  |  |  |     String? fieldName, | 
					
						
							|  |  |  |     Uint8List? typeOptionData, | 
					
						
							|  |  |  |   }) { | 
					
						
							|  |  |  |     return createField( | 
					
						
							|  |  |  |       viewId: viewId, | 
					
						
							|  |  |  |       fieldType: fieldType, | 
					
						
							|  |  |  |       fieldName: fieldName, | 
					
						
							|  |  |  |       typeOptionData: typeOptionData, | 
					
						
							|  |  |  |       position: OrderObjectPositionPB( | 
					
						
							|  |  |  |         position: OrderObjectPositionTypePB.Before, | 
					
						
							|  |  |  |         objectId: fieldId, | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   Future<FlowyResult<FieldPB, FlowyError>> createAfter({ | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |     FieldType fieldType = FieldType.RichText, | 
					
						
							|  |  |  |     String? fieldName, | 
					
						
							|  |  |  |     Uint8List? typeOptionData, | 
					
						
							|  |  |  |   }) { | 
					
						
							|  |  |  |     return createField( | 
					
						
							|  |  |  |       viewId: viewId, | 
					
						
							|  |  |  |       fieldType: fieldType, | 
					
						
							|  |  |  |       fieldName: fieldName, | 
					
						
							|  |  |  |       typeOptionData: typeOptionData, | 
					
						
							|  |  |  |       position: OrderObjectPositionPB( | 
					
						
							|  |  |  |         position: OrderObjectPositionTypePB.After, | 
					
						
							|  |  |  |         objectId: fieldId, | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   Future<FlowyResult<void, FlowyError>> updateType({ | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |     required FieldType fieldType, | 
					
						
							| 
									
										
										
										
											2024-01-25 16:37:36 +01:00
										 |  |  |   }) => | 
					
						
							|  |  |  |       updateFieldType( | 
					
						
							|  |  |  |         viewId: viewId, | 
					
						
							|  |  |  |         fieldId: fieldId, | 
					
						
							|  |  |  |         fieldType: fieldType, | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   Future<FlowyResult<void, FlowyError>> delete() => | 
					
						
							| 
									
										
										
										
											2024-01-25 16:37:36 +01:00
										 |  |  |       deleteField(viewId: viewId, fieldId: fieldId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   Future<FlowyResult<void, FlowyError>> duplicate() => | 
					
						
							| 
									
										
										
										
											2024-01-25 16:37:36 +01:00
										 |  |  |       duplicateField(viewId: viewId, fieldId: fieldId); | 
					
						
							| 
									
										
										
										
											2022-03-24 17:09:05 +08:00
										 |  |  | } |