|
@@ -1,10 +1,11 @@
|
|
|
import 'dart:async';
|
|
|
+import 'package:app_flowy/core/folder_notification.dart';
|
|
|
+import 'package:app_flowy/core/user_notification.dart';
|
|
|
import 'package:dartz/dartz.dart';
|
|
|
import 'package:flowy_sdk/protobuf/flowy-error-code/code.pb.dart';
|
|
|
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/workspace.pb.dart';
|
|
|
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
|
|
import 'dart:typed_data';
|
|
|
-import 'package:app_flowy/core/notification_helper.dart';
|
|
|
import 'package:flowy_infra/notifier.dart';
|
|
|
import 'package:flowy_sdk/protobuf/dart-notify/protobuf.dart';
|
|
|
import 'package:flowy_sdk/protobuf/flowy-folder/dart_notification.pb.dart';
|
|
@@ -14,108 +15,140 @@ import 'package:flowy_sdk/rust_stream.dart';
|
|
|
|
|
|
typedef UserProfileNotifyValue = Either<UserProfile, FlowyError>;
|
|
|
typedef AuthNotifyValue = Either<Unit, FlowyError>;
|
|
|
-typedef WorkspaceListNotifyValue = Either<List<Workspace>, FlowyError>;
|
|
|
-typedef WorkspaceSettingNotifyValue = Either<CurrentWorkspaceSetting, FlowyError>;
|
|
|
|
|
|
class UserListener {
|
|
|
StreamSubscription<SubscribeObject>? _subscription;
|
|
|
- final _profileNotifier = PublishNotifier<UserProfileNotifyValue>();
|
|
|
- final _authNotifier = PublishNotifier<AuthNotifyValue>();
|
|
|
- final _workspaceListNotifier = PublishNotifier<WorkspaceListNotifyValue>();
|
|
|
- final _workSettingNotifier = PublishNotifier<WorkspaceSettingNotifyValue>();
|
|
|
+ PublishNotifier<AuthNotifyValue>? _authNotifier = PublishNotifier();
|
|
|
+ PublishNotifier<UserProfileNotifyValue>? _profileNotifier = PublishNotifier();
|
|
|
|
|
|
- FolderNotificationParser? _workspaceParser;
|
|
|
UserNotificationParser? _userParser;
|
|
|
- final UserProfile _user;
|
|
|
+ final UserProfile _userProfile;
|
|
|
UserListener({
|
|
|
- required UserProfile user,
|
|
|
- }) : _user = user;
|
|
|
+ required UserProfile userProfile,
|
|
|
+ }) : _userProfile = userProfile;
|
|
|
|
|
|
void start({
|
|
|
void Function(AuthNotifyValue)? onAuthChanged,
|
|
|
void Function(UserProfileNotifyValue)? onProfileUpdated,
|
|
|
- void Function(WorkspaceListNotifyValue)? onWorkspaceListUpdated,
|
|
|
- void Function(WorkspaceSettingNotifyValue)? onWorkspaceSettingUpdated,
|
|
|
}) {
|
|
|
- if (onAuthChanged != null) {
|
|
|
- _authNotifier.addListener(() {
|
|
|
- onAuthChanged(_authNotifier.currentValue!);
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
if (onProfileUpdated != null) {
|
|
|
- _profileNotifier.addListener(() {
|
|
|
- onProfileUpdated(_profileNotifier.currentValue!);
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- if (onWorkspaceListUpdated != null) {
|
|
|
- _workspaceListNotifier.addListener(() {
|
|
|
- onWorkspaceListUpdated(_workspaceListNotifier.currentValue!);
|
|
|
- });
|
|
|
+ _profileNotifier?.addPublishListener(onProfileUpdated);
|
|
|
}
|
|
|
|
|
|
- if (onWorkspaceSettingUpdated != null) {
|
|
|
- _workSettingNotifier.addListener(() {
|
|
|
- onWorkspaceSettingUpdated(_workSettingNotifier.currentValue!);
|
|
|
- });
|
|
|
+ if (onAuthChanged != null) {
|
|
|
+ _authNotifier?.addPublishListener(onAuthChanged);
|
|
|
}
|
|
|
|
|
|
- _workspaceParser = FolderNotificationParser(id: _user.token, callback: _notificationCallback);
|
|
|
- _userParser = UserNotificationParser(id: _user.token, callback: _userNotificationCallback);
|
|
|
+ _userParser = UserNotificationParser(id: _userProfile.token, callback: _userNotificationCallback);
|
|
|
_subscription = RustStreamReceiver.listen((observable) {
|
|
|
- _workspaceParser?.parse(observable);
|
|
|
_userParser?.parse(observable);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
Future<void> stop() async {
|
|
|
- _workspaceParser = null;
|
|
|
_userParser = null;
|
|
|
await _subscription?.cancel();
|
|
|
- _profileNotifier.dispose();
|
|
|
- _authNotifier.dispose();
|
|
|
- _workspaceListNotifier.dispose();
|
|
|
+ _profileNotifier?.dispose();
|
|
|
+ _profileNotifier = null;
|
|
|
+
|
|
|
+ _authNotifier?.dispose();
|
|
|
+ _authNotifier = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ void _userNotificationCallback(user.UserNotification ty, Either<Uint8List, FlowyError> result) {
|
|
|
+ switch (ty) {
|
|
|
+ case user.UserNotification.UserUnauthorized:
|
|
|
+ result.fold(
|
|
|
+ (_) {},
|
|
|
+ (error) => _authNotifier?.value = right(error),
|
|
|
+ );
|
|
|
+ break;
|
|
|
+ case user.UserNotification.UserProfileUpdated:
|
|
|
+ result.fold(
|
|
|
+ (payload) => _profileNotifier?.value = left(UserProfile.fromBuffer(payload)),
|
|
|
+ (error) => _profileNotifier?.value = right(error),
|
|
|
+ );
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+typedef WorkspaceListNotifyValue = Either<List<Workspace>, FlowyError>;
|
|
|
+typedef WorkspaceSettingNotifyValue = Either<CurrentWorkspaceSetting, FlowyError>;
|
|
|
+
|
|
|
+class UserWorkspaceListener {
|
|
|
+ PublishNotifier<AuthNotifyValue>? _authNotifier = PublishNotifier();
|
|
|
+ PublishNotifier<WorkspaceListNotifyValue>? _workspacesChangedNotifier = PublishNotifier();
|
|
|
+ PublishNotifier<WorkspaceSettingNotifyValue>? _settingChangedNotifier = PublishNotifier();
|
|
|
+
|
|
|
+ FolderNotificationListener? _listener;
|
|
|
+ final UserProfile _userProfile;
|
|
|
+
|
|
|
+ UserWorkspaceListener({
|
|
|
+ required UserProfile userProfile,
|
|
|
+ }) : _userProfile = userProfile;
|
|
|
+
|
|
|
+ void start({
|
|
|
+ void Function(AuthNotifyValue)? onAuthChanged,
|
|
|
+ void Function(WorkspaceListNotifyValue)? onWorkspacesUpdated,
|
|
|
+ void Function(WorkspaceSettingNotifyValue)? onSettingUpdated,
|
|
|
+ }) {
|
|
|
+ if (onAuthChanged != null) {
|
|
|
+ _authNotifier?.addPublishListener(onAuthChanged);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (onWorkspacesUpdated != null) {
|
|
|
+ _workspacesChangedNotifier?.addPublishListener(onWorkspacesUpdated);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (onSettingUpdated != null) {
|
|
|
+ _settingChangedNotifier?.addPublishListener(onSettingUpdated);
|
|
|
+ }
|
|
|
+
|
|
|
+ _listener = FolderNotificationListener(
|
|
|
+ objectId: _userProfile.token,
|
|
|
+ handler: _handleObservableType,
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
- void _notificationCallback(FolderNotification ty, Either<Uint8List, FlowyError> result) {
|
|
|
+ void _handleObservableType(FolderNotification ty, Either<Uint8List, FlowyError> result) {
|
|
|
switch (ty) {
|
|
|
case FolderNotification.UserCreateWorkspace:
|
|
|
case FolderNotification.UserDeleteWorkspace:
|
|
|
case FolderNotification.WorkspaceListUpdated:
|
|
|
result.fold(
|
|
|
- (payload) => _workspaceListNotifier.value = left(RepeatedWorkspace.fromBuffer(payload).items),
|
|
|
- (error) => _workspaceListNotifier.value = right(error),
|
|
|
+ (payload) => _workspacesChangedNotifier?.value = left(RepeatedWorkspace.fromBuffer(payload).items),
|
|
|
+ (error) => _workspacesChangedNotifier?.value = right(error),
|
|
|
);
|
|
|
break;
|
|
|
case FolderNotification.WorkspaceSetting:
|
|
|
result.fold(
|
|
|
- (payload) => _workSettingNotifier.value = left(CurrentWorkspaceSetting.fromBuffer(payload)),
|
|
|
- (error) => _workSettingNotifier.value = right(error),
|
|
|
+ (payload) => _settingChangedNotifier?.value = left(CurrentWorkspaceSetting.fromBuffer(payload)),
|
|
|
+ (error) => _settingChangedNotifier?.value = right(error),
|
|
|
);
|
|
|
break;
|
|
|
case FolderNotification.UserUnauthorized:
|
|
|
result.fold(
|
|
|
(_) {},
|
|
|
- (error) => _authNotifier.value = right(FlowyError.create()..code = ErrorCode.UserUnauthorized.value),
|
|
|
+ (error) => _authNotifier?.value = right(FlowyError.create()..code = ErrorCode.UserUnauthorized.value),
|
|
|
);
|
|
|
break;
|
|
|
-
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- void _userNotificationCallback(user.UserNotification ty, Either<Uint8List, FlowyError> result) {
|
|
|
- switch (ty) {
|
|
|
- case user.UserNotification.UserUnauthorized:
|
|
|
- result.fold(
|
|
|
- (payload) => _profileNotifier.value = left(UserProfile.fromBuffer(payload)),
|
|
|
- (error) => _profileNotifier.value = right(error),
|
|
|
- );
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
+ Future<void> stop() async {
|
|
|
+ await _listener?.stop();
|
|
|
+ _workspacesChangedNotifier?.dispose();
|
|
|
+ _workspacesChangedNotifier = null;
|
|
|
+
|
|
|
+ _settingChangedNotifier?.dispose();
|
|
|
+ _settingChangedNotifier = null;
|
|
|
+
|
|
|
+ _authNotifier?.dispose();
|
|
|
+ _authNotifier = null;
|
|
|
}
|
|
|
}
|