Skip to content

Commit 448c134

Browse files
committed
fix: update workspace setting
1 parent 6c2c3b0 commit 448c134

File tree

16 files changed

+196
-120
lines changed

16 files changed

+196
-120
lines changed

frontend/app_flowy/lib/startup/deps_resolver.dart

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import 'package:app_flowy/workspace/application/grid/prelude.dart';
77
import 'package:app_flowy/workspace/application/trash/prelude.dart';
88
import 'package:app_flowy/workspace/application/workspace/prelude.dart';
99
import 'package:app_flowy/workspace/application/edit_pannel/edit_pannel_bloc.dart';
10-
import 'package:app_flowy/workspace/application/home/home_bloc.dart';
1110
import 'package:app_flowy/workspace/application/view/prelude.dart';
12-
import 'package:app_flowy/workspace/application/home/prelude.dart';
1311
import 'package:app_flowy/workspace/application/menu/prelude.dart';
1412
import 'package:app_flowy/user/application/prelude.dart';
1513
import 'package:app_flowy/user/presentation/router.dart';
@@ -45,7 +43,6 @@ void _resolveUserDeps(GetIt getIt) {
4543
getIt.registerFactory<SignUpBloc>(() => SignUpBloc(getIt<AuthService>()));
4644

4745
getIt.registerFactory<SplashRoute>(() => SplashRoute());
48-
getIt.registerFactory<HomeBloc>(() => HomeBloc());
4946
getIt.registerFactory<EditPannelBloc>(() => EditPannelBloc());
5047
getIt.registerFactory<SplashBloc>(() => SplashBloc());
5148
getIt.registerLazySingleton<NetworkListener>(() => NetworkListener());
@@ -58,10 +55,6 @@ void _resolveHomeDeps(GetIt getIt) {
5855
(user, _) => UserListener(user: user),
5956
);
6057

61-
getIt.registerFactoryParam<HomeListenBloc, UserProfile, void>(
62-
(user, _) => HomeListenBloc(getIt<UserListener>(param1: user)),
63-
);
64-
6558
//
6659
getIt.registerLazySingleton<HomeStackManager>(() => HomeStackManager());
6760

frontend/app_flowy/lib/user/application/user_listener.dart

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,55 @@ import 'package:flowy_sdk/protobuf/flowy-user-data-model/user_profile.pb.dart';
1212
import 'package:flowy_sdk/protobuf/flowy-user/dart_notification.pb.dart' as user;
1313
import 'package:flowy_sdk/rust_stream.dart';
1414

15-
typedef UserProfileUpdatedNotifierValue = Either<UserProfile, FlowyError>;
16-
typedef AuthNotifierValue = Either<Unit, FlowyError>;
17-
typedef WorkspaceUpdatedNotifierValue = Either<List<Workspace>, FlowyError>;
15+
typedef UserProfileDidUpdate = Either<UserProfile, FlowyError>;
16+
typedef AuthDidUpdate = Either<Unit, FlowyError>;
17+
typedef WorkspaceListDidUpdate = Either<List<Workspace>, FlowyError>;
18+
typedef WorkspaceSettingDidUpdate = Either<CurrentWorkspaceSetting, FlowyError>;
1819

1920
class UserListener {
2021
StreamSubscription<SubscribeObject>? _subscription;
21-
final profileUpdatedNotifier = PublishNotifier<UserProfileUpdatedNotifierValue>();
22-
final authDidChangedNotifier = PublishNotifier<AuthNotifierValue>();
23-
final workspaceUpdatedNotifier = PublishNotifier<WorkspaceUpdatedNotifierValue>();
22+
final _profileNotifier = PublishNotifier<UserProfileDidUpdate>();
23+
final _authNotifier = PublishNotifier<AuthDidUpdate>();
24+
final _workspaceListNotifier = PublishNotifier<WorkspaceListDidUpdate>();
25+
final _workSettingNotifier = PublishNotifier<WorkspaceSettingDidUpdate>();
2426

2527
FolderNotificationParser? _workspaceParser;
2628
UserNotificationParser? _userParser;
27-
late UserProfile _user;
29+
final UserProfile _user;
2830
UserListener({
2931
required UserProfile user,
32+
}) : _user = user;
33+
34+
void start({
35+
void Function(AuthDidUpdate)? authDidChange,
36+
void Function(UserProfileDidUpdate)? profileDidUpdate,
37+
void Function(WorkspaceListDidUpdate)? workspaceListDidUpdate,
38+
void Function(WorkspaceSettingDidUpdate)? workspaceSettingDidUpdate,
3039
}) {
31-
_user = user;
32-
}
40+
if (authDidChange != null) {
41+
_authNotifier.addListener(() {
42+
authDidChange(_authNotifier.currentValue!);
43+
});
44+
}
45+
46+
if (profileDidUpdate != null) {
47+
_profileNotifier.addListener(() {
48+
profileDidUpdate(_profileNotifier.currentValue!);
49+
});
50+
}
51+
52+
if (workspaceListDidUpdate != null) {
53+
_workspaceListNotifier.addListener(() {
54+
workspaceListDidUpdate(_workspaceListNotifier.currentValue!);
55+
});
56+
}
57+
58+
if (workspaceSettingDidUpdate != null) {
59+
_workSettingNotifier.addListener(() {
60+
workspaceSettingDidUpdate(_workSettingNotifier.currentValue!);
61+
});
62+
}
3363

34-
void start() {
3564
_workspaceParser = FolderNotificationParser(id: _user.token, callback: _notificationCallback);
3665
_userParser = UserNotificationParser(id: _user.token, callback: _userNotificationCallback);
3766
_subscription = RustStreamReceiver.listen((observable) {
@@ -44,9 +73,9 @@ class UserListener {
4473
_workspaceParser = null;
4574
_userParser = null;
4675
await _subscription?.cancel();
47-
profileUpdatedNotifier.dispose();
48-
authDidChangedNotifier.dispose();
49-
workspaceUpdatedNotifier.dispose();
76+
_profileNotifier.dispose();
77+
_authNotifier.dispose();
78+
_workspaceListNotifier.dispose();
5079
}
5180

5281
void _notificationCallback(FolderNotification ty, Either<Uint8List, FlowyError> result) {
@@ -55,16 +84,23 @@ class UserListener {
5584
case FolderNotification.UserDeleteWorkspace:
5685
case FolderNotification.WorkspaceListUpdated:
5786
result.fold(
58-
(payload) => workspaceUpdatedNotifier.value = left(RepeatedWorkspace.fromBuffer(payload).items),
59-
(error) => workspaceUpdatedNotifier.value = right(error),
87+
(payload) => _workspaceListNotifier.value = left(RepeatedWorkspace.fromBuffer(payload).items),
88+
(error) => _workspaceListNotifier.value = right(error),
89+
);
90+
break;
91+
case FolderNotification.WorkspaceSetting:
92+
result.fold(
93+
(payload) => _workSettingNotifier.value = left(CurrentWorkspaceSetting.fromBuffer(payload)),
94+
(error) => _workSettingNotifier.value = right(error),
6095
);
6196
break;
6297
case FolderNotification.UserUnauthorized:
6398
result.fold(
6499
(_) {},
65-
(error) => authDidChangedNotifier.value = right(FlowyError.create()..code = ErrorCode.UserUnauthorized.value),
100+
(error) => _authNotifier.value = right(FlowyError.create()..code = ErrorCode.UserUnauthorized.value),
66101
);
67102
break;
103+
68104
default:
69105
break;
70106
}
@@ -74,8 +110,8 @@ class UserListener {
74110
switch (ty) {
75111
case user.UserNotification.UserUnauthorized:
76112
result.fold(
77-
(payload) => profileUpdatedNotifier.value = left(UserProfile.fromBuffer(payload)),
78-
(error) => profileUpdatedNotifier.value = right(error),
113+
(payload) => _profileNotifier.value = left(UserProfile.fromBuffer(payload)),
114+
(error) => _profileNotifier.value = right(error),
79115
);
80116
break;
81117
default:

frontend/app_flowy/lib/workspace/application/home/home_bloc.dart

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
1+
import 'package:app_flowy/user/application/user_listener.dart';
12
import 'package:app_flowy/workspace/application/edit_pannel/edit_context.dart';
3+
import 'package:flowy_sdk/log.dart';
4+
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
5+
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/workspace.pb.dart' show CurrentWorkspaceSetting;
6+
import 'package:flowy_sdk/protobuf/flowy-user-data-model/errors.pb.dart';
7+
import 'package:flowy_sdk/protobuf/flowy-user-data-model/user_profile.pb.dart';
28
import 'package:flutter_bloc/flutter_bloc.dart';
39
import 'package:freezed_annotation/freezed_annotation.dart';
410
import 'package:dartz/dartz.dart';
511
part 'home_bloc.freezed.dart';
612

713
class HomeBloc extends Bloc<HomeEvent, HomeState> {
8-
HomeBloc() : super(HomeState.initial()) {
14+
final UserListener _listener;
15+
16+
HomeBloc(UserProfile user, CurrentWorkspaceSetting workspaceSetting)
17+
: _listener = UserListener(user: user),
18+
super(HomeState.initial(workspaceSetting)) {
919
on<HomeEvent>((event, emit) async {
1020
await event.map(
21+
initial: (_Initial value) {
22+
_listener.start(
23+
authDidChange: (result) {
24+
_authDidChanged(result);
25+
},
26+
workspaceSettingDidUpdate: (result) {
27+
result.fold(
28+
(setting) => add(HomeEvent.didReceiveWorkspaceSetting(setting)),
29+
(r) => Log.error(r),
30+
);
31+
},
32+
);
33+
},
1134
showLoading: (e) async {
1235
emit(state.copyWith(isLoading: e.isLoading));
1336
},
@@ -20,22 +43,40 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
2043
forceCollapse: (e) async {
2144
emit(state.copyWith(forceCollapse: e.forceCollapse));
2245
},
46+
didReceiveWorkspaceSetting: (_DidReceiveWorkspaceSetting value) {
47+
emit(state.copyWith(workspaceSetting: value.setting));
48+
},
49+
unauthorized: (_Unauthorized value) {
50+
emit(state.copyWith(unauthorized: true));
51+
},
2352
);
2453
});
2554
}
2655

2756
@override
28-
Future<void> close() {
57+
Future<void> close() async {
58+
await _listener.stop();
2959
return super.close();
3060
}
61+
62+
void _authDidChanged(Either<Unit, FlowyError> errorOrNothing) {
63+
errorOrNothing.fold((_) {}, (error) {
64+
if (error.code == ErrorCode.UserUnauthorized.value) {
65+
add(HomeEvent.unauthorized(error.msg));
66+
}
67+
});
68+
}
3169
}
3270

3371
@freezed
3472
class HomeEvent with _$HomeEvent {
73+
const factory HomeEvent.initial() = _Initial;
3574
const factory HomeEvent.showLoading(bool isLoading) = _ShowLoading;
3675
const factory HomeEvent.forceCollapse(bool forceCollapse) = _ForceCollapse;
3776
const factory HomeEvent.setEditPannel(EditPannelContext editContext) = _ShowEditPannel;
3877
const factory HomeEvent.dismissEditPannel() = _DismissEditPannel;
78+
const factory HomeEvent.didReceiveWorkspaceSetting(CurrentWorkspaceSetting setting) = _DidReceiveWorkspaceSetting;
79+
const factory HomeEvent.unauthorized(String msg) = _Unauthorized;
3980
}
4081

4182
@freezed
@@ -44,11 +85,15 @@ class HomeState with _$HomeState {
4485
required bool isLoading,
4586
required bool forceCollapse,
4687
required Option<EditPannelContext> pannelContext,
88+
required CurrentWorkspaceSetting workspaceSetting,
89+
required bool unauthorized,
4790
}) = _HomeState;
4891

49-
factory HomeState.initial() => HomeState(
92+
factory HomeState.initial(CurrentWorkspaceSetting workspaceSetting) => HomeState(
5093
isLoading: false,
5194
forceCollapse: false,
5295
pannelContext: none(),
96+
workspaceSetting: workspaceSetting,
97+
unauthorized: false,
5398
);
5499
}

frontend/app_flowy/lib/workspace/application/home/home_listen_bloc.dart

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export 'home_listen_bloc.dart';
1+

frontend/app_flowy/lib/workspace/application/menu/menu_user_bloc.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ class MenuUserBloc extends Bloc<MenuUserEvent, MenuUserState> {
1919
on<MenuUserEvent>((event, emit) async {
2020
await event.map(
2121
initial: (_) async {
22-
userListener.profileUpdatedNotifier.addPublishListener(_profileUpdated);
23-
userListener.workspaceUpdatedNotifier.addPublishListener(_workspacesUpdated);
24-
userListener.start();
22+
userListener.start(
23+
profileDidUpdate: _profileUpdated,
24+
workspaceListDidUpdate: _workspaceListUpdated,
25+
);
2526
await _initUser();
2627
},
2728
fetchWorkspaces: (_FetchWorkspaces value) async {},
@@ -41,7 +42,7 @@ class MenuUserBloc extends Bloc<MenuUserEvent, MenuUserState> {
4142
}
4243

4344
void _profileUpdated(Either<UserProfile, FlowyError> userOrFailed) {}
44-
void _workspacesUpdated(Either<List<Workspace>, FlowyError> workspacesOrFailed) {
45+
void _workspaceListUpdated(Either<List<Workspace>, FlowyError> workspacesOrFailed) {
4546
// fetch workspaces
4647
// iUserImpl.fetchWorkspaces().then((result) {
4748
// result.fold(

frontend/app_flowy/lib/workspace/application/workspace/welcome_bloc.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ class WelcomeBloc extends Bloc<WelcomeEvent, WelcomeState> {
1616
on<WelcomeEvent>(
1717
(event, emit) async {
1818
await event.map(initial: (e) async {
19-
userListener.workspaceUpdatedNotifier.addPublishListener(_workspacesUpdated);
20-
userListener.start();
19+
userListener.start(
20+
workspaceListDidUpdate: (result) => add(WelcomeEvent.workspacesReveived(result)),
21+
);
2122
//
2223
await _fetchWorkspaces(emit);
2324
}, openWorkspace: (e) async {
@@ -74,10 +75,6 @@ class WelcomeBloc extends Bloc<WelcomeEvent, WelcomeState> {
7475
},
7576
));
7677
}
77-
78-
void _workspacesUpdated(Either<List<Workspace>, FlowyError> workspacesOrFail) {
79-
add(WelcomeEvent.workspacesReveived(workspacesOrFail));
80-
}
8178
}
8279

8380
@freezed

0 commit comments

Comments
 (0)