Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delay duration to append and prepend function #35

Merged
merged 2 commits into from
Jul 22, 2024
Merged
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
90 changes: 63 additions & 27 deletions lib/src/data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ abstract base class DataSource<PageKey, Value> {
try {
await _refresh();
} on Exception catch (e) {
_manager.setError(e);
_manager.setError(
exception: e,
);
}
}

/// Same as [refresh], but does not change the state of the [PageManager].
@Deprecated('Use refresh() instead of smoothRefresh()')
Future<void> smoothRefresh() async {
refresh();
await refresh();
}

/// Run the load function according to the [LoadType].
Expand All @@ -59,21 +61,31 @@ abstract base class DataSource<PageKey, Value> {
await _append();
}
} on Exception catch (e) {
_manager.setError(e);
_manager.setError(
exception: e,
);
}
}

Future<void> _init() async {
_manager.changeState(LoadType.init);
_manager.changeState(
type: LoadType.init,
);

final result = await load(const Refresh());
switch (result) {
case Success(page: final page):
_manager.append(page);
case Failure(e: final e):
_manager.setError(e);
case Success(:final page):
await _manager.append(
newPage: page,
);
case Failure(:final e):
_manager.setError(
exception: e,
);
case None():
_manager.append(null);
await _manager.append(
newPage: null,
);
}
}

Expand All @@ -84,15 +96,23 @@ abstract base class DataSource<PageKey, Value> {
return;
}

_manager.changeState(LoadType.refresh);
_manager.changeState(
type: LoadType.refresh,
);
final result = await load(const Refresh());
switch (result) {
case Success(page: final page):
_manager.refresh(page);
case Failure(e: final e):
_manager.setError(e);
case Success(:final page):
_manager.refresh(
newPage: page,
);
case Failure(:final e):
_manager.setError(
exception: e,
);
case None():
_manager.append(null);
_manager.refresh(
newPage: null,
);
}
}

Expand All @@ -109,19 +129,27 @@ abstract base class DataSource<PageKey, Value> {
return;
}

_manager.changeState(LoadType.prepend);
_manager.changeState(
type: LoadType.prepend,
);
final result = await load(
Prepend(
key: key,
),
);
switch (result) {
case Success(page: final page):
_manager.prepend(page);
case Failure(e: final e):
_manager.setError(e);
case Success(:final page):
await _manager.prepend(
newPage: page,
);
case Failure(:final e):
_manager.setError(
exception: e,
);
case None():
_manager.prepend(null);
await _manager.prepend(
newPage: null,
);
}
}

Expand All @@ -138,20 +166,28 @@ abstract base class DataSource<PageKey, Value> {
return;
}

_manager.changeState(LoadType.append);
_manager.changeState(
type: LoadType.append,
);
final result = await load(
Append(
key: key,
),
);

switch (result) {
case Success(page: final page):
_manager.append(page);
case Failure(e: final e):
_manager.setError(e);
case Success(:final page):
await _manager.append(
newPage: page,
);
case Failure(:final e):
_manager.setError(
exception: e,
);
case None():
_manager.append(null);
await _manager.append(
newPage: null,
);
}
}
}
10 changes: 8 additions & 2 deletions lib/src/private/entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ class Paging<PageKey, Value> extends PageManagerState<PageKey, Value> {

@override
int get hashCode => Object.hash(
runtimeType, state, const DeepCollectionEquality().hash(data));
runtimeType,
state,
const DeepCollectionEquality().hash(data),
);
}

class Warning<PageKey, Value> extends PageManagerState<PageKey, Value> {
Expand All @@ -82,7 +85,10 @@ class Warning<PageKey, Value> extends PageManagerState<PageKey, Value> {
other.exception == exception));

@override
int get hashCode => Object.hash(runtimeType, exception);
int get hashCode => Object.hash(
runtimeType,
exception,
);
}

