From 6a5dcefb439a0e5e0e5e0b22365eb7950859d103 Mon Sep 17 00:00:00 2001 From: Luis Burgos Date: Tue, 6 Sep 2022 23:55:14 -0500 Subject: [PATCH] feat: update example app with events dashboard BREAKING CHANGE: Buzz dependency injection was changed. Minor upgrades to lib and example infra --- example/lib/get/core_module.dart | 30 +++++------- example/lib/get/get_main.dart | 15 ++++++ example/lib/get/home/page.dart | 9 +++- example/lib/modular/core_module.dart | 21 ++------- example/lib/modular/home/home_module.dart | 9 +++- example/lib/modular/modular_main.dart | 11 +++++ .../lib/shared/modules/home/home_page.dart | 29 ++++++++---- example/packages/core/lib/named_page.dart | 46 +++++++++++++++---- lib/buzz.dart | 1 + lib/buzz_impl.dart | 9 ++-- .../action_bar/repository.dart | 2 + lib/event_dashboard/buzz_registry.dart | 11 +++-- .../components/dashboard_view_template.dart | 2 + .../events_dashboard_page.dart | 9 ++++ lib/infra/errors.dart | 3 +- lib/utils/logger.dart | 5 -- 16 files changed, 140 insertions(+), 72 deletions(-) delete mode 100644 lib/utils/logger.dart diff --git a/example/lib/get/core_module.dart b/example/lib/get/core_module.dart index 0e44bfa..a570eb2 100644 --- a/example/lib/get/core_module.dart +++ b/example/lib/get/core_module.dart @@ -3,36 +3,28 @@ import 'package:get/get.dart'; import 'extensions/get_module.dart'; import 'home/module.dart'; -import 'overrides/app_navigator.dart'; -import 'overrides/feedbacks_executor.dart'; import 'profile/module.dart'; class CoreModule extends GetModule { @override - List get binds => [ - GetBind(() { - Get.put( - Buzz - ..init( - navigator: GetAppNavigator(), - feedbacksExecutor: GetFeedbacksExecutor(), - /*registries: [ - ProfileModuleRegistries( - () => Get.find(), - ), - ],*/ - ), - ); - }), - ]; + List get binds => []; @override - List get routes => [ + List get routes => [ GetModuleRoute( module: HomeModule(), ), GetModuleRoute( module: ProfileModule(), ), + BuzzDashboardGetRoute(), ]; } + +class BuzzDashboardGetRoute extends GetRoute { + @override + GetPage? get asGetPage => GetPage( + name: EventsDashboardPage.routeName, + page: () => const EventsDashboardPage(), + ); +} diff --git a/example/lib/get/get_main.dart b/example/lib/get/get_main.dart index 9c6b4ef..56d4fcf 100644 --- a/example/lib/get/get_main.dart +++ b/example/lib/get/get_main.dart @@ -1,11 +1,26 @@ +import 'package:buzz/buzz.dart'; import 'package:get/get.dart'; import '../bootstrap.dart'; import '../get/core_module.dart'; import '../shared/app_routes.dart'; import '../shared/not_found_page.dart'; +import 'overrides/app_navigator.dart'; +import 'overrides/feedbacks_executor.dart'; void main() { + Buzz.init( + withDebugDashboard: true, + rootAppRoute: '/', + navigator: GetAppNavigator(), + feedbacksExecutor: GetFeedbacksExecutor(), + /*registries: [ + ProfileModuleRegistries( + () => Get.find(), + ), + ],*/ + ); + return bootstrap( GetMaterialApp( title: 'Get App Test', diff --git a/example/lib/get/home/page.dart b/example/lib/get/home/page.dart index f989b5a..71ff90d 100644 --- a/example/lib/get/home/page.dart +++ b/example/lib/get/home/page.dart @@ -10,8 +10,13 @@ class HomeRoute extends GetRoute { GetPage get asGetPage => GetPage( name: AppRoutes.root, page: () => HomePage( - onGoToProfilePressed: () { - Get.find().fire( + onGoToBuzzTapped: () { + Buzz.fire( + GoToBuzzEventsDashboard(), + ); + }, + onGoToProfileTapped: () { + Buzz.fire( NavigateToCommand.named(AppRoutes.profileRoot), ); }, diff --git a/example/lib/modular/core_module.dart b/example/lib/modular/core_module.dart index 8ea7058..1704338 100644 --- a/example/lib/modular/core_module.dart +++ b/example/lib/modular/core_module.dart @@ -4,26 +4,9 @@ import 'package:flutter_modular/flutter_modular.dart'; import '../shared/app_routes.dart'; import '../shared/not_found_page.dart'; import 'home/home_module.dart'; -import 'overrides/app_navigator.dart'; import 'profile/module.dart'; class CoreModule extends Module { - @override - List get binds => [ - Bind.singleton( - (i) => Buzz - ..init( - navigator: ModularAppNavigator(), - /*registries: [ - ProfileModuleRegistries( - () => Modular.get(), - ), - ],*/ - ), - export: true, - ), - ]; - @override List get routes => [ ModuleRoute( @@ -34,6 +17,10 @@ class CoreModule extends Module { AppRoutes.profileRoot, module: ProfileModule(), ), + ChildRoute( + EventsDashboardPage.routeName, + child: (_, __) => const EventsDashboardPage(), + ), WildcardRoute( child: (context, args) => const NotFoundPage(), ), diff --git a/example/lib/modular/home/home_module.dart b/example/lib/modular/home/home_module.dart index 02640d0..53fee51 100644 --- a/example/lib/modular/home/home_module.dart +++ b/example/lib/modular/home/home_module.dart @@ -10,8 +10,13 @@ class HomeModule extends Module { ChildRoute( AppRoutes.root, child: (context, args) => HomePage( - onGoToProfilePressed: () { - Modular.get().fire( + onGoToBuzzTapped: () { + Buzz.fire( + GoToBuzzEventsDashboard(), + ); + }, + onGoToProfileTapped: () { + Buzz.fire( NavigateToCommand.named(AppRoutes.profileRoot), ); }, diff --git a/example/lib/modular/modular_main.dart b/example/lib/modular/modular_main.dart index f76c02c..a2d32af 100644 --- a/example/lib/modular/modular_main.dart +++ b/example/lib/modular/modular_main.dart @@ -1,10 +1,21 @@ +import 'package:buzz/buzz.dart'; import 'package:flutter/material.dart'; import 'package:flutter_modular/flutter_modular.dart'; import '../bootstrap.dart'; import 'core_module.dart'; +import 'overrides/app_navigator.dart'; void main() { + Buzz.init( + navigator: ModularAppNavigator(), + /*registries: [ + ProfileModuleRegistries( + () => Get.find(), + ), + ],*/ + ); + return bootstrap( ModularApp( module: CoreModule(), diff --git a/example/lib/shared/modules/home/home_page.dart b/example/lib/shared/modules/home/home_page.dart index cf9714d..790292b 100644 --- a/example/lib/shared/modules/home/home_page.dart +++ b/example/lib/shared/modules/home/home_page.dart @@ -4,22 +4,33 @@ import 'package:flutter/material.dart'; class HomePage extends StatelessWidget { const HomePage({ Key? key, - required this.onGoToProfilePressed, + required this.onGoToBuzzTapped, + required this.onGoToProfileTapped, }) : super(key: key); - final Function() onGoToProfilePressed; + final Function() onGoToBuzzTapped; + final Function() onGoToProfileTapped; @override Widget build(BuildContext context) { return BasePage( name: 'Home', - action: MainAction( - label: 'Go Profile', - onPressed: () { - debugPrint('$runtimeType onGoToProfilePressed'); - onGoToProfilePressed(); - }, - ), + actions: [ + MainAction( + label: 'Go to Profile', + onPressed: () { + debugPrint('$runtimeType onGoToProfileTapped'); + onGoToProfileTapped(); + }, + ), + MainAction( + label: 'Go to Buzz', + onPressed: () { + debugPrint('$runtimeType onGoToBuzzTapped'); + onGoToBuzzTapped(); + }, + ) + ], ); } } diff --git a/example/packages/core/lib/named_page.dart b/example/packages/core/lib/named_page.dart index 50430e2..9a26c9f 100644 --- a/example/packages/core/lib/named_page.dart +++ b/example/packages/core/lib/named_page.dart @@ -17,15 +17,26 @@ class BasePage extends StatelessWidget { this.action, this.body, this.onSettingsPressed, + this.actions = const [], }) : super(key: key); final String name; final MainAction? action; final Widget? body; final Function()? onSettingsPressed; + final List actions; @override Widget build(BuildContext context) { + List actionWidgets = []; + if (actions.isNotEmpty) { + actionWidgets = actions + .map( + (action) => _MainActionWidget(action: action), + ) + .toList(); + } + return Scaffold( appBar: AppBar( title: Text(name), @@ -48,15 +59,8 @@ class BasePage extends StatelessWidget { children: [ Text(name), if (body != null) Expanded(child: body!), - if (action != null) - ElevatedButton( - onPressed: () { - if (action!.onPressed != null) { - action!.onPressed!(); - } - }, - child: Text(action!.label), - ) + if (action != null) _MainActionWidget(action: action!), + ...actionWidgets, ], ), ), @@ -64,6 +68,30 @@ class BasePage extends StatelessWidget { } } +class _MainActionWidget extends StatelessWidget { + const _MainActionWidget({ + Key? key, + required this.action, + }) : super(key: key); + + final MainAction action; + + @override + Widget build(BuildContext context) { + return Padding( + padding: const EdgeInsets.all(12), + child: ElevatedButton( + onPressed: () { + if (action.onPressed != null) { + action.onPressed!(); + } + }, + child: Text(action.label), + ), + ); + } +} + class _ActionsIconButton extends StatelessWidget { const _ActionsIconButton({ Key? key, diff --git a/lib/buzz.dart b/lib/buzz.dart index 2f21b81..0c09072 100644 --- a/lib/buzz.dart +++ b/lib/buzz.dart @@ -1,6 +1,7 @@ library buzz; export 'buzz_impl.dart'; +export 'event_dashboard/events_dashboard_page.dart'; export 'feedbacks/feedbacks.dart'; export 'infra/buzz_event_handler.dart'; export 'infra/errors.dart'; diff --git a/lib/buzz_impl.dart b/lib/buzz_impl.dart index 94e39c9..2c77b0e 100644 --- a/lib/buzz_impl.dart +++ b/lib/buzz_impl.dart @@ -5,7 +5,7 @@ import 'feedbacks/buzz_registry.dart'; import 'feedbacks/feedbacks_executor.dart'; import 'navigation/buzz_registry.dart'; import 'navigation/navigator.dart'; -import 'utils/logger.dart'; +import 'utils/utils.dart'; ///ignore: non_constant_identifier_names BuzzBase Buzz = BuzzBase(); @@ -32,7 +32,7 @@ class BuzzBase extends EventBus { this.feedbacksExecutor = feedbacksExecutor; Buzz.on().listen( - (event) => buzzLogger('event fired: ${event.runtimeType}'), + (event) => buzzLog('event fired: ${event.runtimeType}'), ); registries.addAll(initialRegistries); @@ -40,7 +40,7 @@ class BuzzBase extends EventBus { if (withDebugDashboard) { if (rootAppRoute == null) { throw ArgumentError( - 'mainAppRoute cannot be null when withDebugDashboard is true', + 'rootAppRoute cannot be null when withDebugDashboard is true', ); } @@ -60,7 +60,8 @@ class BuzzBase extends EventBus { } for (var element in registries) { - element.register(Buzz); + buzzLog('register: $element'); + element.register(this); } initDone = true; diff --git a/lib/event_dashboard/action_bar/repository.dart b/lib/event_dashboard/action_bar/repository.dart index c75109e..717816a 100644 --- a/lib/event_dashboard/action_bar/repository.dart +++ b/lib/event_dashboard/action_bar/repository.dart @@ -6,6 +6,8 @@ import 'action_model.dart'; class FireActionsInMemoryRepository { Rx current = FireActions().obs; + static FireActionsInMemoryRepository get to => Get.find(); + void save(String tag, ConsoleEntry eventData) { current.update((val) { val?.addEvent(tag, eventData); diff --git a/lib/event_dashboard/buzz_registry.dart b/lib/event_dashboard/buzz_registry.dart index 2fbd130..fcfdb12 100644 --- a/lib/event_dashboard/buzz_registry.dart +++ b/lib/event_dashboard/buzz_registry.dart @@ -18,33 +18,36 @@ class EventsDashboardBuzzRegistry extends BuzzRegistry { @override void register(BuzzBase buzz) { + Get.put(FireActionsInMemoryRepository()); + buzz.on().listen((event) { - Get.find().save( + FireActionsInMemoryRepository.to.save( 'ui', ConsoleEntry('$event'), ); }); buzz.on().listen((event) { - Get.find().save( + FireActionsInMemoryRepository.to.save( 'command', ConsoleEntry('$event'), ); }); buzz.on().listen((event) { - Get.find().save( + FireActionsInMemoryRepository.to.save( 'app', ConsoleEntry('$event'), ); }); buzz.on().listen((event) { + //TODO: Should we use buzz instead of Buzz here? Buzz.fire(NavigateToCommand.named(event.route ?? '')); }); buzz.on().listen((_) { - buzz.fire(NavigateToCommand.named(mainAppRoute)); + Buzz.fire(NavigateToCommand.named(mainAppRoute)); }); } } diff --git a/lib/event_dashboard/components/dashboard_view_template.dart b/lib/event_dashboard/components/dashboard_view_template.dart index 9b758e8..471b4f1 100644 --- a/lib/event_dashboard/components/dashboard_view_template.dart +++ b/lib/event_dashboard/components/dashboard_view_template.dart @@ -37,6 +37,8 @@ class DashboardViewTemplate extends StatelessWidget { return Obx(() { return Scaffold( + //TODO: Improve colors by supporting dark mode toggling. + backgroundColor: Colors.black87, body: Column( children: [ Row( diff --git a/lib/event_dashboard/events_dashboard_page.dart b/lib/event_dashboard/events_dashboard_page.dart index bc4da3e..f5219ae 100644 --- a/lib/event_dashboard/events_dashboard_page.dart +++ b/lib/event_dashboard/events_dashboard_page.dart @@ -7,6 +7,15 @@ import 'components/dashboard_view_template.dart'; import 'console/console_view.dart'; import 'utils/default_methods.dart'; +class GoToBuzzEventsDashboard extends NavigateToCommand { + GoToBuzzEventsDashboard() + : super( + directions: NavigationDirections( + routeBuilder: () => EventsDashboardPage.routeName, + ), + ); +} + class EventsDashboardPage extends StatelessWidget { static String routeName = '/buzz'; diff --git a/lib/infra/errors.dart b/lib/infra/errors.dart index e25f1a4..47fdac3 100644 --- a/lib/infra/errors.dart +++ b/lib/infra/errors.dart @@ -12,7 +12,8 @@ class UnsupportedBuzzMessageType extends BuzzError { ]) : super('$message type is not supported', stackTrace); } -abstract class BuzzError implements Exception { +//TODO: Should be abstract? +class BuzzError implements Exception { final String message; final StackTrace? stackTrace; diff --git a/lib/utils/logger.dart b/lib/utils/logger.dart deleted file mode 100644 index cc2afaa..0000000 --- a/lib/utils/logger.dart +++ /dev/null @@ -1,5 +0,0 @@ -import 'dart:developer' as developer; - -void buzzLogger(String message, {String name = 'EVENT-BUZZ'}) { - developer.log(message, name: name); -}