76 lines
2.2 KiB
Rust
Raw Normal View History

2022-03-16 10:02:37 +08:00
use crate::services::row::serialize_cell_data;
use flowy_error::{FlowyError, FlowyResult};
2022-03-15 11:07:18 +08:00
use flowy_grid_data_model::entities::{CellMeta, FieldMeta, RowMeta, DEFAULT_ROW_HEIGHT};
2022-03-14 17:24:25 +08:00
use std::collections::HashMap;
2022-03-13 11:06:28 +08:00
pub struct RowMetaContextBuilder<'a> {
2022-03-16 10:02:37 +08:00
field_meta_map: HashMap<&'a String, &'a FieldMeta>,
ctx: RowMetaContext,
2022-03-13 11:06:28 +08:00
}
impl<'a> RowMetaContextBuilder<'a> {
2022-03-15 11:07:18 +08:00
pub fn new(fields: &'a [FieldMeta]) -> Self {
2022-03-16 10:02:37 +08:00
let field_meta_map = fields
.iter()
.map(|field| (&field.id, field))
.collect::<HashMap<&String, &FieldMeta>>();
let ctx = RowMetaContext {
2022-03-14 17:24:25 +08:00
row_id: uuid::Uuid::new_v4().to_string(),
cell_by_field_id: Default::default(),
height: DEFAULT_ROW_HEIGHT,
visibility: true,
};
2022-03-16 10:02:37 +08:00
Self { field_meta_map, ctx }
2022-03-13 11:06:28 +08:00
}
2022-03-16 10:02:37 +08:00
pub fn add_cell(&mut self, field_id: &str, data: String) -> FlowyResult<()> {
match self.field_meta_map.get(&field_id.to_owned()) {
None => {
let msg = format!("Invalid field_id: {}", field_id);
Err(FlowyError::internal().context(msg))
}
Some(field_meta) => {
let data = serialize_cell_data(&data, field_meta)?;
let cell = CellMeta::new(field_id, data);
self.ctx.cell_by_field_id.insert(field_id.to_owned(), cell);
Ok(())
}
}
2022-03-14 17:24:25 +08:00
}
#[allow(dead_code)]
pub fn height(mut self, height: i32) -> Self {
self.ctx.height = height;
self
}
#[allow(dead_code)]
pub fn visibility(mut self, visibility: bool) -> Self {
self.ctx.visibility = visibility;
2022-03-13 11:06:28 +08:00
self
}
pub fn build(self) -> RowMetaContext {
2022-03-14 17:24:25 +08:00
self.ctx
2022-03-13 11:06:28 +08:00
}
}
2022-03-14 17:24:25 +08:00
pub fn row_meta_from_context(block_id: &str, ctx: RowMetaContext) -> RowMeta {
2022-03-14 17:24:25 +08:00
RowMeta {
id: ctx.row_id,
block_id: block_id.to_owned(),
cell_by_field_id: ctx.cell_by_field_id,
height: ctx.height,
visibility: ctx.visibility,
}
}
pub struct RowMetaContext {
2022-03-14 17:24:25 +08:00
pub row_id: String,
pub cell_by_field_id: HashMap<String, CellMeta>,
pub height: i32,
pub visibility: bool,
}