feat(crud): add streaming usecase (closes #180)
continuous-integration/drone/pr Build is failing
continuous-integration/drone/pr Build is failing
This commit is contained in:
@@ -33,11 +33,11 @@ class AdvancedCubitView extends StatelessWidget {
|
||||
),
|
||||
body: BlocProvider(
|
||||
create: (context) =>
|
||||
UserAdvancedCubit(context.read<CrudRepository<User>>())..getAll(),
|
||||
UserAdvancedCubit(context.read<CrudRepository<User>>())
|
||||
..streaming(),
|
||||
child: Builder(builder: (context) {
|
||||
return Column(
|
||||
children: [
|
||||
const Text("Data:"),
|
||||
BlocBuilder<UserAdvancedCubit, CrudState>(
|
||||
buildWhen: (previous, current) {
|
||||
if (current is CrudLoading && current is! CrudReading) {
|
||||
@@ -117,31 +117,6 @@ class AdvancedCubitView extends StatelessWidget {
|
||||
},
|
||||
),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context.read<UserAdvancedCubit>().getAll();
|
||||
},
|
||||
child: BlocBuilder<UserAdvancedCubit, CrudState>(
|
||||
buildWhen: (previous, current) {
|
||||
if (current is CrudLoading && current is! CrudReading) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
builder: (context, state) {
|
||||
return state is CrudReading
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
color: Colors.white,
|
||||
),
|
||||
)
|
||||
: const Text("GetAll");
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
],
|
||||
);
|
||||
}),
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
import 'package:crud_bloc_example/advanced_cubit_view.dart';
|
||||
import 'package:crud_bloc_example/basic_cubit_view.dart';
|
||||
import 'package:crud_bloc_example/streaming_cubit_view.dart';
|
||||
import 'package:crud_bloc_example/user_entity.dart';
|
||||
import 'package:crud_bloc_example/user_model.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -65,32 +66,47 @@ class MyHomePage extends StatelessWidget {
|
||||
appBar: AppBar(
|
||||
title: const Text('Flutter Demo Home Page'),
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const BasicCubitView(),
|
||||
),
|
||||
);
|
||||
},
|
||||
child: const Text('Basic example'),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const AdvancedCubitView(),
|
||||
),
|
||||
);
|
||||
},
|
||||
child: const Text('Advanced example'),
|
||||
),
|
||||
],
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const BasicCubitView(),
|
||||
),
|
||||
);
|
||||
},
|
||||
child: const Text('Basic example'),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const StreamingCubitView(),
|
||||
),
|
||||
);
|
||||
},
|
||||
child: const Text('Streaming example'),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const AdvancedCubitView(),
|
||||
),
|
||||
);
|
||||
},
|
||||
child: const Text('Advanced example'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
// Copyright (C) 2023 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 'dart:math';
|
||||
|
||||
import 'package:crud_bloc_example/user_entity.dart';
|
||||
import 'package:crud_bloc_example/user_streaming_cubit.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:wyatt_crud_bloc/wyatt_crud_bloc.dart';
|
||||
|
||||
class StreamingCubitView extends StatelessWidget {
|
||||
const StreamingCubitView({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text("Streaming Cubit"),
|
||||
),
|
||||
body: BlocProvider(
|
||||
create: (context) =>
|
||||
UserStreamingCubit(context.read<CrudRepository<User>>())..read(),
|
||||
child: Builder(builder: (context) {
|
||||
return Column(
|
||||
children: [
|
||||
BlocBuilder<UserStreamingCubit, CrudState>(
|
||||
buildWhen: (previous, current) {
|
||||
if (current is CrudLoading && current is! CrudReading) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
builder: (context, state) {
|
||||
return Expanded(
|
||||
child: CrudBuilder.typed<CrudListLoaded<User?>>(
|
||||
state: state,
|
||||
builder: ((context, state) {
|
||||
return ListView.builder(
|
||||
itemCount: state.data.length,
|
||||
itemBuilder: (context, index) {
|
||||
final user = state.data.elementAt(index);
|
||||
return ListTile(
|
||||
title: Text(user?.name ?? 'Error'),
|
||||
subtitle: Text(user?.email ?? 'Error'),
|
||||
onTap: () {
|
||||
context
|
||||
.read<UserStreamingCubit>()
|
||||
.delete(id: (user?.id)!);
|
||||
},
|
||||
onLongPress: () {
|
||||
context.read<UserStreamingCubit>().update(
|
||||
single: UpdateParameters(
|
||||
id: user?.id ?? '',
|
||||
raw: {
|
||||
'email': '${user?.id}@updated.io',
|
||||
}),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}),
|
||||
initialBuilder: (context, state) =>
|
||||
const Center(child: CircularProgressIndicator()),
|
||||
loadingBuilder: (context, state) =>
|
||||
const Center(child: CircularProgressIndicator()),
|
||||
errorBuilder: (context, state) => Text("Error: $state"),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
final r = Random().nextInt(1000);
|
||||
context.read<UserStreamingCubit>().create(
|
||||
User(
|
||||
id: '$r',
|
||||
name: 'Wyatt $r',
|
||||
email: '$r@wyattapp.io',
|
||||
phone: '06$r',
|
||||
),
|
||||
);
|
||||
},
|
||||
child: BlocBuilder<UserStreamingCubit, CrudState>(
|
||||
buildWhen: (previous, current) {
|
||||
if (current is CrudLoading && current is! CrudCreating) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
builder: (context, state) {
|
||||
return state is CrudCreating
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
color: Colors.white,
|
||||
),
|
||||
)
|
||||
: const Text("Create");
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,9 @@ class UserAdvancedCubit extends CrudAdvancedCubit<User> {
|
||||
@override
|
||||
Search<User>? get crudSearch => Search(crudRepository);
|
||||
|
||||
@override
|
||||
Streaming<User>? get crudStreaming => Streaming(crudRepository);
|
||||
|
||||
@override
|
||||
Update<User>? get crudUpdate => Update(crudRepository);
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
// 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:crud_bloc_example/user_entity.dart';
|
||||
import 'package:wyatt_crud_bloc/wyatt_crud_bloc.dart';
|
||||
|
||||
/// A [CrudCubit] for [User].
|
||||
class UserStreamingCubit extends CrudCubit<User> {
|
||||
final CrudRepository<User> crudRepository;
|
||||
|
||||
UserStreamingCubit(this.crudRepository);
|
||||
|
||||
@override
|
||||
DefaultCreate<User>? get createOperation => Create(crudRepository);
|
||||
|
||||
@override
|
||||
DefaultDelete? get deleteOperation => Delete(crudRepository);
|
||||
|
||||
@override
|
||||
DefaultRead? get readOperation => Streaming(crudRepository);
|
||||
|
||||
@override
|
||||
DefaultUpdate? get updateOperation => Update(crudRepository);
|
||||
|
||||
@override
|
||||
ModelIdentifier<User> get modelIdentifier => ModelIdentifier(
|
||||
getIdentifier: (user) => user.id ?? '',
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user