Skip to content

Commit

Permalink
fix: manual pagination. (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
Livinglist authored Aug 16, 2024
1 parent 7325a08 commit b76c5dd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 51 deletions.
29 changes: 15 additions & 14 deletions lib/blocs/stories/stories_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> with Loggable {
emit(
state
.copyWithStoryIdsUpdated(type: type, to: ids)
.copyWithCurrentPageUpdated(type: type, to: 0)
.copyWithCurrentPageUpdated(type: type, to: 1)
.copyWithStatusUpdated(type: type, to: Status.inProgress),
);

Expand All @@ -125,7 +125,7 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> with Loggable {
} else {
emit(
state
.copyWithCurrentPageUpdated(type: type, to: 0)
.copyWithCurrentPageUpdated(type: type, to: 1)
.copyWithStatusUpdated(type: type, to: Status.inProgress),
);

Expand Down Expand Up @@ -186,18 +186,11 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> with Loggable {
),
);

late final int currentPage;
final int currentPage = state.currentPageByType[event.type]! + 1;

/// If useApi is true, it means this is a fallback fetch.
/// Don't increment the page number in this case.
if (event.useApi) {
currentPage = state.currentPageByType[event.type]!;
} else {
currentPage = state.currentPageByType[event.type]! + 1;
emit(
state.copyWithCurrentPageUpdated(type: event.type, to: currentPage),
);
}
emit(
state.copyWithCurrentPageUpdated(type: event.type, to: currentPage),
);

if (state.isOfflineReading) {
emit(
Expand All @@ -218,7 +211,7 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> with Loggable {
length = ids!.length;
}

final int lower = apiPageSize * currentPage;
final int lower = min(length, apiPageSize * (currentPage - 1));
final int upper = min(length, lower + apiPageSize);
_hackerNewsRepository
.fetchStoriesStream(
Expand All @@ -241,7 +234,15 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> with Loggable {
case RateLimitedException:
case RateLimitedWithFallbackException:
case PossibleParsingException:

/// Fall back to use API instead.
add(event.copyWith(useApi: true));
emit(
state.copyWithCurrentPageUpdated(
type: event.type,
to: currentPage - 1,
),
);
}
})
.listen(
Expand Down
67 changes: 30 additions & 37 deletions lib/screens/widgets/stories_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:flutter/rendering.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:hacki/blocs/blocs.dart';
import 'package:hacki/config/constants.dart';
import 'package:hacki/cubits/cubits.dart';
import 'package:hacki/extensions/extensions.dart';
import 'package:hacki/models/models.dart';
Expand Down Expand Up @@ -76,13 +75,6 @@ class _StoriesListViewState extends State<StoriesListView>
(previous.statusByType[widget.storyType] !=
current.statusByType[widget.storyType]),
builder: (BuildContext context, StoriesState state) {
bool shouldShowLoadButton() {
return preferenceState.isManualPaginationEnabled &&
state.statusByType[widget.storyType] == Status.success &&
(state.storiesByType[widget.storyType]?.length ?? 0) <
(state.storyIdsByType[widget.storyType]?.length ?? 0);
}

return ItemsListView<Story>(
showOfflineBanner: true,
markReadStories: preferenceState.isMarkReadStoriesEnabled,
Expand Down Expand Up @@ -114,38 +106,39 @@ class _StoriesListViewState extends State<StoriesListView>
onPinned: context.read<PinCubit>().pinStory,
header: state.isOfflineReading ? null : header,
loadStyle: LoadStyle.HideAlways,
footer: Center(
child: AnimatedCrossFade(
alignment: Alignment.center,
crossFadeState: shouldShowLoadButton()
? CrossFadeState.showFirst
: CrossFadeState.showSecond,
duration: AppDurations.ms300,
firstChild: Padding(
padding: const EdgeInsets.only(
left: Dimens.pt48,
right: Dimens.pt48,
top: Dimens.pt36,
bottom: Dimens.pt12,
),
child: OutlinedButton(
onPressed: loadMoreStories,
style: ButtonStyle(
minimumSize: WidgetStateProperty.all(
const Size(double.infinity, Dimens.pt48),
footer: preferenceState.isManualPaginationEnabled
? Center(
child: Padding(
padding: const EdgeInsets.only(
left: Dimens.pt48,
right: Dimens.pt48,
top: Dimens.pt36,
bottom: Dimens.pt12,
),
foregroundColor: WidgetStateColor.resolveWith(
(_) => Theme.of(context).colorScheme.onSurface,
child: OutlinedButton(
onPressed: loadMoreStories,
style: ButtonStyle(
minimumSize: WidgetStateProperty.all(
const Size(double.infinity, Dimens.pt48),
),
foregroundColor: WidgetStateColor.resolveWith(
(_) => Theme.of(context).colorScheme.onSurface,
),
),
child: state.statusByType[widget.storyType] ==
Status.success
? Text(
'''Load Page ${(state.currentPageByType[widget.storyType] ?? 0) + 1}''',
)
: const SizedBox(
height: Dimens.pt6,
width: Dimens.pt6,
child: CustomCircularProgressIndicator(),
),
),
),
child: Text(
'''Load Page ${(state.currentPageByType[widget.storyType] ?? 0) + 2}''',
),
),
),
secondChild: const SizedBox.shrink(),
),
),
)
: const SizedBox.shrink(),
onMoreTapped: onMoreTapped,
itemBuilder: (Widget child, Story story) {
return Slidable(
Expand Down

0 comments on commit b76c5dd

Please sign in to comment.