Skip to content

Commit f617a04

Browse files
committed
refactor: cell with origin data
1 parent f21ca6c commit f617a04

File tree

16 files changed

+241
-113
lines changed

16 files changed

+241
-113
lines changed

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flowy_sdk/log.dart';
12
import 'package:flowy_sdk/protobuf/flowy-grid-data-model/grid.pb.dart' show Cell, Field;
23
import 'package:flowy_sdk/protobuf/flowy-grid/date_type_option.pb.dart';
34
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -6,6 +7,7 @@ import 'package:table_calendar/table_calendar.dart';
67
import 'dart:async';
78
import 'cell_service.dart';
89
import 'package:dartz/dartz.dart';
10+
import 'package:fixnum/fixnum.dart' as $fixnum;
911
part 'date_cal_bloc.freezed.dart';
1012

1113
class DateCalBloc extends Bloc<DateCalEvent, DateCalState> {
@@ -15,9 +17,10 @@ class DateCalBloc extends Bloc<DateCalEvent, DateCalState> {
1517
DateCalBloc({required this.cellContext}) : super(DateCalState.initial(cellContext)) {
1618
on<DateCalEvent>(
1719
(event, emit) async {
18-
event.map(
19-
initial: (_Initial value) {
20+
await event.map(
21+
initial: (_Initial value) async {
2022
_startListening();
23+
await _loadDateTypeOption(emit);
2124
},
2225
selectDay: (_SelectDay value) {
2326
if (!isSameDay(state.selectedDay, value.day)) {
@@ -60,6 +63,30 @@ class DateCalBloc extends Bloc<DateCalEvent, DateCalState> {
6063
);
6164
}
6265

66+
Future<void> _loadDateTypeOption(Emitter<DateCalState> emit) async {
67+
final result = await cellContext.getTypeOptionData();
68+
result.fold(
69+
(data) {
70+
final typeOptionData = DateTypeOption.fromBuffer(data);
71+
72+
DateTime? selectedDay;
73+
final cellData = cellContext.getCellData()?.data;
74+
75+
if (cellData != null) {
76+
final timestamp = $fixnum.Int64.parseInt(cellData).toInt();
77+
selectedDay = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
78+
}
79+
80+
emit(state.copyWith(
81+
typeOptinoData: some(typeOptionData),
82+
includeTime: typeOptionData.includeTime,
83+
selectedDay: selectedDay,
84+
));
85+
},
86+
(err) => Log.error(err),
87+
);
88+
}
89+
6390
void _updateCellData(DateTime day) {
6491
final data = day.millisecondsSinceEpoch ~/ 1000;
6592
cellContext.saveCellData(data.toString());
@@ -83,6 +110,7 @@ class DateCalState with _$DateCalState {
83110
required Option<DateTypeOption> typeOptinoData,
84111
required CalendarFormat format,
85112
required DateTime focusedDay,
113+
required bool includeTime,
86114
DateTime? selectedDay,
87115
}) = _DateCalState;
88116

@@ -91,5 +119,6 @@ class DateCalState with _$DateCalState {
91119
typeOptinoData: none(),
92120
format: CalendarFormat.month,
93121
focusedDay: DateTime.now(),
122+
includeTime: false,
94123
);
95124
}

frontend/app_flowy/lib/workspace/application/grid/field/field_service.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class FieldService {
119119
required FieldType fieldType,
120120
}) {
121121
final payload = EditFieldPayload.create()
122+
..gridId = gridId
122123
..fieldId = fieldId
123124
..fieldType = fieldType;
124125
return GridEventGetFieldTypeOption(payload).send().then((result) {

frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-grid-data-model/grid.pb.dart

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-grid-data-model/grid.pbjson.dart

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/rust-lib/flowy-grid/src/event_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ pub(crate) async fn get_cell_handler(
245245
let params: CellIdentifier = data.into_inner().try_into()?;
246246
let editor = manager.get_grid_editor(&params.grid_id)?;
247247
match editor.get_cell(&params).await {
248-
None => data_result(Cell::new(&params.field_id, "".to_owned())),
248+
None => data_result(Cell::empty(&params.field_id)),
249249
Some(cell) => data_result(cell),
250250
}
251251
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::impl_type_option;
22
use crate::services::field::{BoxTypeOptionBuilder, TypeOptionBuilder};
3-
use crate::services::row::{CellDataChangeset, CellDataOperation, TypeOptionCellData};
3+
use crate::services::row::{CellDataChangeset, CellDataOperation, DecodedCellData, TypeOptionCellData};
44
use bytes::Bytes;
55
use flowy_derive::ProtoBuf;
66
use flowy_error::FlowyError;
@@ -44,18 +44,18 @@ const YES: &str = "Yes";
4444
const NO: &str = "No";
4545

4646
impl CellDataOperation for CheckboxTypeOption {
47-
fn decode_cell_data(&self, data: String, _field_meta: &FieldMeta) -> String {
47+
fn decode_cell_data(&self, data: String, field_meta: &FieldMeta) -> DecodedCellData {
4848
if let Ok(type_option_cell_data) = TypeOptionCellData::from_str(&data) {
4949
if !type_option_cell_data.is_checkbox() {
50-
return String::new();
50+
return DecodedCellData::default();
5151
}
5252
let cell_data = type_option_cell_data.data;
5353
if cell_data == YES || cell_data == NO {
54-
return cell_data;
54+
return DecodedCellData::from_content(cell_data);
5555
}
5656
}
5757

58-
String::new()
58+
DecodedCellData::default()
5959
}
6060

6161
fn apply_changeset<T: Into<CellDataChangeset>>(

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::impl_type_option;
2-
use crate::services::row::{CellDataChangeset, CellDataOperation, TypeOptionCellData};
2+
use crate::services::row::{CellDataChangeset, CellDataOperation, DecodedCellData, TypeOptionCellData};
33
use bytes::Bytes;
44
use chrono::format::strftime::StrftimeItems;
5-
use chrono::NaiveDateTime;
5+
use chrono::{NaiveDateTime, ParseResult};
66
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
77
use flowy_error::FlowyError;
88
use flowy_grid_data_model::entities::{
@@ -53,24 +53,25 @@ impl DateTypeOption {
5353
}
5454

5555
impl CellDataOperation for DateTypeOption {
56-
fn decode_cell_data(&self, data: String, _field_meta: &FieldMeta) -> String {
56+
fn decode_cell_data(&self, data: String, field_meta: &FieldMeta) -> DecodedCellData {
5757
if let Ok(type_option_cell_data) = TypeOptionCellData::from_str(&data) {
5858
if !type_option_cell_data.is_date() {
59-
return String::new();
59+
return DecodedCellData::default();
6060
}
6161

6262
let cell_data = type_option_cell_data.data;
6363
if let Ok(timestamp) = cell_data.parse::<i64>() {
6464
let native = NaiveDateTime::from_timestamp(timestamp, 0);
65-
return self.today_from_native(native);
65+
return DecodedCellData::new(format!("{}", timestamp), self.today_from_native(native));
6666
}
6767

68-
if NaiveDateTime::parse_from_str(&cell_data, &self.fmt_str()).is_ok() {
69-
return cell_data;
70-
}
68+
return match NaiveDateTime::parse_from_str(&cell_data, &self.fmt_str()) {
69+
Ok(date_time) => DecodedCellData::new(format!("{}", date_time.timestamp()), cell_data),
70+
Err(_) => DecodedCellData::default(),
71+
};
7172
}
7273

73-
String::new()
74+
DecodedCellData::default()
7475
}
7576

7677
fn apply_changeset<T: Into<CellDataChangeset>>(

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::impl_type_option;
22
use crate::services::field::{BoxTypeOptionBuilder, TypeOptionBuilder};
3-
use crate::services::row::{CellDataChangeset, CellDataOperation, TypeOptionCellData};
3+
use crate::services::row::{CellDataChangeset, CellDataOperation, DecodedCellData, TypeOptionCellData};
44
use bytes::Bytes;
55
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
66
use flowy_error::FlowyError;
@@ -77,30 +77,36 @@ pub struct NumberTypeOption {
7777
impl_type_option!(NumberTypeOption, FieldType::Number);
7878

7979
impl CellDataOperation for NumberTypeOption {
80-
fn decode_cell_data(&self, data: String, _field_meta: &FieldMeta) -> String {
80+
fn decode_cell_data(&self, data: String, field_meta: &FieldMeta) -> DecodedCellData {
8181
if let Ok(type_option_cell_data) = TypeOptionCellData::from_str(&data) {
8282
if type_option_cell_data.is_date() {
83-
return String::new();
83+
return DecodedCellData::default();
8484
}
8585

8686
let cell_data = type_option_cell_data.data;
87-
match self.format {
87+
return match self.format {
8888
NumberFormat::Number => {
8989
if let Ok(v) = cell_data.parse::<f64>() {
90-
return v.to_string();
90+
return DecodedCellData::from_content(v.to_string());
9191
}
9292

9393
if let Ok(v) = cell_data.parse::<i64>() {
94-
return v.to_string();
94+
return DecodedCellData::from_content(v.to_string());
9595
}
9696

97-
return String::new();
97+
DecodedCellData::default()
9898
}
99-
NumberFormat::Percent => cell_data.parse::<f64>().map_or(String::new(), |v| v.to_string()),
100-
_ => self.money_from_str(&cell_data),
101-
}
99+
NumberFormat::Percent => {
100+
let content = cell_data.parse::<f64>().map_or(String::new(), |v| v.to_string());
101+
DecodedCellData::from_content(content)
102+
}
103+
_ => {
104+
let content = self.money_from_str(&cell_data);
105+
DecodedCellData::from_content(content)
106+
}
107+
};
102108
} else {
103-
String::new()
109+
DecodedCellData::default()
104110
}
105111
}
106112

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::impl_type_option;
22
use crate::services::entities::{CellIdentifier, CellIdentifierPayload};
33
use crate::services::field::{BoxTypeOptionBuilder, TypeOptionBuilder};
4-
use crate::services::row::{CellDataChangeset, CellDataOperation, TypeOptionCellData};
4+
use crate::services::row::{CellDataChangeset, CellDataOperation, DecodedCellData, TypeOptionCellData};
55
use bytes::Bytes;
66
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
77
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
@@ -95,22 +95,21 @@ impl SelectOptionOperation for SingleSelectTypeOption {
9595
}
9696

9797
impl CellDataOperation for SingleSelectTypeOption {
98-
fn decode_cell_data(&self, data: String, _field_meta: &FieldMeta) -> String {
98+
fn decode_cell_data(&self, data: String, _field_meta: &FieldMeta) -> DecodedCellData {
9999
if let Ok(type_option_cell_data) = TypeOptionCellData::from_str(&data) {
100100
if !type_option_cell_data.is_single_select() {
101-
return String::new();
101+
return DecodedCellData::default();
102102
}
103103

104-
match select_option_ids(type_option_cell_data.data).first() {
105-
None => String::new(),
106-
Some(option_id) => match self.options.iter().find(|option| &option.id == option_id) {
107-
None => String::new(),
108-
Some(option) => option.name.clone(),
109-
},
104+
if let Some(option_id) = select_option_ids(type_option_cell_data.data).first() {
105+
return match self.options.iter().find(|option| &option.id == option_id) {
106+
None => DecodedCellData::default(),
107+
Some(option) => DecodedCellData::from_content(option.name.clone()),
108+
};
110109
}
111-
} else {
112-
String::new()
113110
}
111+
112+
DecodedCellData::default()
114113
}
115114

116115
fn apply_changeset<T: Into<CellDataChangeset>>(
@@ -194,20 +193,22 @@ impl SelectOptionOperation for MultiSelectTypeOption {
194193
}
195194

196195
impl CellDataOperation for MultiSelectTypeOption {
197-
fn decode_cell_data(&self, data: String, _field_meta: &FieldMeta) -> String {
196+
fn decode_cell_data(&self, data: String, _field_meta: &FieldMeta) -> DecodedCellData {
198197
if let Ok(type_option_cell_data) = TypeOptionCellData::from_str(&data) {
199198
if !type_option_cell_data.is_multi_select() {
200-
return String::new();
199+
return DecodedCellData::default();
201200
}
202201
let option_ids = select_option_ids(type_option_cell_data.data);
203-
self.options
202+
let content = self
203+
.options
204204
.iter()
205205
.filter(|option| option_ids.contains(&option.id))
206206
.map(|option| option.name.clone())
207207
.collect::<Vec<String>>()
208-
.join(SELECTION_IDS_SEPARATOR)
208+
.join(SELECTION_IDS_SEPARATOR);
209+
DecodedCellData::from_content(content)
209210
} else {
210-
String::new()
211+
DecodedCellData::default()
211212
}
212213
}
213214

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::impl_type_option;
22
use crate::services::field::{BoxTypeOptionBuilder, TypeOptionBuilder};
3-
use crate::services::row::{decode_cell_data, CellDataChangeset, CellDataOperation, TypeOptionCellData};
3+
use crate::services::row::{
4+
decode_cell_data, CellDataChangeset, CellDataOperation, DecodedCellData, TypeOptionCellData,
5+
};
46
use bytes::Bytes;
57
use flowy_derive::ProtoBuf;
68
use flowy_error::FlowyError;
@@ -33,19 +35,20 @@ pub struct RichTextTypeOption {
3335
impl_type_option!(RichTextTypeOption, FieldType::RichText);
3436

3537
impl CellDataOperation for RichTextTypeOption {
36-
fn decode_cell_data(&self, data: String, field_meta: &FieldMeta) -> String {
38+
fn decode_cell_data(&self, data: String, field_meta: &FieldMeta) -> DecodedCellData {
3739
if let Ok(type_option_cell_data) = TypeOptionCellData::from_str(&data) {
3840
if type_option_cell_data.is_date()
3941
|| type_option_cell_data.is_single_select()
4042
|| type_option_cell_data.is_multi_select()
4143
|| type_option_cell_data.is_number()
4244
{
43-
decode_cell_data(data, field_meta, &type_option_cell_data.field_type).unwrap_or_else(|| "".to_owned())
45+
decode_cell_data(data, field_meta, &type_option_cell_data.field_type)
46+
.unwrap_or_else(|| DecodedCellData::default())
4447
} else {
45-
type_option_cell_data.data
48+
DecodedCellData::from_content(type_option_cell_data.data)
4649
}
4750
} else {
48-
String::new()
51+
DecodedCellData::default()
4952
}
5053
}
5154

0 commit comments

Comments
 (0)