field_service.dart 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 GridFieldPB 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 = GridFieldTypeOptionIdPB.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 GridFieldPB field,
  119. }) = _GridFieldCellContext;
  120. }
  121. abstract class IFieldTypeOptionLoader {
  122. String get gridId;
  123. Future<Either<FieldTypeOptionDataPB, FlowyError>> load();
  124. Future<Either<FieldTypeOptionDataPB, FlowyError>> switchToField(String fieldId, FieldType fieldType) {
  125. final payload = EditFieldPayloadPB.create()
  126. ..gridId = gridId
  127. ..fieldId = fieldId
  128. ..fieldType = fieldType;
  129. return GridEventSwitchToField(payload).send();
  130. }
  131. }
  132. class NewFieldTypeOptionLoader extends IFieldTypeOptionLoader {
  133. @override
  134. final String gridId;
  135. NewFieldTypeOptionLoader({
  136. required this.gridId,
  137. });
  138. @override
  139. Future<Either<FieldTypeOptionDataPB, FlowyError>> load() {
  140. final payload = CreateFieldPayloadPB.create()
  141. ..gridId = gridId
  142. ..fieldType = FieldType.RichText;
  143. return GridEventCreateFieldTypeOption(payload).send();
  144. }
  145. }
  146. class FieldTypeOptionLoader extends IFieldTypeOptionLoader {
  147. @override
  148. final String gridId;
  149. final GridFieldPB field;
  150. FieldTypeOptionLoader({
  151. required this.gridId,
  152. required this.field,
  153. });
  154. @override
  155. Future<Either<FieldTypeOptionDataPB, FlowyError>> load() {
  156. final payload = GridFieldTypeOptionIdPB.create()
  157. ..gridId = gridId
  158. ..fieldId = field.id
  159. ..fieldType = field.fieldType;
  160. return GridEventGetFieldTypeOption(payload).send();
  161. }
  162. }
  163. class TypeOptionDataController {
  164. final String gridId;
  165. final IFieldTypeOptionLoader _loader;
  166. late FieldTypeOptionDataPB _data;
  167. final PublishNotifier<GridFieldPB> _fieldNotifier = PublishNotifier();
  168. TypeOptionDataController({
  169. required this.gridId,
  170. required IFieldTypeOptionLoader loader,
  171. }) : _loader = loader;
  172. Future<Either<Unit, FlowyError>> loadData() async {
  173. final result = await _loader.load();
  174. return result.fold(
  175. (data) {
  176. data.freeze();
  177. _data = data;
  178. _fieldNotifier.value = data.field_2;
  179. return left(unit);
  180. },
  181. (err) {
  182. Log.error(err);
  183. return right(err);
  184. },
  185. );
  186. }
  187. GridFieldPB get field => _data.field_2;
  188. set field(GridFieldPB field) {
  189. _updateData(newField: field);
  190. }
  191. List<int> get typeOptionData => _data.typeOptionData;
  192. set fieldName(String name) {
  193. _updateData(newName: name);
  194. }
  195. set typeOptionData(List<int> typeOptionData) {
  196. _updateData(newTypeOptionData: typeOptionData);
  197. }
  198. void _updateData({String? newName, GridFieldPB? newField, List<int>? newTypeOptionData}) {
  199. _data = _data.rebuild((rebuildData) {
  200. if (newName != null) {
  201. rebuildData.field_2 = rebuildData.field_2.rebuild((rebuildField) {
  202. rebuildField.name = newName;
  203. });
  204. }
  205. if (newField != null) {
  206. rebuildData.field_2 = newField;
  207. }
  208. if (newTypeOptionData != null) {
  209. rebuildData.typeOptionData = newTypeOptionData;
  210. }
  211. });
  212. _fieldNotifier.value = _data.field_2;
  213. FieldService.insertField(
  214. gridId: gridId,
  215. field: field,
  216. typeOptionData: typeOptionData,
  217. );
  218. }
  219. Future<void> switchToField(FieldType newFieldType) {
  220. return _loader.switchToField(field.id, newFieldType).then((result) {
  221. return result.fold(
  222. (fieldTypeOptionData) {
  223. _updateData(
  224. newField: fieldTypeOptionData.field_2,
  225. newTypeOptionData: fieldTypeOptionData.typeOptionData,
  226. );
  227. },
  228. (err) {
  229. Log.error(err);
  230. },
  231. );
  232. });
  233. }
  234. void Function() addFieldListener(void Function(GridFieldPB) callback) {
  235. listener() {
  236. callback(field);
  237. }
  238. _fieldNotifier.addListener(listener);
  239. return listener;
  240. }
  241. void removeFieldListener(void Function() listener) {
  242. _fieldNotifier.removeListener(listener);
  243. }
  244. }