fix: open latest after deleting current view

This commit is contained in:
appflowy
2022-09-26 16:59:58 +08:00
parent 81ecbd8ae2
commit e3a1384f7f
11 changed files with 82 additions and 37 deletions

View File

@ -206,6 +206,15 @@ impl std::convert::From<&str> for ViewIdPB {
}
}
#[derive(Default, ProtoBuf, Clone, Debug)]
pub struct DeletedViewPB {
#[pb(index = 1)]
pub view_id: String,
#[pb(index = 2, one_of)]
pub index: Option<i32>,
}
impl std::ops::Deref for ViewIdPB {
type Target = str;

View File

@ -1,5 +1,5 @@
pub use crate::entities::view::ViewDataTypePB;
use crate::entities::{ViewInfoPB, ViewLayoutTypePB};
use crate::entities::{DeletedViewPB, ViewInfoPB, ViewLayoutTypePB};
use crate::manager::{ViewDataProcessor, ViewDataProcessorMap};
use crate::{
dart_notification::{send_dart_notification, FolderNotification},
@ -122,12 +122,12 @@ impl ViewController {
.await
}
#[tracing::instrument(level = "debug", skip(self, view_id), fields(view_id = %view_id.value), err)]
pub(crate) async fn read_view(&self, view_id: ViewIdPB) -> Result<ViewRevision, FlowyError> {
#[tracing::instrument(level = "debug", skip(self, view_id), err)]
pub(crate) async fn read_view(&self, view_id: &str) -> Result<ViewRevision, FlowyError> {
let view_rev = self
.persistence
.begin_transaction(|transaction| {
let view = transaction.read_view(&view_id.value)?;
let view = transaction.read_view(view_id)?;
let trash_ids = self.trash_controller.read_trash_ids(&transaction)?;
if trash_ids.contains(&view.id) {
return Err(FlowyError::record_not_found());
@ -135,7 +135,6 @@ impl ViewController {
Ok(view)
})
.await?;
let _ = self.read_view_on_server(view_id);
Ok(view_rev)
}
@ -201,9 +200,26 @@ impl ViewController {
let _ = KV::remove(LATEST_VIEW_ID);
}
}
let view_id_pb = ViewIdPB::from(view_id.as_str());
let deleted_view = self
.persistence
.begin_transaction(|transaction| {
let view = transaction.read_view(&view_id)?;
let views = read_belonging_views_on_local(&view.app_id, self.trash_controller.clone(), &transaction)?;
let index = views
.iter()
.position(|view| view.id == view_id)
.and_then(|index| Some(index as i32));
Ok(DeletedViewPB {
view_id: view_id.clone(),
index,
})
})
.await?;
send_dart_notification(&view_id, FolderNotification::ViewMoveToTrash)
.payload(view_id_pb)
.payload(deleted_view)
.send();
let processor = self.get_data_processor_from_view_id(&view_id).await?;

View File

@ -31,7 +31,7 @@ pub(crate) async fn read_view_handler(
controller: AppData<Arc<ViewController>>,
) -> DataResult<ViewPB, FlowyError> {
let view_id: ViewIdPB = data.into_inner();
let view_rev = controller.read_view(view_id.clone()).await?;
let view_rev = controller.read_view(&view_id.value).await?;
data_result(view_rev.into())
}