Skip to content

Commit

Permalink
feat: add feedback executor hook
Browse files Browse the repository at this point in the history
  • Loading branch information
luisburgos committed Jul 2, 2022
1 parent 3b680e0 commit 7e3dd88
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 4 deletions.
1 change: 1 addition & 0 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

- [ ] Change Page to Screen?
- [ ] Find a way to justify having Page -> View (ViewTemplate + StateHolder) maybe eventually Page = View + PageController where the controller can access to feature flags.
- [ ] How can we create sharable components between feature modules. Meaning a Component could be part of `ModulaA` but imported and included on `ModuleB` and have the same behavior out of the box. How feature flags could work at this level.

---
From Atomic Design guides:
Expand Down
9 changes: 8 additions & 1 deletion example/lib/get/get_app_navigator.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:buzz/navigation.dart';
import 'package:buzz/buzz.dart';
import 'package:get/get.dart';

class GetAppNavigator extends Navigator {
Expand All @@ -23,3 +23,10 @@ class GetAppNavigator extends Navigator {
Get.toNamed(path);
}
}

class GetFeedbacksExecutor extends FeedbacksExecutor {
@override
void snackBar(String title, String message) {
Get.snackbar(title, message);
}
}
3 changes: 1 addition & 2 deletions example/lib/get/get_main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void main() {
Buzz
..init(
navigator: GetAppNavigator(),
feedbacksExecutor: GetFeedbacksExecutor(),
moduleRegistries: [
ProfileModuleRegistries(
() => Get.find<IProfileRepository>(),
Expand Down Expand Up @@ -50,8 +51,6 @@ void main() {
NavigateBackCommand(),
);
},
//TODO: Validate stream being injected here.
profileStream: Get.find<IProfileRepository>().profileStateChanges,
),
),
],
Expand Down
2 changes: 1 addition & 1 deletion example/lib/modular/modular_app_navigator.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:buzz/navigation.dart';
import 'package:buzz/buzz.dart' show Navigator, FeedbacksExecutor;
import 'package:flutter_modular/flutter_modular.dart';

class ModularAppNavigator extends Navigator {
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 'feedbacks.dart';
export 'infra/event_bus_holder.dart';
export 'infra/registries.dart';
export 'infra/typed_event_bus.dart';
Expand Down
9 changes: 9 additions & 0 deletions lib/buzz_impl.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:buzz/buzz.dart';
import 'package:buzz/feedbacks.dart';

import 'infra/registries.dart';
import 'utils.dart';
Expand All @@ -23,9 +24,11 @@ abstract class IBuzzBase {
UiEventBus get uiEvents;

Navigator get navigator;
FeedbacksExecutor get feedbacksExecutor;

void init({
required Navigator navigator,
FeedbacksExecutor? feedbacksExecutor,
List<ModuleBuzzRegistries>? moduleRegistries,
});

Expand All @@ -37,6 +40,9 @@ class BuzzBase implements IBuzzBase {
@override
Navigator get navigator => _navigator;

@override
FeedbacksExecutor get feedbacksExecutor => _feedbacksExecutor;

@override
AppEventBus get appEvents => EventBusHolder.of<AppEventBus>();

Expand All @@ -47,14 +53,17 @@ class BuzzBase implements IBuzzBase {
UiEventBus get uiEvents => EventBusHolder.of<UiEventBus>();

late Navigator _navigator;
late FeedbacksExecutor _feedbacksExecutor;
List<ModuleBuzzRegistries>? _moduleRegistries;

@override
void init({
required Navigator navigator,
FeedbacksExecutor? feedbacksExecutor,
List<ModuleBuzzRegistries>? moduleRegistries,
}) {
_navigator = navigator;
_feedbacksExecutor = feedbacksExecutor ?? DefaultFeedbacksExecutor();
_moduleRegistries = moduleRegistries;

_bindNavigationCommandHandler();
Expand Down
45 changes: 45 additions & 0 deletions lib/feedbacks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'package:buzz/buzz.dart';
import 'package:buzz/utils.dart';

abstract class FeedbacksCommand extends Command {}

class ShowSnackBarCommand extends FeedbacksCommand {
ShowSnackBarCommand(
this.title, {
required this.message,
});

final String title;
final String message;
}

abstract class FeedbacksExecutor {
void snackBar(String title, String message);
}

class FeedbacksCommandHandler extends TypedEventHandler<FeedbacksCommand> {
FeedbacksCommandHandler({
required this.feedbacksExecutor,
});

final FeedbacksExecutor feedbacksExecutor;

@override
void handle(FeedbacksCommand command) {
if (command is ShowSnackBarCommand) {
feedbacksExecutor.snackBar(command.title, command.message);
Buzz.fire(OnShowedSnackBarEvent());
}

//TODO: Add more cases
}
}

class OnShowedSnackBarEvent extends AppEvent {}

class DefaultFeedbacksExecutor extends FeedbacksExecutor {
@override
void snackBar(String title, String message) {
developerLog('$title:$message');
}
}

0 comments on commit 7e3dd88

Please sign in to comment.