chore: update row state using provider

This commit is contained in:
appflowy 2022-04-02 23:48:56 +08:00
parent ca45673a9e
commit 6ac2617113
4 changed files with 48 additions and 38 deletions

View File

@ -41,12 +41,6 @@ class RowBloc extends Bloc<RowEvent, RowState> {
createRow: (_CreateRow value) { createRow: (_CreateRow value) {
rowService.createRow(); rowService.createRow();
}, },
activeRow: (_ActiveRow value) {
emit(state.copyWith(active: true));
},
disactiveRow: (_DisactiveRow value) {
emit(state.copyWith(active: false));
},
didReceiveFieldUpdate: (_DidReceiveFieldUpdate value) { didReceiveFieldUpdate: (_DidReceiveFieldUpdate value) {
emit(state.copyWith(fields: value.fields)); emit(state.copyWith(fields: value.fields));
add(const RowEvent.didUpdateCell()); add(const RowEvent.didUpdateCell());
@ -133,8 +127,6 @@ class RowBloc extends Bloc<RowEvent, RowState> {
class RowEvent with _$RowEvent { class RowEvent with _$RowEvent {
const factory RowEvent.initial() = _InitialRow; const factory RowEvent.initial() = _InitialRow;
const factory RowEvent.createRow() = _CreateRow; const factory RowEvent.createRow() = _CreateRow;
const factory RowEvent.activeRow() = _ActiveRow;
const factory RowEvent.disactiveRow() = _DisactiveRow;
const factory RowEvent.didReceiveFieldUpdate(List<Field> fields) = _DidReceiveFieldUpdate; const factory RowEvent.didReceiveFieldUpdate(List<Field> fields) = _DidReceiveFieldUpdate;
const factory RowEvent.didUpdateCell() = _DidUpdateCell; const factory RowEvent.didUpdateCell() = _DidUpdateCell;
} }
@ -143,7 +135,6 @@ class RowEvent with _$RowEvent {
class RowState with _$RowState { class RowState with _$RowState {
const factory RowState({ const factory RowState({
required String rowId, required String rowId,
required bool active,
required double rowHeight, required double rowHeight,
required List<Field> fields, required List<Field> fields,
required Future<Option<Row>> row, required Future<Option<Row>> row,
@ -152,7 +143,6 @@ class RowState with _$RowState {
factory RowState.initial(GridRowData data) => RowState( factory RowState.initial(GridRowData data) => RowState(
rowId: data.rowId, rowId: data.rowId,
active: false,
rowHeight: data.height, rowHeight: data.height,
fields: data.fields, fields: data.fields,
row: Future(() => none()), row: Future(() => none()),

View File

@ -6,6 +6,7 @@ import 'package:flowy_infra/theme.dart';
import 'package:flowy_infra_ui/style_widget/icon_button.dart'; import 'package:flowy_infra_ui/style_widget/icon_button.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:provider/provider.dart';
import 'cell_builder.dart'; import 'cell_builder.dart';
import 'cell_container.dart'; import 'cell_container.dart';
@ -19,10 +20,12 @@ class GridRowWidget extends StatefulWidget {
class _GridRowWidgetState extends State<GridRowWidget> { class _GridRowWidgetState extends State<GridRowWidget> {
late RowBloc _rowBloc; late RowBloc _rowBloc;
late RowRegionStateNotifier _rowStateNotifier;
@override @override
void initState() { void initState() {
_rowBloc = getIt<RowBloc>(param1: widget.data)..add(const RowEvent.initial()); _rowBloc = getIt<RowBloc>(param1: widget.data)..add(const RowEvent.initial());
_rowStateNotifier = RowRegionStateNotifier();
super.initState(); super.initState();
} }
@ -30,10 +33,12 @@ class _GridRowWidgetState extends State<GridRowWidget> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocProvider.value( return BlocProvider.value(
value: _rowBloc, value: _rowBloc,
child: ChangeNotifierProvider.value(
value: _rowStateNotifier,
child: MouseRegion( child: MouseRegion(
cursor: SystemMouseCursors.click, cursor: SystemMouseCursors.click,
onEnter: (p) => _rowBloc.add(const RowEvent.activeRow()), onEnter: (p) => _rowStateNotifier.onEnter = true,
onExit: (p) => _rowBloc.add(const RowEvent.disactiveRow()), onExit: (p) => _rowStateNotifier.onEnter = false,
child: BlocBuilder<RowBloc, RowState>( child: BlocBuilder<RowBloc, RowState>(
buildWhen: (p, c) => p.rowHeight != c.rowHeight, buildWhen: (p, c) => p.rowHeight != c.rowHeight,
builder: (context, state) { builder: (context, state) {
@ -51,12 +56,14 @@ class _GridRowWidgetState extends State<GridRowWidget> {
}, },
), ),
), ),
),
); );
} }
@override @override
Future<void> dispose() async { Future<void> dispose() async {
_rowBloc.close(); _rowBloc.close();
_rowStateNotifier.dispose();
super.dispose(); super.dispose();
} }
} }
@ -66,10 +73,9 @@ class _RowLeading extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocSelector<RowBloc, RowState, bool>( return Consumer<RowRegionStateNotifier>(
selector: (state) => state.active, builder: (context, state, _) {
builder: (context, isActive) { return SizedBox(width: GridSize.leadingHeaderPadding, child: state.onEnter ? _activeWidget() : null);
return SizedBox(width: GridSize.leadingHeaderPadding, child: isActive ? _activeWidget() : null);
}, },
); );
} }
@ -133,3 +139,16 @@ class _RowCells extends StatelessWidget {
); );
} }
} }
class RowRegionStateNotifier extends ChangeNotifier {
bool _onEnter = false;
set onEnter(bool value) {
if (_onEnter != value) {
_onEnter = value;
notifyListeners();
}
}
bool get onEnter => _onEnter;
}

View File

@ -233,7 +233,7 @@ impl ClientGridEditor {
} }
} }
pub async fn update_cell(&self, changeset: CellMetaChangeset) -> FlowyResult<()> { pub async fn update_cell(&self, mut changeset: CellMetaChangeset) -> FlowyResult<()> {
if let Some(cell_data) = changeset.data.as_ref() { if let Some(cell_data) = changeset.data.as_ref() {
match self.pad.read().await.get_field(&changeset.field_id) { match self.pad.read().await.get_field(&changeset.field_id) {
None => { None => {
@ -241,7 +241,8 @@ impl ClientGridEditor {
.context(format!("Can not find the field with id: {}", &changeset.field_id))); .context(format!("Can not find the field with id: {}", &changeset.field_id)));
} }
Some(field_meta) => { Some(field_meta) => {
let _ = serialize_cell_data(cell_data, field_meta)?; let cell_data = serialize_cell_data(cell_data, field_meta)?;
changeset.data = Some(cell_data);
} }
} }
} }
@ -300,7 +301,8 @@ impl ClientGridEditor {
pub async fn grid_block_snapshots(&self, block_ids: Option<Vec<String>>) -> FlowyResult<Vec<GridBlockSnapshot>> { pub async fn grid_block_snapshots(&self, block_ids: Option<Vec<String>>) -> FlowyResult<Vec<GridBlockSnapshot>> {
let block_ids = match block_ids { let block_ids = match block_ids {
None => self.pad None => self
.pad
.read() .read()
.await .await
.get_block_metas() .get_block_metas()

View File

@ -24,8 +24,7 @@ pub struct GridBlockMetaPad {
impl GridBlockMetaPad { impl GridBlockMetaPad {
pub fn from_delta(delta: GridBlockMetaDelta) -> CollaborateResult<Self> { pub fn from_delta(delta: GridBlockMetaDelta) -> CollaborateResult<Self> {
let s = delta.to_str()?; let s = delta.to_str()?;
tracing::info!("delta: {}", delta); tracing::trace!("{}", s);
tracing::info!("{}", s);
let meta_data: GridBlockMetaData = serde_json::from_str(&s).map_err(|e| { let meta_data: GridBlockMetaData = serde_json::from_str(&s).map_err(|e| {
let msg = format!("Deserialize delta to block meta failed: {}", e); let msg = format!("Deserialize delta to block meta failed: {}", e);
CollaborateError::internal().context(msg) CollaborateError::internal().context(msg)