From 8893f8bfc72b40ac789aebb739a1f858b14fcfe9 Mon Sep 17 00:00:00 2001 From: shilangyu Date: Wed, 9 Aug 2023 21:32:49 +0200 Subject: [PATCH 1/7] Remove BlocPresentationEvent --- .../lib/bloc_presentation.dart | 1 - .../lib/src/bloc_presentation_event.dart | 17 ----------------- .../lib/src/bloc_presentation_listener.dart | 9 ++++----- .../lib/src/bloc_presentation_mixin.dart | 11 ++++------- .../lib/src/use_bloc_presentation_listener.dart | 9 ++++----- 5 files changed, 12 insertions(+), 35 deletions(-) delete mode 100644 packages/bloc_presentation/lib/src/bloc_presentation_event.dart diff --git a/packages/bloc_presentation/lib/bloc_presentation.dart b/packages/bloc_presentation/lib/bloc_presentation.dart index dbf3585..de83ad6 100644 --- a/packages/bloc_presentation/lib/bloc_presentation.dart +++ b/packages/bloc_presentation/lib/bloc_presentation.dart @@ -1,4 +1,3 @@ -export 'src/bloc_presentation_event.dart'; export 'src/bloc_presentation_listener.dart'; export 'src/bloc_presentation_mixin.dart'; export 'src/use_bloc_presentation_listener.dart'; diff --git a/packages/bloc_presentation/lib/src/bloc_presentation_event.dart b/packages/bloc_presentation/lib/src/bloc_presentation_event.dart deleted file mode 100644 index 6d1ca29..0000000 --- a/packages/bloc_presentation/lib/src/bloc_presentation_event.dart +++ /dev/null @@ -1,17 +0,0 @@ -/// {@template bloc_presentation_event} -/// Marker type all presentation events. -/// -/// Implement it for your events: -/// -/// ```dart -/// class FailedActionEvent implements BlocPresentationEvent { -/// const FailedActionEvent(this.errorMessage); -/// -/// final String errorMessage; -/// } -/// ``` -/// {@endtemplate} -abstract class BlocPresentationEvent { - /// {@macro bloc_presentation_event} - const BlocPresentationEvent(); -} diff --git a/packages/bloc_presentation/lib/src/bloc_presentation_listener.dart b/packages/bloc_presentation/lib/src/bloc_presentation_listener.dart index ef20add..a48045a 100644 --- a/packages/bloc_presentation/lib/src/bloc_presentation_listener.dart +++ b/packages/bloc_presentation/lib/src/bloc_presentation_listener.dart @@ -1,4 +1,3 @@ -import 'package:bloc_presentation/src/bloc_presentation_event.dart'; import 'package:bloc_presentation/src/use_bloc_presentation_listener.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; @@ -10,15 +9,15 @@ import 'bloc_presentation_mixin.dart'; /// Signature for the `listener` function which takes the `BuildContext` along /// with the `event` and is responsible for executing in response to /// new events. -typedef BlocPresentationWidgetListener = void Function( +typedef BlocPresentationWidgetListener

= void Function( BuildContext context, - BlocPresentationEvent event, + P event, ); /// {@template bloc_presentation_listener} /// Widget that listens to new presentation events in a specified [bloc]. /// {@endtemplate} -class BlocPresentationListener> +class BlocPresentationListener, P> extends SingleChildStatelessWidget { /// {@macro bloc_presentation_listener} const BlocPresentationListener({ @@ -34,7 +33,7 @@ class BlocPresentationListener> final B? bloc; /// The [BlocPresentationWidgetListener] which will be called on every new presentation event. - final BlocPresentationWidgetListener listener; + final BlocPresentationWidgetListener

