field_service.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. }) {
  32. var payload = FieldChangesetPB.create()
  33. ..gridId = gridId
  34. ..fieldId = fieldId;
  35. if (name != null) {
  36. payload.name = name;
  37. }
  38. if (fieldType != null) {
  39. payload.fieldType = fieldType;
  40. }
  41. if (frozen != null) {
  42. payload.frozen = frozen;
  43. }
  44. if (visibility != null) {
  45. payload.visibility = visibility;
  46. }
  47. if (width != null) {
  48. payload.width = width.toInt();
  49. }
  50. return GridEventUpdateField(payload).send();
  51. }
  52. static Future<Either<Unit, FlowyError>> updateFieldTypeOption({
  53. required String gridId,
  54. required String fieldId,
  55. required List<int> typeOptionData,
  56. }) {
  57. var payload = TypeOptionChangesetPB.create()
  58. ..gridId = gridId
  59. ..fieldId = fieldId
  60. ..typeOptionData = typeOptionData;
  61. return GridEventUpdateFieldTypeOption(payload).send();
  62. }
  63. Future<Either<Unit, FlowyError>> deleteField() {
  64. final payload = DeleteFieldPayloadPB.create()
  65. ..gridId = gridId
  66. ..fieldId = fieldId;
  67. return GridEventDeleteField(payload).send();
  68. }
  69. Future<Either<Unit, FlowyError>> duplicateField() {
  70. final payload = DuplicateFieldPayloadPB.create()
  71. ..gridId = gridId
  72. ..fieldId = fieldId;
  73. return GridEventDuplicateField(payload).send();
  74. }
  75. Future<Either<TypeOptionPB, FlowyError>> getFieldTypeOptionData({
  76. required FieldType fieldType,
  77. }) {
  78. final payload = TypeOptionPathPB.create()
  79. ..gridId = gridId
  80. ..fieldId = fieldId
  81. ..fieldType = fieldType;
  82. return GridEventGetFieldTypeOption(payload).send().then((result) {
  83. return result.fold(
  84. (data) => left(data),
  85. (err) => right(err),
  86. );
  87. });
  88. }
  89. }
  90. @freezed
  91. class GridFieldCellContext with _$GridFieldCellContext {
  92. const factory GridFieldCellContext({
  93. required String gridId,
  94. required FieldPB field,
  95. }) = _GridFieldCellContext;
  96. }