feat: add a loading indicator when creating, deleting, or switching workspaces. (#5067)

This commit is contained in:
Lucas.Xu
2024-04-07 23:06:33 +08:00
committed by GitHub
parent 3e32fac876
commit 9536cde789
25 changed files with 241 additions and 108 deletions

View File

@ -1,5 +1,9 @@
import 'dart:async';
import 'package:appflowy/startup/plugin/plugin.dart';
import 'package:appflowy/startup/startup.dart';
import 'package:appflowy/workspace/application/tabs/tabs_bloc.dart';
import 'package:appflowy/workspace/application/view/view_ext.dart';
import 'package:appflowy/workspace/application/workspace/workspace_sections_listener.dart';
import 'package:appflowy/workspace/application/workspace/workspace_service.dart';
import 'package:appflowy_backend/log.dart';
@ -152,6 +156,37 @@ class SidebarSectionsBloc
},
);
},
reload: (userProfile, workspaceId) async {
_initial(userProfile, workspaceId);
final sectionViews = await _getSectionViews();
if (sectionViews != null) {
emit(
state.copyWith(
section: sectionViews,
),
);
// try to open the fist view in public section or private section
if (sectionViews.publicViews.isNotEmpty) {
getIt<TabsBloc>().add(
TabsEvent.openPlugin(
plugin: sectionViews.publicViews.first.plugin(),
),
);
} else if (sectionViews.privateViews.isNotEmpty) {
getIt<TabsBloc>().add(
TabsEvent.openPlugin(
plugin: sectionViews.privateViews.first.plugin(),
),
);
} else {
getIt<TabsBloc>().add(
TabsEvent.openPlugin(
plugin: makePlugin(pluginType: PluginType.blank),
),
);
}
}
},
);
},
);
@ -245,6 +280,10 @@ class SidebarSectionsEvent with _$SidebarSectionsEvent {
const factory SidebarSectionsEvent.receiveSectionViewsUpdate(
SectionViewsPB sectionViews,
) = _ReceiveSectionViewsUpdate;
const factory SidebarSectionsEvent.reload(
UserProfilePB userProfile,
String workspaceId,
) = _Reload;
}
@freezed

View File

@ -40,7 +40,7 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
final currentWorkspace = result.$1;
final workspaces = result.$2;
final isCollabWorkspaceOn =
userProfile.authenticator != AuthenticatorPB.Local &&
userProfile.authenticator == AuthenticatorPB.AppFlowyCloud &&
FeatureFlag.collaborativeWorkspace.isOn;
if (currentWorkspace != null && result.$3 == true) {
final result = await _userService
@ -71,6 +71,15 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
);
},
createWorkspace: (name) async {
emit(
state.copyWith(
actionResult: const UserWorkspaceActionResult(
actionType: UserWorkspaceActionType.create,
isLoading: true,
result: null,
),
),
);
final result = await _userService.createUserWorkspace(name);
final workspaces = result.fold(
(s) => [...state.workspaces, s],
@ -81,6 +90,7 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
workspaces: workspaces,
actionResult: UserWorkspaceActionResult(
actionType: UserWorkspaceActionType.create,
isLoading: false,
result: result,
),
),
@ -91,6 +101,15 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
});
},
deleteWorkspace: (workspaceId) async {
emit(
state.copyWith(
actionResult: const UserWorkspaceActionResult(
actionType: UserWorkspaceActionType.delete,
isLoading: true,
result: null,
),
),
);
final remoteWorkspaces = await _fetchWorkspaces().then(
(value) => value.$2,
);
@ -108,6 +127,7 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
actionResult: UserWorkspaceActionResult(
actionType: UserWorkspaceActionType.delete,
result: result,
isLoading: false,
),
),
);
@ -134,11 +154,21 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
actionResult: UserWorkspaceActionResult(
actionType: UserWorkspaceActionType.delete,
result: result,
isLoading: false,
),
),
);
},
openWorkspace: (workspaceId) async {
emit(
state.copyWith(
actionResult: const UserWorkspaceActionResult(
actionType: UserWorkspaceActionType.open,
isLoading: true,
result: null,
),
),
);
final result = await _userService.openWorkspace(workspaceId);
final currentWorkspace = result.fold(
(s) => state.workspaces.firstWhereOrNull(
@ -157,6 +187,7 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
currentWorkspace: currentWorkspace,
actionResult: UserWorkspaceActionResult(
actionType: UserWorkspaceActionType.open,
isLoading: false,
result: result,
),
),
@ -188,6 +219,7 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
currentWorkspace: currentWorkspace,
actionResult: UserWorkspaceActionResult(
actionType: UserWorkspaceActionType.rename,
isLoading: false,
result: result,
),
),
@ -221,6 +253,7 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
currentWorkspace: currentWorkspace,
actionResult: UserWorkspaceActionResult(
actionType: UserWorkspaceActionType.updateIcon,
isLoading: false,
result: result,
),
),
@ -245,6 +278,7 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
workspaces: workspaces,
actionResult: UserWorkspaceActionResult(
actionType: UserWorkspaceActionType.leave,
isLoading: false,
result: result,
),
),
@ -253,7 +287,11 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
updateWorkspaces: (workspaces) async {
emit(
state.copyWith(
workspaces: workspaces.items,
workspaces: workspaces.items
..sort(
(a, b) =>
a.createdAtTimestamp.compareTo(b.createdAtTimestamp),
),
),
);
},
@ -359,11 +397,13 @@ enum UserWorkspaceActionType {
class UserWorkspaceActionResult {
const UserWorkspaceActionResult({
required this.actionType,
required this.isLoading,
required this.result,
});
final UserWorkspaceActionType actionType;
final FlowyResult<void, FlowyError> result;
final bool isLoading;
final FlowyResult<void, FlowyError>? result;
}
@freezed