Skip to content

Commit

Permalink
feat: update example app with events dashboard
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Buzz dependency injection was changed. Minor upgrades to lib and example infra
  • Loading branch information
luisburgos committed Sep 7, 2022
1 parent f8079cf commit 6a5dcef
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 72 deletions.
30 changes: 11 additions & 19 deletions example/lib/get/core_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetBind> get binds => [
GetBind(() {
Get.put(
Buzz
..init(
navigator: GetAppNavigator(),
feedbacksExecutor: GetFeedbacksExecutor(),
/*registries: [
ProfileModuleRegistries(
() => Get.find<IProfileRepository>(),
),
],*/
),
);
}),
];
List<GetBind> get binds => [];

@override
List<GetModuleRoute> get routes => [
List<GetRoute> get routes => [
GetModuleRoute(
module: HomeModule(),
),
GetModuleRoute(
module: ProfileModule(),
),
BuzzDashboardGetRoute(),
];
}

class BuzzDashboardGetRoute extends GetRoute {
@override
GetPage? get asGetPage => GetPage(
name: EventsDashboardPage.routeName,
page: () => const EventsDashboardPage(),
);
}
15 changes: 15 additions & 0 deletions example/lib/get/get_main.dart
Original file line number Diff line number Diff line change
@@ -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<IProfileRepository>(),
),
],*/
);

return bootstrap(
GetMaterialApp(
title: 'Get App Test',
Expand Down
9 changes: 7 additions & 2 deletions example/lib/get/home/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ class HomeRoute extends GetRoute {
GetPage get asGetPage => GetPage(
name: AppRoutes.root,
page: () => HomePage(
onGoToProfilePressed: () {
Get.find<BuzzBase>().fire(
onGoToBuzzTapped: () {
Buzz.fire(
GoToBuzzEventsDashboard(),
);
},
onGoToProfileTapped: () {
Buzz.fire(
NavigateToCommand.named(AppRoutes.profileRoot),
);
},
Expand Down
21 changes: 4 additions & 17 deletions example/lib/modular/core_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bind> get binds => [
Bind.singleton(
(i) => Buzz
..init(
navigator: ModularAppNavigator(),
/*registries: [
ProfileModuleRegistries(
() => Modular.get<IProfileRepository>(),
),
],*/
),
export: true,
),
];

@override
List<ModularRoute> get routes => [
ModuleRoute(
Expand All @@ -34,6 +17,10 @@ class CoreModule extends Module {
AppRoutes.profileRoot,
module: ProfileModule(),
),
ChildRoute(
EventsDashboardPage.routeName,
child: (_, __) => const EventsDashboardPage(),
),
WildcardRoute(
child: (context, args) => const NotFoundPage(),
),
Expand Down
9 changes: 7 additions & 2 deletions example/lib/modular/home/home_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ class HomeModule extends Module {
ChildRoute(
AppRoutes.root,
child: (context, args) => HomePage(
onGoToProfilePressed: () {
Modular.get<BuzzBase>().fire(
onGoToBuzzTapped: () {
Buzz.fire(
GoToBuzzEventsDashboard(),
);
},
onGoToProfileTapped: () {
Buzz.fire(
NavigateToCommand.named(AppRoutes.profileRoot),
);
},
Expand Down
11 changes: 11 additions & 0 deletions example/lib/modular/modular_main.dart
Original file line number Diff line number Diff line change
@@ -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<IProfileRepository>(),
),
],*/
);

return bootstrap(
ModularApp(
module: CoreModule(),
Expand Down
29 changes: 20 additions & 9 deletions example/lib/shared/modules/home/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
)
],
);
}
}
46 changes: 37 additions & 9 deletions example/packages/core/lib/named_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<MainAction> actions;

@override
Widget build(BuildContext context) {
List<Widget> actionWidgets = [];
if (actions.isNotEmpty) {
actionWidgets = actions
.map(
(action) => _MainActionWidget(action: action),
)
.toList();
}

return Scaffold(
appBar: AppBar(
title: Text(name),
Expand All @@ -48,22 +59,39 @@ 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,
],
),
),
);
}
}

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,
Expand Down
1 change: 1 addition & 0 deletions lib/buzz.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
9 changes: 5 additions & 4 deletions lib/buzz_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -32,15 +32,15 @@ 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);

if (withDebugDashboard) {
if (rootAppRoute == null) {
throw ArgumentError(
'mainAppRoute cannot be null when withDebugDashboard is true',
'rootAppRoute cannot be null when withDebugDashboard is true',
);
}

Expand All @@ -60,7 +60,8 @@ class BuzzBase extends EventBus {
}

for (var element in registries) {
element.register(Buzz);
buzzLog('register: $element');
element.register(this);
}

initDone = true;
Expand Down
2 changes: 2 additions & 0 deletions lib/event_dashboard/action_bar/repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'action_model.dart';
class FireActionsInMemoryRepository {
Rx<FireActions> current = FireActions().obs;

static FireActionsInMemoryRepository get to => Get.find();

void save(String tag, ConsoleEntry eventData) {
current.update((val) {
val?.addEvent(tag, eventData);
Expand Down
11 changes: 7 additions & 4 deletions lib/event_dashboard/buzz_registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,36 @@ class EventsDashboardBuzzRegistry extends BuzzRegistry {

@override
void register(BuzzBase buzz) {
Get.put<FireActionsInMemoryRepository>(FireActionsInMemoryRepository());

buzz.on<UiEvent>().listen((event) {
Get.find<FireActionsInMemoryRepository>().save(
FireActionsInMemoryRepository.to.save(
'ui',
ConsoleEntry('$event'),
);
});

buzz.on<Command>().listen((event) {
Get.find<FireActionsInMemoryRepository>().save(
FireActionsInMemoryRepository.to.save(
'command',
ConsoleEntry('$event'),
);
});

buzz.on<AppEvent>().listen((event) {
Get.find<FireActionsInMemoryRepository>().save(
FireActionsInMemoryRepository.to.save(
'app',
ConsoleEntry('$event'),
);
});

buzz.on<GoToDashboardPageUiEvent>().listen((event) {
//TODO: Should we use buzz instead of Buzz here?
Buzz.fire(NavigateToCommand.named(event.route ?? ''));
});

buzz.on<GoToAppTappedUiEvent>().listen((_) {
buzz.fire(NavigateToCommand.named(mainAppRoute));
Buzz.fire(NavigateToCommand.named(mainAppRoute));
});
}
}
2 changes: 2 additions & 0 deletions lib/event_dashboard/components/dashboard_view_template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading

0 comments on commit 6a5dcef

Please sign in to comment.