wyatt-packages/packages/wyatt_crud_bloc/example/lib/streaming_cubit_view.dart
Hugo Pointcheval 3a7b7dfd1d
Some checks failed
continuous-integration/drone/pr Build is failing
feat(crud): add streaming usecase (closes #180)
2023-11-14 14:04:57 +01:00

126 lines
4.9 KiB
Dart

// 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");
},
),
),
],
);
}),
),
);
}
}