This commit is contained in:
appflowy 2021-10-13 11:10:29 +08:00
parent 3e65c66baf
commit fab442997c
11 changed files with 87 additions and 45 deletions

View File

@ -1,5 +1,5 @@
use crate::{
entities::{app::parser::AppId, view::parser::*},
entities::{app::parser::AppId, trash::Trash, view::parser::*},
errors::WorkspaceError,
impl_def_and_def_mut,
};
@ -153,3 +153,14 @@ pub struct RepeatedView {
}
impl_def_and_def_mut!(RepeatedView, View);
impl std::convert::Into(Trash) for View {
fn into(self) -> (Trash) {
Trash {
id: self.id,
name: self.name,
modified_time: self.modified_time,
create_time: self.create_time,
}
}
}

View File

@ -1,15 +1,13 @@
use crate::{
entities::{trash::RepeatedTrash, view::RepeatedView},
errors::WorkspaceError,
services::TrashController,
services::TrashCan,
};
use flowy_dispatch::prelude::{data_result, Data, DataResult, Unit};
use std::{convert::TryInto, sync::Arc};
#[tracing::instrument(skip(data, controller), err)]
pub(crate) async fn read_trash_handler(
controller: Unit<Arc<TrashController>>,
) -> DataResult<RepeatedTrash, WorkspaceError> {
pub(crate) async fn read_trash_handler(controller: Unit<Arc<TrashCan>>) -> DataResult<RepeatedTrash, WorkspaceError> {
let repeated_trash = controller.read_trash()?;
data_result(repeated_trash)
}

View File

@ -5,7 +5,7 @@ use crate::{
services::{server::construct_workspace_server, AppController, ViewController, WorkspaceController},
};
use crate::services::TrashController;
use crate::services::TrashCan;
use flowy_database::DBConnection;
use flowy_dispatch::prelude::*;
use flowy_document::module::FlowyDocument;
@ -38,12 +38,13 @@ pub fn mk_workspace(
) -> Arc<WorkspaceController> {
let server = construct_workspace_server(server_config);
let trash_controller = Arc::new(TrashController::new());
let trash_can = Arc::new(TrashCan::new(database.clone()));
let view_controller = Arc::new(ViewController::new(
user.clone(),
database.clone(),
server.clone(),
trash_can.clone(),
flowy_document,
));
@ -54,7 +55,7 @@ pub fn mk_workspace(
database.clone(),
app_controller.clone(),
view_controller.clone(),
trash_controller.clone(),
trash_can.clone(),
server.clone(),
));
workspace_controller
@ -66,7 +67,7 @@ pub fn create(workspace: Arc<WorkspaceController>) -> Module {
.data(workspace.clone())
.data(workspace.app_controller.clone())
.data(workspace.view_controller.clone())
.data(workspace.trash_controller.clone())
.data(workspace.trash_can.clone())
.event(WorkspaceEvent::InitWorkspace, init_workspace_handler);
module = module

View File

@ -3,7 +3,7 @@ use flowy_derive::ProtoBuf_Enum;
const OBSERVABLE_CATEGORY: &'static str = "Workspace";
#[derive(ProtoBuf_Enum, Debug)]
pub(crate) enum WorkspaceObservable {
pub(crate) enum Notification {
Unknown = 0,
UserCreateWorkspace = 10,
UserDeleteWorkspace = 11,
@ -18,14 +18,16 @@ pub(crate) enum WorkspaceObservable {
UserUnauthorized = 100,
}
impl std::default::Default for WorkspaceObservable {
fn default() -> Self { WorkspaceObservable::Unknown }
impl std::default::Default for Notification {
fn default() -> Self { Notification::Unknown }
}
impl std::convert::Into<i32> for WorkspaceObservable {
impl std::convert::Into<i32> for Notification {
fn into(self) -> i32 { self as i32 }
}
pub(crate) fn dart_notify(id: &str, ty: WorkspaceObservable) -> DartNotifyBuilder {
pub(crate) fn dart_notify(id: &str, ty: Notification) -> DartNotifyBuilder {
tracing::Span::current().record("dart_notify", &format!("{:?} => notify_id: {}", ty, id).as_str());
DartNotifyBuilder::new(id, ty, OBSERVABLE_CATEGORY)
}

View File

@ -59,6 +59,7 @@ impl AppController {
Ok(app_table.into())
}
#[tracing::instrument(level = "debug", skip(self), fields(dart_notify) err)]
pub(crate) async fn delete_app(&self, app_id: &str) -> Result<(), WorkspaceError> {
let conn = &*self.database.db_connection()?;
conn.immediate_transaction::<_, WorkspaceError, _>(|| {

View File

@ -1,5 +1,5 @@
pub(crate) use app_controller::*;
pub(crate) use trash_controller::*;
pub(crate) use trash_can::*;
pub(crate) use view_controller::*;
pub use workspace_controller::*;
@ -7,6 +7,6 @@ mod app_controller;
mod database;
mod helper;
pub mod server;
mod trash_controller;
mod trash_can;
mod view_controller;
mod workspace_controller;

View File

@ -0,0 +1,19 @@
use crate::{
entities::trash::{RepeatedTrash, Trash},
errors::WorkspaceError,
module::WorkspaceDatabase,
};
use std::sync::Arc;
pub struct TrashCan {
database: Arc<dyn WorkspaceDatabase>,
}
impl TrashCan {
pub fn new(database: Arc<dyn WorkspaceDatabase>) -> Self { Self { database } }
pub fn read_trash(&self) -> Result<RepeatedTrash, WorkspaceError> { Ok(RepeatedTrash { items: vec![] }) }
pub fn add<T: Into<Trash>>(&self, trash: T) { let trash = trash.into(); }
pub fn remove(&self, trash_id: &str) {}
}

View File

@ -1,8 +0,0 @@
use crate::{entities::trash::RepeatedTrash, errors::WorkspaceError};
pub struct TrashController {}
impl TrashController {
pub fn new() -> Self { Self {} }
pub fn read_trash(&self) -> Result<RepeatedTrash, WorkspaceError> { Ok(RepeatedTrash { items: vec![] }) }
}

View File

@ -8,10 +8,14 @@ use crate::{
};
use crate::{
entities::view::{DeleteViewParams, QueryViewParams, RepeatedView},
entities::{
trash::Trash,
view::{DeleteViewParams, QueryViewParams, RepeatedView},
},
errors::internal_error,
module::WorkspaceUser,
notify::WorkspaceObservable,
services::TrashCan,
};
use flowy_database::SqliteConnection;
use flowy_document::{
@ -25,6 +29,7 @@ pub(crate) struct ViewController {
sql: Arc<ViewTableSql>,
server: Server,
database: Arc<dyn WorkspaceDatabase>,
trash_can: Arc<TrashCan>,
document: Arc<FlowyDocument>,
}
@ -33,6 +38,7 @@ impl ViewController {
user: Arc<dyn WorkspaceUser>,
database: Arc<dyn WorkspaceDatabase>,
server: Server,
trash_can: Arc<TrashCan>,
document: Arc<FlowyDocument>,
) -> Self {
let sql = Arc::new(ViewTableSql {});
@ -41,6 +47,7 @@ impl ViewController {
sql,
server,
database,
trash_can,
document,
}
}
@ -50,6 +57,7 @@ impl ViewController {
Ok(())
}
#[tracing::instrument(level = "debug", skip(self, params), fields(dart_notify) err)]
pub(crate) async fn create_view(&self, params: CreateViewParams) -> Result<View, WorkspaceError> {
let view = self.create_view_on_server(params.clone()).await?;
let conn = &*self.database.db_connection()?;
@ -58,7 +66,7 @@ impl ViewController {
let _ = self.save_view(view.clone(), conn)?;
self.document.create(CreateDocParams::new(&view.id, params.data))?;
let repeated_view = self.sql.read_views_belong_to(&view.belong_to_id, Some(false), conn)?;
let repeated_view = self.sql.read_views(&view.belong_to_id, Some(false), conn)?;
dart_notify(&view.belong_to_id, WorkspaceObservable::AppCreateView)
.payload(repeated_view)
.send();
@ -88,6 +96,7 @@ impl ViewController {
Ok(edit_context.delta().await.map_err(internal_error)?)
}
#[tracing::instrument(level = "debug", skip(self, params), fields(dart_notify) err)]
pub(crate) async fn delete_view(&self, params: DeleteViewParams) -> Result<(), WorkspaceError> {
let conn = &*self.database.db_connection()?;
let _ = self.delete_view_on_server(&params.view_id);
@ -96,9 +105,7 @@ impl ViewController {
let view_table = self.sql.delete_view(&params.view_id, conn)?;
let _ = self.document.delete(params.into())?;
let repeated_view = self
.sql
.read_views_belong_to(&view_table.belong_to_id, Some(false), conn)?;
let repeated_view = self.sql.read_views(&view_table.belong_to_id, Some(false), conn)?;
dart_notify(&view_table.belong_to_id, WorkspaceObservable::AppDeleteView)
.payload(repeated_view)
@ -114,7 +121,7 @@ impl ViewController {
pub(crate) async fn read_views_belong_to(&self, belong_to_id: &str) -> Result<RepeatedView, WorkspaceError> {
// TODO: read from server
let conn = self.database.db_connection()?;
let repeated_view = self.sql.read_views_belong_to(belong_to_id, Some(false), &*conn)?;
let repeated_view = self.sql.read_views(belong_to_id, Some(false), &*conn)?;
Ok(repeated_view)
}
@ -127,22 +134,33 @@ impl ViewController {
let updated_view = conn.immediate_transaction::<_, WorkspaceError, _>(|| {
let _ = self.sql.update_view(changeset, conn)?;
let view: View = self.sql.read_view(&view_id, None, conn)?.into();
let _ = self.add_to_trash_if_need(view.clone(), params.is_trash.clone())?;
match params.is_trash {
None => {
dart_notify(&view_id, WorkspaceObservable::ViewUpdated)
.payload(view.clone())
.send();
},
Some(is_trash) => {
let repeated_view = self.sql.read_views(&view.belong_to_id, Some(false), conn)?;
dart_notify(&view.belong_to_id, WorkspaceObservable::AppDeleteView)
.payload(repeated_view)
.send();
match is_trash {
true => self.trash_can.add(view.clone()),
false => self.trash_can.remove(&view.id),
}
},
}
if params.is_trash.is_some() {
let repeated_view = self.sql.read_views_belong_to(&view.belong_to_id, Some(false), conn)?;
tracing::Span::current().record(
"dart_notify",
&format!("{:?}: {}", WorkspaceObservable::AppDeleteView, &view.belong_to_id).as_str(),
);
let repeated_view = self.sql.read_views(&view.belong_to_id, Some(false), conn)?;
dart_notify(&view.belong_to_id, WorkspaceObservable::AppDeleteView)
.payload(repeated_view)
.send();
} else {
tracing::Span::current().record(
"dart_notify",
&format!("{:?}: {}", WorkspaceObservable::ViewUpdated, &view_id).as_str(),
);
dart_notify(&view_id, WorkspaceObservable::ViewUpdated)
.payload(view.clone())
.send();

View File

@ -6,7 +6,7 @@ use crate::{
errors::*,
module::{WorkspaceDatabase, WorkspaceUser},
notify::*,
services::{helper::spawn, server::Server, AppController, TrashController, ViewController},
services::{helper::spawn, server::Server, AppController, TrashCan, ViewController},
sql_tables::workspace::{WorkspaceTable, WorkspaceTableChangeset, WorkspaceTableSql},
};
use flowy_database::SqliteConnection;
@ -19,7 +19,7 @@ pub struct WorkspaceController {
pub(crate) view_controller: Arc<ViewController>,
pub(crate) database: Arc<dyn WorkspaceDatabase>,
pub(crate) app_controller: Arc<AppController>,
pub(crate) trash_controller: Arc<TrashController>,
pub(crate) trash_can: Arc<TrashCan>,
server: Server,
}
@ -29,7 +29,7 @@ impl WorkspaceController {
database: Arc<dyn WorkspaceDatabase>,
app_controller: Arc<AppController>,
view_controller: Arc<ViewController>,
trash_controller: Arc<TrashController>,
trash_can: Arc<TrashCan>,
server: Server,
) -> Self {
let workspace_sql = Arc::new(WorkspaceTableSql {});
@ -39,7 +39,7 @@ impl WorkspaceController {
database,
app_controller,
view_controller,
trash_controller,
trash_can,
server,
}
}

View File

@ -39,7 +39,7 @@ impl ViewTableSql {
}
// belong_to_id will be the app_id or view_id.
pub(crate) fn read_views_belong_to(
pub(crate) fn read_views(
&self,
belong_to_id: &str,
is_trash: Option<bool>,