Skip to content

Commit 6c99551

Browse files
committed
refactor: use ht_headlines_client package
- Replaced repository with client package - Simplified dependency injection
1 parent 32b4e47 commit 6c99551

File tree

6 files changed

+59
-63
lines changed

6 files changed

+59
-63
lines changed

lib/headlines-feed/widgets/headline_item_widget.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:go_router/go_router.dart';
3-
import 'package:ht_headlines_repository/ht_headlines_repository.dart'
4-
show Headline;
3+
import 'package:ht_headlines_client/ht_headlines_client.dart' show Headline;
54
import 'package:ht_main/router/routes.dart';
65

76
/// A widget that displays a single headline.
@@ -57,8 +56,8 @@ class HeadlineItemWidget extends StatelessWidget {
5756
width: 75,
5857
height: 75,
5958
fit: BoxFit.cover,
60-
errorBuilder: (context, error, stackTrace) =>
61-
const Icon(Icons.error),
59+
errorBuilder:
60+
(context, error, stackTrace) => const Icon(Icons.error),
6261
),
6362
),
6463
),

lib/headlines-search/bloc/headlines_search_bloc.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:bloc/bloc.dart';
22
import 'package:equatable/equatable.dart';
3+
import 'package:ht_headlines_client/ht_headlines_client.dart';
34
import 'package:ht_headlines_repository/ht_headlines_repository.dart';
45

56
part 'headlines_search_event.dart';
@@ -8,8 +9,8 @@ part 'headlines_search_state.dart';
89
class HeadlinesSearchBloc
910
extends Bloc<HeadlinesSearchEvent, HeadlinesSearchState> {
1011
HeadlinesSearchBloc({required HtHeadlinesRepository headlinesRepository})
11-
: _headlinesRepository = headlinesRepository,
12-
super(HeadlinesSearchLoading()) {
12+
: _headlinesRepository = headlinesRepository,
13+
super(HeadlinesSearchLoading()) {
1314
on<HeadlinesSearchFetchRequested>(_onSearchFetchRequested);
1415
}
1516

@@ -46,11 +47,11 @@ class HeadlinesSearchBloc
4647
response.items.isEmpty
4748
? currentState.copyWith(hasMore: false)
4849
: currentState.copyWith(
49-
headlines: List.of(currentState.headlines)
50-
..addAll(response.items),
51-
hasMore: response.hasMore,
52-
cursor: response.cursor,
53-
),
50+
headlines: List.of(currentState.headlines)
51+
..addAll(response.items),
52+
hasMore: response.hasMore,
53+
cursor: response.cursor,
54+
),
5455
);
5556
} catch (e) {
5657
emit(currentState.copyWith(errorMessage: e.toString()));

lib/headlines-search/bloc/headlines_search_state.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class HeadlinesSearchSuccess extends HeadlinesSearchState {
4747
}
4848

4949
@override
50-
List<Object?> get props =>
51-
[headlines, hasMore, cursor, errorMessage, lastSearchTerm];
50+
List<Object?> get props => [
51+
headlines,
52+
hasMore,
53+
cursor,
54+
errorMessage,
55+
lastSearchTerm,
56+
];
5257
}

