Skip to content

Commit bb6b0f7

Browse files
committed
refactor(search): remove country search model
- Removed country search option - Simplified model type handling - Adjusted UI and logic to match
1 parent d2ede64 commit bb6b0f7

File tree

1 file changed

+62
-60
lines changed

1 file changed

+62
-60
lines changed

lib/headlines-search/view/headlines_search_page.dart

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import 'package:ht_main/headlines-search/bloc/headlines_search_bloc.dart';
1010
import 'package:ht_main/headlines-search/models/search_model_type.dart';
1111
// Import new item widgets
1212
import 'package:ht_main/headlines-search/widgets/category_item_widget.dart';
13-
import 'package:ht_main/headlines-search/widgets/country_item_widget.dart';
13+
// import 'package:ht_main/headlines-search/widgets/country_item_widget.dart'; // Removed
1414
import 'package:ht_main/headlines-search/widgets/source_item_widget.dart';
1515
import 'package:ht_main/l10n/l10n.dart';
1616
import 'package:ht_main/router/routes.dart';
1717
import 'package:ht_main/shared/constants/app_spacing.dart';
1818
import 'package:ht_main/shared/shared.dart'; // Imports new headline tiles
19-
import 'package:ht_shared/ht_shared.dart';
19+
// Adjusted imports to only include what's necessary after country removal
20+
import 'package:ht_shared/ht_shared.dart' show Category, Headline, Source, HeadlineImageStyle, SearchModelType;
2021

