refactor: remove date cell persistence (#3095)

* refactor: remove date cell persistence

* refactor: use i64 rather than String in DateChangeset

* chore: code cleanup

* fix: tauri build

---------

Co-authored-by: nathan <nathan@appflowy.io>
This commit is contained in:
Richard Shiue
2023-09-02 11:50:16 +08:00
committed by GitHub
parent f3aaff77b9
commit b73a34ed94
18 changed files with 159 additions and 197 deletions

View File

@ -13,7 +13,7 @@ typedef NumberCellController = CellController<String, String>;
typedef SelectOptionCellController typedef SelectOptionCellController
= CellController<SelectOptionCellDataPB, String>; = CellController<SelectOptionCellDataPB, String>;
typedef ChecklistCellController = CellController<ChecklistCellDataPB, String>; typedef ChecklistCellController = CellController<ChecklistCellDataPB, String>;
typedef DateCellController = CellController<DateCellDataPB, DateCellData>; typedef DateCellController = CellController<DateCellDataPB, String>;
typedef URLCellController = CellController<URLCellDataPB, String>; typedef URLCellController = CellController<URLCellDataPB, String>;
class CellControllerBuilder { class CellControllerBuilder {
@ -54,7 +54,7 @@ class CellControllerBuilder {
cellCache: _cellCache, cellCache: _cellCache,
cellDataLoader: cellDataLoader, cellDataLoader: cellDataLoader,
cellDataPersistence: cellDataPersistence:
DateCellDataPersistence(cellContext: _cellContext), TextCellDataPersistence(cellContext: _cellContext),
); );
case FieldType.Number: case FieldType.Number:
final cellDataLoader = CellDataLoader( final cellDataLoader = CellDataLoader(

View File

@ -1,7 +1,7 @@
part of 'cell_service.dart'; part of 'cell_service.dart';
/// Save the cell data to disk /// Save the cell data to disk
/// You can extend this class to do custom operations. For example, the DateCellDataPersistence. /// You can extend this class to do custom operations.
abstract class CellDataPersistence<D> { abstract class CellDataPersistence<D> {
Future<Option<FlowyError>> save(D data); Future<Option<FlowyError>> save(D data);
} }
@ -28,51 +28,3 @@ class TextCellDataPersistence implements CellDataPersistence<String> {
}); });
} }
} }
@freezed
class DateCellData with _$DateCellData {
const factory DateCellData({
DateTime? dateTime,
String? time,
required bool includeTime,
bool? clearFlag,
}) = _DateCellData;
}
class DateCellDataPersistence implements CellDataPersistence<DateCellData> {
final DatabaseCellContext cellContext;
DateCellDataPersistence({
required this.cellContext,
});
@override
Future<Option<FlowyError>> save(DateCellData data) {
final payload = DateChangesetPB.create()
..cellId = _makeCellPath(cellContext);
if (data.dateTime != null) {
final date = (data.dateTime!.millisecondsSinceEpoch ~/ 1000).toString();
payload.date = date;
}
if (data.time != null) {
payload.time = data.time!;
}
if (data.clearFlag != null) {
payload.clearFlag = data.clearFlag!;
}
payload.includeTime = data.includeTime;
return DatabaseEventUpdateDateCell(payload).send().then((result) {
return result.fold(
(l) => none(),
(err) => Some(err),
);
});
}
}
CellIdPB _makeCellPath(DatabaseCellContext cellId) {
return CellIdPB.create()
..viewId = cellId.viewId
..fieldId = cellId.fieldId
..rowId = cellId.rowId;
}

View File

@ -0,0 +1,47 @@
import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/cell_entities.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/date_entities.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:dartz/dartz.dart';
import 'package:fixnum/fixnum.dart';
final class DateCellBackendService {
final CellIdPB cellId;
DateCellBackendService({
required String viewId,
required String fieldId,
required String rowId,
}) : cellId = CellIdPB.create()
..viewId = viewId
..fieldId = fieldId
..rowId = rowId;
Future<Either<Unit, FlowyError>> update({
DateTime? date,
String? time,
required includeTime,
}) {
final payload = DateChangesetPB.create()
..cellId = cellId
..includeTime = includeTime;
if (date != null) {
final dateTimestamp = date.millisecondsSinceEpoch ~/ 1000;
payload.date = Int64(dateTimestamp);
}
if (time != null) {
payload.time = time;
}
return DatabaseEventUpdateDateCell(payload).send();
}
Future<Either<Unit, FlowyError>> clear() {
final payload = DateChangesetPB.create()
..cellId = cellId
..clearFlag = true;
return DatabaseEventUpdateDateCell(payload).send();
}
}

View File