listener; @override Widget buildWithChild(BuildContext context, Widget? child) { diff --git a/packages/bloc_presentation/lib/src/bloc_presentation_mixin.dart b/packages/bloc_presentation/lib/src/bloc_presentation_mixin.dart index 55083c9..cbc6a0d 100644 --- a/packages/bloc_presentation/lib/src/bloc_presentation_mixin.dart +++ b/packages/bloc_presentation/lib/src/bloc_presentation_mixin.dart @@ -1,22 +1,19 @@ import 'dart:async'; -import 'package:bloc_presentation/src/bloc_presentation_event.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; /// [BlocPresentationMixin] adds a presentation stream to a [BlocBase] /// which is automatically disposed. -mixin BlocPresentationMixin on BlocBase { - final _presentationStream = - StreamController.broadcast(); +mixin BlocPresentationMixin on BlocBase { + final _presentationStream = StreamController

.broadcast(); /// Stream emitting non-unique presentation events. - Stream get presentation => _presentationStream.stream; + Stream

get presentation => _presentationStream.stream; /// Emits a new presentation event. @protected - void emitPresentation(BlocPresentationEvent event) => - _presentationStream.add(event); + void emitPresentation(P event) => _presentationStream.add(event); @override @mustCallSuper diff --git a/packages/bloc_presentation/lib/src/use_bloc_presentation_listener.dart b/packages/bloc_presentation/lib/src/use_bloc_presentation_listener.dart index b34ec04..8471d9b 100644 --- a/packages/bloc_presentation/lib/src/use_bloc_presentation_listener.dart +++ b/packages/bloc_presentation/lib/src/use_bloc_presentation_listener.dart @@ -6,15 +6,14 @@ import 'package:provider/provider.dart'; /// /// If [bloc] is omitted, [useBlocPresentationListener] will automatically perform /// a lookup using [Provider] and the current `BuildContext`. -void useBlocPresentationListener>({ - required BlocPresentationWidgetListener listener, +void useBlocPresentationListener, + P>({ + required BlocPresentationWidgetListener

listener, B? bloc, }) { final context = useContext(); final effectiveStream = bloc?.presentation ?? - context.select>( - (bloc) => bloc.presentation, - ); + context.select>((bloc) => bloc.presentation); useEffect( () { From 7d69828b2f93e13bea25b4e1e143f8209517fd02 Mon Sep 17 00:00:00 2001 From: shilangyu Date: Wed, 9 Aug 2023 21:32:57 +0200 Subject: [PATCH 2/7] Update tests --- .../test/bloc_presentation_mixin_test.dart | 8 ++++--- packages/bloc_presentation/test/fakes.dart | 6 ----- .../use_bloc_presentation_listener_test.dart | 24 ++++++++++++------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/packages/bloc_presentation/test/bloc_presentation_mixin_test.dart b/packages/bloc_presentation/test/bloc_presentation_mixin_test.dart index c0c6655..21b83b3 100644 --- a/packages/bloc_presentation/test/bloc_presentation_mixin_test.dart +++ b/packages/bloc_presentation/test/bloc_presentation_mixin_test.dart @@ -2,14 +2,16 @@ import 'package:bloc_presentation/bloc_presentation.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_test/flutter_test.dart'; -class _TestCubit extends Cubit with BlocPresentationMixin { +class _TestCubit extends Cubit with BlocPresentationMixin { _TestCubit() : super(0); void emitValueEvent() => emitPresentation(_ValueEvent()); void emitReferenceEvent() => emitPresentation(_ReferenceEvent()); } -class _ValueEvent implements BlocPresentationEvent { +sealed class _Event {} + +class _ValueEvent implements _Event { @override bool operator ==(Object other) => other is _ValueEvent; @@ -17,7 +19,7 @@ class _ValueEvent implements BlocPresentationEvent { int get hashCode => 0; } -class _ReferenceEvent implements BlocPresentationEvent {} +class _ReferenceEvent implements _Event {} void main() { group('BlocPresentationMixin', () { diff --git a/packages/bloc_presentation/test/fakes.dart b/packages/bloc_presentation/test/fakes.dart index 5e11886..818dc08 100644 --- a/packages/bloc_presentation/test/fakes.dart +++ b/packages/bloc_presentation/test/fakes.dart @@ -1,14 +1,8 @@ -import 'package:bloc_presentation/bloc_presentation.dart'; import 'package:flutter/material.dart'; import 'package:mocktail/mocktail.dart'; class _FakeBuildContext extends Fake implements BuildContext {} -class _FakeBlocPresentationEvent extends Fake implements BlocPresentationEvent { - // Flutter 3.3 and 3.7 format this class differently, hence the comment... -} - void registerFakes() { registerFallbackValue(_FakeBuildContext()); - registerFallbackValue(_FakeBlocPresentationEvent()); } diff --git a/packages/bloc_presentation/test/use_bloc_presentation_listener_test.dart b/packages/bloc_presentation/test/use_bloc_presentation_listener_test.dart index cd61867..f1cf3d0 100644 --- a/packages/bloc_presentation/test/use_bloc_presentation_listener_test.dart +++ b/packages/bloc_presentation/test/use_bloc_presentation_listener_test.dart @@ -8,29 +8,33 @@ import 'package:provider/provider.dart'; import 'fakes.dart'; -class _TestCubit extends Cubit with BlocPresentationMixin { +class _TestCubit extends Cubit + with BlocPresentationMixin { _TestCubit() : super(0); - void emitEvent(BlocPresentationEvent event) => emitPresentation(event); + void emitEvent(_PresentationEvent event) => emitPresentation(event); } -class _PresentationEvent implements BlocPresentationEvent {} +class _PresentationEvent {} class _MockListener extends Mock { void call( BuildContext context, - BlocPresentationEvent event, + _PresentationEvent event, ); } void main() { group('useBlocPresentationListener', () { late _TestCubit cubit; - late BlocPresentationWidgetListener listener; - late BlocPresentationEvent event; + late BlocPresentationWidgetListener<_PresentationEvent> listener; + late _PresentationEvent event; late HookElement element; - setUpAll(registerFakes); + setUpAll(() { + registerFakes(); + registerFallbackValue(_PresentationEvent()); + }); setUp(() { cubit = _TestCubit(); @@ -70,7 +74,9 @@ void main() { child: HookBuilder( builder: (context) { element = context as HookElement; - useBlocPresentationListener<_TestCubit>(listener: listener); + useBlocPresentationListener<_TestCubit, _PresentationEvent>( + listener: listener, + ); return Container(); }, @@ -172,7 +178,7 @@ void main() { await tester.pumpWidget( HookBuilder( builder: (context) { - useBlocPresentationListener( + useBlocPresentationListener<_TestCubit, _PresentationEvent>( listener: listener2, bloc: cubit2, ); From 53751066d502fd2b77fa46eb046e97fae33c5d96 Mon Sep 17 00:00:00 2001 From: shilangyu Date: Wed, 30 Aug 2023 10:04:30 +0200 Subject: [PATCH 3/7] Update changelog for v0.4.0 --- packages/bloc_presentation/CHANGELOG.md | 5 +++++ packages/bloc_presentation/pubspec.yaml | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/bloc_presentation/CHANGELOG.md b/packages/bloc_presentation/CHANGELOG.md index a96ce49..97fad73 100644 --- a/packages/bloc_presentation/CHANGELOG.md +++ b/packages/bloc_presentation/CHANGELOG.md @@ -1,3 +1,8 @@ +# 0.4.0 + +- **BREAKING**: `BlocPresentationMixin` is now generic over the type of the presentation event (see #17) +- **BREAKING**: removed `BlocPresentationEvent` marker type + # 0.3.0 - Bump minimum Flutter version to `3.10.0` (#22) diff --git a/packages/bloc_presentation/pubspec.yaml b/packages/bloc_presentation/pubspec.yaml index 9af751d..7da9510 100644 --- a/packages/bloc_presentation/pubspec.yaml +++ b/packages/bloc_presentation/pubspec.yaml @@ -2,7 +2,7 @@ name: bloc_presentation description: > Extends blocs with an additional stream which can serve as a way of indicating single-time events (so-called "presentation events"). -version: 0.3.0 +version: 0.4.0 homepage: https://github.com/leancodepl/bloc_presentation/tree/master/packages/bloc_presentation environment: From d3de5aa829679e24d2e72616ff52db76e20d7391 Mon Sep 17 00:00:00 2001 From: shilangyu Date: Wed, 30 Aug 2023 10:36:35 +0200 Subject: [PATCH 4/7] Update CI sdk versions --- .github/workflows/bloc_presentation-test.yml | 2 +- .github/workflows/bloc_presentation_test-test.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/bloc_presentation-test.yml b/.github/workflows/bloc_presentation-test.yml index 378ed15..36b1ecd 100644 --- a/.github/workflows/bloc_presentation-test.yml +++ b/.github/workflows/bloc_presentation-test.yml @@ -21,7 +21,7 @@ jobs: strategy: fail-fast: false matrix: - version: ['3.10.x'] + version: ['3.10.x', '3.13.x'] defaults: run: diff --git a/.github/workflows/bloc_presentation_test-test.yml b/.github/workflows/bloc_presentation_test-test.yml index cc4d8f8..a7a17ad 100644 --- a/.github/workflows/bloc_presentation_test-test.yml +++ b/.github/workflows/bloc_presentation_test-test.yml @@ -21,7 +21,7 @@ jobs: strategy: fail-fast: false matrix: - version: ['3.10.x'] + version: ['3.10.x', '3.13.x'] defaults: run: From cfa90210b5df4430aa1fd6b697ff61e9d4e1bd4a Mon Sep 17 00:00:00 2001 From: shilangyu Date: Wed, 30 Aug 2023 10:42:10 +0200 Subject: [PATCH 5/7] Update bloc_presentation example --- .../example/android/build.gradle | 6 +++--- .../gradle/wrapper/gradle-wrapper.properties | 2 +- .../example/lib/comment_cubit.dart | 9 ++++++--- .../bloc_presentation/example/lib/main.dart | 19 ++++++++++--------- .../bloc_presentation/example/pubspec.yaml | 2 +- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/packages/bloc_presentation/example/android/build.gradle b/packages/bloc_presentation/example/android/build.gradle index 24047dc..d02fac2 100644 --- a/packages/bloc_presentation/example/android/build.gradle +++ b/packages/bloc_presentation/example/android/build.gradle @@ -1,12 +1,12 @@ buildscript { - ext.kotlin_version = '1.3.50' + ext.kotlin_version = '1.8.21' repositories { google() mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:4.1.0' + classpath 'com.android.tools.build:gradle:7.4.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } @@ -26,6 +26,6 @@ subprojects { project.evaluationDependsOn(':app') } -task clean(type: Delete) { +tasks.register("clean", Delete) { delete rootProject.buildDir } diff --git a/packages/bloc_presentation/example/android/gradle/wrapper/gradle-wrapper.properties b/packages/bloc_presentation/example/android/gradle/wrapper/gradle-wrapper.properties index bc6a58a..cfe88f6 100644 --- a/packages/bloc_presentation/example/android/gradle/wrapper/gradle-wrapper.properties +++ b/packages/bloc_presentation/example/android/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip diff --git a/packages/bloc_presentation/example/lib/comment_cubit.dart b/packages/bloc_presentation/example/lib/comment_cubit.dart index a8890ac..3906f3f 100644 --- a/packages/bloc_presentation/example/lib/comment_cubit.dart +++ b/packages/bloc_presentation/example/lib/comment_cubit.dart @@ -3,7 +3,8 @@ import 'dart:math'; import 'package:bloc_presentation/bloc_presentation.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -class CommentCubit extends Cubit with BlocPresentationMixin { +class CommentCubit extends Cubit + with BlocPresentationMixin { CommentCubit() : super(const CommentInitialState()); void fetch() { @@ -32,13 +33,15 @@ class CommentCubit extends Cubit with BlocPresentationMixin { } } -class FailedToUpvote implements BlocPresentationEvent { +sealed class CommentEvent {} + +class FailedToUpvote implements CommentEvent { const FailedToUpvote(this.reason); final String reason; } -class SuccessfulUpvote implements BlocPresentationEvent { +class SuccessfulUpvote implements CommentEvent { const SuccessfulUpvote(this.message); final String message; diff --git a/packages/bloc_presentation/example/lib/main.dart b/packages/bloc_presentation/example/lib/main.dart index 602ff70..2ad83c9 100644 --- a/packages/bloc_presentation/example/lib/main.dart +++ b/packages/bloc_presentation/example/lib/main.dart @@ -28,17 +28,18 @@ class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocPresentationListener( + return BlocPresentationListener( listener: (context, event) { // we know we will receive this event once - if (event is FailedToUpvote) { - ScaffoldMessenger.of(context) - ..hideCurrentSnackBar() - ..showSnackBar(SnackBar(content: Text(event.reason))); - } else if (event is SuccessfulUpvote) { - ScaffoldMessenger.of(context) - ..hideCurrentSnackBar() - ..showSnackBar(SnackBar(content: Text(event.message))); + switch (event) { + case FailedToUpvote(): + ScaffoldMessenger.of(context) + ..hideCurrentSnackBar() + ..showSnackBar(SnackBar(content: Text(event.reason))); + case SuccessfulUpvote(): + ScaffoldMessenger.of(context) + ..hideCurrentSnackBar() + ..showSnackBar(SnackBar(content: Text(event.message))); } }, child: Scaffold( diff --git a/packages/bloc_presentation/example/pubspec.yaml b/packages/bloc_presentation/example/pubspec.yaml index 0d1ecc9..0ea50b5 100644 --- a/packages/bloc_presentation/example/pubspec.yaml +++ b/packages/bloc_presentation/example/pubspec.yaml @@ -18,7 +18,7 @@ dev_dependencies: bloc_test: ^9.1.2 flutter_test: sdk: flutter - leancode_lint: '^5.0.0' + leancode_lint: ^6.0.0 flutter: uses-material-design: true From 6c0fd07a5b744fb689c877f1d301b05f39661957 Mon Sep 17 00:00:00 2001 From: shilangyu Date: Wed, 30 Aug 2023 10:48:41 +0200 Subject: [PATCH 6/7] Update example dep bound --- packages/bloc_presentation/example/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/bloc_presentation/example/pubspec.yaml b/packages/bloc_presentation/example/pubspec.yaml index 0ea50b5..7c340e8 100644 --- a/packages/bloc_presentation/example/pubspec.yaml +++ b/packages/bloc_presentation/example/pubspec.yaml @@ -18,7 +18,7 @@ dev_dependencies: bloc_test: ^9.1.2 flutter_test: sdk: flutter - leancode_lint: ^6.0.0 + leancode_lint: ">=5.0.0 <7.0.0" flutter: uses-material-design: true From 4bf96f2df5e7d09b7c7b9a9d468fa669929782be Mon Sep 17 00:00:00 2001 From: shilangyu Date: Wed, 30 Aug 2023 10:55:24 +0200 Subject: [PATCH 7/7] Update readme --- packages/bloc_presentation/README.md | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/bloc_presentation/README.md b/packages/bloc_presentation/README.md index e7ff702..e7be3d0 100644 --- a/packages/bloc_presentation/README.md +++ b/packages/bloc_presentation/README.md @@ -17,7 +17,9 @@ flutter pub add bloc_presentation First, create an event which will be emitted: ```dart -class FailedToUpvote implements BlocPresentationEvent { +sealed class CommentCubitEvent {} + +class FailedToUpvote implements CommentCubitEvent { const FailedToUpvote(this.reason); final String reason; @@ -28,7 +30,7 @@ Next, extend your Bloc/Cubit with the presentation mixin which will give you access to the `emitPresentation` method: ```dart -class CommentCubit extends Cubit with BlocPresentationMixin { +class CommentCubit extends Cubit with BlocPresentationMixin { // body } ``` @@ -55,12 +57,13 @@ new states. Then, in the UI code one can react to such events using `BlocPresentationListener` or `useBlocPresentationListener`: ```dart -BlocPresentationListener( +BlocPresentationListener( listener: (context, event) { - if (event is FailedToUpvote) { - ScaffoldMessenger.of(context) - ..hideCurrentSnackBar() - ..showSnackBar(SnackBar(content: Text(event.reason))); + switch (event) { + case FailedToUpvote(): + ScaffoldMessenger.of(context) + ..hideCurrentSnackBar() + ..showSnackBar(SnackBar(content: Text(event.reason))); } }, child: MyWidget(),