Skip to content

Commit cad5e5d

Browse files
committed
refactor: Date's cell data serde
1 parent 4b88aa8 commit cad5e5d

File tree

2 files changed

+12
-42
lines changed

2 files changed

+12
-42
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class GridCellContextBuilder {
3131
final cellDataLoader = GridCellDataLoader(
3232
gridCell: _gridCell,
3333
parser: DateCellDataParser(),
34+
config: const GridCellDataConfig(reloadOnFieldChanged: true),
3435
);
3536

3637
return GridDateCellContext(

frontend/rust-lib/flowy-grid/src/services/field/type_options/date_type_option.rs

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
use crate::entities::{CellIdentifier, CellIdentifierPayload};
22
use crate::impl_type_option;
33
use crate::services::field::{BoxTypeOptionBuilder, TypeOptionBuilder};
4-
use crate::services::row::{CellContentChangeset, CellDataOperation, DecodedCellData, EncodedCellData};
4+
use crate::services::row::{CellContentChangeset, CellDataOperation, DecodedCellData};
55
use bytes::Bytes;
66
use chrono::format::strftime::StrftimeItems;
77
use chrono::{NaiveDateTime, Timelike};
88
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
9-
use flowy_error::{internal_error, ErrorCode, FlowyError, FlowyResult};
9+
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
1010
use flowy_grid_data_model::entities::{
1111
CellChangeset, CellMeta, FieldMeta, FieldType, TypeOptionDataDeserializer, TypeOptionDataEntry,
1212
};
1313
use serde::{Deserialize, Serialize};
14-
use std::str::FromStr;
1514
use strum_macros::EnumIter;
1615

1716
// Date
@@ -117,15 +116,15 @@ impl DateTypeOption {
117116
}
118117
}
119118

120-
impl CellDataOperation<EncodedCellData<DateCellDataSerde>> for DateTypeOption {
119+
impl CellDataOperation<String> for DateTypeOption {
121120
fn decode_cell_data<T>(
122121
&self,
123122
encoded_data: T,
124123
decoded_field_type: &FieldType,
125124
_field_meta: &FieldMeta,
126125
) -> FlowyResult<DecodedCellData>
127126
where
128-
T: Into<EncodedCellData<DateCellDataSerde>>,
127+
T: Into<String>,
129128
{
130129
// Return default data if the type_option_cell_data is not FieldType::DateTime.
131130
// It happens when switching from one field to another.
@@ -135,8 +134,8 @@ impl CellDataOperation<EncodedCellData<DateCellDataSerde>> for DateTypeOption {
135134
return Ok(DecodedCellData::default());
136135
}
137136

138-
let encoded_data = encoded_data.into().try_into_inner()?;
139-
let date = self.today_desc_from_timestamp(encoded_data.timestamp);
137+
let timestamp = encoded_data.into().parse::<i64>().unwrap_or(0);
138+
let date = self.today_desc_from_timestamp(timestamp);
140139
DecodedCellData::try_from_bytes(date)
141140
}
142141

@@ -146,15 +145,15 @@ impl CellDataOperation<EncodedCellData<DateCellDataSerde>> for DateTypeOption {
146145
{
147146
let content_changeset: DateCellContentChangeset = serde_json::from_str(&changeset.into())?;
148147
let cell_data = match content_changeset.date_timestamp() {
149-
None => DateCellDataSerde::default(),
148+
None => 0,
150149
Some(date_timestamp) => match (self.include_time, content_changeset.time) {
151150
(true, Some(time)) => {
152151
let time = Some(time.trim().to_uppercase());
153152
let utc = self.utc_date_time_from_timestamp(date_timestamp);
154153
let timestamp = self.timestamp_from_utc_with_time(&utc, &time)?;
155-
DateCellDataSerde::new(timestamp, time)
154+
timestamp
156155
}
157-
_ => DateCellDataSerde::new(date_timestamp, None),
156+
_ => date_timestamp,
158157
},
159158
};
160159

@@ -282,34 +281,6 @@ pub struct DateCellData {
282281
pub timestamp: i64,
283282
}
284283

285-
#[derive(Default, Serialize, Deserialize)]
286-
pub(crate) struct DateCellDataSerde {
287-
pub timestamp: i64,
288-
289-
// #[deprecated(since = "0.0.4", note = "No need to same the time that user input")]
290-
pub time: Option<String>,
291-
}
292-
293-
impl DateCellDataSerde {
294-
pub(crate) fn new(timestamp: i64, _time: Option<String>) -> Self {
295-
Self { timestamp, time: None }
296-
}
297-
}
298-
299-
impl FromStr for DateCellDataSerde {
300-
type Err = FlowyError;
301-
302-
fn from_str(s: &str) -> Result<Self, Self::Err> {
303-
serde_json::from_str::<DateCellDataSerde>(s).map_err(internal_error)
304-
}
305-
}
306-
307-
impl ToString for DateCellDataSerde {
308-
fn to_string(&self) -> String {
309-
serde_json::to_string(&self).unwrap_or_else(|_| "".to_string())
310-
}
311-
}
312-
313284
#[derive(Clone, Debug, Default, ProtoBuf)]
314285
pub struct DateChangesetPayload {
315286
#[pb(index = 1)]
@@ -386,9 +357,7 @@ impl std::convert::From<DateCellContentChangeset> for CellContentChangeset {
386357
#[cfg(test)]
387358
mod tests {
388359
use crate::services::field::FieldBuilder;
389-
use crate::services::field::{
390-
DateCellContentChangeset, DateCellData, DateCellDataSerde, DateFormat, DateTypeOption, TimeFormat,
391-
};
360+
use crate::services::field::{DateCellContentChangeset, DateCellData, DateFormat, DateTypeOption, TimeFormat};
392361
use crate::services::row::{CellDataOperation, EncodedCellData};
393362
use flowy_grid_data_model::entities::{FieldMeta, FieldType, TypeOptionDataEntry};
394363
use strum::IntoEnumIterator;
@@ -639,7 +608,7 @@ mod tests {
639608
);
640609
}
641610

642-
fn decode_cell_data<T: Into<EncodedCellData<DateCellDataSerde>>>(
611+
fn decode_cell_data<T: Into<String>>(
643612
encoded_data: T,
644613
type_option: &DateTypeOption,
645614
field_meta: &FieldMeta,

0 commit comments

Comments
 (0)