fix: make grid with view_id

This commit is contained in:
appflowy 2022-03-06 11:28:24 +08:00
parent 264166fa29
commit 9bb516786e
10 changed files with 46 additions and 39 deletions

View File

@ -7,6 +7,8 @@ import 'package:app_flowy/workspace/application/doc/doc_bloc.dart';
import 'package:app_flowy/workspace/application/doc/doc_service.dart';
import 'package:app_flowy/workspace/application/doc/share_bloc.dart';
import 'package:app_flowy/workspace/application/doc/share_service.dart';
import 'package:app_flowy/workspace/application/grid/grid_bloc.dart';
import 'package:app_flowy/workspace/application/grid/grid_service.dart';
import 'package:app_flowy/workspace/application/home/home_listen_bloc.dart';
import 'package:app_flowy/workspace/application/menu/menu_bloc.dart';
import 'package:app_flowy/workspace/application/menu/menu_user_bloc.dart';
@ -97,6 +99,14 @@ class HomeDepsResolver {
),
);
// Grid
getIt.registerFactoryParam<GridBloc, View, void>(
(view, _) => GridBloc(
view: view,
service: GridService(),
),
);
// trash
getIt.registerLazySingleton<TrashService>(() => TrashService());
getIt.registerLazySingleton<TrashListener>(() => TrashListener());

View File

