134 lines
3.9 KiB
Dart
Raw Normal View History

import 'package:dartz/dartz.dart';
2021-07-21 22:41:44 +08:00
import 'package:expandable/expandable.dart';
import 'package:flowy_infra_ui/widget/error_page.dart';
2021-07-21 22:41:44 +08:00
import 'package:flowy_sdk/protobuf/flowy-workspace/app_create.pb.dart';
import 'package:flowy_sdk/protobuf/flowy-workspace/view_create.pb.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:app_flowy/startup/startup.dart';
import 'package:app_flowy/workspace/application/app/app_bloc.dart';
import 'package:app_flowy/workspace/application/app/app_watch_bloc.dart';
import 'package:app_flowy/workspace/presentation/app/view_list.dart';
import 'package:app_flowy/workspace/presentation/widgets/menu/menu_list.dart';
import 'package:provider/provider.dart';
import 'app_header.dart';
2021-07-21 22:41:44 +08:00
2021-07-26 15:25:30 +08:00
class AppWidgetSize {
static double expandedIconSize = 24;
static double expandedIconRightSpace = 8;
static double scale = 1;
static double get expandedPadding =>
expandedIconSize * scale + expandedIconRightSpace;
}
class ViewListData extends ChangeNotifier {
List<View>? innerViews;
ViewListData();
set views(List<View> views) {
innerViews = views;
notifyListeners();
}
List<View> get views => innerViews ?? [];
}
2021-07-28 15:13:48 +08:00
class AppWidgetContext {
2021-07-21 22:41:44 +08:00
final App app;
final viewListData = ViewListData();
2021-07-28 15:13:48 +08:00
AppWidgetContext(
this.app,
);
2021-07-28 15:13:48 +08:00
Key valueKey() => ValueKey("${app.id}${app.version}");
}
class AppWidget extends MenuItem {
final AppWidgetContext appCtx;
AppWidget(this.appCtx, {Key? key}) : super(key: appCtx.valueKey());
2021-07-21 22:41:44 +08:00
@override
Widget build(BuildContext context) {
2021-07-22 12:23:14 +08:00
return MultiBlocProvider(
providers: [
2021-07-22 17:06:53 +08:00
BlocProvider<AppBloc>(create: (context) {
2021-07-28 15:13:48 +08:00
final appBloc = getIt<AppBloc>(param1: appCtx.app.id);
2021-07-22 17:06:53 +08:00
appBloc.add(const AppEvent.initial());
return appBloc;
}),
BlocProvider<AppWatchBloc>(create: (context) {
2021-07-28 15:13:48 +08:00
final watchBloc = getIt<AppWatchBloc>(param1: appCtx.app.id);
2021-07-22 17:06:53 +08:00
watchBloc.add(const AppWatchEvent.started());
return watchBloc;
}),
2021-07-22 12:23:14 +08:00
],
child: BlocBuilder<AppWatchBloc, AppWatchState>(
builder: (context, state) {
final child = state.map(
initial: (_) => BlocBuilder<AppBloc, AppState>(
2021-07-27 14:38:51 +08:00
builder: (context, state) => _renderViewList(state.views),
2021-07-22 12:23:14 +08:00
),
2021-07-27 14:38:51 +08:00
loadViews: (s) => _renderViewList(some(s.views)),
2021-07-22 17:06:53 +08:00
loadFail: (s) => FlowyErrorPage(s.error.toString()),
2021-07-22 12:23:14 +08:00
);
return expandableWrapper(context, child);
},
),
);
2021-07-21 22:41:44 +08:00
}
ExpandableNotifier expandableWrapper(BuildContext context, Widget child) {
return ExpandableNotifier(
child: ScrollOnExpand(
scrollOnExpand: true,
scrollOnCollapse: false,
2021-07-26 15:25:30 +08:00
child: Column(
children: <Widget>[
ExpandablePanel(
theme: const ExpandableThemeData(
headerAlignment: ExpandablePanelHeaderAlignment.center,
tapBodyToExpand: false,
tapBodyToCollapse: false,
2021-07-27 14:38:51 +08:00
tapHeaderToExpand: false,
2021-07-26 15:25:30 +08:00
iconPadding: EdgeInsets.zero,
hasIcon: false,
2021-07-21 22:41:44 +08:00
),
2021-07-28 15:13:48 +08:00
header: AppHeader(appCtx.app),
2021-07-27 09:11:53 +08:00
expanded: child,
2021-07-26 15:25:30 +08:00
collapsed: const SizedBox(),
),
],
2021-07-21 22:41:44 +08:00
),
),
);
}
2021-07-26 08:34:40 +08:00
2021-07-28 13:41:39 +08:00
Widget _renderViewList(Option<List<View>> some) {
some.fold(
() {
appCtx.viewListData.views = List.empty(growable: true);
},
(views) {
appCtx.viewListData.views = views;
},
2021-07-27 14:38:51 +08:00
);
2021-07-28 13:41:39 +08:00
return MultiProvider(
providers: [
ChangeNotifierProvider.value(value: appCtx.viewListData),
2021-07-26 15:25:30 +08:00
],
child: Consumer(builder: (context, ViewListData notifier, child) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: ViewList(notifier.views));
}),
2021-07-21 22:41:44 +08:00
);
}
@override
MenuItemType get type => MenuItemType.app;
2021-07-21 22:41:44 +08:00
}