extension PagingStateExt<PageKey, Value> on PageManagerState<PageKey, Value> {
Expand Down
55 changes: 47 additions & 8 deletions lib/src/private/page_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@ import 'package:flutter/foundation.dart';
import 'package:paging_view/src/entity.dart';
import 'package:paging_view/src/private/entity.dart';

/// Manager class that manages [PageManagerState].
class PageManager<PageKey, Value>
extends ValueNotifier<PageManagerState<PageKey, Value>> {
PageManager() : super(Paging.init());
/// Creates a [PageManager].
PageManager({
this.delay = const Duration(
milliseconds: 100,
),
}) : super(Paging.init());

/// The delay time for reflecting the request result in the UI.
final Duration delay;

bool get isLoading => value.isLoading;

Expand All @@ -14,7 +23,9 @@ class PageManager<PageKey, Value>

List<Value> get values => value.items;

void changeState(LoadType type) {
void changeState({
required LoadType type,
}) {
value = Paging(
state: LoadStateLoading(
state: type,
Expand All @@ -23,13 +34,17 @@ class PageManager<PageKey, Value>
);
}

void setError(Exception exception) {
void setError({
required Exception exception,
}) {
value = Warning(
exception: exception,
);
}

void refresh(PageData<PageKey, Value>? newPage) {
void refresh({
required PageData<PageKey, Value>? newPage,
}) {
if (newPage == null) {
value = const Paging(
state: LoadStateLoaded(),
Expand All @@ -44,7 +59,9 @@ class PageManager<PageKey, Value>
);
}

void prepend(PageData<PageKey, Value>? newPage) {
Future<void> prepend({
required PageData<PageKey, Value>? newPage,
}) async {
if (newPage == null) {
value = Paging(
state: const LoadStateLoaded(),
Expand All @@ -54,12 +71,24 @@ class PageManager<PageKey, Value>
}

value = Paging(
state: const LoadStateLoaded(),
state: const LoadStateLoading(
state: LoadType.prepend,
),
data: [newPage, ...value.pages],
);

// Reflect the request result in the UI
await Future.delayed(delay);

value = Paging(
state: const LoadStateLoaded(),
data: value.pages,
);
}

void append(PageData<PageKey, Value>? newPage) {
Future<void> append({
required PageData<PageKey, Value>? newPage,
}) async {
if (newPage == null) {
value = Paging(
state: const LoadStateLoaded(),
Expand All @@ -69,8 +98,18 @@ class PageManager<PageKey, Value>
}

value = Paging(
state: const LoadStateLoaded(),
state: const LoadStateLoading(
state: LoadType.append,
),
data: [...value.pages, newPage],
);

// Reflect the request result in the UI
await Future.delayed(delay);

value = Paging(
state: const LoadStateLoaded(),
data: value.pages,
);
}
}
18 changes: 9 additions & 9 deletions lib/src/widget/sliver_paging_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class SliverPagingGrid<PageKey, Value> extends StatelessWidget {
return ValueListenableBuilder<PageManagerState<PageKey, Value>>(
valueListenable: dataSource.notifier,
builder: (context, value, child) => switch (value) {
Paging(state: final state, data: final pages) => _Grid<PageKey, Value>(
Paging(:final state, :final data) => _Grid<PageKey, Value>(
state: state,
pages: pages,
pages: data,
gridDelegate: gridDelegate,
dataSource: dataSource,
builder: builder,
Expand All @@ -75,7 +75,7 @@ class SliverPagingGrid<PageKey, Value> extends StatelessWidget {
fillEmptyWidget: fillRemainEmptyWidget,
padding: padding,
),
Warning(exception: final exception) => SliverPadding(
Warning(:final exception) => SliverPadding(
padding: padding,
sliver: fillRemainErrorWidget
? SliverFillRemaining(
Expand Down Expand Up @@ -129,8 +129,8 @@ class _Grid<PageKey, Value> extends StatelessWidget {
Widget build(BuildContext context) {
final state = this.state;
if (state is LoadStateInit) {
WidgetsBinding.instance.addPostFrameCallback((_) {
dataSource.update(LoadType.init);
WidgetsBinding.instance.addPostFrameCallback((_) async {
await dataSource.update(LoadType.init);
});

return SliverPadding(
Expand Down Expand Up @@ -188,13 +188,13 @@ class _Grid<PageKey, Value> extends StatelessWidget {
(context, index) {
if (index == 0) {
// prepend
WidgetsBinding.instance.addPostFrameCallback((_) {
dataSource.update(LoadType.prepend);
WidgetsBinding.instance.addPostFrameCallback((_) async {
await dataSource.update(LoadType.prepend);
});
} else if (index == items.length - 1) {
// append
WidgetsBinding.instance.addPostFrameCallback((_) {
dataSource.update(LoadType.append);
WidgetsBinding.instance.addPostFrameCallback((_) async {
await dataSource.update(LoadType.append);
});
}

Expand Down
Loading