|
@@ -14,49 +14,73 @@ class RowDetailBloc extends Bloc<RowDetailEvent, RowDetailState> {
|
|
|
|
|
|
RowDetailBloc({
|
|
|
required this.rowController,
|
|
|
- }) : super(RowDetailState.initial()) {
|
|
|
+ }) : super(RowDetailState.initial(rowController.loadData())) {
|
|
|
on<RowDetailEvent>(
|
|
|
(event, emit) async {
|
|
|
await event.when(
|
|
|
initial: () async {
|
|
|
await _startListening();
|
|
|
- final cells = rowController.loadData();
|
|
|
- if (!isClosed) {
|
|
|
- add(RowDetailEvent.didReceiveCellDatas(cells.values.toList()));
|
|
|
- }
|
|
|
},
|
|
|
- didReceiveCellDatas: (cells) {
|
|
|
- emit(state.copyWith(cells: cells));
|
|
|
+ didReceiveCellDatas: (visibleCells, allCells, numHiddenFields) {
|
|
|
+ emit(
|
|
|
+ state.copyWith(
|
|
|
+ visibleCells: visibleCells,
|
|
|
+ allCells: allCells,
|
|
|
+ numHiddenFields: numHiddenFields,
|
|
|
+ ),
|
|
|
+ );
|
|
|
},
|
|
|
deleteField: (fieldId) {
|
|
|
- _fieldBackendService(fieldId).deleteField();
|
|
|
- },
|
|
|
- showField: (fieldId) async {
|
|
|
- final result =
|
|
|
- await FieldSettingsBackendService(viewId: rowController.viewId)
|
|
|
- .updateFieldSettings(
|
|
|
+ final fieldService = FieldBackendService(
|
|
|
+ viewId: rowController.viewId,
|
|
|
fieldId: fieldId,
|
|
|
- fieldVisibility: FieldVisibility.AlwaysShown,
|
|
|
- );
|
|
|
- result.fold(
|
|
|
- (l) {},
|
|
|
- (err) => Log.error(err),
|
|
|
);
|
|
|
+ fieldService.deleteField();
|
|
|
},
|
|
|
- hideField: (fieldId) async {
|
|
|
+ toggleFieldVisibility: (fieldId) async {
|
|
|
+ final fieldInfo = state.allCells
|
|
|
+ .where((cellContext) => cellContext.fieldId == fieldId)
|
|
|
+ .first
|
|
|
+ .fieldInfo;
|
|
|
+ final fieldVisibility =
|
|
|
+ fieldInfo.visibility == FieldVisibility.AlwaysShown
|
|
|
+ ? FieldVisibility.AlwaysHidden
|
|
|
+ : FieldVisibility.AlwaysShown;
|
|
|
final result =
|
|
|
await FieldSettingsBackendService(viewId: rowController.viewId)
|
|
|
.updateFieldSettings(
|
|
|
fieldId: fieldId,
|
|
|
- fieldVisibility: FieldVisibility.AlwaysHidden,
|
|
|
+ fieldVisibility: fieldVisibility,
|
|
|
);
|
|
|
result.fold(
|
|
|
(l) {},
|
|
|
(err) => Log.error(err),
|
|
|
);
|
|
|
},
|
|
|
- reorderField: (fieldId, fromIndex, toIndex) async {
|
|
|
- await _reorderField(fieldId, fromIndex, toIndex, emit);
|
|
|
+ reorderField:
|
|
|
+ (reorderedFieldId, targetFieldId, fromIndex, toIndex) async {
|
|
|
+ await _reorderField(
|
|
|
+ reorderedFieldId,
|
|
|
+ targetFieldId,
|
|
|
+ fromIndex,
|
|
|
+ toIndex,
|
|
|
+ emit,
|
|
|
+ );
|
|
|
+ },
|
|
|
+ toggleHiddenFieldVisibility: () {
|
|
|
+ final showHiddenFields = !state.showHiddenFields;
|
|
|
+ final visibleCells = List<DatabaseCellContext>.from(state.allCells);
|
|
|
+ visibleCells.retainWhere(
|
|
|
+ (cellContext) =>
|
|
|
+ !cellContext.fieldInfo.isPrimary &&
|
|
|
+ cellContext.isVisible(showHiddenFields: showHiddenFields),
|
|
|
+ );
|
|
|
+ emit(
|
|
|
+ state.copyWith(
|
|
|
+ showHiddenFields: showHiddenFields,
|
|
|
+ visibleCells: visibleCells,
|
|
|
+ ),
|
|
|
+ );
|
|
|
},
|
|
|
);
|
|
|
},
|
|
@@ -71,36 +95,60 @@ class RowDetailBloc extends Bloc<RowDetailEvent, RowDetailState> {
|
|
|
|
|
|
Future<void> _startListening() async {
|
|
|
rowController.addListener(
|
|
|
- onRowChanged: (cells, reason) {
|
|
|
- if (!isClosed) {
|
|
|
- add(RowDetailEvent.didReceiveCellDatas(cells.values.toList()));
|
|
|
+ onRowChanged: (cellMap, reason) {
|
|
|
+ if (isClosed) {
|
|
|
+ return;
|
|
|
}
|
|
|
- },
|
|
|
- );
|
|
|
- }
|
|
|
+ final allCells = cellMap.values.toList();
|
|
|
+ int numHiddenFields = 0;
|
|
|
+ final visibleCells = <DatabaseCellContext>[];
|
|
|
+ for (final cell in allCells) {
|
|
|
+ final isPrimary = cell.fieldInfo.isPrimary;
|
|
|
|
|
|
- FieldBackendService _fieldBackendService(String fieldId) {
|
|
|
- return FieldBackendService(
|
|
|
- viewId: rowController.viewId,
|
|
|
- fieldId: fieldId,
|
|
|
+ if (cell.isVisible(showHiddenFields: state.showHiddenFields) &&
|
|
|
+ !isPrimary) {
|
|
|
+ visibleCells.add(cell);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!cell.isVisible() && !isPrimary) {
|
|
|
+ numHiddenFields++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ add(
|
|
|
+ RowDetailEvent.didReceiveCellDatas(
|
|
|
+ visibleCells,
|
|
|
+ allCells,
|
|
|
+ numHiddenFields,
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ },
|
|
|
);
|
|
|
}
|
|
|
|
|
|
Future<void> _reorderField(
|
|
|
- String fieldId,
|
|
|
+ String reorderedFieldId,
|
|
|
+ String targetFieldId,
|
|
|
int fromIndex,
|
|
|
int toIndex,
|
|
|
Emitter<RowDetailState> emit,
|
|
|
) async {
|
|
|
- final cells = List<DatabaseCellContext>.from(state.cells);
|
|
|
+ final cells = List<DatabaseCellContext>.from(state.visibleCells);
|
|
|
cells.insert(toIndex, cells.removeAt(fromIndex));
|
|
|
- emit(state.copyWith(cells: cells));
|
|
|
+ emit(state.copyWith(visibleCells: cells));
|
|
|
+
|
|
|
+ final fromIndexInAllFields =
|
|
|
+ state.allCells.indexWhere((cell) => cell.fieldId == reorderedFieldId);
|
|
|
+ final toIndexInAllFields =
|
|
|
+ state.allCells.indexWhere((cell) => cell.fieldId == targetFieldId);
|
|
|
|
|
|
- final fieldService =
|
|
|
- FieldBackendService(viewId: rowController.viewId, fieldId: fieldId);
|
|
|
+ final fieldService = FieldBackendService(
|
|
|
+ viewId: rowController.viewId,
|
|
|
+ fieldId: reorderedFieldId,
|
|
|
+ );
|
|
|
final result = await fieldService.moveField(
|
|
|
- fromIndex,
|
|
|
- toIndex,
|
|
|
+ fromIndexInAllFields,
|
|
|
+ toIndexInAllFields,
|
|
|
);
|
|
|
result.fold((l) {}, (err) => Log.error(err));
|
|
|
}
|
|
@@ -110,25 +158,54 @@ class RowDetailBloc extends Bloc<RowDetailEvent, RowDetailState> {
|
|
|
class RowDetailEvent with _$RowDetailEvent {
|
|
|
const factory RowDetailEvent.initial() = _Initial;
|
|
|
const factory RowDetailEvent.deleteField(String fieldId) = _DeleteField;
|
|
|
- const factory RowDetailEvent.showField(String fieldId) = _ShowField;
|
|
|
- const factory RowDetailEvent.hideField(String fieldId) = _HideField;
|
|
|
+ const factory RowDetailEvent.toggleFieldVisibility(String fieldId) =
|
|
|
+ _ToggleFieldVisibility;
|
|
|
const factory RowDetailEvent.reorderField(
|
|
|
- String fieldId,
|
|
|
+ String reorderFieldID,
|
|
|
+ String targetFieldID,
|
|
|
int fromIndex,
|
|
|
int toIndex,
|
|
|
) = _ReorderField;
|
|
|
+ const factory RowDetailEvent.toggleHiddenFieldVisibility() =
|
|
|
+ _ToggleHiddenFieldVisibility;
|
|
|
const factory RowDetailEvent.didReceiveCellDatas(
|
|
|
- List<DatabaseCellContext> gridCells,
|
|
|
+ List<DatabaseCellContext> visibleCells,
|
|
|
+ List<DatabaseCellContext> allCells,
|
|
|
+ int numHiddenFields,
|
|
|
) = _DidReceiveCellDatas;
|
|
|
}
|
|
|
|
|
|
@freezed
|
|
|
class RowDetailState with _$RowDetailState {
|
|
|
const factory RowDetailState({
|
|
|
- required List<DatabaseCellContext> cells,
|
|
|
+ required List<DatabaseCellContext> visibleCells,
|
|
|
+ required List<DatabaseCellContext> allCells,
|
|
|
+ required bool showHiddenFields,
|
|
|
+ required int numHiddenFields,
|
|
|
}) = _RowDetailState;
|
|
|
|
|
|
- factory RowDetailState.initial() => RowDetailState(
|
|
|
- cells: List.empty(),
|
|
|
- );
|
|
|
+ factory RowDetailState.initial(CellContextByFieldId cellByFieldId) {
|
|
|
+ final allCells = cellByFieldId.values.toList();
|
|
|
+ int numHiddenFields = 0;
|
|
|
+ final visibleCells = <DatabaseCellContext>[];
|
|
|
+ for (final cell in allCells) {
|
|
|
+ final isVisible = cell.isVisible();
|
|
|
+ final isPrimary = cell.fieldInfo.isPrimary;
|
|
|
+
|
|
|
+ if (isVisible && !isPrimary) {
|
|
|
+ visibleCells.add(cell);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!isVisible && !isPrimary) {
|
|
|
+ numHiddenFields++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return RowDetailState(
|
|
|
+ visibleCells: visibleCells,
|
|
|
+ allCells: allCells,
|
|
|
+ showHiddenFields: false,
|
|
|
+ numHiddenFields: numHiddenFields,
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|