djangoflow / list_bloc

BLoC management of simple, filtered, sorted and paged lists.
3 stars 10 forks source link

create pet store example #1

Open ayushin opened 4 years ago

ayushin commented 4 years ago

The primary goal of this library was to create a simple abstraction layer between an open API client automatically generated by https://openapi-generator.tech/docs/generators/dart-dio/ and the UI layer using BLOC architecture.

We do this internally at our company, and it would be great to have an example of how to use this library with a standard Pet Store example from

We should create an example of using ListBloc and PaginatedBloc using dart-dio api generated with https://openapi-generator.tech/

In the example we should show how to generate https://openapi-generator.tech/docs/generators/dart-dio client from the Pt Store example swagger.yaml then we should create a DataRepository and PagedRepository based on that client and finally show how to use it in a simple Flutter application that would display the pets from the Pet Store using this library.

AntonVTR commented 4 years ago

Can you share your implementation?

ayushin commented 4 years ago

Hi Anton,

Most of the code it too proprietary, but here are a few bits:

repository:

class ActionRepository extends SupplierBaseRepository implements PaginatedRepository<Action, ActionFilter> {

  ActionRepository(s) : super(s);

  Future<Action> read(int id) async => (await api.supplierActionsRead(id.toString())).data;
  Future<Action> create(Action data) async => (await api.supplierActionsCreate(data)).data;
  Future<Action> update(int id, Action data) async => (await api.supplierActionsUpdate(id.toString(), data)).data;

  @override
  Future<ListPage<Action>> load({ActionFilter filter}) async {
    final r = await api.supplierActionsList(
        shop: filter?.shopId?.toString(),
        offset: filter.offset,
        limit: filter.limit);
    return ListPage<Action>(
      number: filter.offset ~/ filter.limit,
      size: filter.limit,
      count: r.data.count,
      data: r.data.results.toList(growable: false)
    );
  }
}

builder excerpt:

PageViewBlocBuilder<action.Action, ActionFilter>(
        bloc: _listBloc,
//        headerBuilder: (context, state) {
//          return ListTile(
//            leading: state.filter?.category?.id == null ? null : IconButton(icon: Icon(Icons.chevron_left),
//              onPressed: () {
//                _refresh(state.filter.rebuild((b) => b.category = null));
//              }),
//            title: Text(state.filter?.category?.title ?? 'Items in all categories', textAlign: TextAlign.center,),
//            trailing: Row(
//              mainAxisSize: MainAxisSize.min,
//              children: [
//                Tooltip(
//                    message: (state.filter?.active ?? true) ? "Show active items only" : "Show inactive items only",
//                    child: Switch(
//                      value: state.filter?.active ?? true,
//                      onChanged: (value) => _refresh(state.filter.rebuild((b) => b..active=value)),
//                    )
//                ),
//                IconButton(
//                    icon: Icon(Icons.add),
//                    color: AColors.blue,
//                    onPressed: widget.onAdd)
//              ])
//          );
//        },
        onPageChanged: (context, state, index) => _loadPage(index),
        pageBuilder: (context, state) => _pagedList(state.data),
        emptyBuilder: (context, state) => Center(child: Text('No items found')),
        footerBuilder: (context, state) => state.data.pages > 1 ? DotsIndicator(
                            onTap: (index) => _loadPage(index),
                            dotsCount: state.data.pages,
                            position: state.data.number.toDouble()) : SizedBox()
      )

Hope this helps!

ayushin commented 4 years ago

oh and this might help too, we use https://pub.dev/packages/built_value for the models and filters

import 'package:built_value/built_value.dart';
import 'package:list_bloc/list_bloc.dart';

import 'shop_filter.dart';

part 'action_filter.g.dart';

abstract class ActionFilter extends Object implements ShopFilter, OffsetLimitFilter, Built<ActionFilter, ActionFilterBuilder> {
  ActionFilter._();

  factory ActionFilter([void Function(ActionFilterBuilder) updates]) = _$ActionFilter;

  static void _initializeBuilder(ActionFilterBuilder b) => b
    ..offset = 0
    ..limit = OffsetLimitFilter.kPageSize;

  @override
  String toString() => "$runtimeType: shop: $shopId";
}