Skip to content

Commit

Permalink
fix: view title update issue
Browse files Browse the repository at this point in the history
  • Loading branch information
appflowy committed May 5, 2022
1 parent 448c134 commit 2f2b69d
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 82 deletions.
40 changes: 20 additions & 20 deletions frontend/app_flowy/lib/user/application/user_listener.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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!);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {

void _startListening() {
appListener.start(
viewsChanged: (result) {
onViewsChanged: (result) {
result.fold(
(views) {
if (!isClosed) {
Expand All @@ -51,7 +51,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
(error) => Log.error(error),
);
},
appUpdated: (app) {
onAppUpdated: (app) {
if (!isClosed) {
add(AppEvent.appDidUpdate(app));
}
Expand Down Expand Up @@ -97,7 +97,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -60,7 +60,7 @@ class AppListener {
}
}

Future<void> close() async {
Future<void> stop() async {
_parser = null;
await _subscription?.cancel();
_viewsChanged = null;
Expand Down
31 changes: 15 additions & 16 deletions frontend/app_flowy/lib/workspace/application/doc/doc_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -70,21 +70,20 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
}

Future<void> _initial(Initial value, Emitter<DocumentState> emit) async {
listener.deletedNotifier.addPublishListener((result) {
result.fold(
(view) => add(const DocumentEvent.deleted()),
(error) {},
);
});

listener.restoredNotifier.addPublishListener((result) {
result.fold(
(view) => add(const DocumentEvent.restore()),
(error) {},
);
});

listener.start();
listener.start(
onViewDeleted: (result) {
result.fold(
(view) => add(const DocumentEvent.deleted()),
(error) {},
);
},
onViewRestored: (result) {
result.fold(
(view) => add(const DocumentEvent.restore()),
(error) {},
);
},
);
final result = await service.openDocument(docId: view.id);
result.fold(
(block) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
Expand Down
16 changes: 5 additions & 11 deletions frontend/app_flowy/lib/workspace/application/view/view_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,22 @@ 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) {
emit(state.copyWith(isEditing: e.isEditing));
},
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)),
Expand Down Expand Up @@ -74,7 +69,7 @@ class ViewBloc extends Bloc<ViewEvent, ViewState> {

@override
Future<void> close() async {
await listener.close();
await listener.stop();
return super.close();
}
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,39 @@ 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;

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) {
Expand All @@ -40,32 +62,32 @@ 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:
break;
}
}

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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -69,12 +69,11 @@ class DocumentPlugin implements Plugin {
(error) {},
);
});
_listener?.start();
}

@override
void dispose() {
_listener?.close();
_listener?.stop();
_listener = null;
}

Expand Down
Loading

0 comments on commit 2f2b69d

Please sign in to comment.