@ -14,8 +14,8 @@ part 'grid_bloc.freezed.dart';
class GridBloc extends Bloc<GridEvent, GridState> {
final GridService service;
final View view;
late Grid? _grid;
late List<Field>? _fields;
Grid? _grid;
List<Field>? _fields;
GridBloc({required this.view, required this.service}) : super(GridState.initial()) {
on<GridEvent>(

View File

@ -31,7 +31,9 @@ class _GridPageState extends State<GridPage> {
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<GridBloc>(create: (context) => getIt<GridBloc>()),
BlocProvider<GridBloc>(
create: (context) => getIt<GridBloc>(param1: widget.view)..add(const GridEvent.initial()),
),
],
child: BlocBuilder<GridBloc, GridState>(
builder: (context, state) {

View File

@ -241,7 +241,7 @@ pub trait ViewDataProcessor {
fn delta_str(&self, view_id: &str) -> FutureResult<String, FlowyError>;
fn default_view_data(&self) -> String;
fn default_view_data(&self, view_id: &str) -> String;
fn data_type(&self) -> ViewDataType;
}

View File

@ -59,7 +59,7 @@ impl ViewController {
pub(crate) async fn create_view_from_params(&self, mut params: CreateViewParams) -> Result<View, FlowyError> {
let processor = self.get_data_processor(&params.data_type)?;
let content = if params.data.is_empty() {
let default_view_data = processor.default_view_data();
let default_view_data = processor.default_view_data(&params.view_id);
params.data = default_view_data.clone();
default_view_data
} else {

View File

@ -4,7 +4,7 @@ use flowy_grid_data_model::entities::{Grid, GridId, QueryFieldPayload, QueryRowP
use lib_dispatch::prelude::{data_result, AppData, Data, DataResult};
use std::sync::Arc;
#[tracing::instrument(skip(data, manager), err)]
#[tracing::instrument(level = "debug", skip(data, manager), err)]
pub(crate) async fn open_grid_handler(
data: Data<GridId>,
manager: AppData<Arc<GridManager>>,
@ -15,7 +15,7 @@ pub(crate) async fn open_grid_handler(
data_result(grid)
}
#[tracing::instrument(skip(data, manager), err)]
#[tracing::instrument(level = "debug", skip(data, manager), err)]
pub(crate) async fn get_rows_handler(
data: Data<QueryRowPayload>,
manager: AppData<Arc<GridManager>>,
@ -26,7 +26,7 @@ pub(crate) async fn get_rows_handler(
data_result(repeated_row)
}
#[tracing::instrument(skip(data, manager), err)]
#[tracing::instrument(level = "debug", skip(data, manager), err)]
pub(crate) async fn get_fields_handler(
data: Data<QueryFieldPayload>,
manager: AppData<Arc<GridManager>>,
@ -37,7 +37,7 @@ pub(crate) async fn get_fields_handler(
data_result(repeated_field)
}
#[tracing::instrument(skip(data, manager), err)]
#[tracing::instrument(level = "debug", skip(data, manager), err)]
pub(crate) async fn create_row_handler(
data: Data<GridId>,
manager: AppData<Arc<GridManager>>,

View File

@ -17,20 +17,20 @@ pub trait GridUser: Send + Sync {
}
pub struct GridManager {
grid_editors: Arc<GridEditors>,
editor_map: Arc<GridEditorMap>,
grid_user: Arc<dyn GridUser>,
kv_persistence: Arc<RwLock<Option<Arc<GridKVPersistence>>>>,
}
impl GridManager {
pub fn new(grid_user: Arc<dyn GridUser>, _rev_web_socket: Arc<dyn RevisionWebSocket>) -> Self {
let grid_editors = Arc::new(GridEditors::new());
let grid_editors = Arc::new(GridEditorMap::new());
// kv_persistence will be initialized after first access.
// See get_kv_persistence function below
let kv_persistence = Arc::new(RwLock::new(None));
Self {
grid_editors,
editor_map: grid_editors,
grid_user,
kv_persistence,
}
@ -56,7 +56,7 @@ impl GridManager {
pub fn close_grid<T: AsRef<str>>(&self, grid_id: T) -> FlowyResult<()> {
let grid_id = grid_id.as_ref();
tracing::Span::current().record("grid_id", &grid_id);
self.grid_editors.remove(grid_id);
self.editor_map.remove(grid_id);
Ok(())
}
@ -64,22 +64,26 @@ impl GridManager {
pub fn delete_grid<T: AsRef<str>>(&self, grid_id: T) -> FlowyResult<()> {
let grid_id = grid_id.as_ref();
tracing::Span::current().record("grid_id", &grid_id);
self.grid_editors.remove(grid_id);
self.editor_map.remove(grid_id);
Ok(())
}
#[tracing::instrument(level = "debug", skip(self), err)]
pub fn get_grid_editor(&self, grid_id: &str) -> FlowyResult<Arc<ClientGridEditor>> {
match self.grid_editors.get(grid_id) {
match self.editor_map.get(grid_id) {
None => Err(FlowyError::internal().context("Should call open_grid function first")),
Some(editor) => Ok(editor),
}
}
async fn get_or_create_grid_editor(&self, grid_id: &str) -> FlowyResult<Arc<ClientGridEditor>> {
match self.grid_editors.get(grid_id) {
match self.editor_map.get(grid_id) {
None => {
tracing::trace!("Create grid editor with id: {}", grid_id);
let db_pool = self.grid_user.db_pool()?;
self.make_grid_editor(grid_id, db_pool).await
let editor = self.make_grid_editor(grid_id, db_pool).await?;
self.editor_map.insert(grid_id, &editor);
Ok(editor)
}
Some(editor) => Ok(editor),
}
@ -94,7 +98,6 @@ impl GridManager {
let rev_manager = self.make_grid_rev_manager(grid_id, pool.clone())?;
let kv_persistence = self.get_kv_persistence()?;
let grid_editor = ClientGridEditor::new(grid_id, user, rev_manager, kv_persistence).await?;
self.grid_editors.insert(grid_id, &grid_editor);
Ok(grid_editor)
}
@ -120,8 +123,7 @@ impl GridManager {
}
use lib_infra::uuid;
pub fn default_grid() -> String {
let grid_id = uuid();
pub fn default_grid(grid_id: &str) -> String {
let fields = vec![
Field {
id: uuid(),
@ -146,12 +148,12 @@ pub fn default_grid() -> String {
let rows = vec![
RawRow {
id: uuid(),
grid_id: grid_id.clone(),
grid_id: grid_id.to_string(),
cell_by_field_id: Default::default(),
},
RawRow {
id: uuid(),
grid_id: grid_id.clone(),
grid_id: grid_id.to_string(),
cell_by_field_id: Default::default(),
},
];
@ -172,11 +174,11 @@ pub fn make_grid(grid_id: &str, fields: Vec<Field>, rows: Vec<RawRow>) -> String
delta.to_delta_str()
}
pub struct GridEditors {
pub struct GridEditorMap {
inner: DashMap<String, Arc<ClientGridEditor>>,
}
impl GridEditors {
impl GridEditorMap {
fn new() -> Self {
Self { inner: DashMap::new() }
}
@ -188,16 +190,8 @@ impl GridEditors {
self.inner.insert(grid_id.to_string(), grid_editor.clone());
}
pub(crate) fn contains(&self, grid_id: &str) -> bool {
self.inner.get(grid_id).is_some()
}
pub(crate) fn get(&self, grid_id: &str) -> Option<Arc<ClientGridEditor>> {
if !self.contains(grid_id) {
return None;
}
let opened_grid = self.inner.get(grid_id).unwrap();
Some(opened_grid.clone())
Some(self.inner.get(grid_id)?.clone())
}
pub(crate) fn remove(&self, grid_id: &str) {

View File

@ -173,7 +173,7 @@ impl ViewDataProcessor for BlockManagerViewDataImpl {
})
}
fn default_view_data(&self) -> String {
fn default_view_data(&self, _view_id: &str) -> String {
initial_quill_delta_string()
}
@ -225,8 +225,8 @@ impl ViewDataProcessor for GridManagerViewDataImpl {
})
}
fn default_view_data(&self) -> String {
default_grid()
fn default_view_data(&self, view_id: &str) -> String {
default_grid(view_id)
}
fn data_type(&self) -> ViewDataType {

View File

@ -67,7 +67,8 @@ fn crate_log_filter(level: String) -> String {
filters.push(format!("flowy_sdk={}", level));
filters.push(format!("flowy_folder={}", level));
filters.push(format!("flowy_user={}", level));
filters.push(format!("flowy_document={}", level));
filters.push(format!("flowy_block={}", level));
filters.push(format!("flowy_grid={}", level));
filters.push(format!("flowy_collaboration={}", "debug"));
filters.push(format!("dart_notify={}", level));
filters.push(format!("lib_ot={}", level));
@ -78,7 +79,7 @@ fn crate_log_filter(level: String) -> String {
filters.push(format!("flowy_database={}", "info"));
filters.push(format!("flowy_net={}", "info"));
filters.push(format!("flowy_sync={}", "info"));
filters.push(format!("flowy_sync={}", "info"));
filters.join(",")
}

View File

@ -83,7 +83,7 @@ where
}
}
#[tracing::instrument(level = "debug", skip(self, user, repeated_revision), err)]
#[tracing::instrument(level = "trace", skip(self, user, repeated_revision), err)]
pub async fn sync_revisions(
&self,
user: Arc<dyn RevisionUser>,