feat(bloc): add provider, consumer and helpers

This commit is contained in:
2022-05-20 22:32:30 +02:00
parent 2ac2b4d2be
commit 9ef37633cb
45 changed files with 1435 additions and 0 deletions
@@ -0,0 +1,40 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wyatt_bloc_helper/src/bloc_base/bloc_base_consumer_screen.dart';
import 'package:wyatt_bloc_helper/src/bloc_base/bloc_base_provider_screen.dart';
import 'package:wyatt_bloc_helper/src/bloc_base/bloc_base_screen.dart';
import 'package:wyatt_bloc_helper/src/mixins/bloc_base_provider_mixin.dart';
import 'package:wyatt_bloc_helper/src/mixins/bloc_provider_mixin.dart';
abstract class BlocProviderScreen<B extends Bloc<E, S>, E, S extends Object>
extends BlocBaseProviderScreen<B, S>
with BlocBaseProviderMixin<B>, BlocProviderMixin<B, E> {
const BlocProviderScreen({super.key});
}
abstract class BlocConsumerScreen<B extends Bloc<E, S>, E, S extends Object>
extends BlocBaseConsumerScreen<B, S>
with BlocBaseProviderMixin<B>, BlocProviderMixin<B, E> {
const BlocConsumerScreen({super.key});
}
abstract class BlocScreen<B extends Bloc<E, S>, E, S extends Object>
extends BlocBaseScreen<B, S>
with BlocBaseProviderMixin<B>, BlocProviderMixin<B, E> {
const BlocScreen({super.key});
}
@@ -0,0 +1,52 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
abstract class BlocBaseConsumerScreen<B extends BlocBase<S>, S extends Object>
extends StatelessWidget {
const BlocBaseConsumerScreen({super.key});
/// Takes the previous `state` and the current `state` and is responsible for
/// returning a [bool] which determines whether or not to trigger
/// [onBuild] with the current `state`.
bool shouldBuildWhen(S previous, S current) => true;
/// Takes the previous `state` and the current `state` and is responsible for
/// returning a [bool] which determines whether or not to trigger
/// [onListen] with the current `state`.
bool shouldListenWhen(S previous, S current) => true;
/// The [onBuild] function which will be invoked on each widget build.
/// The [onBuild] takes the `BuildContext` and current `state` and
/// must return a widget.
Widget onBuild(BuildContext context, S state);
/// Takes the `BuildContext` along with the `state`
/// and is responsible for executing in response to `state` changes.
void onListen(BuildContext context, S state) {}
@override
Widget build(BuildContext context) {
return BlocConsumer<B, S>(
listenWhen: shouldListenWhen,
listener: onListen,
buildWhen: shouldBuildWhen,
builder: onBuild,
);
}
}
@@ -0,0 +1,37 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
abstract class BlocBaseProviderScreen<B extends BlocBase<S>, S extends Object>
extends StatelessWidget {
const BlocBaseProviderScreen({super.key});
/// Creates the [Cubit] or [Bloc] to be used.
B create(BuildContext context);
/// Creates the child [Widget] to be used.
Widget buildChild(BuildContext context);
@override
Widget build(BuildContext context) {
return BlocProvider<B>(
create: (_) => create(context),
child: Builder(builder: buildChild),
);
}
}
@@ -0,0 +1,35 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wyatt_bloc_helper/src/bloc_base/bloc_base_consumer_screen.dart';
abstract class BlocBaseScreen<B extends BlocBase<S>, S extends Object>
extends BlocBaseConsumerScreen<B, S> {
const BlocBaseScreen({super.key});
/// Creates the [Cubit] or [Bloc] to be used.
B create(BuildContext context);
@override
Widget build(BuildContext context) {
return BlocProvider<B>(
create: (_) => create(context),
child: super.build(context),
);
}
}
@@ -0,0 +1,36 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wyatt_bloc_helper/src/bloc_base/bloc_base_consumer_screen.dart';
import 'package:wyatt_bloc_helper/src/bloc_base/bloc_base_provider_screen.dart';
import 'package:wyatt_bloc_helper/src/bloc_base/bloc_base_screen.dart';
import 'package:wyatt_bloc_helper/src/mixins/bloc_base_provider_mixin.dart';
abstract class CubitProviderScreen<B extends Cubit<S>, S extends Object>
extends BlocBaseProviderScreen<B, S> with BlocBaseProviderMixin<B> {
const CubitProviderScreen({super.key});
}
abstract class CubitConsumerScreen<B extends Cubit<S>, S extends Object>
extends BlocBaseConsumerScreen<B, S> with BlocBaseProviderMixin<B> {
const CubitConsumerScreen({super.key});
}
abstract class CubitScreen<B extends Cubit<S>, S extends Object>
extends BlocBaseScreen<B, S> with BlocBaseProviderMixin<B> {
const CubitScreen({super.key});
}
@@ -0,0 +1,28 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
/// A mixin that provides implementation of helper methods for
/// [Bloc] and [Cubit] widgets.
mixin BlocBaseProviderMixin<B extends BlocBase<Object>> {
/// Returns the [BlocBase] used by this [BlocBaseProviderMixin].
B bloc(BuildContext context) => BlocProvider.of<B>(context);
/// Returns the [BlocBase] used by this [BlocBaseProviderMixin].
R repo<R>(BuildContext context) => RepositoryProvider.of<R>(context);
}
@@ -0,0 +1,26 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
/// [Bloc] specific mixin that provides implementation
/// of helper methods for events.
mixin BlocProviderMixin<B extends Bloc<E, Object>, E> {
void add(BuildContext context, E event) {
BlocProvider.of<B>(context).add(event);
}
}
@@ -0,0 +1,20 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
library wyatt_bloc_helper;
export 'src/bloc.dart';
export 'src/cubit.dart';