@ -2,7 +2,7 @@ import 'dart:async';
import 'package:appflowy/generated/locale_keys.g.dart'; import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/plugins/database_view/application/cell/cell_controller_builder.dart'; import 'package:appflowy/plugins/database_view/application/cell/cell_controller_builder.dart';
import 'package:appflowy/plugins/database_view/application/cell/cell_service.dart'; import 'package:appflowy/plugins/database_view/application/cell/date_cell_service.dart';
import 'package:appflowy/plugins/database_view/application/field/field_service.dart'; import 'package:appflowy/plugins/database_view/application/field/field_service.dart';
import 'package:appflowy_backend/log.dart'; import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/date_entities.pb.dart'; import 'package:appflowy_backend/protobuf/flowy-database2/date_entities.pb.dart';
@ -19,6 +19,7 @@ part 'date_cal_bloc.freezed.dart';
class DateCellCalendarBloc class DateCellCalendarBloc
extends Bloc<DateCellCalendarEvent, DateCellCalendarState> { extends Bloc<DateCellCalendarEvent, DateCellCalendarState> {
final DateCellBackendService _dateCellBackendService;
final DateCellController cellController; final DateCellController cellController;
void Function()? _onCellChangedFn; void Function()? _onCellChangedFn;
@ -26,18 +27,24 @@ class DateCellCalendarBloc
required DateTypeOptionPB dateTypeOptionPB, required DateTypeOptionPB dateTypeOptionPB,
required DateCellDataPB? cellData, required DateCellDataPB? cellData,
required this.cellController, required this.cellController,
}) : super(DateCellCalendarState.initial(dateTypeOptionPB, cellData)) { }) : _dateCellBackendService = DateCellBackendService(
viewId: cellController.viewId,
fieldId: cellController.fieldId,
rowId: cellController.rowId,
),
super(DateCellCalendarState.initial(dateTypeOptionPB, cellData)) {
on<DateCellCalendarEvent>( on<DateCellCalendarEvent>(
(event, emit) async { (event, emit) async {
await event.when( await event.when(
initial: () async => _startListening(), initial: () async => _startListening(),
didReceiveCellUpdate: (DateCellDataPB? cellData) { didReceiveCellUpdate: (DateCellDataPB? cellData) {
final dateData = _dateDataFromCellData(cellData); final (dateTime, time, includeTime) =
_dateDataFromCellData(cellData);
emit( emit(
state.copyWith( state.copyWith(
dateTime: dateData.dateTime, dateTime: dateTime,
time: dateData.time, time: time,
includeTime: dateData.includeTime, includeTime: includeTime,
), ),
); );
}, },
@ -45,13 +52,13 @@ class DateCellCalendarBloc
emit(state.copyWith(timeFormatError: timeFormatError)); emit(state.copyWith(timeFormatError: timeFormatError));
}, },
selectDay: (date) async { selectDay: (date) async {
await _updateDateData(emit, date: date); await _updateDateData(date: date);
}, },
setIncludeTime: (includeTime) async { setIncludeTime: (includeTime) async {
await _updateDateData(emit, includeTime: includeTime); await _updateDateData(includeTime: includeTime);
}, },
setTime: (time) async { setTime: (time) async {
await _updateDateData(emit, time: time); await _updateDateData(time: time);
}, },
setDateFormat: (dateFormat) async { setDateFormat: (dateFormat) async {
await _updateTypeOption(emit, dateFormat: dateFormat); await _updateTypeOption(emit, dateFormat: dateFormat);
@ -66,15 +73,14 @@ class DateCellCalendarBloc
emit(state.copyWith(focusedDay: focusedDay)); emit(state.copyWith(focusedDay: focusedDay));
}, },
clearDate: () async { clearDate: () async {
await _clearDate(emit); await _clearDate();
}, },
); );
}, },
); );
} }
Future<void> _updateDateData( Future<void> _updateDateData({
Emitter<DateCellCalendarState> emit, {
DateTime? date, DateTime? date,
String? time, String? time,
bool? includeTime, bool? includeTime,
@ -86,76 +92,60 @@ class DateCellCalendarBloc
date != null && time == null, date != null && time == null,
); );
final String? newTime = time ?? state.time; final String? newTime = time ?? state.time;
DateTime? newDate = _utcToLocalAddTime(date); DateTime? newDate = _utcToLocalAndAddCurrentTime(date);
if (time != null && time.isNotEmpty) { if (time != null && time.isNotEmpty) {
newDate = state.dateTime ?? DateTime.now(); newDate = state.dateTime ?? DateTime.now();
} }
final DateCellData newDateData = DateCellData( final result = await _dateCellBackendService.update(
dateTime: newDate, date: newDate,
time: newTime, time: newTime,
includeTime: includeTime ?? state.includeTime, includeTime: includeTime ?? state.includeTime,
); );
cellController.saveCellData( result.fold(
newDateData, (_) {
onFinish: (result) { if (!isClosed && state.timeFormatError != null) {
result.fold( add(const DateCellCalendarEvent.didReceiveTimeFormatError(null));
() { }
if (!isClosed && state.timeFormatError != null) { },
add(const DateCellCalendarEvent.didReceiveTimeFormatError(null)); (err) {
} switch (err.code) {
}, case ErrorCode.InvalidDateTimeFormat:
(err) { if (isClosed) return;
switch (err.code) { add(
case ErrorCode.InvalidDateTimeFormat: DateCellCalendarEvent.didReceiveTimeFormatError(
if (isClosed) return; timeFormatPrompt(err),
add( ),
DateCellCalendarEvent.didReceiveTimeFormatError( );
timeFormatPrompt(err), break;
), default:
); Log.error(err);
break; }
default:
Log.error(err);
}
},
);
}, },
); );
} }
Future<void> _clearDate(Emitter<DateCellCalendarState> emit) async { Future<void> _clearDate() async {
final DateCellData newDateData = DateCellData( final result = await _dateCellBackendService.clear();
dateTime: null, result.fold(
time: null, (_) {
includeTime: state.includeTime, if (!isClosed) {
clearFlag: true, add(const DateCellCalendarEvent.didReceiveTimeFormatError(null));
); }
cellController.saveCellData(
newDateData,
onFinish: (result) {
result.fold(
() {
if (!isClosed) {
add(const DateCellCalendarEvent.didReceiveTimeFormatError(null));
}
},
(err) => Log.error(err),
);
}, },
(err) => Log.error(err),
); );
} }
DateTime? _utcToLocalAddTime(DateTime? date) { DateTime? _utcToLocalAndAddCurrentTime(DateTime? date) {
if (date == null) { if (date == null) {
return null; return null;
} }
final now = DateTime.now(); final now = DateTime.now();
// the incoming date is Utc. this trick converts it into Local // the incoming date is Utc. This trick converts it into Local
// and add the current time, though the time may be overwritten by // and add the current time. The time may be overwritten by
// explicitly provided time string // explicitly provided time string in the backend though
return DateTime( return DateTime(
date.year, date.year,
date.month, date.month,
@ -285,14 +275,14 @@ class DateCellCalendarState with _$DateCellCalendarState {
DateTypeOptionPB dateTypeOptionPB, DateTypeOptionPB dateTypeOptionPB,
DateCellDataPB? cellData, DateCellDataPB? cellData,
) { ) {
final dateData = _dateDataFromCellData(cellData); final (dateTime, time, includeTime) = _dateDataFromCellData(cellData);
return DateCellCalendarState( return DateCellCalendarState(
dateTypeOptionPB: dateTypeOptionPB, dateTypeOptionPB: dateTypeOptionPB,
format: CalendarFormat.month, format: CalendarFormat.month,
focusedDay: DateTime.now(), focusedDay: DateTime.now(),
dateTime: dateData.dateTime, dateTime: dateTime,
time: dateData.time, time: time,
includeTime: dateData.includeTime, includeTime: includeTime,
timeFormatError: null, timeFormatError: null,
timeHintText: _timeHintText(dateTypeOptionPB), timeHintText: _timeHintText(dateTypeOptionPB),
); );
@ -310,11 +300,11 @@ String _timeHintText(DateTypeOptionPB typeOption) {
} }
} }
DateCellData _dateDataFromCellData(DateCellDataPB? cellData) { (DateTime?, String?, bool) _dateDataFromCellData(DateCellDataPB? cellData) {
// a null DateCellDataPB may be returned, indicating that all the fields are // a null DateCellDataPB may be returned, indicating that all the fields are
// at their default values: empty strings and false booleans // at their default values: empty strings and false booleans
if (cellData == null) { if (cellData == null) {
return const DateCellData(includeTime: false); return (null, null, false);
} }
DateTime? dateTime; DateTime? dateTime;
@ -326,5 +316,5 @@ DateCellData _dateDataFromCellData(DateCellDataPB? cellData) {
} }
final bool includeTime = cellData.includeTime; final bool includeTime = cellData.includeTime;
return DateCellData(dateTime: dateTime, time: time, includeTime: includeTime); return (dateTime, time, includeTime);
} }

View File

@ -25,7 +25,7 @@ export class DateCellDataPersistence extends CellDataPersistence<CalendarData> {
save(data: CalendarData): Promise<Result<void, FlowyError>> { save(data: CalendarData): Promise<Result<void, FlowyError>> {
const payload = DateChangesetPB.fromObject({ cell_id: _makeCellId(this.cellIdentifier) }); const payload = DateChangesetPB.fromObject({ cell_id: _makeCellId(this.cellIdentifier) });
payload.date = ((data.date.getTime() / 1000) | 0).toString(); payload.date = (data.date.getTime() / 1000) | 0;
if (data.time !== undefined) { if (data.time !== undefined) {
payload.time = data.time; payload.time = data.time;
} }

View File

@ -28,7 +28,7 @@ pub struct DateChangesetPB {
pub cell_id: CellIdPB, pub cell_id: CellIdPB,
#[pb(index = 2, one_of)] #[pb(index = 2, one_of)]
pub date: Option<String>, pub date: Option<i64>,
#[pb(index = 3, one_of)] #[pb(index = 3, one_of)]
pub time: Option<String>, pub time: Option<String>,

View File

@ -843,7 +843,7 @@ pub(crate) async fn move_calendar_event_handler(
let data = data.into_inner(); let data = data.into_inner();
let cell_id: CellIdParams = data.cell_path.try_into()?; let cell_id: CellIdParams = data.cell_path.try_into()?;
let cell_changeset = DateCellChangeset { let cell_changeset = DateCellChangeset {
date: Some(data.timestamp.to_string()), date: Some(data.timestamp),
..Default::default() ..Default::default()
}; };
let database_editor = manager.get_database_with_view_id(&cell_id.view_id).await?; let database_editor = manager.get_database_with_view_id(&cell_id.view_id).await?;

View File

@ -209,7 +209,7 @@ pub fn insert_checkbox_cell(is_check: bool, field: &Field) -> Cell {
pub fn insert_date_cell(timestamp: i64, include_time: Option<bool>, field: &Field) -> Cell { pub fn insert_date_cell(timestamp: i64, include_time: Option<bool>, field: &Field) -> Cell {
let cell_data = serde_json::to_string(&DateCellChangeset { let cell_data = serde_json::to_string(&DateCellChangeset {
date: Some(timestamp.to_string()), date: Some(timestamp),
time: None, time: None,
include_time, include_time,
clear_flag: None, clear_flag: None,

View File

@ -25,7 +25,7 @@ mod tests {
&type_option, &type_option,
&field, &field,
DateCellChangeset { DateCellChangeset {
date: Some("1647251762".to_owned()), date: Some(1647251762),
time: None, time: None,
include_time: None, include_time: None,
clear_flag: None, clear_flag: None,
@ -39,7 +39,7 @@ mod tests {
&type_option, &type_option,
&field, &field,
DateCellChangeset { DateCellChangeset {
date: Some("1647251762".to_owned()), date: Some(1647251762),
time: None, time: None,
include_time: None, include_time: None,
clear_flag: None, clear_flag: None,
@ -53,7 +53,7 @@ mod tests {
&type_option, &type_option,
&field, &field,
DateCellChangeset { DateCellChangeset {
date: Some("1647251762".to_owned()), date: Some(1647251762),
time: None, time: None,
include_time: None, include_time: None,
clear_flag: None, clear_flag: None,
@ -67,7 +67,7 @@ mod tests {
&type_option, &type_option,
&field, &field,
DateCellChangeset { DateCellChangeset {
date: Some("1647251762".to_owned()), date: Some(1647251762),
time: None, time: None,
include_time: None, include_time: None,
clear_flag: None, clear_flag: None,
@ -81,7 +81,7 @@ mod tests {
&type_option, &type_option,
&field, &field,
DateCellChangeset { DateCellChangeset {
date: Some("1647251762".to_owned()), date: Some(1647251762),
time: None, time: None,
include_time: None, include_time: None,
clear_flag: None, clear_flag: None,
@ -107,7 +107,7 @@ mod tests {
&type_option, &type_option,
&field, &field,
DateCellChangeset { DateCellChangeset {
date: Some("1653609600".to_owned()), date: Some(1653609600),
time: None, time: None,
include_time: Some(true), include_time: Some(true),
clear_flag: None, clear_flag: None,
@ -119,7 +119,7 @@ mod tests {
&type_option, &type_option,
&field, &field,
DateCellChangeset { DateCellChangeset {
date: Some("1653609600".to_owned()), date: Some(1653609600),
time: Some("9:00".to_owned()), time: Some("9:00".to_owned()),
include_time: Some(true), include_time: Some(true),
clear_flag: None, clear_flag: None,
@ -131,7 +131,7 @@ mod tests {
&type_option, &type_option,
&field, &field,
DateCellChangeset { DateCellChangeset {
date: Some("1653609600".to_owned()), date: Some(1653609600),
time: Some("23:00".to_owned()), time: Some("23:00".to_owned()),
include_time: Some(true), include_time: Some(true),
clear_flag: None, clear_flag: None,
@ -145,7 +145,7 @@ mod tests {
&type_option, &type_option,
&field, &field,
DateCellChangeset { DateCellChangeset {
date: Some("1653609600".to_owned()), date: Some(1653609600),
time: None, time: None,
include_time: Some(true), include_time: Some(true),
clear_flag: None, clear_flag: None,
@ -157,7 +157,7 @@ mod tests {
&type_option, &type_option,
&field, &field,
DateCellChangeset { DateCellChangeset {
date: Some("1653609600".to_owned()), date: Some(1653609600),
time: Some("9:00 AM".to_owned()), time: Some("9:00 AM".to_owned()),
include_time: Some(true), include_time: Some(true),
clear_flag: None, clear_flag: None,
@ -169,7 +169,7 @@ mod tests {
&type_option, &type_option,
&field, &field,
DateCellChangeset { DateCellChangeset {
date: Some("1653609600".to_owned()), date: Some(1653609600),
time: Some("11:23 pm".to_owned()), time: Some("11:23 pm".to_owned()),
include_time: Some(true), include_time: Some(true),
clear_flag: None, clear_flag: None,
@ -182,25 +182,6 @@ mod tests {
} }
} }
#[test]
fn date_type_option_invalid_date_str_test() {
let field_type = FieldType::DateTime;
let type_option = DateTypeOption::test();
let field = FieldBuilder::from_field_type(field_type).build();
assert_date(
&type_option,
&field,
DateCellChangeset {
date: Some("abc".to_owned()),
time: None,
include_time: None,
clear_flag: None,
},
None,
"",
);
}
#[test] #[test]
#[should_panic] #[should_panic]
fn date_type_option_invalid_include_time_str_test() { fn date_type_option_invalid_include_time_str_test() {
@ -212,7 +193,7 @@ mod tests {
&type_option, &type_option,
&field, &field,
DateCellChangeset { DateCellChangeset {
date: Some("1653609600".to_owned()), date: Some(1653609600),
time: Some("1:".to_owned()), time: Some("1:".to_owned()),
include_time: Some(true), include_time: Some(true),
clear_flag: None, clear_flag: None,
@ -233,7 +214,7 @@ mod tests {
&type_option, &type_option,
&field, &field,
DateCellChangeset { DateCellChangeset {
date: Some("1653609600".to_owned()), date: Some(1653609600),
time: Some("".to_owned()), time: Some("".to_owned()),
include_time: Some(true), include_time: Some(true),
clear_flag: None, clear_flag: None,
@ -252,7 +233,7 @@ mod tests {
&type_option, &type_option,
&field, &field,
DateCellChangeset { DateCellChangeset {
date: Some("1653609600".to_owned()), date: Some(1653609600),
time: Some("00:00".to_owned()), time: Some("00:00".to_owned()),
include_time: Some(true), include_time: Some(true),
clear_flag: None, clear_flag: None,
@ -273,7 +254,7 @@ mod tests {
&type_option, &type_option,
&field, &field,
DateCellChangeset { DateCellChangeset {
date: Some("1653609600".to_owned()), date: Some(1653609600),
time: Some("1:00 am".to_owned()), time: Some("1:00 am".to_owned()),
include_time: Some(true), include_time: Some(true),
clear_flag: None, clear_flag: None,
@ -297,7 +278,7 @@ mod tests {
&type_option, &type_option,
&field, &field,
DateCellChangeset { DateCellChangeset {
date: Some("1653609600".to_owned()), date: Some(1653609600),
time: Some("20:00".to_owned()), time: Some("20:00".to_owned()),
include_time: Some(true), include_time: Some(true),
clear_flag: None, clear_flag: None,
@ -345,7 +326,7 @@ mod tests {
let old_cell_data = initialize_date_cell( let old_cell_data = initialize_date_cell(
&type_option, &type_option,
DateCellChangeset { DateCellChangeset {
date: Some("1700006400".to_owned()), date: Some(1700006400),
time: Some("08:00".to_owned()), time: Some("08:00".to_owned()),
include_time: Some(true), include_time: Some(true),
clear_flag: None, clear_flag: None,
@ -355,7 +336,7 @@ mod tests {
&type_option, &type_option,
&field, &field,
DateCellChangeset { DateCellChangeset {
date: Some("1701302400".to_owned()), date: Some(1701302400),
time: None, time: None,
include_time: None, include_time: None,
clear_flag: None, clear_flag: None,
@ -373,7 +354,7 @@ mod tests {
let old_cell_data = initialize_date_cell( let old_cell_data = initialize_date_cell(
&type_option, &type_option,
DateCellChangeset { DateCellChangeset {
date: Some("1700006400".to_owned()), date: Some(1700006400),
time: Some("08:00".to_owned()), time: Some("08:00".to_owned()),
include_time: Some(true), include_time: Some(true),
clear_flag: None, clear_flag: None,
@ -401,7 +382,7 @@ mod tests {
let old_cell_data = initialize_date_cell( let old_cell_data = initialize_date_cell(
&type_option, &type_option,
DateCellChangeset { DateCellChangeset {
date: Some("1700006400".to_owned()), date: Some(1700006400),
time: Some("08:00".to_owned()), time: Some("08:00".to_owned()),
include_time: Some(true), include_time: Some(true),
clear_flag: None, clear_flag: None,

View File

@ -256,7 +256,7 @@ impl CellDataChangeset for DateTypeOption {
// order to change the day without changing the time, the old time string // order to change the day without changing the time, the old time string
// should be passed in as well. // should be passed in as well.
let changeset_timestamp = changeset.date_timestamp(); let changeset_timestamp = changeset.date;
// parse the time string, which is in the local timezone // parse the time string, which is in the local timezone
let parsed_time = match (include_time, changeset.time) { let parsed_time = match (include_time, changeset.time) {

View File

@ -18,18 +18,12 @@ use crate::services::field::{TypeOptionCellData, CELL_DATA};
#[derive(Clone, Debug, Default, Serialize, Deserialize)] #[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct DateCellChangeset { pub struct DateCellChangeset {
pub date: Option<String>, pub date: Option<i64>,
pub time: Option<String>, pub time: Option<String>,
pub include_time: Option<bool>, pub include_time: Option<bool>,
pub clear_flag: Option<bool>, pub clear_flag: Option<bool>,
} }
impl DateCellChangeset {
pub fn date_timestamp(&self) -> Option<i64> {
self.date.as_ref().and_then(|date| date.parse::<i64>().ok())
}
}
impl FromCellChangeset for DateCellChangeset { impl FromCellChangeset for DateCellChangeset {
fn from_changeset(changeset: String) -> FlowyResult<Self> fn from_changeset(changeset: String) -> FlowyResult<Self>
where where

View File

@ -26,7 +26,7 @@ async fn grid_cell_update() {
FieldType::RichText => "".to_string(), FieldType::RichText => "".to_string(),
FieldType::Number => "123".to_string(), FieldType::Number => "123".to_string(),
FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => { FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => {
make_date_cell_string("123") make_date_cell_string(123)
}, },
FieldType::SingleSelect => { FieldType::SingleSelect => {
let type_option = field let type_option = field

View File

@ -322,13 +322,13 @@ impl<'a> TestRowBuilder<'a> {
pub fn insert_date_cell( pub fn insert_date_cell(
&mut self, &mut self,
data: &str, data: i64,
time: Option<String>, time: Option<String>,
include_time: Option<bool>, include_time: Option<bool>,
field_type: &FieldType, field_type: &FieldType,
) -> String { ) -> String {
let value = serde_json::to_string(&DateCellChangeset { let value = serde_json::to_string(&DateCellChangeset {
date: Some(data.to_string()), date: Some(data),
time, time,
include_time, include_time,
clear_flag: None, clear_flag: None,

View File

@ -78,9 +78,9 @@ pub fn create_date_field(grid_id: &str, field_type: FieldType) -> (CreateFieldPa
// The grid will contains all existing field types and there are three empty rows in this grid. // The grid will contains all existing field types and there are three empty rows in this grid.
pub fn make_date_cell_string(s: &str) -> String { pub fn make_date_cell_string(timestamp: i64) -> String {
serde_json::to_string(&DateCellChangeset { serde_json::to_string(&DateCellChangeset {
date: Some(s.to_string()), date: Some(timestamp),
time: None, time: None,
include_time: Some(false), include_time: Some(false),
clear_flag: None, clear_flag: None,

View File

@ -129,7 +129,7 @@ pub fn make_test_board() -> DatabaseData {
FieldType::Number => row_builder.insert_number_cell("1"), FieldType::Number => row_builder.insert_number_cell("1"),
// 1647251762 => Mar 14,2022 // 1647251762 => Mar 14,2022
FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => { FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => {
row_builder.insert_date_cell("1647251762", None, None, &field_type) row_builder.insert_date_cell(1647251762, None, None, &field_type)
}, },
FieldType::SingleSelect => { FieldType::SingleSelect => {
row_builder.insert_single_select_cell(|mut options| options.remove(0)) row_builder.insert_single_select_cell(|mut options| options.remove(0))
@ -149,7 +149,7 @@ pub fn make_test_board() -> DatabaseData {
FieldType::Number => row_builder.insert_number_cell("2"), FieldType::Number => row_builder.insert_number_cell("2"),
// 1647251762 => Mar 14,2022 // 1647251762 => Mar 14,2022
FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => { FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => {
row_builder.insert_date_cell("1647251762", None, None, &field_type) row_builder.insert_date_cell(1647251762, None, None, &field_type)
}, },
FieldType::SingleSelect => { FieldType::SingleSelect => {
row_builder.insert_single_select_cell(|mut options| options.remove(0)) row_builder.insert_single_select_cell(|mut options| options.remove(0))
@ -168,7 +168,7 @@ pub fn make_test_board() -> DatabaseData {
FieldType::Number => row_builder.insert_number_cell("3"), FieldType::Number => row_builder.insert_number_cell("3"),
// 1647251762 => Mar 14,2022 // 1647251762 => Mar 14,2022
FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => { FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => {
row_builder.insert_date_cell("1647251762", None, None, &field_type) row_builder.insert_date_cell(1647251762, None, None, &field_type)
}, },
FieldType::SingleSelect => { FieldType::SingleSelect => {
row_builder.insert_single_select_cell(|mut options| options.remove(1)) row_builder.insert_single_select_cell(|mut options| options.remove(1))
@ -190,7 +190,7 @@ pub fn make_test_board() -> DatabaseData {
FieldType::RichText => row_builder.insert_text_cell("DA"), FieldType::RichText => row_builder.insert_text_cell("DA"),
FieldType::Number => row_builder.insert_number_cell("4"), FieldType::Number => row_builder.insert_number_cell("4"),
FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => { FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => {
row_builder.insert_date_cell("1668704685", None, None, &field_type) row_builder.insert_date_cell(1668704685, None, None, &field_type)
}, },
FieldType::SingleSelect => { FieldType::SingleSelect => {
row_builder.insert_single_select_cell(|mut options| options.remove(1)) row_builder.insert_single_select_cell(|mut options| options.remove(1))
@ -207,7 +207,7 @@ pub fn make_test_board() -> DatabaseData {
FieldType::RichText => row_builder.insert_text_cell("AE"), FieldType::RichText => row_builder.insert_text_cell("AE"),
FieldType::Number => row_builder.insert_number_cell(""), FieldType::Number => row_builder.insert_number_cell(""),
FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => { FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => {
row_builder.insert_date_cell("1668359085", None, None, &field_type) row_builder.insert_date_cell(1668359085, None, None, &field_type)
}, },
FieldType::SingleSelect => { FieldType::SingleSelect => {
row_builder.insert_single_select_cell(|mut options| options.remove(2)) row_builder.insert_single_select_cell(|mut options| options.remove(2))

View File

@ -51,7 +51,7 @@ pub fn make_test_calendar() -> DatabaseData {
match field_type { match field_type {
FieldType::RichText => row_builder.insert_text_cell("A"), FieldType::RichText => row_builder.insert_text_cell("A"),
FieldType::DateTime => { FieldType::DateTime => {
row_builder.insert_date_cell("1678090778", None, None, &field_type) row_builder.insert_date_cell(1678090778, None, None, &field_type)
}, },
_ => "".to_owned(), _ => "".to_owned(),
}; };
@ -62,7 +62,7 @@ pub fn make_test_calendar() -> DatabaseData {
match field_type { match field_type {
FieldType::RichText => row_builder.insert_text_cell("B"), FieldType::RichText => row_builder.insert_text_cell("B"),
FieldType::DateTime => { FieldType::DateTime => {
row_builder.insert_date_cell("1677917978", None, None, &field_type) row_builder.insert_date_cell(1677917978, None, None, &field_type)
}, },
_ => "".to_owned(), _ => "".to_owned(),
}; };
@ -73,7 +73,7 @@ pub fn make_test_calendar() -> DatabaseData {
match field_type { match field_type {
FieldType::RichText => row_builder.insert_text_cell("C"), FieldType::RichText => row_builder.insert_text_cell("C"),
FieldType::DateTime => { FieldType::DateTime => {
row_builder.insert_date_cell("1679213978", None, None, &field_type) row_builder.insert_date_cell(1679213978, None, None, &field_type)
}, },
_ => "".to_owned(), _ => "".to_owned(),
}; };
@ -84,7 +84,7 @@ pub fn make_test_calendar() -> DatabaseData {
match field_type { match field_type {
FieldType::RichText => row_builder.insert_text_cell("D"), FieldType::RichText => row_builder.insert_text_cell("D"),
FieldType::DateTime => { FieldType::DateTime => {
row_builder.insert_date_cell("1678695578", None, None, &field_type) row_builder.insert_date_cell(1678695578, None, None, &field_type)
}, },
_ => "".to_owned(), _ => "".to_owned(),
}; };
@ -95,7 +95,7 @@ pub fn make_test_calendar() -> DatabaseData {
match field_type { match field_type {
FieldType::RichText => row_builder.insert_text_cell("E"), FieldType::RichText => row_builder.insert_text_cell("E"),
FieldType::DateTime => { FieldType::DateTime => {
row_builder.insert_date_cell("1678695578", None, None, &field_type) row_builder.insert_date_cell(1678695578, None, None, &field_type)
}, },
_ => "".to_owned(), _ => "".to_owned(),
}; };

View File

@ -130,7 +130,7 @@ pub fn make_test_grid() -> DatabaseData {
FieldType::RichText => row_builder.insert_text_cell("A"), FieldType::RichText => row_builder.insert_text_cell("A"),
FieldType::Number => row_builder.insert_number_cell("1"), FieldType::Number => row_builder.insert_number_cell("1"),
FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => { FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => {
row_builder.insert_date_cell("1647251762", None, None, &field_type) row_builder.insert_date_cell(1647251762, None, None, &field_type)
}, },
FieldType::MultiSelect => row_builder FieldType::MultiSelect => row_builder
.insert_multi_select_cell(|mut options| vec![options.remove(0), options.remove(0)]), .insert_multi_select_cell(|mut options| vec![options.remove(0), options.remove(0)]),
@ -151,7 +151,7 @@ pub fn make_test_grid() -> DatabaseData {
FieldType::RichText => row_builder.insert_text_cell(""), FieldType::RichText => row_builder.insert_text_cell(""),
FieldType::Number => row_builder.insert_number_cell("2"), FieldType::Number => row_builder.insert_number_cell("2"),
FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => { FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => {
row_builder.insert_date_cell("1647251762", None, None, &field_type) row_builder.insert_date_cell(1647251762, None, None, &field_type)
}, },
FieldType::MultiSelect => row_builder FieldType::MultiSelect => row_builder
.insert_multi_select_cell(|mut options| vec![options.remove(0), options.remove(1)]), .insert_multi_select_cell(|mut options| vec![options.remove(0), options.remove(1)]),
@ -166,7 +166,7 @@ pub fn make_test_grid() -> DatabaseData {
FieldType::RichText => row_builder.insert_text_cell("C"), FieldType::RichText => row_builder.insert_text_cell("C"),
FieldType::Number => row_builder.insert_number_cell("3"), FieldType::Number => row_builder.insert_number_cell("3"),
FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => { FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => {
row_builder.insert_date_cell("1647251762", None, None, &field_type) row_builder.insert_date_cell(1647251762, None, None, &field_type)
}, },
FieldType::SingleSelect => { FieldType::SingleSelect => {
row_builder.insert_single_select_cell(|mut options| options.remove(0)) row_builder.insert_single_select_cell(|mut options| options.remove(0))
@ -185,7 +185,7 @@ pub fn make_test_grid() -> DatabaseData {
FieldType::RichText => row_builder.insert_text_cell("DA"), FieldType::RichText => row_builder.insert_text_cell("DA"),
FieldType::Number => row_builder.insert_number_cell("14"), FieldType::Number => row_builder.insert_number_cell("14"),
FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => { FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => {
row_builder.insert_date_cell("1668704685", None, None, &field_type) row_builder.insert_date_cell(1668704685, None, None, &field_type)
}, },
FieldType::SingleSelect => { FieldType::SingleSelect => {
row_builder.insert_single_select_cell(|mut options| options.remove(0)) row_builder.insert_single_select_cell(|mut options| options.remove(0))
@ -201,7 +201,7 @@ pub fn make_test_grid() -> DatabaseData {
FieldType::RichText => row_builder.insert_text_cell("AE"), FieldType::RichText => row_builder.insert_text_cell("AE"),
FieldType::Number => row_builder.insert_number_cell(""), FieldType::Number => row_builder.insert_number_cell(""),
FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => { FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => {
row_builder.insert_date_cell("1668359085", None, None, &field_type) row_builder.insert_date_cell(1668359085, None, None, &field_type)
}, },
FieldType::SingleSelect => { FieldType::SingleSelect => {
row_builder.insert_single_select_cell(|mut options| options.remove(1)) row_builder.insert_single_select_cell(|mut options| options.remove(1))
@ -219,7 +219,7 @@ pub fn make_test_grid() -> DatabaseData {
FieldType::RichText => row_builder.insert_text_cell("AE"), FieldType::RichText => row_builder.insert_text_cell("AE"),
FieldType::Number => row_builder.insert_number_cell("5"), FieldType::Number => row_builder.insert_number_cell("5"),
FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => { FieldType::DateTime | FieldType::LastEditedTime | FieldType::CreatedTime => {
row_builder.insert_date_cell("1671938394", None, None, &field_type) row_builder.insert_date_cell(1671938394, None, None, &field_type)
}, },
FieldType::SingleSelect => { FieldType::SingleSelect => {
row_builder.insert_single_select_cell(|mut options| options.remove(1)) row_builder.insert_single_select_cell(|mut options| options.remove(1))

View File

@ -524,11 +524,10 @@ async fn update_date_cell_event_test() {
// Insert data into the date cell of the first row. // Insert data into the date cell of the first row.
let timestamp = 1686300557; let timestamp = 1686300557;
let timestamp_str = 1686300557.to_string();
let error = test let error = test
.update_date_cell(DateChangesetPB { .update_date_cell(DateChangesetPB {
cell_id: cell_path, cell_id: cell_path,
date: Some(timestamp_str.clone()), date: Some(timestamp.clone()),
time: None, time: None,
include_time: None, include_time: None,
clear_flag: None, clear_flag: None,
@ -566,7 +565,7 @@ async fn update_date_cell_event_with_empty_time_str_test() {
let error = test let error = test
.update_date_cell(DateChangesetPB { .update_date_cell(DateChangesetPB {
cell_id: cell_path, cell_id: cell_path,
date: Some("".to_string()), date: None,
..Default::default() ..Default::default()
}) })
.await; .await;
@ -885,7 +884,6 @@ async fn create_calendar_event_test() {
let row = test.create_row(&calendar_view.id, None, None).await; let row = test.create_row(&calendar_view.id, None, None).await;
// Insert data into the date cell of the first row. // Insert data into the date cell of the first row.
let timestamp_str = timestamp().to_string();
let error = test let error = test
.update_date_cell(DateChangesetPB { .update_date_cell(DateChangesetPB {
cell_id: CellIdPB { cell_id: CellIdPB {
@ -893,7 +891,7 @@ async fn create_calendar_event_test() {
field_id: date_field.id.clone(), field_id: date_field.id.clone(),
row_id: row.id, row_id: row.id,
}, },
date: Some(timestamp_str.clone()), date: Some(timestamp()),
time: None, time: None,
include_time: None, include_time: None,
clear_flag: None, clear_flag: None,