lib/headlines-search/view/headlines_search_page.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ class HeadlinesSearchPage extends StatelessWidget {
1414
@override
1515
Widget build(BuildContext context) {
1616
return BlocProvider(
17-
create: (_) => HeadlinesSearchBloc(
18-
headlinesRepository: context.read<HtHeadlinesRepository>(),
19-
),
17+
create:
18+
(_) => HeadlinesSearchBloc(
19+
headlinesRepository: context.read<HtHeadlinesRepository>(),
20+
),
2021
child: const HeadlinesSearchView(),
2122
);
2223
}

lib/headlines-search/view/headlines_search_view.dart

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ class _HeadlinesSearchViewState extends State<HeadlinesSearchView> {
3535
if (_isBottom && state is HeadlinesSearchSuccess) {
3636
final searchTerm = state.lastSearchTerm;
3737
if (state.hasMore) {
38-
context
39-
.read<HeadlinesSearchBloc>()
40-
.add(HeadlinesSearchFetchRequested(searchTerm: searchTerm!));
38+
context.read<HeadlinesSearchBloc>().add(
39+
HeadlinesSearchFetchRequested(searchTerm: searchTerm!),
40+
);
4141
}
4242
}
4343
}
@@ -54,9 +54,7 @@ class _HeadlinesSearchViewState extends State<HeadlinesSearchView> {
5454
return Scaffold(
5555
appBar: AppBar(
5656
title: TextField(
57-
decoration: const InputDecoration(
58-
hintText: 'Search Headlines...',
59-
),
57+
decoration: const InputDecoration(hintText: 'Search Headlines...'),
6058
onChanged: (value) {
6159
searchTerm = value;
6260
},
@@ -66,8 +64,8 @@ class _HeadlinesSearchViewState extends State<HeadlinesSearchView> {
6664
icon: const Icon(Icons.search),
6765
onPressed: () {
6866
context.read<HeadlinesSearchBloc>().add(
69-
HeadlinesSearchFetchRequested(searchTerm: searchTerm ?? ''),
70-
);
67+
HeadlinesSearchFetchRequested(searchTerm: searchTerm ?? ''),
68+
);
7169
},
7270
),
7371
],
@@ -76,47 +74,43 @@ class _HeadlinesSearchViewState extends State<HeadlinesSearchView> {
7674
builder: (context, state) {
7775
return switch (state) {
7876
HeadlinesSearchLoading() => const InitialStateWidget(
79-
icon: Icons.search,
80-
headline: 'Search Headlines',
81-
subheadline: 'Enter keywords to find articles',
82-
),
77+
icon: Icons.search,
78+
headline: 'Search Headlines',
79+
subheadline: 'Enter keywords to find articles',
80+
),
8381
HeadlinesSearchSuccess(
8482
:final headlines,
8583
:final hasMore,
86-
:final errorMessage
84+
:final errorMessage,
8785
) =>
8886
errorMessage != null
8987
? FailureStateWidget(
90-
message: errorMessage,
91-
onRetry: () {
92-
context.read<HeadlinesSearchBloc>().add(
93-
HeadlinesSearchFetchRequested(
94-
searchTerm: searchTerm ?? '',
95-
),
96-
);
97-
},
98-
)
99-
: headlines.isEmpty
100-
? const InitialStateWidget(
101-
icon: Icons.search_off,
102-
headline: 'No results',
103-
subheadline: 'Try a different search term',
104-
)
105-
: ListView.builder(
106-
controller: _scrollController,
107-
itemCount:
108-
hasMore ? headlines.length + 1 : headlines.length,
109-
itemBuilder: (context, index) {
110-
if (index >= headlines.length) {
111-
return const Center(
112-
child: CircularProgressIndicator(),
113-
);
114-
}
115-
return HeadlineItemWidget(
116-
headline: headlines[index],
117-
);
118-
},
88+
message: errorMessage,
89+
onRetry: () {
90+
context.read<HeadlinesSearchBloc>().add(
91+
HeadlinesSearchFetchRequested(
92+
searchTerm: searchTerm ?? '',
11993
),
94+
);
95+
},
96+
)
97+
: headlines.isEmpty
98+
? const InitialStateWidget(
99+
icon: Icons.search_off,
100+
headline: 'No results',
101+
subheadline: 'Try a different search term',
102+
)
103+
: ListView.builder(
104+
controller: _scrollController,
105+
itemCount:
106+
hasMore ? headlines.length + 1 : headlines.length,
107+
itemBuilder: (context, index) {
108+
if (index >= headlines.length) {
109+
return const Center(child: CircularProgressIndicator());
110+
}
111+
return HeadlineItemWidget(headline: headlines[index]);
112+
},
113+
),
120114
_ => const SizedBox.shrink(),
121115
};
122116
},

lib/shared/widgets/failure_state_widget.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ class FailureStateWidget extends StatelessWidget {
88
///
99
/// The [onRetry] is an optional callback to be called
1010
/// when the retry button is pressed.
11-
const FailureStateWidget({
12-
required this.message,
13-
super.key,
14-
this.onRetry,
15-
});
11+
const FailureStateWidget({required this.message, super.key, this.onRetry});
1612

1713
/// The error message to display.
1814
final String message;

0 commit comments

Comments
 (0)