diff --git a/README.md b/README.md index 9060c4a..1573214 100644 --- a/README.md +++ b/README.md @@ -47,10 +47,12 @@ class MyBloc implements BaseBloc { ## Example: A port of the standard "Counter Button" example from Flutter -### 1. File `counter_bloc.dart`: +### 1. File `counter_bloc.dart` + ```dart import 'dart:async'; +import 'package:disposebag/disposebag.dart'; import 'package:flutter_bloc_pattern/flutter_bloc_pattern.dart'; import 'package:rxdart_ext/rxdart_ext.dart'; @@ -62,41 +64,36 @@ class CounterBloc extends DisposeCallbackBaseBloc { final StateStream state; CounterBloc._({ - required void Function() dispose, + required VoidAction dispose, required this.increment, required this.state, }) : super(dispose); factory CounterBloc() { - // ignore: close_sinks final incrementController = StreamController(); - final state = incrementController.stream + final state$ = incrementController.stream .scan((acc, _, __) => acc + 1, 0) .publishState(0); - final connection = state.connect(); return CounterBloc._( - dispose: () async { - await connection.cancel(); - await incrementController.close(); - print('CounterBloc::disposed'); - }, - increment: () => incrementController.add(null), - state: state, + dispose: DisposeBag([incrementController, state$.connect()]).dispose, + increment: incrementController.addNull, + state: state$, ); } } ``` -### 2. File `main.dart`: +### 2. File `main.dart` + ```dart import 'package:example/bloc.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc_pattern/flutter_bloc_pattern.dart'; class TextCounter1 extends StatelessWidget { - const TextCounter1({Key? key}) : super(key: key); + const TextCounter1({super.key}); @override Widget build(BuildContext context) { @@ -107,7 +104,7 @@ class TextCounter1 extends StatelessWidget { builder: (context, state) { return Text( 'COUNTER 1: $state', - style: Theme.of(context).textTheme.headline6, + style: Theme.of(context).textTheme.titleLarge, ); }, ); @@ -115,7 +112,7 @@ class TextCounter1 extends StatelessWidget { } class IncrementButton extends StatelessWidget { - const IncrementButton({Key? key}) : super(key: key); + const IncrementButton({super.key}); @override Widget build(BuildContext context) { @@ -124,9 +121,8 @@ class IncrementButton extends StatelessWidget { return FloatingActionButton( onPressed: bloc.increment, tooltip: 'Increment', - child: Icon(Icons.add), + child: const Icon(Icons.add), ); } } - ``` diff --git a/example/README.md b/example/README.md index e676197..85367ae 100644 --- a/example/README.md +++ b/example/README.md @@ -4,44 +4,37 @@ ```dart import 'dart:async'; -import 'package:distinct_value_connectable_stream/distinct_value_connectable_stream.dart'; +import 'package:disposebag/disposebag.dart'; import 'package:flutter_bloc_pattern/flutter_bloc_pattern.dart'; import 'package:rxdart_ext/rxdart_ext.dart'; class CounterBloc extends DisposeCallbackBaseBloc { /// Inputs - final void Function() increment; + final VoidAction increment; /// Outputs - final DistinctValueStream state; + final StateStream state; CounterBloc._({ - required void Function() dispose, + required VoidAction dispose, required this.increment, required this.state, }) : super(dispose); factory CounterBloc() { - // ignore: close_sinks final incrementController = StreamController(); - final state = incrementController.stream - .scan((acc, _, __) => acc! + 1, 0) - .publishValueDistinct(0); - final connection = state.connect(); + final state$ = incrementController.stream + .scan((acc, _, __) => acc + 1, 0) + .publishState(0); return CounterBloc._( - dispose: () async { - await connection.cancel(); - await incrementController.close(); - print('>>> disposed'); - }, - increment: () => incrementController.add(null), - state: state, + dispose: DisposeBag([incrementController, state$.connect()]).dispose, + increment: incrementController.addNull, + state: state$, ); } } - ``` ### 2. File `main.dart`: diff --git a/example/lib/bloc_with_deps.dart b/example/lib/bloc_with_deps.dart index a252c6f..56fa64f 100644 --- a/example/lib/bloc_with_deps.dart +++ b/example/lib/bloc_with_deps.dart @@ -28,7 +28,7 @@ class Bloc1 extends DisposeCallbackBaseBloc { final StateStream string$; Bloc1._({ - required void Function() dispose, + required VoidAction dispose, required this.load, required this.string$, }) : super(dispose); diff --git a/example/lib/counter_bloc.dart b/example/lib/counter_bloc.dart index 0394312..20184d2 100644 --- a/example/lib/counter_bloc.dart +++ b/example/lib/counter_bloc.dart @@ -12,7 +12,7 @@ class CounterBloc extends DisposeCallbackBaseBloc { final StateStream state; CounterBloc._({ - required void Function() dispose, + required VoidAction dispose, required this.increment, required this.state, }) : super(dispose); diff --git a/lib/src/base.dart b/lib/src/base.dart index 6fa4afc..47d5360 100644 --- a/lib/src/base.dart +++ b/lib/src/base.dart @@ -7,7 +7,7 @@ abstract class BaseBloc { /// Base bloc that implements [BaseBloc.dispose] by passing callback to constructor, /// and call it when [BaseBloc.dispose] called. class DisposeCallbackBaseBloc implements BaseBloc { - final void Function() _dispose; + final VoidAction _dispose; /// Create a [DisposeCallbackBaseBloc] by a dispose callback. DisposeCallbackBaseBloc(this._dispose); @@ -19,8 +19,13 @@ class DisposeCallbackBaseBloc implements BaseBloc { // Function types /// Represents a function that have no arguments and return no data. +/// See also [VoidFunc0]. typedef VoidAction = void Function(); +/// Represents a function that have no arguments and return no data. +/// This is an alias of [VoidAction]. +typedef VoidFunc0 = VoidAction; + /// Represents a function with zero arguments: `() -> R`. typedef Func0 = R Function(); diff --git a/test/flutter_bloc_pattern_test.dart b/test/flutter_bloc_pattern_test.dart index 3c0cb69..82623d1 100644 --- a/test/flutter_bloc_pattern_test.dart +++ b/test/flutter_bloc_pattern_test.dart @@ -51,7 +51,10 @@ void main() { group('Base and Error', () { test('Function types', () { // ignore: omit_local_variable_types - final VoidAction a = () {}; + final VoidAction a1 = () {}; + + // ignore: omit_local_variable_types + final VoidFunc0 a2 = () {}; // ignore: omit_local_variable_types final Func0 f0 = () {}; @@ -88,7 +91,7 @@ void main() { final Func9, Map, void, bool, bool, bool> f9 = (i, s, d, i2, li, map, _, b, b2) => b && b2; - [a, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9].forEach(print); + [a1, a2, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9].forEach(print); }); test('DisposeCallbackBaseBloc', () {