field_service.dart 3.8 KB

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