Skip to content

Commit

Permalink
feat: add loading for creating, opening, deleting workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 committed Apr 4, 2024
1 parent 5851370 commit 46d8ff1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -81,6 +90,7 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
workspaces: workspaces,
actionResult: UserWorkspaceActionResult(
actionType: UserWorkspaceActionType.create,
isLoading: false,
result: result,
),
),
Expand All @@ -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,
);
Expand All @@ -108,6 +127,7 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
actionResult: UserWorkspaceActionResult(
actionType: UserWorkspaceActionType.delete,
result: result,
isLoading: false,
),
),
);
Expand All @@ -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(
Expand All @@ -157,6 +187,7 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
currentWorkspace: currentWorkspace,
actionResult: UserWorkspaceActionResult(
actionType: UserWorkspaceActionType.open,
isLoading: false,
result: result,
),
),
Expand Down Expand Up @@ -188,6 +219,7 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
currentWorkspace: currentWorkspace,
actionResult: UserWorkspaceActionResult(
actionType: UserWorkspaceActionType.rename,
isLoading: false,
result: result,
),
),
Expand Down Expand Up @@ -221,6 +253,7 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
currentWorkspace: currentWorkspace,
actionResult: UserWorkspaceActionResult(
actionType: UserWorkspaceActionType.updateIcon,
isLoading: false,
result: result,
),
),
Expand All @@ -245,6 +278,7 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
workspaces: workspaces,
actionResult: UserWorkspaceActionResult(
actionType: UserWorkspaceActionType.leave,
isLoading: false,
result: result,
),
),
Expand All @@ -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),
),
),
);
},
Expand Down Expand Up @@ -298,6 +336,8 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
}
}
currentWorkspaceInList ??= workspaces.firstOrNull;
debugPrint('current workspace: $currentWorkspaceInList');
debugPrint('workspaces: $workspaces');
return (
currentWorkspaceInList,
workspaces
Expand Down Expand Up @@ -359,11 +399,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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:appflowy/generated/flowy_svgs.g.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/openai/widgets/loading.dart';
import 'package:appflowy/workspace/application/user/user_workspace_bloc.dart';
import 'package:appflowy/workspace/presentation/home/menu/sidebar/sidebar_setting.dart';
import 'package:appflowy/workspace/presentation/home/menu/sidebar/workspace/_sidebar_workspace_icon.dart';
Expand All @@ -16,14 +17,21 @@ import 'package:flowy_infra_ui/flowy_infra_ui.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class SidebarWorkspace extends StatelessWidget {
class SidebarWorkspace extends StatefulWidget {
const SidebarWorkspace({
super.key,
required this.userProfile,
});

final UserProfilePB userProfile;

@override
State<SidebarWorkspace> createState() => _SidebarWorkspaceState();
}

class _SidebarWorkspaceState extends State<SidebarWorkspace> {
Loading? loadingIndicator;

@override
Widget build(BuildContext context) {
return BlocConsumer<UserWorkspaceBloc, UserWorkspaceState>(
Expand All @@ -37,11 +45,11 @@ class SidebarWorkspace extends StatelessWidget {
children: [
Expanded(
child: SidebarSwitchWorkspaceButton(
userProfile: userProfile,
userProfile: widget.userProfile,
currentWorkspace: currentWorkspace,
),
),
UserSettingButton(userProfile: userProfile),
UserSettingButton(userProfile: widget.userProfile),
const HSpace(4),
const NotificationButton(),
],
Expand All @@ -58,6 +66,21 @@ class SidebarWorkspace extends StatelessWidget {

final actionType = actionResult.actionType;
final result = actionResult.result;
final isLoading = actionResult.isLoading;

if (isLoading) {
loadingIndicator?.stop();
loadingIndicator = Loading(context);
loadingIndicator?.start();
return;
}

loadingIndicator?.stop();
loadingIndicator = null;

if (result == null) {
return;
}

result.onFailure((f) {
Log.error(
Expand Down

0 comments on commit 46d8ff1

Please sign in to comment.