Skip to content

Commit

Permalink
fix: update workspace setting
Browse files Browse the repository at this point in the history
  • Loading branch information
appflowy committed May 5, 2022
1 parent 6c2c3b0 commit 448c134
Show file tree
Hide file tree
Showing 16 changed files with 196 additions and 120 deletions.
7 changes: 0 additions & 7 deletions frontend/app_flowy/lib/startup/deps_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import 'package:app_flowy/workspace/application/grid/prelude.dart';
import 'package:app_flowy/workspace/application/trash/prelude.dart';
import 'package:app_flowy/workspace/application/workspace/prelude.dart';
import 'package:app_flowy/workspace/application/edit_pannel/edit_pannel_bloc.dart';
import 'package:app_flowy/workspace/application/home/home_bloc.dart';
import 'package:app_flowy/workspace/application/view/prelude.dart';
import 'package:app_flowy/workspace/application/home/prelude.dart';
import 'package:app_flowy/workspace/application/menu/prelude.dart';
import 'package:app_flowy/user/application/prelude.dart';
import 'package:app_flowy/user/presentation/router.dart';
Expand Down Expand Up @@ -45,7 +43,6 @@ void _resolveUserDeps(GetIt getIt) {
getIt.registerFactory<SignUpBloc>(() => SignUpBloc(getIt<AuthService>()));

getIt.registerFactory<SplashRoute>(() => SplashRoute());
getIt.registerFactory<HomeBloc>(() => HomeBloc());
getIt.registerFactory<EditPannelBloc>(() => EditPannelBloc());
getIt.registerFactory<SplashBloc>(() => SplashBloc());
getIt.registerLazySingleton<NetworkListener>(() => NetworkListener());
Expand All @@ -58,10 +55,6 @@ void _resolveHomeDeps(GetIt getIt) {
(user, _) => UserListener(user: user),
);

getIt.registerFactoryParam<HomeListenBloc, UserProfile, void>(
(user, _) => HomeListenBloc(getIt<UserListener>(param1: user)),
);

//
getIt.registerLazySingleton<HomeStackManager>(() => HomeStackManager());

Expand Down
72 changes: 54 additions & 18 deletions frontend/app_flowy/lib/user/application/user_listener.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 UserProfileDidUpdate = Either<UserProfile, FlowyError>;
typedef AuthDidUpdate = Either<Unit, FlowyError>;
typedef WorkspaceListDidUpdate = Either<List<Workspace>, FlowyError>;
typedef WorkspaceSettingDidUpdate = Either<CurrentWorkspaceSetting, FlowyError>;

class UserListener {
StreamSubscription<SubscribeObject>? _subscription;
final profileUpdatedNotifier = PublishNotifier<UserProfileUpdatedNotifierValue>();
final authDidChangedNotifier = PublishNotifier<AuthNotifierValue>();
final workspaceUpdatedNotifier = PublishNotifier<WorkspaceUpdatedNotifierValue>();
final _profileNotifier = PublishNotifier<UserProfileDidUpdate>();
final _authNotifier = PublishNotifier<AuthDidUpdate>();
final _workspaceListNotifier = PublishNotifier<WorkspaceListDidUpdate>();
final _workSettingNotifier = PublishNotifier<WorkspaceSettingDidUpdate>();

FolderNotificationParser? _workspaceParser;
UserNotificationParser? _userParser;
late UserProfile _user;
final UserProfile _user;
UserListener({
required UserProfile user,
}) : _user = user;

void start({
void Function(AuthDidUpdate)? authDidChange,
void Function(UserProfileDidUpdate)? profileDidUpdate,
void Function(WorkspaceListDidUpdate)? workspaceListDidUpdate,
void Function(WorkspaceSettingDidUpdate)? workspaceSettingDidUpdate,
}) {
_user = user;
}
if (authDidChange != null) {
_authNotifier.addListener(() {
authDidChange(_authNotifier.currentValue!);
});
}

if (profileDidUpdate != null) {
_profileNotifier.addListener(() {
profileDidUpdate(_profileNotifier.currentValue!);
});
}

if (workspaceListDidUpdate != null) {
_workspaceListNotifier.addListener(() {
workspaceListDidUpdate(_workspaceListNotifier.currentValue!);
});
}

if (workspaceSettingDidUpdate != null) {
_workSettingNotifier.addListener(() {
workspaceSettingDidUpdate(_workSettingNotifier.currentValue!);
});
}

void start() {
_workspaceParser = FolderNotificationParser(id: _user.token, callback: _notificationCallback);
_userParser = UserNotificationParser(id: _user.token, callback: _userNotificationCallback);
_subscription = RustStreamReceiver.listen((observable) {
Expand All @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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:
Expand Down
51 changes: 48 additions & 3 deletions frontend/app_flowy/lib/workspace/application/home/home_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
import 'package:app_flowy/user/application/user_listener.dart';
import 'package:app_flowy/workspace/application/edit_pannel/edit_context.dart';
import 'package:flowy_sdk/log.dart';
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/workspace.pb.dart' show CurrentWorkspaceSetting;
import 'package:flowy_sdk/protobuf/flowy-user-data-model/errors.pb.dart';
import 'package:flowy_sdk/protobuf/flowy-user-data-model/user_profile.pb.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:dartz/dartz.dart';
part 'home_bloc.freezed.dart';

class HomeBloc extends Bloc<HomeEvent, HomeState> {
HomeBloc() : super(HomeState.initial()) {
final UserListener _listener;

HomeBloc(UserProfile user, CurrentWorkspaceSetting workspaceSetting)
: _listener = UserListener(user: user),
super(HomeState.initial(workspaceSetting)) {
on<HomeEvent>((event, emit) async {
await event.map(
initial: (_Initial value) {
_listener.start(
authDidChange: (result) {
_authDidChanged(result);
},
workspaceSettingDidUpdate: (result) {
result.fold(
(setting) => add(HomeEvent.didReceiveWorkspaceSetting(setting)),
(r) => Log.error(r),
);
},
);
},
showLoading: (e) async {
emit(state.copyWith(isLoading: e.isLoading));
},
Expand All @@ -20,22 +43,40 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
forceCollapse: (e) async {
emit(state.copyWith(forceCollapse: e.forceCollapse));
},
didReceiveWorkspaceSetting: (_DidReceiveWorkspaceSetting value) {
emit(state.copyWith(workspaceSetting: value.setting));
},
unauthorized: (_Unauthorized value) {
emit(state.copyWith(unauthorized: true));
},
);
});
}

@override
Future<void> close() {
Future<void> close() async {
await _listener.stop();
return super.close();
}

void _authDidChanged(Either<Unit, FlowyError> errorOrNothing) {
errorOrNothing.fold((_) {}, (error) {
if (error.code == ErrorCode.UserUnauthorized.value) {
add(HomeEvent.unauthorized(error.msg));
}
});
}
}

@freezed
class HomeEvent with _$HomeEvent {
const factory HomeEvent.initial() = _Initial;
const factory HomeEvent.showLoading(bool isLoading) = _ShowLoading;
const factory HomeEvent.forceCollapse(bool forceCollapse) = _ForceCollapse;
const factory HomeEvent.setEditPannel(EditPannelContext editContext) = _ShowEditPannel;
const factory HomeEvent.dismissEditPannel() = _DismissEditPannel;
const factory HomeEvent.didReceiveWorkspaceSetting(CurrentWorkspaceSetting setting) = _DidReceiveWorkspaceSetting;
const factory HomeEvent.unauthorized(String msg) = _Unauthorized;
}

@freezed
Expand All @@ -44,11 +85,15 @@ class HomeState with _$HomeState {
required bool isLoading,
required bool forceCollapse,
required Option<EditPannelContext> pannelContext,
required CurrentWorkspaceSetting workspaceSetting,
required bool unauthorized,
}) = _HomeState;

factory HomeState.initial() => HomeState(
factory HomeState.initial(CurrentWorkspaceSetting workspaceSetting) => HomeState(
isLoading: false,
forceCollapse: false,
pannelContext: none(),
workspaceSetting: workspaceSetting,
unauthorized: false,
);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
export 'home_listen_bloc.dart';

Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ class MenuUserBloc extends Bloc<MenuUserEvent, MenuUserState> {
on<MenuUserEvent>((event, emit) async {
await event.map(
initial: (_) async {
userListener.profileUpdatedNotifier.addPublishListener(_profileUpdated);
userListener.workspaceUpdatedNotifier.addPublishListener(_workspacesUpdated);
userListener.start();
userListener.start(
profileDidUpdate: _profileUpdated,
workspaceListDidUpdate: _workspaceListUpdated,
);
await _initUser();
},
fetchWorkspaces: (_FetchWorkspaces value) async {},
Expand All @@ -41,7 +42,7 @@ class MenuUserBloc extends Bloc<MenuUserEvent, MenuUserState> {
}

void _profileUpdated(Either<UserProfile, FlowyError> userOrFailed) {}
void _workspacesUpdated(Either<List<Workspace>, FlowyError> workspacesOrFailed) {
void _workspaceListUpdated(Either<List<Workspace>, FlowyError> workspacesOrFailed) {
// fetch workspaces
// iUserImpl.fetchWorkspaces().then((result) {
// result.fold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ class WelcomeBloc extends Bloc<WelcomeEvent, WelcomeState> {
on<WelcomeEvent>(
(event, emit) async {
await event.map(initial: (e) async {
userListener.workspaceUpdatedNotifier.addPublishListener(_workspacesUpdated);
userListener.start();
userListener.start(
workspaceListDidUpdate: (result) => add(WelcomeEvent.workspacesReveived(result)),
);
//
await _fetchWorkspaces(emit);
}, openWorkspace: (e) async {
Expand Down Expand Up @@ -74,10 +75,6 @@ class WelcomeBloc extends Bloc<WelcomeEvent, WelcomeState> {
},
));
}

void _workspacesUpdated(Either<List<Workspace>, FlowyError> workspacesOrFail) {
add(WelcomeEvent.workspacesReveived(workspacesOrFail));
}
}

@freezed
Expand Down
Loading

0 comments on commit 448c134

Please sign in to comment.