Skip to content

Commit 800b9c6

Browse files
committed
refactor(content): use country_picker for edit source
1 parent b7b6b61 commit 800b9c6

File tree

4 files changed

+27
-56
lines changed

4 files changed

+27
-56
lines changed

lib/content_management/bloc/edit_source/edit_source_bloc.dart

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import 'package:bloc/bloc.dart';
22
import 'package:core/core.dart';
3+
import 'package:country_picker/country_picker.dart' as picker;
34
import 'package:data_repository/data_repository.dart';
45
import 'package:equatable/equatable.dart';
56
import 'package:flutter/foundation.dart';
67
import 'package:flutter_news_app_web_dashboard_full_source_code/l10n/app_localizations.dart';
8+
import 'package:flutter_news_app_web_dashboard_full_source_code/shared/shared.dart';
79

810
part 'edit_source_event.dart';
911
part 'edit_source_state.dart';
@@ -13,10 +15,8 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
1315
/// {@macro edit_source_bloc}
1416
EditSourceBloc({
1517
required DataRepository<Source> sourcesRepository,
16-
required DataRepository<Country> countriesRepository,
1718
required String sourceId,
1819
}) : _sourcesRepository = sourcesRepository,
19-
_countriesRepository = countriesRepository,
2020
_sourceId = sourceId,
2121
super(const EditSourceState()) {
2222
on<EditSourceLoaded>(_onLoaded);
@@ -31,7 +31,6 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
3131
}
3232

3333
final DataRepository<Source> _sourcesRepository;
34-
final DataRepository<Country> _countriesRepository;
3534
final String _sourceId;
3635