2122
/// Page widget responsible for providing the BLoC for the headlines search feature.
2223
class HeadlinesSearchPage extends StatelessWidget {
@@ -58,8 +59,10 @@ class _HeadlinesSearchViewState extends State<_HeadlinesSearchView> {
5859
_showClearButton = _textController.text.isNotEmpty;
5960
});
6061
});
61-
// Set initial model type in BLoC if not already set (e.g. on first load)
62-
// Though BLoC state now defaults, this ensures UI and BLoC are in sync.
62+
// Ensure _selectedModelType is valid (it should be, as .country is removed from enum)
63+
if (!SearchModelType.values.contains(_selectedModelType)) {
64+
_selectedModelType = SearchModelType.headline;
65+
}
6366
context.read<HeadlinesSearchBloc>().add(
6467
HeadlinesSearchModelTypeChanged(_selectedModelType),
6568
);
@@ -102,21 +105,36 @@ class _HeadlinesSearchViewState extends State<_HeadlinesSearchView> {
102105
final colorScheme = theme.colorScheme;
103106
final appBarTheme = theme.appBarTheme;
104107

108+
// Use all values from SearchModelType as .country is already removed from the enum itself
109+
final availableSearchModelTypes = SearchModelType.values.toList();
110+
111+
// Ensure _selectedModelType is still valid if it somehow was .country
112+
// (though this shouldn't happen if initState logic is correct and enum is updated)
113+
if (!availableSearchModelTypes.contains(_selectedModelType)) {
114+
_selectedModelType = SearchModelType.headline;
115+
WidgetsBinding.instance.addPostFrameCallback((_) {
116+
if (mounted) {
117+
context.read<HeadlinesSearchBloc>().add(
118+
HeadlinesSearchModelTypeChanged(_selectedModelType),
119+
);
120+
}
121+
});
122+
}
123+
105124
return Scaffold(
106125
appBar: AppBar(
107-
// Removed leading and leadingWidth
108-
titleSpacing: AppSpacing.paddingSmall, // Adjust title spacing if needed
126+
titleSpacing: AppSpacing.paddingSmall,
109127
title: Row(
110128
children: [
111129
SizedBox(
112-
width: 140, // Constrain dropdown width
130+
width: 140,
113131
child: DropdownButtonFormField<SearchModelType>(
114132
value: _selectedModelType,
115133
decoration: const InputDecoration(
116134
border: InputBorder.none,
117135
contentPadding: EdgeInsets.symmetric(
118-
horizontal: AppSpacing.xs, // Minimal horizontal padding
119-
vertical: AppSpacing.xs, // Reduce vertical padding
136+
horizontal: AppSpacing.xs,
137+
vertical: AppSpacing.xs,
120138
),
121139
),
122140
style: theme.textTheme.titleMedium?.copyWith(
@@ -129,30 +147,30 @@ class _HeadlinesSearchViewState extends State<_HeadlinesSearchView> {
129147
Icons.arrow_drop_down,
130148
color: appBarTheme.iconTheme?.color ?? colorScheme.onSurface,
131149
),
132-
items:
133-
SearchModelType.values.map((SearchModelType type) {
134-
String displayLocalizedName;
135-
switch (type) {
136-
case SearchModelType.headline:
137-
displayLocalizedName = l10n.searchModelTypeHeadline;
138-
case SearchModelType.category:
139-
displayLocalizedName = l10n.searchModelTypeCategory;
140-
case SearchModelType.source:
141-
displayLocalizedName = l10n.searchModelTypeSource;
142-
case SearchModelType.country:
143-
displayLocalizedName = l10n.searchModelTypeCountry;
144-
}
145-
return DropdownMenuItem<SearchModelType>(
146-
value: type,
147-
child: Text(
148-
displayLocalizedName,
149-
style: theme.textTheme.titleMedium?.copyWith(
150-
// Consistent style
151-
color: colorScheme.onSurface,
152-
),
153-
),
154-
);
155-
}).toList(),
150+
items: availableSearchModelTypes.map((SearchModelType type) {
151+
String displayLocalizedName;
152+
// The switch is now exhaustive as SearchModelType.country is removed from the enum
153+
switch (type) {
154+
case SearchModelType.headline:
155+
displayLocalizedName = l10n.searchModelTypeHeadline;
156+
break;
157+
case SearchModelType.category:
158+
displayLocalizedName = l10n.searchModelTypeCategory;
159+
break;
160+
case SearchModelType.source:
161+
displayLocalizedName = l10n.searchModelTypeSource;
162+
break;
163+
}
164+
return DropdownMenuItem<SearchModelType>(
165+
value: type,
166+
child: Text(
167+
displayLocalizedName,
168+
style: theme.textTheme.titleMedium?.copyWith(
169+
color: colorScheme.onSurface,
170+
),
171+
),
172+
);
173+
}).toList(),
156174
onChanged: (SearchModelType? newValue) {
157175
if (newValue != null) {
158176
setState(() {
@@ -165,9 +183,7 @@ class _HeadlinesSearchViewState extends State<_HeadlinesSearchView> {
165183
},
166184
),
167185
),
168-
const SizedBox(
169-
width: AppSpacing.sm,
170-
), // Spacing between dropdown and textfield
186+
const SizedBox(width: AppSpacing.sm),
171187
Expanded(
172188
child: TextField(
173189
controller: _textController,
@@ -184,9 +200,7 @@ class _HeadlinesSearchViewState extends State<_HeadlinesSearchView> {
184200
fillColor: colorScheme.surface.withAlpha(26),
185201
contentPadding: const EdgeInsets.symmetric(
186202
horizontal: AppSpacing.paddingMedium,
187-
vertical:
188-
AppSpacing.paddingSmall +
189-
3, // Fine-tune vertical padding for alignment
203+
vertical: AppSpacing.paddingSmall + 3,
190204
),
191205
suffixIcon:
192206
_showClearButton
@@ -218,16 +232,13 @@ class _HeadlinesSearchViewState extends State<_HeadlinesSearchView> {
218232
builder: (context, state) {
219233
return switch (state) {
220234
HeadlinesSearchInitial() => InitialStateWidget(
221-
icon: Icons.search, // Changed icon
222-
headline: l10n.searchPageInitialHeadline, // Use new generic key
223-
subheadline:
224-
l10n.searchPageInitialSubheadline, // Use new generic key
235+
icon: Icons.search,
236+
headline: l10n.searchPageInitialHeadline,
237+
subheadline: l10n.searchPageInitialSubheadline,
225238
),
226-
// Use more generic loading text or existing keys
227239
HeadlinesSearchLoading() => InitialStateWidget(
228240
icon: Icons.manage_search,
229-
headline:
230-
l10n.headlinesFeedLoadingHeadline, // Re-use feed loading
241+
headline: l10n.headlinesFeedLoadingHeadline,
231242
subheadline:
232243
'Searching ${state.selectedModelType.displayName.toLowerCase()}...',
233244
),
@@ -255,24 +266,19 @@ class _HeadlinesSearchViewState extends State<_HeadlinesSearchView> {
255266
)
256267
: ListView.separated(
257268
controller: _scrollController,
258-
padding: const EdgeInsets.all(
259-
AppSpacing.paddingMedium,
260-
), // Add overall padding
269+
padding: const EdgeInsets.all(AppSpacing.paddingMedium),
261270
itemCount: hasMore ? results.length + 1 : results.length,
262271
separatorBuilder:
263-
(context, index) => const SizedBox(
264-
height: AppSpacing.md,
265-
), // Add separator
272+
(context, index) => const SizedBox(height: AppSpacing.md),
266273
itemBuilder: (context, index) {
267274
if (index >= results.length) {
268275
return const Padding(
269-
padding: EdgeInsets.symmetric(
270-
vertical: AppSpacing.lg,
271-
), // Adjusted padding for loader
276+
padding: EdgeInsets.symmetric(vertical: AppSpacing.lg),
272277
child: Center(child: CircularProgressIndicator()),
273278
);
274279
}
275280
final item = results[index];
281+
// The switch is now exhaustive for the remaining SearchModelType values
276282
switch (resultsModelType) {
277283
case SearchModelType.headline:
278284
final headline = item as Headline;
@@ -321,8 +327,6 @@ class _HeadlinesSearchViewState extends State<_HeadlinesSearchView> {
321327
return CategoryItemWidget(category: item as Category);
322328
case SearchModelType.source:
323329
return SourceItemWidget(source: item as Source);
324-
case SearchModelType.country:
325-
return CountryItemWidget(country: item as Country);
326330
}
327331
},
328332
),
@@ -339,7 +343,6 @@ class _HeadlinesSearchViewState extends State<_HeadlinesSearchView> {
339343
HeadlinesSearchFetchRequested(searchTerm: lastSearchTerm),
340344
),
341345
),
342-
// Add default case for exhaustiveness
343346
_ => const SizedBox.shrink(),
344347
};
345348
},
@@ -351,15 +354,14 @@ class _HeadlinesSearchViewState extends State<_HeadlinesSearchView> {
351354
SearchModelType modelType,
352355
AppLocalizations l10n,
353356
) {
357+
// The switch is now exhaustive for the remaining SearchModelType values
354358
switch (modelType) {
355359
case SearchModelType.headline:
356360
return l10n.searchHintTextHeadline;
357361
case SearchModelType.category:
358362
return l10n.searchHintTextCategory;
359363
case SearchModelType.source:
360364
return l10n.searchHintTextSource;
361-
case SearchModelType.country:
362-
return l10n.searchHintTextCountry;
363365
}
364366
}
365367
}

0 commit comments

Comments
 (0)