Skip to content

Commit

Permalink
refactor: improve PagingStateBase constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
clragon committed Jan 28, 2025
1 parent 262781a commit 245f5d6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 34 deletions.
14 changes: 8 additions & 6 deletions lib/src/core/paging_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ abstract class PagingState<PageKeyType extends Object,
/// If a field is not provided, it will default to the current value.
///
/// While this implementation technically accepts Futures, passing a Future is invalid.
/// The FutureOr type is used to allow for the Omit sentinel value,
/// The Defaulted type is used to allow for the Omit sentinel value,
/// which is required to distinguish between a parameter being omitted and a parameter being set to null.
// copyWith a la Remi Rousselet: https://github.com/dart-lang/language/issues/137#issuecomment-583783054
PagingState<PageKeyType, ItemType> copyWith({
FutureOr<List<List<ItemType>>?>? pages = const Omit(),
FutureOr<List<PageKeyType>?>? keys = const Omit(),
FutureOr<Object?>? error = const Omit(),
FutureOr<bool>? hasNextPage = const Omit(),
FutureOr<bool>? isLoading = const Omit(),
Defaulted<List<List<ItemType>>?>? pages = const Omit(),
Defaulted<List<PageKeyType>?>? keys = const Omit(),
Defaulted<Object?>? error = const Omit(),
Defaulted<bool>? hasNextPage = const Omit(),
Defaulted<bool>? isLoading = const Omit(),
});

/// Returns a copy this [PagingState] but
Expand All @@ -70,6 +70,8 @@ extension ItemListExtension<PageKeyType extends Object, ItemType extends Object>
List<ItemType>? get items => pages?.expand((e) => e).toList();
}

typedef Defaulted<T> = FutureOr<T>;

/// Sentinel value to omit a parameter from a copyWith call.
/// This is used to distinguish between a parameter being omitted and a parameter
/// being set to null.
Expand Down
43 changes: 15 additions & 28 deletions lib/src/core/paging_state_base.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:async';

import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:infinite_scroll_pagination/src/core/paging_state.dart';
Expand All @@ -10,34 +8,23 @@ import 'package:infinite_scroll_pagination/src/core/paging_state.dart';
/// all of its fields are deeply equal.
base class PagingStateBase<PageKeyType extends Object, ItemType extends Object>
implements PagingState<PageKeyType, ItemType> {
factory PagingStateBase({
/// Creates a [PagingStateBase] with the given parameters.
///
/// Ensures that [pages] and [keys] are unmodifiable lists.
PagingStateBase({
List<List<ItemType>>? pages,
List<PageKeyType>? keys,
Object? error,
bool hasNextPage = true,
bool isLoading = false,
}) =>
PagingStateBase._(
pages: switch (pages) {
this.error,
this.hasNextPage = true,
this.isLoading = false,
}) : pages = switch (pages) {
null => null,
_ => List.unmodifiable(pages),
},
keys: switch (keys) {
keys = switch (keys) {
null => null,
_ => List.unmodifiable(keys),
},
error: error,
hasNextPage: hasNextPage,
isLoading: isLoading,
);

const PagingStateBase._({
required this.pages,
required this.keys,
required this.error,
required this.hasNextPage,
required this.isLoading,
});
};

@override
final List<List<ItemType>>? pages;
Expand All @@ -56,11 +43,11 @@ base class PagingStateBase<PageKeyType extends Object, ItemType extends Object>

@override
PagingState<PageKeyType, ItemType> copyWith({
FutureOr<List<List<ItemType>>?>? pages = const Omit(),
FutureOr<List<PageKeyType>?>? keys = const Omit(),
FutureOr<Object?>? error = const Omit(),
FutureOr<bool>? hasNextPage = const Omit(),
FutureOr<bool>? isLoading = const Omit(),
Defaulted<List<List<ItemType>>?>? pages = const Omit(),
Defaulted<List<PageKeyType>?>? keys = const Omit(),
Defaulted<Object?>? error = const Omit(),
Defaulted<bool>? hasNextPage = const Omit(),
Defaulted<bool>? isLoading = const Omit(),
}) =>
PagingStateBase(
pages: pages is Omit ? this.pages : pages as List<List<ItemType>>?,
Expand Down

0 comments on commit 245f5d6

Please sign in to comment.