91 lines
3.0 KiB
Rust
Raw Normal View History

use crate::services::cell::apply_cell_data_changeset;
2022-07-13 11:09:13 +08:00
use crate::services::field::SelectOptionCellChangeset;
2022-03-16 10:02:37 +08:00
use flowy_error::{FlowyError, FlowyResult};
use flowy_grid_data_model::revision::{gen_row_id, CellRevision, FieldRevision, RowRevision, DEFAULT_ROW_HEIGHT};
use indexmap::IndexMap;
2022-03-14 17:24:25 +08:00
use std::collections::HashMap;
2022-07-14 09:29:05 +08:00
use std::sync::Arc;
2022-03-13 11:06:28 +08:00
2022-07-12 15:49:14 +08:00
pub struct RowRevisionBuilder<'a> {
2022-07-14 09:29:05 +08:00
field_rev_map: HashMap<&'a String, Arc<FieldRevision>>,
payload: CreateRowRevisionPayload,
2022-03-13 11:06:28 +08:00
}
2022-07-12 15:49:14 +08:00
impl<'a> RowRevisionBuilder<'a> {
2022-07-14 09:29:05 +08:00
pub fn new(fields: &'a [Arc<FieldRevision>]) -> Self {
let field_rev_map = fields
2022-03-16 10:02:37 +08:00
.iter()
2022-07-14 09:29:05 +08:00
.map(|field| (&field.id, field.clone()))
.collect::<HashMap<&String, Arc<FieldRevision>>>();
2022-03-16 10:02:37 +08:00
let payload = CreateRowRevisionPayload {
2022-04-11 15:27:03 +08:00
row_id: gen_row_id(),
2022-03-14 17:24:25 +08:00
cell_by_field_id: Default::default(),
height: DEFAULT_ROW_HEIGHT,
visibility: true,
};
2022-03-16 10:02:37 +08:00
2022-06-15 17:24:46 +08:00
Self { field_rev_map, payload }
2022-03-13 11:06:28 +08:00
}
2022-07-12 15:49:14 +08:00
pub fn insert_cell(&mut self, field_id: &str, data: String) -> FlowyResult<()> {
match self.field_rev_map.get(&field_id.to_owned()) {
2022-03-16 10:02:37 +08:00
None => {
2022-07-12 15:49:14 +08:00
let msg = format!("Can't find the field with id: {}", field_id);
2022-03-16 10:02:37 +08:00
Err(FlowyError::internal().context(msg))
}
Some(field_rev) => {
2022-07-08 14:54:11 +08:00
let data = apply_cell_data_changeset(data, None, field_rev)?;
let cell = CellRevision::new(data);
2022-04-07 15:34:00 +08:00
self.payload.cell_by_field_id.insert(field_id.to_owned(), cell);
Ok(())
}
}
}
2022-07-12 15:49:14 +08:00
pub fn insert_select_option_cell(&mut self, field_id: &str, data: String) -> FlowyResult<()> {
match self.field_rev_map.get(&field_id.to_owned()) {
2022-04-07 15:34:00 +08:00
None => {
let msg = format!("Invalid field_id: {}", field_id);
Err(FlowyError::internal().context(msg))
}
Some(field_rev) => {
2022-07-08 14:54:11 +08:00
let cell_data = SelectOptionCellChangeset::from_insert(&data).to_str();
let data = apply_cell_data_changeset(cell_data, None, field_rev)?;
let cell = CellRevision::new(data);
2022-03-18 21:04:01 +08:00
self.payload.cell_by_field_id.insert(field_id.to_owned(), cell);
2022-03-16 10:02:37 +08:00
Ok(())
}
}
2022-03-14 17:24:25 +08:00
}
#[allow(dead_code)]
pub fn height(mut self, height: i32) -> Self {
2022-03-18 21:04:01 +08:00
self.payload.height = height;
2022-03-14 17:24:25 +08:00
self
}
#[allow(dead_code)]
pub fn visibility(mut self, visibility: bool) -> Self {
2022-03-18 21:04:01 +08:00
self.payload.visibility = visibility;
2022-03-13 11:06:28 +08:00
self
}
2022-07-12 15:49:14 +08:00
pub fn build(self, block_id: &str) -> RowRevision {
RowRevision {
id: self.payload.row_id,
block_id: block_id.to_owned(),
cells: self.payload.cell_by_field_id,
height: self.payload.height,
visibility: self.payload.visibility,
}
2022-03-14 17:24:25 +08:00
}
}
pub struct CreateRowRevisionPayload {
2022-03-14 17:24:25 +08:00
pub row_id: String,
pub cell_by_field_id: IndexMap<String, CellRevision>,
2022-03-14 17:24:25 +08:00
pub height: i32,
pub visibility: bool,
}