fix: view title update issue

This commit is contained in:
appflowy 2022-05-05 21:15:01 +08:00
parent 448c134704
commit 2f2b69d1a6
11 changed files with 113 additions and 82 deletions

View File

@ -12,17 +12,17 @@ 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 UserProfileDidUpdate = Either<UserProfile, FlowyError>;
typedef AuthDidUpdate = Either<Unit, FlowyError>;
typedef WorkspaceListDidUpdate = Either<List<Workspace>, FlowyError>;
typedef WorkspaceSettingDidUpdate = Either<CurrentWorkspaceSetting, 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 _profileNotifier = PublishNotifier<UserProfileDidUpdate>();
final _authNotifier = PublishNotifier<AuthDidUpdate>();
final _workspaceListNotifier = PublishNotifier<WorkspaceListDidUpdate>();
final _workSettingNotifier = PublishNotifier<WorkspaceSettingDidUpdate>();
final _profileNotifier = PublishNotifier<UserProfileNotifyValue>();
final _authNotifier = PublishNotifier<AuthNotifyValue>();
final _workspaceListNotifier = PublishNotifier<WorkspaceListNotifyValue>();
final _workSettingNotifier = PublishNotifier<WorkspaceSettingNotifyValue>();
FolderNotificationParser? _workspaceParser;
UserNotificationParser? _userParser;
@ -32,32 +32,32 @@ class UserListener {
}) : _user = user;
void start({
void Function(AuthDidUpdate)? authDidChange,
void Function(UserProfileDidUpdate)? profileDidUpdate,
void Function(WorkspaceListDidUpdate)? workspaceListDidUpdate,
void Function(WorkspaceSettingDidUpdate)? workspaceSettingDidUpdate,
void Function(AuthNotifyValue)? onAuthChanged,
void Function(UserProfileNotifyValue)? onProfileUpdated,
void Function(WorkspaceListNotifyValue)? onWorkspaceListUpdated,
void Function(WorkspaceSettingNotifyValue)? onWorkspaceSettingUpdated,
}) {
if (authDidChange != null) {
if (onAuthChanged != null) {
_authNotifier.addListener(() {
authDidChange(_authNotifier.currentValue!);
onAuthChanged(_authNotifier.currentValue!);
});
}
if (profileDidUpdate != null) {
if (onProfileUpdated != null) {
_profileNotifier.addListener(() {
profileDidUpdate(_profileNotifier.currentValue!);
onProfileUpdated(_profileNotifier.currentValue!);
});
}
if (workspaceListDidUpdate != null) {
if (onWorkspaceListUpdated != null) {
_workspaceListNotifier.addListener(() {
workspaceListDidUpdate(_workspaceListNotifier.currentValue!);
onWorkspaceListUpdated(_workspaceListNotifier.currentValue!);
});
}
if (workspaceSettingDidUpdate != null) {
if (onWorkspaceSettingUpdated != null) {
_workSettingNotifier.addListener(() {
workspaceSettingDidUpdate(_workSettingNotifier.currentValue!);
onWorkspaceSettingUpdated(_workSettingNotifier.currentValue!);
});
}

View File

@ -41,7 +41,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
void _startListening() {
appListener.start(
viewsChanged: (result) {
onViewsChanged: (result) {
result.fold(
(views) {
if (!isClosed) {
@ -51,7 +51,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
(error) => Log.error(error),
);
},
appUpdated: (app) {
onAppUpdated: (app) {
if (!isClosed) {
add(AppEvent.appDidUpdate(app));
}
@ -97,7 +97,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
@override
Future<void> close() async {
await appListener.close();
await appListener.stop();
return super.close();
}

View File

@ -24,9 +24,9 @@ class AppListener {
required this.appId,
});
void start({ViewsDidChangeCallback? viewsChanged, AppDidUpdateCallback? appUpdated}) {
_viewsChanged = viewsChanged;
_updated = appUpdated;
void start({ViewsDidChangeCallback? onViewsChanged, AppDidUpdateCallback? onAppUpdated}) {
_viewsChanged = onViewsChanged;
_updated = onAppUpdated;
_parser = FolderNotificationParser(id: appId, callback: _bservableCallback);
_subscription = RustStreamReceiver.listen((observable) => _parser?.parse(observable));
}
@ -60,7 +60,7 @@ class AppListener {
}
}
Future<void> close() async {
Future<void> stop() async {
_parser = null;
await _subscription?.cancel();
_viewsChanged = null;

View File

@ -59,7 +59,7 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
@override
Future<void> close() async {
await listener.close();
await listener.stop();
if (_subscription != null) {
await _subscription?.cancel();
@ -70,21 +70,20 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
}
Future<void> _initial(Initial value, Emitter<DocumentState> emit) async {
listener.deletedNotifier.addPublishListener((result) {
listener.start(
onViewDeleted: (result) {
result.fold(
(view) => add(const DocumentEvent.deleted()),
(error) {},
);
});
listener.restoredNotifier.addPublishListener((result) {
},
onViewRestored: (result) {
result.fold(
(view) => add(const DocumentEvent.restore()),
(error) {},
);
});
listener.start();
},
);
final result = await service.openDocument(docId: view.id);
result.fold(
(block) {

View File

@ -20,10 +20,10 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
await event.map(
initial: (_Initial value) {
_listener.start(
authDidChange: (result) {
onAuthChanged: (result) {
_authDidChanged(result);
},
workspaceSettingDidUpdate: (result) {
onWorkspaceSettingUpdated: (result) {
result.fold(
(setting) => add(HomeEvent.didReceiveWorkspaceSetting(setting)),
(r) => Log.error(r),

View File

@ -20,8 +20,8 @@ class MenuUserBloc extends Bloc<MenuUserEvent, MenuUserState> {
await event.map(
initial: (_) async {
userListener.start(
profileDidUpdate: _profileUpdated,
workspaceListDidUpdate: _workspaceListUpdated,
onProfileUpdated: _profileUpdated,
onWorkspaceListUpdated: _workspaceListUpdated,
);
await _initUser();
},

View File

@ -21,12 +21,9 @@ class ViewBloc extends Bloc<ViewEvent, ViewState> {
on<ViewEvent>((event, emit) async {
await event.map(
initial: (e) {
// TODO: Listener can be refactored to a stream.
listener.updatedNotifier.addPublishListener((result) {
// emit.forEach(stream, onData: onData)
listener.start(onViewUpdated: (result) {
add(ViewEvent.viewDidUpdate(result));
});
listener.start();
emit(state);
},
setIsEditing: (e) {
@ -34,14 +31,12 @@ class ViewBloc extends Bloc<ViewEvent, ViewState> {
},
viewDidUpdate: (e) {
e.result.fold(
(view) =>
emit(state.copyWith(view: view, successOrFailure: left(unit))),
(view) => emit(state.copyWith(view: view, successOrFailure: left(unit))),
(error) => emit(state.copyWith(successOrFailure: right(error))),
);
},
rename: (e) async {
final result =
await service.updateView(viewId: view.id, name: e.newName);
final result = await service.updateView(viewId: view.id, name: e.newName);
emit(
result.fold(
(l) => state.copyWith(successOrFailure: left(unit)),
@ -74,7 +69,7 @@ class ViewBloc extends Bloc<ViewEvent, ViewState> {
@override
Future<void> close() async {
await listener.close();
await listener.stop();
return super.close();
}
}
@ -86,8 +81,7 @@ class ViewEvent with _$ViewEvent {
const factory ViewEvent.rename(String newName) = Rename;
const factory ViewEvent.delete() = Delete;
const factory ViewEvent.duplicate() = Duplicate;
const factory ViewEvent.viewDidUpdate(Either<View, FlowyError> result) =
ViewDidUpdate;
const factory ViewEvent.viewDidUpdate(Either<View, FlowyError> result) = ViewDidUpdate;
}
@freezed

View File

@ -15,9 +15,9 @@ typedef RestoreViewNotifiedValue = Either<View, FlowyError>;
class ViewListener {
StreamSubscription<SubscribeObject>? _subscription;
PublishNotifier<UpdateViewNotifiedValue> updatedNotifier = PublishNotifier<UpdateViewNotifiedValue>();
PublishNotifier<DeleteViewNotifyValue> deletedNotifier = PublishNotifier<DeleteViewNotifyValue>();
PublishNotifier<RestoreViewNotifiedValue> restoredNotifier = PublishNotifier<RestoreViewNotifiedValue>();
final PublishNotifier<UpdateViewNotifiedValue> _updatedViewNotifier = PublishNotifier();
final PublishNotifier<DeleteViewNotifyValue> _deletedNotifier = PublishNotifier();
final PublishNotifier<RestoreViewNotifiedValue> _restoredNotifier = PublishNotifier();
FolderNotificationParser? _parser;
View view;
@ -25,7 +25,29 @@ class ViewListener {
required this.view,
});
void start() {
void start({
void Function(UpdateViewNotifiedValue)? onViewUpdated,
void Function(DeleteViewNotifyValue)? onViewDeleted,
void Function(RestoreViewNotifiedValue)? onViewRestored,
}) {
if (onViewUpdated != null) {
_updatedViewNotifier.addListener(() {
onViewUpdated(_updatedViewNotifier.currentValue!);
});
}
if (onViewDeleted != null) {
_deletedNotifier.addListener(() {
onViewDeleted(_deletedNotifier.currentValue!);
});
}
if (onViewRestored != null) {
_restoredNotifier.addListener(() {
onViewRestored(_restoredNotifier.currentValue!);
});
}
_parser = FolderNotificationParser(
id: view.id,
callback: (ty, result) {
@ -40,20 +62,20 @@ class ViewListener {
switch (ty) {
case FolderNotification.ViewUpdated:
result.fold(
(payload) => updatedNotifier.value = left(View.fromBuffer(payload)),
(error) => updatedNotifier.value = right(error),
(payload) => _updatedViewNotifier.value = left(View.fromBuffer(payload)),
(error) => _updatedViewNotifier.value = right(error),
);
break;
case FolderNotification.ViewDeleted:
result.fold(
(payload) => deletedNotifier.value = left(View.fromBuffer(payload)),
(error) => deletedNotifier.value = right(error),
(payload) => _deletedNotifier.value = left(View.fromBuffer(payload)),
(error) => _deletedNotifier.value = right(error),
);
break;
case FolderNotification.ViewRestored:
result.fold(
(payload) => restoredNotifier.value = left(View.fromBuffer(payload)),
(error) => restoredNotifier.value = right(error),
(payload) => _restoredNotifier.value = left(View.fromBuffer(payload)),
(error) => _restoredNotifier.value = right(error),
);
break;
default:
@ -61,11 +83,11 @@ class ViewListener {
}
}
Future<void> close() async {
Future<void> stop() async {
_parser = null;
await _subscription?.cancel();
updatedNotifier.dispose();
deletedNotifier.dispose();
restoredNotifier.dispose();
_updatedViewNotifier.dispose();
_deletedNotifier.dispose();
_restoredNotifier.dispose();
}
}

View File

@ -17,7 +17,7 @@ class WelcomeBloc extends Bloc<WelcomeEvent, WelcomeState> {
(event, emit) async {
await event.map(initial: (e) async {
userListener.start(
workspaceListDidUpdate: (result) => add(WelcomeEvent.workspacesReveived(result)),
onWorkspaceListUpdated: (result) => add(WelcomeEvent.workspacesReveived(result)),
);
//
await _fetchWorkspaces(emit);

View File

@ -60,7 +60,7 @@ class DocumentPlugin implements Plugin {
DocumentPlugin({required PluginType pluginType, required View view, Key? key}) : _view = view {
_pluginType = pluginType;
_listener = getIt<ViewListener>(param1: view);
_listener?.updatedNotifier.addPublishListener((result) {
_listener?.start(onViewUpdated: (result) {
result.fold(
(newView) {
_view = newView;
@ -69,12 +69,11 @@ class DocumentPlugin implements Plugin {
(error) {},
);
});
_listener?.start();
}
@override
void dispose() {
_listener?.close();
_listener?.stop();
_listener = null;
}

View File

@ -1,5 +1,7 @@
import 'package:app_flowy/workspace/application/view/view_listener.dart';
import 'package:app_flowy/workspace/application/view/view_service.dart';
import 'package:flowy_infra/theme.dart';
import 'package:flowy_sdk/log.dart';
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/view.pb.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
@ -16,12 +18,26 @@ class ViewLeftBarItem extends StatefulWidget {
class _ViewLeftBarItemState extends State<ViewLeftBarItem> {
final _controller = TextEditingController();
final _focusNode = FocusNode();
late ViewService serviceService;
late ViewService _viewService;
late ViewListener _viewListener;
late View view;
@override
void initState() {
serviceService = ViewService(/*view: widget.view*/);
view = widget.view;
_viewService = ViewService();
_focusNode.addListener(_handleFocusChanged);
_viewListener = ViewListener(view: widget.view);
_viewListener.start(onViewUpdated: (result) {
result.fold(
(updatedView) {
if (mounted) {
setState(() => view = updatedView);
}
},
(err) => Log.error(err),
);
});
super.initState();
}
@ -30,12 +46,13 @@ class _ViewLeftBarItemState extends State<ViewLeftBarItem> {
_controller.dispose();
_focusNode.removeListener(_handleFocusChanged);
_focusNode.dispose();
_viewListener.stop();
super.dispose();
}
@override
Widget build(BuildContext context) {
_controller.text = widget.view.name;
_controller.text = view.name;
final theme = context.watch<AppTheme>();
return IntrinsicWidth(
@ -63,12 +80,12 @@ class _ViewLeftBarItemState extends State<ViewLeftBarItem> {
void _handleFocusChanged() {
if (_controller.text.isEmpty) {
_controller.text = widget.view.name;
_controller.text = view.name;
return;
}
if (_controller.text != widget.view.name) {
serviceService.updateView(viewId: widget.view.id, name: _controller.text);
if (_controller.text != view.name) {
_viewService.updateView(viewId: view.id, name: _controller.text);
}
}
}