Skip to content

Commit c3bb0bc

Browse files
committed
Allow non SourcedModel item with source constructor, move more to bloc paging
1 parent 1ec95bb commit c3bb0bc

File tree

17 files changed

+369
-378
lines changed

17 files changed

+369
-378
lines changed

app/lib/blocs/sourced_paging.dart

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class SourcedPagingBloc<T>
4040
_init();
4141
}
4242

43-
SourcedPagingBloc.simple(
43+
SourcedPagingBloc.item(
4444
{required this.cubit,
4545
this.sources,
4646
required Future<List<T>?> Function(
@@ -53,6 +53,20 @@ class SourcedPagingBloc<T>
5353
_init();
5454
}
5555

56+
SourcedPagingBloc.source({
57+
required this.cubit,
58+
required String source,
59+
required Future<List<T>?> Function(
60+
SourceService service, int offset, int limit)
61+
fetch,
62+
this.pageSize = 50,
63+
}) : sources = [source],
64+
useDates = false,
65+
_fetch = _buildDatedFetchSource(fetch),
66+
super(const SourcedPagingInitial()) {
67+
_init();
68+
}
69+
5670
void _init() {
5771
on<SourcedPagingFetched>(_onFetched);
5872
on<SourcedPagingRefresh>((event, emit) {
@@ -62,8 +76,11 @@ class SourcedPagingBloc<T>
6276
on<SourcedPagingRemoved>((event, emit) {
6377
final state = this.state;
6478
if (state is SourcedPagingSuccess<T>) {
65-
final items =
66-
state.dates.map((e) => e.where((i) => i != event.item).toList());
79+
final items = state.dates.map((e) => e
80+
.where((i) =>
81+
i.model == event.item &&
82+
(event.source == null || (i.source == event.source)))
83+
.toList());
6784
emit(SourcedPagingSuccess(
6885
currentPageKey: state.currentPageKey,
6986
dates: items.toList(),
@@ -150,7 +167,9 @@ class SourcedPagingBloc<T>
150167
add(SourcedPagingFetched());
151168
}
152169

153-
void remove(SourcedModel<T> item) => add(SourcedPagingRemoved(item));
170+
void remove(SourcedModel<T> item) =>
171+
add(SourcedPagingRemoved(item.model, item.source));
172+
void removeSourced(T item) => add(SourcedPagingRemoved(item));
154173
}
155174

156175
_buildDatedFetch<T>(
@@ -162,3 +181,11 @@ _buildDatedFetch<T>(
162181
final items = await fetch(source, service, offset, limit);
163182
return items;
164183
};
184+
_buildDatedFetchSource<T>(
185+
Future<List<T>?> Function(SourceService service, int offset, int limit)
186+
fetch) =>
187+
(String source, SourceService service, int offset, int limit,
188+
int date) async {
189+
final items = await fetch(service, offset, limit);
190+
return items;
191+
};

app/lib/blocs/sourced_paging.mapper.dart

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/lib/blocs/sourced_paging_event.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ class SourcedPagingRefresh extends SourcedPagingEvent {
2020
}
2121

2222
class SourcedPagingRemoved extends SourcedPagingEvent {
23-
final Object item;
23+
final String? source;
24+
final Object? item;
2425

25-
SourcedPagingRemoved(this.item);
26+
SourcedPagingRemoved(this.item, [this.source]);
2627

2728
@override
28-
List<Object> get props => [item];
29+
List<Object> get props => [source, item].nonNulls.toList(growable: false);
2930
}

app/lib/pages/calendar/pending.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class _CalendarPendingViewState extends State<CalendarPendingView> {
3434
void initState() {
3535
super.initState();
3636
_cubit = context.read<FlowCubit>();
37-
_bloc = SourcedPagingBloc.simple(
37+
_bloc = SourcedPagingBloc.item(
3838
cubit: _cubit,
3939
fetch: (source, service, offset, limit) async =>
4040
service.calendarItem?.getCalendarItems(
@@ -80,7 +80,7 @@ class _CalendarPendingViewState extends State<CalendarPendingView> {
8080
Expanded(
8181
child: LayoutBuilder(
8282
builder: (context, constraints) =>
83-
PagedListView<ConnectedModel<CalendarItem, Event?>>.simple(
83+
PagedListView<ConnectedModel<CalendarItem, Event?>>.item(
8484
bloc: _bloc,
8585
itemBuilder: (context, item, index) {
8686
return ConstrainedBox(

app/lib/pages/events/page.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class _EventsBodyViewState extends State<EventsBodyView> {
109109
@override
110110
void initState() {
111111
_flowCubit = context.read<FlowCubit>();
112-
_bloc = SourcedPagingBloc.simple(
112+
_bloc = SourcedPagingBloc.item(
113113
cubit: _flowCubit,
114114
fetch: (source, service, offset, limit) async =>
115115
_filter.source != null && _filter.source != source
@@ -154,7 +154,7 @@ class _EventsBodyViewState extends State<EventsBodyView> {
154154
),
155155
const SizedBox(height: 8),
156156
Expanded(
157-
child: PagedListView.simple(
157+
child: PagedListView.item(
158158
bloc: _bloc,
159159
itemBuilder: (ctx, item, index) => Align(
160160
alignment: Alignment.topCenter,

app/lib/pages/groups/page.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class _GroupsBodyViewState extends State<GroupsBodyView> {
9292
@override
9393
void initState() {
9494
_flowCubit = context.read<FlowCubit>();
95-
_bloc = SourcedPagingBloc.simple(
95+
_bloc = SourcedPagingBloc.item(
9696
cubit: _flowCubit,
9797
fetch: (source, service, offset, limit) async =>
9898
service.group?.getGroups(offset: offset, limit: limit));
@@ -117,7 +117,7 @@ class _GroupsBodyViewState extends State<GroupsBodyView> {
117117
@override
118118
Widget build(BuildContext context) {
119119
return Scaffold(
120-
body: PagedListView.simple(
120+
body: PagedListView.item(
121121
bloc: _bloc,
122122
itemBuilder: (ctx, item, index) => Align(
123123
alignment: Alignment.topCenter,

app/lib/pages/groups/view.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ class _GroupsViewState<T extends DescriptiveModel>
4444
final cubit = context.read<FlowCubit>();
4545
final service = cubit.getService(widget.source);
4646
_groupService = service.group;
47-
_bloc = SourcedPagingBloc.simple(
47+
_bloc = SourcedPagingBloc.source(
4848
cubit: cubit,
49-
fetch: (source, service, offset, limit) => widget.connector
49+
source: widget.source,
50+
fetch: (service, offset, limit) => widget.connector
5051
.getItems(widget.model.id!, offset: offset, limit: limit),
5152
);
5253
super.initState();
@@ -58,25 +59,24 @@ class _GroupsViewState<T extends DescriptiveModel>
5859
Column(
5960
children: [
6061
Flexible(
61-
child: PagedListView.simple(
62+
child: PagedListView.source(
6263
bloc: _bloc,
6364
itemBuilder: (context, item, index) {
64-
final group = item.model;
6565
return Dismissible(
66-
key: ValueKey(group.id),
66+
key: ValueKey(item.id),
6767
background: Container(color: Colors.red),
6868
onDismissed: (direction) {
69-
_groupService?.deleteGroup(group.id!);
70-
_bloc.remove(item);
69+
_groupService?.deleteGroup(item.id!);
70+
_bloc.removeSourced(item);
7171
},
7272
child: ListTile(
73-
title: Text(group.name),
73+
title: Text(item.name),
7474
onTap: () async {
7575
await showDialog<SourcedModel<Group>>(
7676
context: context,
7777
builder: (context) => GroupDialog(
7878
source: widget.source,
79-
group: group,
79+
group: item,
8080
),
8181
);
8282
_bloc.refresh();

app/lib/pages/notes/details.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'dart:math';
22

33
import 'package:collection/collection.dart';
4-
import 'package:flow/helpers/sourced_paging_controller.dart';
4+
import 'package:flow/blocs/sourced_paging.dart';
55
import 'package:flow/pages/notes/select.dart';
66
import 'package:flow/widgets/markdown_field.dart';
77
import 'package:flutter/material.dart';
@@ -22,13 +22,13 @@ import 'label.dart';
2222
class NoteDetailsView extends StatefulWidget {
2323
final String source;
2424
final Note note;
25-
final SourcedPagingController<Note> controller;
25+
final SourcedPagingBloc<Note> bloc;
2626

2727
const NoteDetailsView({
2828
super.key,
2929
required this.source,
3030
required this.note,
31-
required this.controller,
31+
required this.bloc,
3232
});
3333

3434
@override
@@ -202,7 +202,7 @@ class _NoteDetailsViewState extends State<NoteDetailsView> {
202202
AppLocalizations.of(context).delete,
203203
() async {
204204
await _noteService?.deleteNote(_newNote.id!);
205-
widget.controller.refresh();
205+
widget.bloc.refresh();
206206
}
207207
)
208208
]

app/lib/pages/notes/list.dart

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
part of 'navigator/drawer.dart';
22

33
class NotesListView extends StatelessWidget {
4-
final SourcedPagingController<Note> controller;
4+
final SourcedPagingBloc<Note> bloc;
55

66
const NotesListView({
77
super.key,
8-
required this.controller,
8+
required this.bloc,
99
});
1010

1111
@override
1212
Widget build(BuildContext context) {
13-
return PagedListView(
14-
pagingController: controller,
15-
builderDelegate: buildMaterialPagedDelegate<SourcedModel<Note>>(
16-
controller,
17-
(ctx, item, index) => NoteListTile(
18-
note: item.model,
19-
source: item.source,
20-
controller: controller,
21-
),
13+
return PagedListView.item(
14+
bloc: bloc,
15+
itemBuilder: (ctx, item, index) => NoteListTile(
16+
note: item.model,
17+
source: item.source,
18+
bloc: bloc,
2219
),
2320
);
2421
}

app/lib/pages/notes/navigator/drawer.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import 'package:flow/blocs/sourced_paging.dart';
12
import 'package:flow/cubits/flow.dart';
2-
import 'package:flow/helpers/sourced_paging_controller.dart';
33
import 'package:flow/pages/notes/tile.dart';
44
import 'package:flow/pages/notes/filter.dart';
55
import 'package:flow/pages/notes/label.dart';
66
import 'package:flow/pages/notes/notebook.dart';
7-
import 'package:flow/widgets/builder_delegate.dart';
7+
import 'package:flow/widgets/paging/list.dart';
88
import 'package:flow/widgets/select.dart';
99
import 'package:flow_api/models/label/model.dart';
1010
import 'package:flow_api/models/model.dart';
@@ -13,7 +13,6 @@ import 'package:flow_api/services/database.dart';
1313
import 'package:flutter/material.dart';
1414
import 'package:flutter_bloc/flutter_bloc.dart';
1515
import 'package:flow/src/generated/i18n/app_localizations.dart';
16-
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
1716
import 'dart:typed_data';
1817
import 'package:material_leap/material_leap.dart';
1918
import 'package:material_leap/widgets.dart';
@@ -28,14 +27,14 @@ class NotesNavigatorDrawer extends StatelessWidget {
2827
final ValueChanged<NoteFilter>? onFilterChanged;
2928
final NoteFilter filter;
3029
final bool isSearching;
31-
final SourcedPagingController<Note> controller;
30+
final SourcedPagingBloc<Note> bloc;
3231

3332
const NotesNavigatorDrawer({
3433
super.key,
3534
this.note,
3635
this.onFilterChanged,
3736
required this.filter,
38-
required this.controller,
37+
required this.bloc,
3938
required this.isSearching,
4039
});
4140

@@ -75,7 +74,7 @@ class NotesNavigatorDrawer extends StatelessWidget {
7574
if (note != null && !isSearching)
7675
Expanded(
7776
child: NotesListView(
78-
controller: controller,
77+
bloc: bloc,
7978
),
8079
),
8180
],

0 commit comments

Comments
 (0)