|
@@ -12,26 +12,55 @@ import 'package:flowy_sdk/protobuf/flowy-user-data-model/user_profile.pb.dart';
|
|
|
import 'package:flowy_sdk/protobuf/flowy-user/dart_notification.pb.dart' as user;
|
|
|
import 'package:flowy_sdk/rust_stream.dart';
|
|
|
|
|
|
-typedef UserProfileUpdatedNotifierValue = Either<UserProfile, FlowyError>;
|
|
|
-typedef AuthNotifierValue = Either<Unit, FlowyError>;
|
|
|
-typedef WorkspaceUpdatedNotifierValue = Either<List<Workspace>, FlowyError>;
|
|
|
+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 profileUpdatedNotifier = PublishNotifier<UserProfileUpdatedNotifierValue>();
|
|
|
- final authDidChangedNotifier = PublishNotifier<AuthNotifierValue>();
|
|
|
- final workspaceUpdatedNotifier = PublishNotifier<WorkspaceUpdatedNotifierValue>();
|
|
|
+ final _profileNotifier = PublishNotifier<UserProfileNotifyValue>();
|
|
|
+ final _authNotifier = PublishNotifier<AuthNotifyValue>();
|
|
|
+ final _workspaceListNotifier = PublishNotifier<WorkspaceListNotifyValue>();
|
|
|
+ final _workSettingNotifier = PublishNotifier<WorkspaceSettingNotifyValue>();
|
|
|
|
|
|
FolderNotificationParser? _workspaceParser;
|
|
|
UserNotificationParser? _userParser;
|
|
|
- late UserProfile _user;
|
|
|
+ final UserProfile _user;
|
|
|
UserListener({
|
|
|
required UserProfile user,
|
|
|
+ }) : _user = user;
|
|
|
+
|
|
|
+ void start({
|
|
|
+ void Function(AuthNotifyValue)? onAuthChanged,
|
|
|
+ void Function(UserProfileNotifyValue)? onProfileUpdated,
|
|
|
+ void Function(WorkspaceListNotifyValue)? onWorkspaceListUpdated,
|
|
|
+ void Function(WorkspaceSettingNotifyValue)? onWorkspaceSettingUpdated,
|
|
|
}) {
|
|
|
- _user = user;
|
|
|
- }
|
|
|
+ if (onAuthChanged != null) {
|
|
|
+ _authNotifier.addListener(() {
|
|
|
+ onAuthChanged(_authNotifier.currentValue!);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ if (onProfileUpdated != null) {
|
|
|
+ _profileNotifier.addListener(() {
|
|
|
+ onProfileUpdated(_profileNotifier.currentValue!);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ if (onWorkspaceListUpdated != null) {
|
|
|
+ _workspaceListNotifier.addListener(() {
|
|
|
+ onWorkspaceListUpdated(_workspaceListNotifier.currentValue!);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ if (onWorkspaceSettingUpdated != null) {
|
|
|
+ _workSettingNotifier.addListener(() {
|
|
|
+ onWorkspaceSettingUpdated(_workSettingNotifier.currentValue!);
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
- void start() {
|
|
|
_workspaceParser = FolderNotificationParser(id: _user.token, callback: _notificationCallback);
|
|
|
_userParser = UserNotificationParser(id: _user.token, callback: _userNotificationCallback);
|
|
|
_subscription = RustStreamReceiver.listen((observable) {
|
|
@@ -44,9 +73,9 @@ class UserListener {
|
|
|
_workspaceParser = null;
|
|
|
_userParser = null;
|
|
|
await _subscription?.cancel();
|
|
|
- profileUpdatedNotifier.dispose();
|
|
|
- authDidChangedNotifier.dispose();
|
|
|
- workspaceUpdatedNotifier.dispose();
|
|
|
+ _profileNotifier.dispose();
|
|
|
+ _authNotifier.dispose();
|
|
|
+ _workspaceListNotifier.dispose();
|
|
|
}
|
|
|
|
|
|
void _notificationCallback(FolderNotification ty, Either<Uint8List, FlowyError> result) {
|
|
@@ -55,16 +84,23 @@ class UserListener {
|
|
|
case FolderNotification.UserDeleteWorkspace:
|
|
|
case FolderNotification.WorkspaceListUpdated:
|
|
|
result.fold(
|
|
|
- (payload) => workspaceUpdatedNotifier.value = left(RepeatedWorkspace.fromBuffer(payload).items),
|
|
|
- (error) => workspaceUpdatedNotifier.value = right(error),
|
|
|
+ (payload) => _workspaceListNotifier.value = left(RepeatedWorkspace.fromBuffer(payload).items),
|
|
|
+ (error) => _workspaceListNotifier.value = right(error),
|
|
|
+ );
|
|
|
+ break;
|
|
|
+ case FolderNotification.WorkspaceSetting:
|
|
|
+ result.fold(
|
|
|
+ (payload) => _workSettingNotifier.value = left(CurrentWorkspaceSetting.fromBuffer(payload)),
|
|
|
+ (error) => _workSettingNotifier.value = right(error),
|
|
|
);
|
|
|
break;
|
|
|
case FolderNotification.UserUnauthorized:
|
|
|
result.fold(
|
|
|
(_) {},
|
|
|
- (error) => authDidChangedNotifier.value = right(FlowyError.create()..code = ErrorCode.UserUnauthorized.value),
|
|
|
+ (error) => _authNotifier.value = right(FlowyError.create()..code = ErrorCode.UserUnauthorized.value),
|
|
|
);
|
|
|
break;
|
|
|
+
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
@@ -74,8 +110,8 @@ class UserListener {
|
|
|
switch (ty) {
|
|
|
case user.UserNotification.UserUnauthorized:
|
|
|
result.fold(
|
|
|
- (payload) => profileUpdatedNotifier.value = left(UserProfile.fromBuffer(payload)),
|
|
|
- (error) => profileUpdatedNotifier.value = right(error),
|
|
|
+ (payload) => _profileNotifier.value = left(UserProfile.fromBuffer(payload)),
|
|
|
+ (error) => _profileNotifier.value = right(error),
|
|
|
);
|
|
|
break;
|
|
|
default:
|