Skip to content

Commit

Permalink
refactor: Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
koji-1009 committed Jul 21, 2024
1 parent ae75d96 commit 1bdaed6
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 79 deletions.
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
20 changes: 15 additions & 5 deletions lib/src/private/page_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,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 +25,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 +50,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 @@ -59,7 +67,9 @@ class PageManager<PageKey, Value>
);
}

void append(PageData<PageKey, Value>? newPage) {
Future<void> append({
required PageData<PageKey, Value>? newPage,
}) async {
if (newPage == null) {
value = Paging(
state: const LoadStateLoaded(),
Expand Down
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
71 changes: 35 additions & 36 deletions lib/src/widget/sliver_paging_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,36 +78,35 @@ class SliverPagingList<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) =>
_separatorBuilder != null
? _List<PageKey, Value>.separated(
state: state,
pages: pages,
separatorBuilder: _separatorBuilder!,
dataSource: dataSource,
builder: builder,
errorBuilder: errorBuilder,
initialLoadingWidget: initialLoadingWidget,
prependLoadingWidget: prependLoadingWidget,
appendLoadingWidget: appendLoadingWidget,
emptyWidget: emptyWidget,
fillRemainEmptyWidget: fillRemainEmptyWidget,
padding: padding,
)
: _List<PageKey, Value>(
state: state,
pages: pages,
dataSource: dataSource,
builder: builder,
errorBuilder: errorBuilder,
initialLoadingWidget: initialLoadingWidget,
prependLoadingWidget: prependLoadingWidget,
appendLoadingWidget: appendLoadingWidget,
emptyWidget: emptyWidget,
fillRemainEmptyWidget: fillRemainEmptyWidget,
padding: padding,
),
Warning(exception: final exception) => SliverPadding(
Paging(:final state, :final data) => _separatorBuilder != null
? _List<PageKey, Value>.separated(
state: state,
pages: data,
separatorBuilder: _separatorBuilder!,
dataSource: dataSource,
builder: builder,
errorBuilder: errorBuilder,
initialLoadingWidget: initialLoadingWidget,
prependLoadingWidget: prependLoadingWidget,
appendLoadingWidget: appendLoadingWidget,
emptyWidget: emptyWidget,
fillRemainEmptyWidget: fillRemainEmptyWidget,
padding: padding,
)
: _List<PageKey, Value>(
state: state,
pages: data,
dataSource: dataSource,
builder: builder,
errorBuilder: errorBuilder,
initialLoadingWidget: initialLoadingWidget,
prependLoadingWidget: prependLoadingWidget,
appendLoadingWidget: appendLoadingWidget,
emptyWidget: emptyWidget,
fillRemainEmptyWidget: fillRemainEmptyWidget,
padding: padding,
),
Warning(:final exception) => SliverPadding(
padding: padding,
sliver: fillRemainErrorWidget
? SliverFillRemaining(
Expand Down Expand Up @@ -177,8 +176,8 @@ class _List<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 @@ -218,13 +217,13 @@ class _List<PageKey, Value> extends StatelessWidget {
Widget? itemBuilder(BuildContext context, int 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

0 comments on commit 1bdaed6

Please sign in to comment.