Skip to content

Commit c6edd1a

Browse files
committed
chore: update data & time format
1 parent efbac6c commit c6edd1a

File tree

14 files changed

+283
-214
lines changed

14 files changed

+283
-214
lines changed

frontend/app_flowy/lib/workspace/application/grid/cell/cell_service/context_builder.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class GridCellContextBuilder {
2727
gridCell: _gridCell,
2828
cellCache: _cellCache,
2929
cellDataLoader: DateCellDataLoader(gridCell: _gridCell),
30-
cellDataPersistence: NumberCellDataPersistence(gridCell: _gridCell),
30+
cellDataPersistence: DateCellDataPersistence(gridCell: _gridCell),
3131
);
3232
case FieldType.Number:
3333
return GridCellContext(
@@ -120,7 +120,7 @@ class _GridCellContext<T, D> extends Equatable {
120120
_onFieldChangedFn = () {
121121
_loadData();
122122
};
123-
cellCache.addListener(_cacheKey, _onFieldChangedFn!);
123+
cellCache.addFieldListener(_cacheKey, _onFieldChangedFn!);
124124
}
125125

126126
onCellChangedFn() {
@@ -172,7 +172,7 @@ class _GridCellContext<T, D> extends Equatable {
172172
_delayOperation?.cancel();
173173

174174
if (_onFieldChangedFn != null) {
175-
cellCache.removeListener(_cacheKey, _onFieldChangedFn!);
175+
cellCache.removeFieldListener(_cacheKey, _onFieldChangedFn!);
176176
_onFieldChangedFn = null;
177177
}
178178
}

frontend/app_flowy/lib/workspace/application/grid/cell/cell_service/data_cache.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class GridCellCache {
3030
final GridCellFieldDelegate fieldDelegate;
3131

3232
/// fieldId: {objectId: callback}
33-
final Map<String, Map<String, List<VoidCallback>>> _listenerByFieldId = {};
33+
final Map<String, Map<String, List<VoidCallback>>> _fieldListenerByFieldId = {};
3434

3535
/// fieldId: {cacheKey: cacheData}
3636
final Map<String, Map<String, dynamic>> _cellDataByFieldId = {};
@@ -40,7 +40,7 @@ class GridCellCache {
4040
}) {
4141
fieldDelegate.onFieldChanged((fieldId) {
4242
_cellDataByFieldId.remove(fieldId);
43-
final map = _listenerByFieldId[fieldId];
43+
final map = _fieldListenerByFieldId[fieldId];
4444
if (map != null) {
4545
for (final callbacks in map.values) {
4646
for (final callback in callbacks) {
@@ -51,24 +51,24 @@ class GridCellCache {
5151
});
5252
}
5353

54-
void addListener(GridCellCacheKey cacheKey, VoidCallback callback) {
55-
var map = _listenerByFieldId[cacheKey.fieldId];
54+
void addFieldListener(GridCellCacheKey cacheKey, VoidCallback onFieldChanged) {
55+
var map = _fieldListenerByFieldId[cacheKey.fieldId];
5656
if (map == null) {
57-
_listenerByFieldId[cacheKey.fieldId] = {};
58-
map = _listenerByFieldId[cacheKey.fieldId];
59-
map![cacheKey.objectId] = [callback];
57+
_fieldListenerByFieldId[cacheKey.fieldId] = {};
58+
map = _fieldListenerByFieldId[cacheKey.fieldId];
59+
map![cacheKey.objectId] = [onFieldChanged];
6060
} else {
6161
var objects = map[cacheKey.objectId];
6262
if (objects == null) {
63-
map[cacheKey.objectId] = [callback];
63+
map[cacheKey.objectId] = [onFieldChanged];
6464
} else {
65-
objects.add(callback);
65+
objects.add(onFieldChanged);
6666
}
6767
}
6868
}
6969

70-
void removeListener(GridCellCacheKey cacheKey, VoidCallback fn) {
71-
var callbacks = _listenerByFieldId[cacheKey.fieldId]?[cacheKey.objectId];
70+
void removeFieldListener(GridCellCacheKey cacheKey, VoidCallback fn) {
71+
var callbacks = _fieldListenerByFieldId[cacheKey.fieldId]?[cacheKey.objectId];
7272
final index = callbacks?.indexWhere((callback) => callback == fn);
7373
if (index != null && index != -1) {
7474
callbacks?.removeAt(index);

frontend/app_flowy/lib/workspace/application/grid/cell/cell_service/data_loader.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ class CellDataLoader extends _GridCellDataLoader<Cell> {
6161

6262
class DateCellDataLoader extends _GridCellDataLoader<DateCellData> {
6363
final GridCell gridCell;
64+
final GridCellDataConfig _config;
6465
DateCellDataLoader({
6566
required this.gridCell,
66-
});
67+
}) : _config = DefaultCellDataConfig(reloadOnFieldChanged: true);
6768

6869
@override
69-
GridCellDataConfig get config => DefaultCellDataConfig();
70+
GridCellDataConfig get config => _config;
7071

7172
@override
7273
Future<DateCellData?> loadData() {

frontend/app_flowy/lib/workspace/application/grid/cell/cell_service/data_persistence.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ class DateCellPersistenceData with _$DateCellPersistenceData {
3535
const factory DateCellPersistenceData({required DateTime date, String? time}) = _DateCellPersistenceData;
3636
}
3737

38-
class NumberCellDataPersistence implements _GridCellDataPersistence<DateCellPersistenceData> {
38+
class DateCellDataPersistence implements _GridCellDataPersistence<DateCellPersistenceData> {
3939
final GridCell gridCell;
40-
NumberCellDataPersistence({
40+
DateCellDataPersistence({
4141
required this.gridCell,
4242
});
4343

@@ -47,10 +47,7 @@ class NumberCellDataPersistence implements _GridCellDataPersistence<DateCellPers
4747

4848
final date = (data.date.millisecondsSinceEpoch ~/ 1000).toString();
4949
payload.date = date;
50-
51-
if (data.time != null) {
52-
payload.time = data.time!;
53-
}
50+
payload.time = data.time ?? "";
5451

5552
return GridEventUpdateDateCell(payload).send().then((result) {
5653
return result.fold(

frontend/app_flowy/lib/workspace/application/grid/cell/date_cal_bloc.dart

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:app_flowy/workspace/application/grid/field/field_service.dart';
22
import 'package:flowy_sdk/log.dart';
3-
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
3+
import 'package:flowy_sdk/protobuf/flowy-error-code/code.pb.dart';
44
import 'package:flowy_sdk/protobuf/flowy-grid/date_type_option.pb.dart';
55
import 'package:flutter_bloc/flutter_bloc.dart';
66
import 'package:freezed_annotation/freezed_annotation.dart';
@@ -25,8 +25,8 @@ class DateCalBloc extends Bloc<DateCalEvent, DateCalState> {
2525
(event, emit) async {
2626
await event.when(
2727
initial: () async => _startListening(),
28-
selectDay: (date) {
29-
_updateDateData(emit, date: date);
28+
selectDay: (date) async {
29+
await _updateDateData(emit, date: date, time: state.time);
3030
},
3131
setCalFormat: (format) {
3232
emit(state.copyWith(format: format));
@@ -44,22 +44,19 @@ class DateCalBloc extends Bloc<DateCalEvent, DateCalState> {
4444
setTimeFormat: (timeFormat) async {
4545
await _updateTypeOption(emit, timeFormat: timeFormat);
4646
},
47-
setTime: (time) {
48-
_updateDateData(emit, time: time);
47+
setTime: (time) async {
48+
await _updateDateData(emit, time: time);
4949
},
5050
);
5151
},
5252
);
5353
}
5454

55-
void _updateDateData(Emitter<DateCalState> emit, {DateTime? date, String? time}) {
56-
state.dateData.fold(
55+
Future<void> _updateDateData(Emitter<DateCalState> emit, {DateTime? date, String? time}) {
56+
final DateCellPersistenceData newDateData = state.dateData.fold(
5757
() {
5858
var newDateData = DateCellPersistenceData(date: date ?? DateTime.now());
59-
if (time != null) {
60-
newDateData = newDateData.copyWith(time: time);
61-
}
62-
emit(state.copyWith(dateData: Some(newDateData)));
59+
return newDateData.copyWith(time: time);
6360
},
6461
(dateData) {
6562
var newDateData = dateData;
@@ -70,9 +67,34 @@ class DateCalBloc extends Bloc<DateCalEvent, DateCalState> {
7067
if (newDateData.time != time) {
7168
newDateData = newDateData.copyWith(time: time);
7269
}
70+
return newDateData;
71+
},
72+
);
7373

74-
if (newDateData != dateData) {
75-
emit(state.copyWith(dateData: Some(newDateData)));
74+
return _saveDateData(emit, newDateData);
75+
}
76+
77+
Future<void> _saveDateData(Emitter<DateCalState> emit, DateCellPersistenceData newDateData) async {
78+
if (state.dateData == Some(newDateData)) {
79+
return;
80+
}
81+
82+
final result = await cellContext.saveCellData(newDateData);
83+
result.fold(
84+
() => emit(state.copyWith(
85+
dateData: Some(newDateData),
86+
timeFormatError: none(),
87+
)),
88+
(err) {
89+
switch (ErrorCode.valueOf(err.code)!) {
90+
case ErrorCode.InvalidDateTimeFormat:
91+
emit(state.copyWith(
92+
dateData: Some(newDateData),
93+
timeFormatError: Some(err.toString()),
94+
));
95+
break;
96+
default:
97+
Log.error(err);
7698
}
7799
},
78100
);
@@ -152,7 +174,7 @@ class DateCalState with _$DateCalState {
152174
required CalendarFormat format,
153175
required DateTime focusedDay,
154176
required String time,
155-
required Option<FlowyError> inputTimeError,
177+
required Option<String> timeFormatError,
156178
required Option<DateCellPersistenceData> dateData,
157179
}) = _DateCalState;
158180

@@ -175,7 +197,7 @@ class DateCalState with _$DateCalState {
175197
focusedDay: DateTime.now(),
176198
dateData: dateData,
177199
time: time,
178-
inputTimeError: none(),
200+
timeFormatError: none(),
179201
);
180202
}
181203
}

frontend/app_flowy/lib/workspace/application/grid/cell/date_cell_bloc.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class DateCellBloc extends Bloc<DateCellEvent, DateCellState> {
1616
(event, emit) async {
1717
event.when(
1818
initial: () => _startListening(),
19-
selectDate: (DateCellPersistenceData value) => cellContext.saveCellData(value),
2019
didReceiveCellUpdate: (DateCellData value) => emit(state.copyWith(data: Some(value))),
2120
didReceiveFieldUpdate: (Field value) => emit(state.copyWith(field: value)),
2221
);
@@ -48,7 +47,6 @@ class DateCellBloc extends Bloc<DateCellEvent, DateCellState> {
4847
@freezed
4948
class DateCellEvent with _$DateCellEvent {
5049
const factory DateCellEvent.initial() = _InitialCell;
51-
const factory DateCellEvent.selectDate(DateCellPersistenceData data) = _SelectDay;
5250
const factory DateCellEvent.didReceiveCellUpdate(DateCellData data) = _DidReceiveCellUpdate;
5351
const factory DateCellEvent.didReceiveFieldUpdate(Field field) = _DidReceiveFieldUpdate;
5452
}

0 commit comments

Comments
 (0)