202 lines
8.1 KiB
Dart
Raw Normal View History

2021-10-10 11:40:45 +08:00
import 'package:app_flowy/workspace/presentation/widgets/menu/widget/top_bar.dart';
2021-07-12 23:27:58 +08:00
import 'package:dartz/dartz.dart';
2021-07-13 08:47:26 +08:00
import 'package:flowy_infra/size.dart';
2021-10-14 22:58:20 +08:00
import 'package:flowy_infra_ui/style_widget/scrolling/styled_list.dart';
import 'package:flowy_infra_ui/widget/error_page.dart';
2021-10-10 11:16:27 +08:00
import 'package:flowy_infra_ui/widget/spacing.dart';
2021-09-04 16:53:58 +08:00
import 'package:flowy_sdk/protobuf/flowy-user/user_profile.pb.dart';
2021-07-26 08:34:40 +08:00
import 'package:flowy_sdk/protobuf/flowy-workspace/app_create.pb.dart';
2021-07-12 23:27:58 +08:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:styled_widget/styled_widget.dart';
2021-10-11 13:15:41 +08:00
import 'package:expandable/expandable.dart';
import 'package:flowy_infra/time/duration.dart';
2021-07-26 11:51:39 +08:00
import 'package:app_flowy/startup/startup.dart';
import 'package:app_flowy/workspace/application/menu/menu_bloc.dart';
2021-10-11 13:15:41 +08:00
import 'package:app_flowy/workspace/application/menu/menu_listen.dart';
2021-07-26 11:51:39 +08:00
import 'package:app_flowy/workspace/domain/page_stack/page_stack.dart';
2021-10-11 13:15:41 +08:00
import 'package:app_flowy/workspace/presentation/widgets/menu/widget/menu_user.dart';
2021-07-26 11:51:39 +08:00
2021-10-11 13:15:41 +08:00
import 'widget/app/menu_app.dart';
2021-10-10 11:40:45 +08:00
import 'widget/app/create_button.dart';
2021-10-12 16:58:05 +08:00
import 'widget/menu_trash.dart';
2021-07-12 23:27:58 +08:00
2021-10-11 13:15:41 +08:00
// [[diagram: HomeMenu's widget structure]]
// get user profile or modify user
// ┌──────┐
// ┌──────────┐ ┌──▶│IUser │
// ┌─▶│MenuTopBar│ ┌────────┐ ┌─────────────┐ │ └──────┘
// │ └──────────┘ ┌───│MenuUser│─▶│MenuUserBloc │──┤
// ┌──────────┐ │ │ └────────┘ └─────────────┘ │ ┌─────────────┐
// │ HomeMenu │─┤ │ └──▶│IUserListener│
// └──────────┘ │ │ └─────────────┘
// │ │ listen workspace changes or user
// │ impl │ profile changes
// │ ┌──────────┐ ┌─────────┐ │
// └─▶│ MenuList │───▶│MenuItem │◀─┤
// └──────────┘ └─────────┘ │ ┌────────┐
// │ ┌─▶│AppBloc │ fetch app's views or modify view
// │ │ └────────┘
// │ ┌────────┐ │
// └───│MenuApp │──┤
// └────────┘ │
// │ ┌──────────────┐
// └─▶│AppListenBloc │ Receive view changes
// └──────────────┘
2021-07-12 23:27:58 +08:00
class HomeMenu extends StatelessWidget {
2021-10-10 15:58:57 +08:00
final Function(HomeStackContext) pageContextChanged;
2021-07-12 23:27:58 +08:00
final Function(bool) isCollapseChanged;
2021-09-04 16:53:58 +08:00
final UserProfile user;
final String workspaceId;
2021-07-12 23:27:58 +08:00
const HomeMenu({
Key? key,
required this.pageContextChanged,
required this.isCollapseChanged,
required this.user,
required this.workspaceId,
}) : super(key: key);
2021-07-12 23:27:58 +08:00
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<MenuBloc>(
2021-10-10 11:16:27 +08:00
create: (context) => getIt<MenuBloc>(param1: user, param2: workspaceId)..add(const MenuEvent.initial())),
BlocProvider(
create: (context) =>
2021-10-11 13:15:41 +08:00
getIt<MenuListenBloc>(param1: user, param2: workspaceId)..add(const MenuListenEvent.started())),
],
2021-07-20 23:51:08 +08:00
child: MultiBlocListener(
listeners: [
BlocListener<MenuBloc, MenuState>(
2021-10-10 15:58:57 +08:00
listenWhen: (p, c) => p.context != c.context,
listener: (context, state) => pageContextChanged(state.context),
2021-07-12 23:27:58 +08:00
),
2021-07-20 23:51:08 +08:00
BlocListener<MenuBloc, MenuState>(
listenWhen: (p, c) => p.isCollapse != c.isCollapse,
listener: (context, state) => isCollapseChanged(state.isCollapse),
)
],
child: BlocBuilder<MenuBloc, MenuState>(
builder: (context, state) => _renderBody(context),
),
),
);
2021-07-12 23:27:58 +08:00
}
2021-07-20 23:51:08 +08:00
Widget _renderBody(BuildContext context) {
2021-07-25 23:12:58 +08:00
// nested cloumn: https://siddharthmolleti.com/flutter-box-constraints-nested-column-s-row-s-3dfacada7361
2021-07-20 23:51:08 +08:00
return Container(
2021-07-25 23:12:58 +08:00
color: Theme.of(context).colorScheme.background,
2021-07-20 23:51:08 +08:00
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
2021-07-25 23:12:58 +08:00
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
_renderTopBar(context),
2021-10-10 11:16:27 +08:00
const VSpace(32),
2021-07-26 08:34:40 +08:00
_renderMenuList(context),
2021-07-25 23:12:58 +08:00
],
).padding(horizontal: Insets.l),
),
2021-10-12 16:58:05 +08:00
const VSpace(20),
_renderTrash(context).padding(horizontal: Insets.l),
const VSpace(20),
2021-07-25 23:12:58 +08:00
_renderNewAppButton(context),
2021-07-20 23:51:08 +08:00
],
2021-07-25 23:12:58 +08:00
),
2021-07-20 23:51:08 +08:00
);
2021-07-12 23:27:58 +08:00
}
2021-07-26 08:34:40 +08:00
Widget _renderMenuList(BuildContext context) {
2021-10-11 13:15:41 +08:00
return BlocBuilder<MenuListenBloc, MenuListenState>(
2021-07-26 08:34:40 +08:00
builder: (context, state) {
return state.map(
2021-07-26 11:51:39 +08:00
initial: (_) => MenuList(
2021-10-12 16:58:05 +08:00
menuItems: buildMenuItems(context.read<MenuBloc>().state.apps),
2021-07-26 11:51:39 +08:00
),
loadApps: (s) => MenuList(
2021-10-12 16:58:05 +08:00
menuItems: buildMenuItems(some(s.apps)),
2021-07-26 08:34:40 +08:00
),
loadFail: (s) => FlowyErrorPage(s.error.toString()),
);
},
);
}
2021-10-12 16:58:05 +08:00
Widget _renderTrash(BuildContext context) {
return const MenuTrash();
}
2021-07-25 23:12:58 +08:00
Widget _renderNewAppButton(BuildContext context) {
return NewAppButton(
2021-10-10 11:16:27 +08:00
press: (appName) => context.read<MenuBloc>().add(MenuEvent.createApp(appName, desc: "")),
);
}
2021-07-25 23:12:58 +08:00
Widget _renderTopBar(BuildContext context) {
2021-10-10 11:16:27 +08:00
return const MenuTopBar();
2021-07-25 23:12:58 +08:00
}
2021-07-26 08:34:40 +08:00
2021-10-12 16:58:05 +08:00
List<MenuItem> buildMenuItems(Option<List<App>> apps) {
List<MenuItem> items = [];
2021-07-26 11:51:39 +08:00
items.add(MenuUser(user));
2021-10-12 16:58:05 +08:00
List<MenuItem> appWidgets =
apps.foldRight([], (apps, _) => apps.map((app) => MenuApp(MenuAppContext(app))).toList());
2021-07-20 23:51:08 +08:00
2021-10-12 16:58:05 +08:00
items.addAll(appWidgets);
2021-07-26 11:51:39 +08:00
return items;
2021-07-20 23:51:08 +08:00
}
}
2021-10-11 13:15:41 +08:00
enum MenuItemType {
userProfile,
dashboard,
favorites,
app,
}
abstract class MenuItem extends StatelessWidget {
const MenuItem({Key? key}) : super(key: key);
MenuItemType get type;
}
class MenuList extends StatelessWidget {
final List<MenuItem> menuItems;
const MenuList({required this.menuItems, Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return ExpandableTheme(
data: ExpandableThemeData(useInkWell: true, animationDuration: Durations.medium),
child: Expanded(
2021-10-14 22:58:20 +08:00
child: ScrollConfiguration(
behavior: const ScrollBehavior(),
child: ListView.separated(
itemCount: menuItems.length,
separatorBuilder: (context, index) {
if (index == 0) {
return const VSpace(29);
} else {
return const VSpace(24);
}
},
physics: StyledScrollPhysics(),
itemBuilder: (BuildContext context, int index) {
return menuItems[index];
},
),
2021-10-11 13:15:41 +08:00
),
),
);
}
}
2021-10-14 22:58:20 +08:00
class _NoGlowBehavior extends ScrollBehavior {}