Skip to content

Commit

Permalink
fix: pagination when fetching from API. (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
Livinglist authored Aug 20, 2024
1 parent 0332cd5 commit b9ff92a
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 181 deletions.
28 changes: 10 additions & 18 deletions lib/blocs/stories/stories_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> with Loggable {
on<StoriesLoadMore>(onLoadMore);
on<StoryLoaded>(
onStoryLoaded,
transformer: sequential(),
transformer: concurrent(),
);
on<StoryRead>(onStoryRead);
on<StoryUnread>(onStoryUnread);
Expand All @@ -53,8 +53,14 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> with Loggable {
on<StoryDownloaded>(onStoryDownloaded);
on<StoriesEnterOfflineMode>(onEnterOfflineMode);
on<StoriesExitOfflineMode>(onExitOfflineMode);
on<StoriesPageSizeChanged>(onPageSizeChanged);
on<ClearAllReadStories>(onClearAllReadStories);

_preferenceSubscription = _preferenceCubit.stream
.distinct((PreferenceState lhs, PreferenceState rhs) {
return lhs.dataSource == rhs.dataSource;
}).listen((PreferenceState prefState) {
add(StoriesInitialize());
});
}

final PreferenceCubit _preferenceCubit;
Expand All @@ -71,15 +77,6 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> with Loggable {
StoriesInitialize event,
Emitter<StoriesState> emit,
) async {
_preferenceSubscription = _preferenceCubit.stream
.distinct(
(PreferenceState lhs, PreferenceState rhs) =>
lhs.dataSource == rhs.dataSource,
)
.listen((PreferenceState prefState) {
add(StoriesInitialize());
});

final HackerNewsDataSource dataSource = _preferenceCubit.state.dataSource;

emit(
Expand Down Expand Up @@ -138,6 +135,7 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> with Loggable {
.listen((Story story) {
add(StoryLoaded(story: story, type: type));
}).asFuture<void>();
add(StoryLoadingCompleted(type: type));
} else {
emit(
state
Expand Down Expand Up @@ -223,6 +221,7 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> with Loggable {
final List<int> ids =
await _hackerNewsRepository.fetchStoryIds(type: event.type);
length = ids.length;
emit(state.copyWith());
} else {
length = ids!.length;
}
Expand Down Expand Up @@ -492,13 +491,6 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> with Loggable {
}
}

Future<void> onPageSizeChanged(
StoriesPageSizeChanged event,
Emitter<StoriesState> emit,
) async {
add(StoriesInitialize());
}

Future<void> onExitOfflineMode(
StoriesExitOfflineMode event,
Emitter<StoriesState> emit,
Expand Down
9 changes: 0 additions & 9 deletions lib/blocs/stories/stories_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,6 @@ class StoriesEnterOfflineMode extends StoriesEvent {
List<Object?> get props => <Object?>[];
}

class StoriesPageSizeChanged extends StoriesEvent {
StoriesPageSizeChanged({required this.pageSize});

final int pageSize;

@override
List<Object?> get props => <Object?>[pageSize];
}

class StoryLoaded extends StoriesEvent {
StoryLoaded({required this.story, required this.type});

Expand Down
2 changes: 1 addition & 1 deletion lib/models/item/story.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Story extends Item {
}

String get metadata =>
'''$score point${score > 1 ? 's' : ''} by $by $timeAgo | $descendants comment${descendants > 1 ? 's' : ''}''';
'''$score point${score > 1 ? 's' : ''}${by.isNotEmpty ? ' $by ' : ' '}$timeAgo | $descendants comment${descendants > 1 ? 's' : ''}''';

String get screenReaderLabel =>
'''$title, at $readableUrl, by $by $timeAgo. This story has $score point${score > 1 ? 's' : ''} and $descendants comment${descendants > 1 ? 's' : ''}''';
Expand Down
5 changes: 4 additions & 1 deletion lib/repositories/hacker_news_web_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ class HackerNewsWebRepository {
parts: const <int>[],
);

/// If it is a story about launching or from ask section, then
/// we need to fetch it from API since the html doesn't contain
/// too much info.
if (timestamp == null ||
url.isEmpty ||
url.contains('item?id=') ||
Expand All @@ -214,7 +217,7 @@ class HackerNewsWebRepository {
}
}

/// Duplicate comment means we are done fetching all the comments.
/// Duplicate story means we are done fetching all the stories.
if (fetchedStoryIds.contains(story.id)) return;

fetchedStoryIds.add(story.id);
Expand Down
Loading

0 comments on commit b9ff92a

Please sign in to comment.