notification.rs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. use std::sync::Arc;
  2. use collab_folder::core::{View, Workspace};
  3. use flowy_derive::ProtoBuf_Enum;
  4. use flowy_notification::NotificationBuilder;
  5. use lib_dispatch::prelude::ToBytes;
  6. use crate::entities::{view_pb_without_child_views, WorkspacePB, WorkspaceSettingPB};
  7. const FOLDER_OBSERVABLE_SOURCE: &str = "Workspace";
  8. #[derive(ProtoBuf_Enum, Debug, Default)]
  9. pub(crate) enum FolderNotification {
  10. #[default]
  11. Unknown = 0,
  12. /// Trigger after creating a workspace
  13. DidCreateWorkspace = 1,
  14. // /// Trigger after updating a workspace
  15. DidUpdateWorkspace = 2,
  16. DidUpdateWorkspaceViews = 3,
  17. /// Trigger when the settings of the workspace are changed. The changes including the latest visiting view, etc
  18. DidUpdateWorkspaceSetting = 4,
  19. DidUpdateView = 10,
  20. DidUpdateChildViews = 11,
  21. /// Trigger after deleting the view
  22. DidDeleteView = 12,
  23. /// Trigger when restore the view from trash
  24. DidRestoreView = 13,
  25. /// Trigger after moving the view to trash
  26. DidMoveViewToTrash = 14,
  27. /// Trigger when the number of trash is changed
  28. DidUpdateTrash = 15,
  29. DidUpdateFolderSnapshotState = 16,
  30. DidUpdateFolderSyncUpdate = 17,
  31. }
  32. impl std::convert::From<FolderNotification> for i32 {
  33. fn from(notification: FolderNotification) -> Self {
  34. notification as i32
  35. }
  36. }
  37. impl std::convert::From<i32> for FolderNotification {
  38. fn from(value: i32) -> Self {
  39. match value {
  40. 1 => FolderNotification::DidCreateWorkspace,
  41. 2 => FolderNotification::DidUpdateWorkspace,
  42. 3 => FolderNotification::DidUpdateWorkspaceViews,
  43. 4 => FolderNotification::DidUpdateWorkspaceSetting,
  44. 10 => FolderNotification::DidUpdateView,
  45. 11 => FolderNotification::DidUpdateChildViews,
  46. 12 => FolderNotification::DidDeleteView,
  47. 13 => FolderNotification::DidRestoreView,
  48. 14 => FolderNotification::DidMoveViewToTrash,
  49. 15 => FolderNotification::DidUpdateTrash,
  50. 16 => FolderNotification::DidUpdateFolderSnapshotState,
  51. 17 => FolderNotification::DidUpdateFolderSyncUpdate,
  52. _ => FolderNotification::Unknown,
  53. }
  54. }
  55. }
  56. #[tracing::instrument(level = "trace")]
  57. pub(crate) fn send_notification(id: &str, ty: FolderNotification) -> NotificationBuilder {
  58. NotificationBuilder::new(id, ty, FOLDER_OBSERVABLE_SOURCE)
  59. }
  60. /// The [CURRENT_WORKSPACE] represents as the current workspace that opened by the
  61. /// user. Only one workspace can be opened at a time.
  62. const CURRENT_WORKSPACE: &str = "current-workspace";
  63. pub(crate) fn send_workspace_notification<T: ToBytes>(ty: FolderNotification, payload: T) {
  64. send_notification(CURRENT_WORKSPACE, ty)
  65. .payload(payload)
  66. .send();
  67. }
  68. pub(crate) fn send_workspace_setting_notification(
  69. current_workspace: Option<Workspace>,
  70. current_view: Option<Arc<View>>,
  71. ) -> Option<()> {
  72. let workspace: WorkspacePB = current_workspace?.into();
  73. let latest_view = current_view.map(view_pb_without_child_views);
  74. let setting = WorkspaceSettingPB {
  75. workspace,
  76. latest_view,
  77. };
  78. send_workspace_notification(FolderNotification::DidUpdateWorkspaceSetting, setting);
  79. None
  80. }