205 lines
6.4 KiB
Dart
Raw Normal View History

2021-07-22 17:06:53 +08:00
import 'package:app_flowy/workspace/application/menu/menu_bloc.dart';
import 'package:app_flowy/workspace/application/menu/menu_watch.dart';
import 'package:app_flowy/workspace/domain/page_stack/page_stack.dart';
2021-07-12 23:27:58 +08:00
import 'package:app_flowy/startup/startup.dart';
2021-07-26 08:34:40 +08:00
import 'package:app_flowy/workspace/presentation/app/app_widget.dart';
2021-07-22 17:06:53 +08:00
import 'package:app_flowy/workspace/presentation/home/home_sizes.dart';
2021-07-25 23:12:58 +08:00
import 'package:app_flowy/workspace/presentation/widgets/menu/create_app_dialog.dart';
2021-07-26 08:34:40 +08:00
import 'package:app_flowy/workspace/presentation/widgets/menu/user_profile.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-07-20 23:51:08 +08:00
import 'package:flowy_infra_ui/widget/dialog/styled_dialogs.dart';
import 'package:flowy_infra_ui/widget/error_page.dart';
2021-07-20 23:51:08 +08:00
import 'package:flowy_infra_ui/widget/spacing.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-07-26 08:34:40 +08:00
import 'menu_list.dart';
2021-07-12 23:27:58 +08:00
class HomeMenu extends StatelessWidget {
2021-07-22 18:04:24 +08:00
final Function(HomeStackView?) pageContextChanged;
2021-07-12 23:27:58 +08:00
final Function(bool) isCollapseChanged;
2021-07-21 22:41:44 +08:00
final String workspaceId;
2021-07-12 23:27:58 +08:00
const HomeMenu(
{Key? key,
required this.pageContextChanged,
2021-07-21 22:41:44 +08:00
required this.isCollapseChanged,
required this.workspaceId})
2021-07-12 23:27:58 +08:00
: super(key: key);
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<MenuBloc>(
create: (context) => getIt<MenuBloc>(param1: workspaceId)
..add(const MenuEvent.initial())),
BlocProvider(
create: (context) => getIt<MenuWatchBloc>(param1: workspaceId)
..add(const MenuWatchEvent.started())),
],
2021-07-20 23:51:08 +08:00
child: MultiBlocListener(
listeners: [
BlocListener<MenuBloc, MenuState>(
2021-07-22 18:04:24 +08:00
listenWhen: (p, c) => p.stackView != c.stackView,
listener: (context, state) => pageContextChanged(state.stackView),
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-07-26 08:34:40 +08:00
_renderMenuList(context),
2021-07-25 23:12:58 +08:00
],
).padding(horizontal: Insets.l),
),
_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) {
return BlocBuilder<MenuWatchBloc, MenuWatchState>(
2021-07-26 08:34:40 +08:00
builder: (context, state) {
return state.map(
initial: (_) => BlocBuilder<MenuBloc, MenuState>(
builder: (context, s) => MenuList(
menuItems: menuItemsWithApps(s.apps),
),
),
loadApps: (s) => MenuList(menuItems: menuItemsWithApps(some(s.apps))),
loadFail: (s) => FlowyErrorPage(s.error.toString()),
);
},
);
}
2021-07-25 23:12:58 +08:00
Widget _renderNewAppButton(BuildContext context) {
return NewAppButton(
2021-07-25 23:12:58 +08:00
press: (appName) =>
context.read<MenuBloc>().add(MenuEvent.createApp(appName, desc: "")),
);
}
2021-07-25 23:12:58 +08:00
Widget _renderTopBar(BuildContext context) {
return SizedBox(
height: HomeSizes.menuTopBarHeight,
child: const MenuTopBar(),
);
}
2021-07-26 08:34:40 +08:00
List<MenuItem> menuItemsWithApps(Option<List<App>> someApps) {
List<MenuItem> menuItems = [
const UserProfile(),
];
// apps
List<MenuItem> appWidgets = someApps.fold(
() => [],
(apps) => apps.map((app) => AppWidget(app)).toList(),
);
menuItems.addAll(appWidgets);
return menuItems;
}
2021-07-12 23:27:58 +08:00
}
class MenuTopBar extends StatelessWidget {
const MenuTopBar({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocBuilder<MenuBloc, MenuState>(
builder: (context, state) {
2021-07-25 23:12:58 +08:00
return Row(
children: [
const Image(
fit: BoxFit.cover,
width: 25,
height: 25,
image: AssetImage('assets/images/app_flowy_logo.jpg')),
const HSpace(8),
const Text(
'AppFlowy',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
),
const Spacer(),
IconButton(
icon: const Icon(Icons.arrow_left),
alignment: Alignment.centerRight,
padding: EdgeInsets.zero,
onPressed: () =>
context.read<MenuBloc>().add(const MenuEvent.collapse()),
),
],
2021-07-12 23:27:58 +08:00
);
},
);
}
}
class NewAppButton extends StatelessWidget {
2021-07-25 23:12:58 +08:00
final Function(String)? press;
2021-07-20 23:51:08 +08:00
2021-07-25 23:12:58 +08:00
const NewAppButton({this.press, Key? key}) : super(key: key);
2021-07-12 23:27:58 +08:00
@override
Widget build(BuildContext context) {
2021-07-25 23:12:58 +08:00
return Container(
decoration: BoxDecoration(
border: Border(
top: BorderSide(width: 1, color: Colors.grey.shade300),
),
),
2021-07-12 23:27:58 +08:00
height: HomeSizes.menuAddButtonHeight,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
2021-07-25 23:12:58 +08:00
const Icon(Icons.add_circle_rounded, size: 30),
2021-07-12 23:27:58 +08:00
TextButton(
2021-07-20 23:51:08 +08:00
onPressed: () async => await _showCreateAppDialog(context),
2021-07-25 23:12:58 +08:00
child: const Text(
'New App',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
2021-07-20 23:51:08 +08:00
)
],
2021-07-25 23:12:58 +08:00
).padding(horizontal: Insets.l),
2021-07-20 23:51:08 +08:00
);
}
Future<void> _showCreateAppDialog(BuildContext context) async {
await Dialogs.showWithContext(CreateAppDialogContext(
confirm: (appName) {
2021-07-25 23:12:58 +08:00
if (appName.isNotEmpty && press != null) {
press!(appName);
2021-07-20 23:51:08 +08:00
}
},
), context);
}
}