Skip to content

Commit

Permalink
refacor and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
hoc081098 committed Jun 18, 2024
1 parent 931e0a1 commit b3ba6c4
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 40 deletions.
32 changes: 14 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -62,41 +64,36 @@ class CounterBloc extends DisposeCallbackBaseBloc {
final StateStream<int> 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<void>();
final state = incrementController.stream
final state$ = incrementController.stream
.scan<int>((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) {
Expand All @@ -107,15 +104,15 @@ class TextCounter1 extends StatelessWidget {
builder: (context, state) {
return Text(
'COUNTER 1: $state',
style: Theme.of(context).textTheme.headline6,
style: Theme.of(context).textTheme.titleLarge,
);
},
);
}
}
class IncrementButton extends StatelessWidget {
const IncrementButton({Key? key}) : super(key: key);
const IncrementButton({super.key});
@override
Widget build(BuildContext context) {
Expand All @@ -124,9 +121,8 @@ class IncrementButton extends StatelessWidget {
return FloatingActionButton(
onPressed: bloc.increment,
tooltip: 'Increment',
child: Icon(Icons.add),
child: const Icon(Icons.add),
);
}
}
```
27 changes: 10 additions & 17 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> state;
final StateStream<int> 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<void>();
final state = incrementController.stream
.scan<int>((acc, _, __) => acc! + 1, 0)
.publishValueDistinct(0);
final connection = state.connect();
final state$ = incrementController.stream
.scan<int>((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`:
Expand Down
2 changes: 1 addition & 1 deletion example/lib/bloc_with_deps.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Bloc1 extends DisposeCallbackBaseBloc {
final StateStream<String?> string$;

Bloc1._({
required void Function() dispose,
required VoidAction dispose,
required this.load,
required this.string$,
}) : super(dispose);
Expand Down
2 changes: 1 addition & 1 deletion example/lib/counter_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CounterBloc extends DisposeCallbackBaseBloc {
final StateStream<int> state;

CounterBloc._({
required void Function() dispose,
required VoidAction dispose,
required this.increment,
required this.state,
}) : super(dispose);
Expand Down
7 changes: 6 additions & 1 deletion lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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> = R Function();

Expand Down
7 changes: 5 additions & 2 deletions test/flutter_bloc_pattern_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> f0 = () {};
Expand Down Expand Up @@ -88,7 +91,7 @@ void main() {
final Func9<int, String, double, int, List<int>, Map<int, int>, 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', () {
Expand Down

0 comments on commit b3ba6c4

Please sign in to comment.