field_service.dart 3.8 KB

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