field_service.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import 'package:dartz/dartz.dart';
  2. import 'package:flowy_infra/notifier.dart';
  3. import 'package:flowy_sdk/dispatch/dispatch.dart';
  4. import 'package:flowy_sdk/log.dart';
  5. import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
  6. import 'package:flowy_sdk/protobuf/flowy-grid/field_entities.pb.dart';
  7. import 'package:flowy_sdk/protobuf/flowy-grid/grid_entities.pb.dart';
  8. import 'package:flutter/foundation.dart';
  9. import 'package:freezed_annotation/freezed_annotation.dart';
  10. import 'package:protobuf/protobuf.dart';
  11. part 'field_service.freezed.dart';
  12. /// FieldService consists of lots of event functions. We define the events in the backend(Rust),
  13. /// you can find the corresponding event implementation in event_map.rs of the corresponding crate.
  14. ///
  15. /// You could check out the rust-lib/flowy-grid/event_map.rs for more information.
  16. class FieldService {
  17. final String gridId;
  18. final String fieldId;
  19. FieldService({required this.gridId, required this.fieldId});
  20. Future<Either<Unit, FlowyError>> moveField(int fromIndex, int toIndex) {
  21. final payload = MoveItemPayloadPB.create()
  22. ..gridId = gridId
  23. ..itemId = fieldId
  24. ..ty = MoveItemTypePB.MoveField
  25. ..fromIndex = fromIndex
  26. ..toIndex = toIndex;
  27. return GridEventMoveItem(payload).send();
  28. }
  29. Future<Either<Unit, FlowyError>> updateField({
  30. String? name,
  31. FieldType? fieldType,
  32. bool? frozen,
  33. bool? visibility,
  34. double? width,
  35. List<int>? typeOptionData,
  36. }) {
  37. var payload = FieldChangesetPayloadPB.create()
  38. ..gridId = gridId
  39. ..fieldId = fieldId;
  40. if (name != null) {
  41. payload.name = name;
  42. }
  43. if (fieldType != null) {
  44. payload.fieldType = fieldType;
  45. }
  46. if (frozen != null) {
  47. payload.frozen = frozen;
  48. }
  49. if (visibility != null) {
  50. payload.visibility = visibility;
  51. }
  52. if (width != null) {
  53. payload.width = width.toInt();
  54. }
  55. if (typeOptionData != null) {
  56. payload.typeOptionData = typeOptionData;
  57. }
  58. return GridEventUpdateField(payload).send();
  59. }
  60. // Create the field if it does not exist. Otherwise, update the field.
  61. static Future<Either<Unit, FlowyError>> insertField({
  62. required String gridId,
  63. required FieldPB field,
  64. List<int>? typeOptionData,
  65. String? startFieldId,
  66. }) {
  67. var payload = InsertFieldPayloadPB.create()
  68. ..gridId = gridId
  69. ..field_2 = field
  70. ..typeOptionData = typeOptionData ?? [];
  71. if (startFieldId != null) {
  72. payload.startFieldId = startFieldId;
  73. }
  74. return GridEventInsertField(payload).send();
  75. }
  76. static Future<Either<Unit, FlowyError>> updateFieldTypeOption({
  77. required String gridId,
  78. required String fieldId,
  79. required List<int> typeOptionData,
  80. }) {
  81. var payload = UpdateFieldTypeOptionPayloadPB.create()
  82. ..gridId = gridId
  83. ..fieldId = fieldId
  84. ..typeOptionData = typeOptionData;
  85. return GridEventUpdateFieldTypeOption(payload).send();
  86. }
  87. Future<Either<Unit, FlowyError>> deleteField() {
  88. final payload = DeleteFieldPayloadPB.create()
  89. ..gridId = gridId
  90. ..fieldId = fieldId;
  91. return GridEventDeleteField(payload).send();
  92. }
  93. Future<Either<Unit, FlowyError>> duplicateField() {
  94. final payload = DuplicateFieldPayloadPB.create()
  95. ..gridId = gridId
  96. ..fieldId = fieldId;
  97. return GridEventDuplicateField(payload).send();
  98. }
  99. Future<Either<FieldTypeOptionDataPB, FlowyError>> getFieldTypeOptionData({
  100. required FieldType fieldType,
  101. }) {
  102. final payload = FieldTypeOptionIdPB.create()
  103. ..gridId = gridId
  104. ..fieldId = fieldId
  105. ..fieldType = fieldType;
  106. return GridEventGetFieldTypeOption(payload).send().then((result) {
  107. return result.fold(
  108. (data) => left(data),
  109. (err) => right(err),
  110. );
  111. });
  112. }
  113. }
  114. @freezed
  115. class GridFieldCellContext with _$GridFieldCellContext {
  116. const factory GridFieldCellContext({
  117. required String gridId,
  118. required FieldPB field,
  119. }) = _GridFieldCellContext;
  120. }