Skip to content

Commit

Permalink
✨ 再読み込み時に動的に読み込み表示から結果表示に切り替わるように
Browse files Browse the repository at this point in the history
  • Loading branch information
Aosanori committed May 21, 2024
1 parent ecf3d4d commit c82f97a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
12 changes: 9 additions & 3 deletions packages/cores/core/lib/src/extension/async_value.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:cores_core/util.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

extension AsyncValueExtension<T> on AsyncValue<T> {
Expand All @@ -20,7 +21,11 @@ extension AsyncValueExtension<T> on AsyncValue<T> {
/// even if subsequent fetch attempts result in errors,
/// ideal for maintaining a seamless user experience.
R whenIgnorableError<R>({
required R Function(T data, {required bool hasError}) data,
required R Function(
T data, {
required bool hasError,
required bool isLoading,
}) data,
required R Function(Object error, StackTrace stackTrace) error,
required R Function() loading,
bool skipLoadingOnReload = false,
Expand All @@ -29,16 +34,17 @@ extension AsyncValueExtension<T> on AsyncValue<T> {
bool skipErrorOnHasValue = false,
}) {
if (skipErrorOnHasValue) {
logger.shout(isLoading);
if (hasValue && hasError) {
return data(requireValue, hasError: true);
return data(requireValue, hasError: true, isLoading: isLoading);
}
}

return when(
skipLoadingOnReload: skipLoadingOnReload,
skipLoadingOnRefresh: skipLoadingOnRefresh,
skipError: skipError,
data: (d) => data(d, hasError: hasError),
data: (d) => data(d, hasError: hasError, isLoading: isLoading),
error: error,
loading: loading,
);
Expand Down
30 changes: 23 additions & 7 deletions packages/cores/core/lib/src/ui/util/common_paging_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,35 @@ class CommonPagingView<N extends PagingAsyncNotifier<D, T>,
);

return ref.watch(_provider).whenIgnorableError(
data: (data, {required hasError}) {
data: (
data, {
required hasError,
required isLoading,
}) {
return RefreshIndicator(
onRefresh: () async => ref.refresh(_provider.future),
child: _contentBuilder(
data,
// Displays EndItem to detect scroll end
// if more data is available and no errors.
data.hasMore && !hasError
? _EndItem(
onScrollEnd: () async =>
ref.read(_provider.notifier).loadNext(),
)
: null,
switch (data.hasMore) {
true when hasError && isLoading => const Center(
child: Padding(
padding: EdgeInsets.all(16),
child: CircularProgressIndicator(),
),
),
true when hasError && !isLoading => _EndItemWhenError(
onPressed: () async =>
ref.read(_provider.notifier).loadNext(),
),
true when !hasError => _EndItem(
onScrollEnd: () async =>
ref.read(_provider.notifier).loadNext(),
),
true => null,
false => null,
},
),
);
},
Expand Down

0 comments on commit c82f97a

Please sign in to comment.