3736
Future<void> _onLoaded(
@@ -40,14 +39,7 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
4039
) async {
4140
emit(state.copyWith(status: EditSourceStatus.loading));
4241
try {
43-
final [sourceResponse, countriesResponse] = await Future.wait([
44-
_sourcesRepository.read(id: _sourceId),
45-
_countriesRepository.readAll(
46-
sort: [const SortOption('updatedAt', SortOrder.desc)],
47-
),
48-
]);
49-
final source = sourceResponse as Source;
50-
final countries = (countriesResponse as PaginatedResponse<Country>).items;
42+
final source = await _sourcesRepository.read(id: _sourceId);
5143
emit(
5244
state.copyWith(
5345
status: EditSourceStatus.initial,
@@ -59,7 +51,6 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
5951
language: source.language,
6052
headquarters: () => source.headquarters,
6153
contentStatus: source.status,
62-
countries: countries,
6354
),
6455
);
6556
} on HttpException catch (e) {
@@ -128,12 +119,16 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
128119
EditSourceHeadquartersChanged event,
129120
Emitter<EditSourceState> emit,
130121
) {
131-
emit(
132-
state.copyWith(
133-
headquarters: () => event.headquarters,
134-
status: EditSourceStatus.initial,
135-
),
136-
);
122+
final packageCountry = event.headquarters;
123+
if (packageCountry == null) {
124+
emit(state.copyWith(headquarters: () => null));
125+
} else {
126+
final coreCountry = adaptPackageCountryToCoreCountry(packageCountry);
127+
emit(
128+
state.copyWith(
129+
headquarters: () => coreCountry, status: EditSourceStatus.initial),
130+
);
131+
}
137132
}
138133

139134
void _onStatusChanged(

lib/content_management/bloc/edit_source/edit_source_event.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ final class EditSourceLanguageChanged extends EditSourceEvent {
6767
final class EditSourceHeadquartersChanged extends EditSourceEvent {
6868
const EditSourceHeadquartersChanged(this.headquarters);
6969

70-
final Country? headquarters;
70+
final picker.Country? headquarters;
7171

7272
@override
7373
List<Object?> get props => [headquarters];

lib/content_management/bloc/edit_source/edit_source_state.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ final class EditSourceState extends Equatable {
2929
this.sourceType,
3030
this.language = '',
3131
this.headquarters,
32-
this.countries = const [],
3332
this.contentStatus = ContentStatus.active,
3433
this.exception,
3534
this.updatedSource,
@@ -43,7 +42,6 @@ final class EditSourceState extends Equatable {
4342
final SourceType? sourceType;
4443
final String language;
4544
final Country? headquarters;
46-
final List<Country> countries;
4745
final ContentStatus contentStatus;
4846
final HttpException? exception;
4947
final Source? updatedSource;
@@ -66,7 +64,6 @@ final class EditSourceState extends Equatable {
6664
ValueGetter<SourceType?>? sourceType,
6765
String? language,
6866
ValueGetter<Country?>? headquarters,
69-
List<Country>? countries,
7067
ContentStatus? contentStatus,
7168
HttpException? exception,
7269
Source? updatedSource,
@@ -80,7 +77,6 @@ final class EditSourceState extends Equatable {
8077
sourceType: sourceType != null ? sourceType() : this.sourceType,
8178
language: language ?? this.language,
8279
headquarters: headquarters != null ? headquarters() : this.headquarters,
83-
countries: countries ?? this.countries,
8480
contentStatus: contentStatus ?? this.contentStatus,
8581
exception: exception,
8682
updatedSource: updatedSource ?? this.updatedSource,
@@ -97,7 +93,6 @@ final class EditSourceState extends Equatable {
9793
sourceType,
9894
language,
9995
headquarters,
100-
countries,
10196
contentStatus,
10297
exception,
10398
updatedSource,

lib/content_management/view/edit_source_page.dart

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:core/core.dart';
2+
import 'package:country_picker/country_picker.dart' as picker;
23
import 'package:data_repository/data_repository.dart';
34
import 'package:flutter/material.dart';
45
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -25,7 +26,6 @@ class EditSourcePage extends StatelessWidget {
2526
return BlocProvider(
2627
create: (context) => EditSourceBloc(
2728
sourcesRepository: context.read<DataRepository<Source>>(),
28-
countriesRepository: context.read<DataRepository<Country>>(),
2929
sourceId: sourceId,
3030
)..add(const EditSourceLoaded()),
3131
child: const _EditSourceView(),
@@ -151,19 +151,6 @@ class _EditSourceViewState extends State<_EditSourceView> {
151151
);
152152
}
153153

154-
// Find the correct Country instance from the list to ensure
155-
// the Dropdown can display the selection correctly.
156-
Country? selectedHeadquarters;
157-
if (state.headquarters != null) {
158-
try {
159-
selectedHeadquarters = state.countries.firstWhere(
160-
(c) => c.id == state.headquarters!.id,
161-
);
162-
} catch (_) {
163-
selectedHeadquarters = null;
164-
}
165-
}
166-
167154
return SingleChildScrollView(
168155
child: Padding(
169156
padding: const EdgeInsets.all(AppSpacing.lg),
@@ -237,24 +224,18 @@ class _EditSourceViewState extends State<_EditSourceView> {
237224
),
238225
),
239226
const SizedBox(height: AppSpacing.lg),
240-
DropdownButtonFormField<Country?>(
241-
value: selectedHeadquarters,
242-
decoration: InputDecoration(
243-
labelText: l10n.headquarters,
244-
border: const OutlineInputBorder(),
245-
),
246-
items: [
247-
DropdownMenuItem(value: null, child: Text(l10n.none)),
248-
...state.countries.map(
249-
(country) => DropdownMenuItem(
250-
value: country,
251-
child: Text(country.name),
252-
),
253-
),
254-
],
255-
onChanged: (value) => context.read<EditSourceBloc>().add(
256-
EditSourceHeadquartersChanged(value),
257-
),
227+
CountryPickerFormField(
228+
labelText: l10n.headquarters,
229+
initialValue: state.headquarters != null
230+
? adaptCoreCountryToPackageCountry(
231+
state.headquarters!,
232+
)
233+
: null,
234+
onChanged: (picker.Country country) {
235+
context.read<EditSourceBloc>().add(
236+
EditSourceHeadquartersChanged(country),
237+
);
238+
},
258239
),
259240
const SizedBox(height: AppSpacing.lg),
260241
DropdownButtonFormField<ContentStatus>(

0 commit comments

Comments
 (0)