Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 35 additions & 38 deletions app/lib/helpers/sourced_paging_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,46 @@ import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:flow_api/models/model.dart';
import 'package:flow_api/services/source.dart';

class SourcedPagingController<T>
extends PagingController<SourcedModel<int>, SourcedModel<T>> {
final FlowCubit cubit;
final int pageSize;
const kDefaultPageSize = 50;

List<String> get sources => cubit.getCurrentSources();
typedef SourcedPagingController<T>
= PagingController<SourcedModel<int>, SourcedModel<T>>;

SourcedPagingController(this.cubit, {this.pageSize = 50})
: super(firstPageKey: const SourcedModel("", -1));

PageRequestListener<SourcedModel<int>> addFetchListener(
Future<List<T>?> Function(String, SourceService, int offset, int limit)
fetch) {
FutureOr<void> listener(SourcedModel<int> pageKey) async {
final isFirstPage = pageKey.model < 0;
var currentPageKey = pageKey;
if (isFirstPage) {
currentPageKey = SourcedModel(sources.first, 0);
}
SourcedPagingController<T> createSourcedPagingController<T>({
required FlowCubit cubit,
int pageSize = kDefaultPageSize,
required Future<List<T>?> Function(
String, SourceService, int offset, int limit)
fetch,
}) {
final sources = cubit.getCurrentSources();
return SourcedPagingController(
fetchPage: (pageKey) async {
final fetched = (await fetch(
currentPageKey.source,
cubit.getService(currentPageKey.source),
currentPageKey.model * pageSize,
pageKey.source,
cubit.getService(pageKey.source),
pageKey.model * pageSize,
pageSize) ??
<T>[])
.map((e) => SourcedModel(currentPageKey.source, e))
.map((e) => SourcedModel(pageKey.source, e))
.toList();
final index = sources.indexOf(currentPageKey.source);
final currentSource = isFirstPage ? sources.first : currentPageKey.source;
final keepSource = fetched.length >= pageSize;
final isLastSource = index >= sources.length - 1;
if (isLastSource && !keepSource) {
appendLastPage(fetched);
} else if (keepSource) {
appendPage(
fetched, SourcedModel(currentSource, currentPageKey.model + 1));
} else {
final nextSource = sources[index + 1];
appendPage(fetched, SourcedModel(nextSource, 0));
return fetched;
},
getNextPageKey: (state) {
final keys = state.keys?.lastOrNull;
if (keys == null) {
return SourcedModel(sources.first, 0);
}
}

addPageRequestListener(listener);
return listener;
}
final items = state.pages?.lastOrNull;
final isFinished = items == null || items.length < pageSize;
if (!isFinished) {
return SourcedModel(keys.source, keys.model + 1);
}
final index = sources.indexOf(keys.source);
if (index >= sources.length - 1 || index < 0) {
return null;
}
return SourcedModel(sources[index + 1], 0);
},
);
}
62 changes: 34 additions & 28 deletions app/lib/pages/calendar/pending.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,20 @@ class _CalendarPendingViewState extends State<CalendarPendingView> {
void initState() {
super.initState();
_cubit = context.read<FlowCubit>();
_controller = SourcedPagingController(_cubit);
_controller.addFetchListener((source, service, offset, limit) async =>
service.calendarItem?.getCalendarItems(
status: EventStatus.values
.where(
(element) => !widget.filter.hiddenStatuses.contains(element))
.toList(),
search: widget.search,
pending: true,
offset: offset,
limit: limit,
resourceIds: widget.filter.resources,
));
_controller = createSourcedPagingController(
cubit: _cubit,
fetch: (source, service, offset, limit) async =>
service.calendarItem?.getCalendarItems(
status: EventStatus.values
.where((element) =>
!widget.filter.hiddenStatuses.contains(element))
.toList(),
search: widget.search,
pending: true,
offset: offset,
limit: limit,
resourceIds: widget.filter.resources,
));
}

@override
Expand Down Expand Up @@ -80,23 +81,28 @@ class _CalendarPendingViewState extends State<CalendarPendingView> {
const SizedBox(height: 8),
Expanded(
child: LayoutBuilder(
builder: (context, constraints) => PagedListView(
pagingController: _controller,
builderDelegate: buildMaterialPagedDelegate<
SourcedConnectedModel<CalendarItem, Event?>>(
_controller,
(context, item, index) {
return ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 1000),
child: CalendarListTile(
key: ValueKey('${item.source}@${item.main.id}'),
eventItem: item,
onRefresh: _controller.refresh,
builder: (context, constraints) => PagingListener(
controller: _controller,
builder: (context, state, fetchNextPage) {
return PagedListView(
state: state,
fetchNextPage: fetchNextPage,
builderDelegate: buildMaterialPagedDelegate<
SourcedConnectedModel<CalendarItem, Event?>>(
_controller,
(context, item, index) {
return ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 1000),
child: CalendarListTile(
key: ValueKey('${item.source}@${item.main.id}'),
eventItem: item,
onRefresh: _controller.refresh,
),
);
},
),
);
},
),
),
}),
),
),
],
Expand Down
55 changes: 30 additions & 25 deletions app/lib/pages/events/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,31 +155,36 @@ class _EventsBodyViewState extends State<EventsBodyView> {
),
const SizedBox(height: 8),
Expanded(
child: PagedListView(
pagingController: _controller,
builderDelegate: buildMaterialPagedDelegate<SourcedModel<Event>>(
_controller,
(ctx, item, index) => Align(
alignment: Alignment.topCenter,
child: Container(
constraints: const BoxConstraints(maxWidth: 800),
child: Dismissible(
key: ValueKey('${item.model.id}@${item.source}'),
onDismissed: (direction) async {
await _flowCubit
.getService(item.source)
.event
?.deleteEvent(item.model.id!);
_controller.itemList!.remove(item);
},
background: Container(
color: Colors.red,
),
child: EventTile(
flowCubit: _flowCubit,
pagingController: _controller,
source: item.source,
event: item.model,
child: PagingListener(
controller: _controller,
builder: (context, state, fetchNextPage) => PagedListView(
state: state,
fetchNextPage: fetchNextPage,
builderDelegate:
buildMaterialPagedDelegate<SourcedModel<Event>>(
_controller,
(ctx, item, index) => Align(
alignment: Alignment.topCenter,
child: Container(
constraints: const BoxConstraints(maxWidth: 800),
child: Dismissible(
key: ValueKey('${item.model.id}@${item.source}'),
onDismissed: (direction) async {
await _flowCubit
.getService(item.source)
.event
?.deleteEvent(item.model.id!);
_controller.refresh();
},
background: Container(
color: Colors.red,
),
child: EventTile(
flowCubit: _flowCubit,
pagingController: _controller,
source: item.source,
event: item.model,
),
),
),
),
Expand Down
20 changes: 10 additions & 10 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ packages:
dependency: transitive
description:
name: dev_build
sha256: "5ed4fe61cead9fd561f13c7a05f6c11002ecc8ec6f5be33f7b9674f49f245434"
sha256: "43422b4e090a3bad7c1d612862bdf26c64e78a3624ecd780679df21384e8e91a"
url: "https://pub.dev"
source: hosted
version: "1.1.2"
version: "1.1.2+2"
dynamic_color:
dependency: "direct main"
description:
Expand Down Expand Up @@ -585,10 +585,10 @@ packages:
dependency: "direct main"
description:
name: infinite_scroll_pagination
sha256: "4047eb8191e8b33573690922a9e995af64c3949dc87efc844f936b039ea279df"
sha256: ff5232d9066f664d31eee1b7dd0dedf79c71e7d8874567a7af5a51630e38b234
url: "https://pub.dev"
source: hosted
version: "4.1.0"
version: "5.0.0"
intl:
dependency: "direct main"
description:
Expand Down Expand Up @@ -756,18 +756,18 @@ packages:
dependency: "direct main"
description:
name: package_info_plus
sha256: "67eae327b1b0faf761964a1d2e5d323c797f3799db0e85aa232db8d9e922bc35"
sha256: "7976bfe4c583170d6cdc7077e3237560b364149fcd268b5f53d95a991963b191"
url: "https://pub.dev"
source: hosted
version: "8.2.1"
version: "8.3.0"
package_info_plus_platform_interface:
dependency: transitive
description:
name: package_info_plus_platform_interface
sha256: "205ec83335c2ab9107bbba3f8997f9356d72ca3c715d2f038fc773d0366b4c76"
sha256: "6c935fb612dff8e3cc9632c2b301720c77450a126114126ffaafe28d2e87956c"
url: "https://pub.dev"
source: hosted
version: "3.1.0"
version: "3.2.0"
path:
dependency: "direct main"
description:
Expand Down Expand Up @@ -1145,10 +1145,10 @@ packages:
dependency: "direct main"
description:
name: sqlite3_flutter_libs
sha256: "57fafacd815c981735406215966ff7caaa8eab984b094f52e692accefcbd9233"
sha256: "7adb4cc96dc08648a5eb1d80a7619070796ca6db03901ff2b6dcb15ee30468f3"
url: "https://pub.dev"
source: hosted
version: "0.5.30"
version: "0.5.31"
stack_trace:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ dependencies:
flutter_bloc: ^9.0.0
shared_preferences: ^2.2.3
collection: ^1.17.1
infinite_scroll_pagination: ^4.0.0
infinite_scroll_pagination: ^5.0.0
window_manager: ^0.4.3
package_info_plus: ^8.0.0
# Database
Expand Down
Loading