Skip to content

Commit b7b6b61

Browse files
committed
refactor(content): use country_picker for create source
1 parent 18ab4a3 commit b7b6b61

File tree

4 files changed

+27
-54
lines changed

4 files changed

+27
-54
lines changed

lib/content_management/bloc/create_source/create_source_bloc.dart

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
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';
7+
import 'package:flutter_news_app_web_dashboard_full_source_code/shared/shared.dart';
68
import 'package:uuid/uuid.dart';
79

810
part 'create_source_event.dart';
@@ -13,9 +15,7 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
1315
/// {@macro create_source_bloc}
1416
CreateSourceBloc({
1517
required DataRepository<Source> sourcesRepository,
16-
required DataRepository<Country> countriesRepository,
1718
}) : _sourcesRepository = sourcesRepository,
18-
_countriesRepository = countriesRepository,
1919
super(const CreateSourceState()) {
2020
on<CreateSourceDataLoaded>(_onDataLoaded);
2121
on<CreateSourceNameChanged>(_onNameChanged);
@@ -29,36 +29,15 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
2929
}
3030

3131
final DataRepository<Source> _sourcesRepository;
32-
final DataRepository<Country> _countriesRepository;
3332
final _uuid = const Uuid();
3433

3534
Future<void> _onDataLoaded(
3635
CreateSourceDataLoaded event,
3736
Emitter<CreateSourceState> emit,
3837
) async {
39-
emit(state.copyWith(status: CreateSourceStatus.loading));
40-
try {
41-
final countriesResponse = await _countriesRepository.readAll(
42-
sort: [const SortOption('updatedAt', SortOrder.desc)],
43-
);
44-
final countries = countriesResponse.items;
45-
46-
emit(
47-
state.copyWith(
48-
status: CreateSourceStatus.initial,
49-
countries: countries,
50-
),
51-
);
52-
} on HttpException catch (e) {
53-
emit(state.copyWith(status: CreateSourceStatus.failure, exception: e));
54-
} catch (e) {
55-
emit(
56-
state.copyWith(
57-
status: CreateSourceStatus.failure,
58-
exception: UnknownException('An unexpected error occurred: $e'),
59-
),
60-
);
61-
}
38+
// This event is now a no-op since we don't need to load countries.
39+
// We just ensure the BLoC is in the initial state.
40+
emit(state.copyWith(status: CreateSourceStatus.initial));
6241
}
6342

6443
void _onNameChanged(
@@ -100,7 +79,13 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
10079
CreateSourceHeadquartersChanged event,
10180
Emitter<CreateSourceState> emit,
10281
) {
103-
emit(state.copyWith(headquarters: () => event.headquarters));
82+
final packageCountry = event.headquarters;
83+
if (packageCountry == null) {
84+
emit(state.copyWith(headquarters: () => null));
85+
} else {
86+
final coreCountry = adaptPackageCountryToCoreCountry(packageCountry);
87+
emit(state.copyWith(headquarters: () => coreCountry));
88+
}
10489
}
10590

10691
void _onStatusChanged(

lib/content_management/bloc/create_source/create_source_event.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ final class CreateSourceLanguageChanged extends CreateSourceEvent {
5656
/// Event for when the source's headquarters is changed.
5757
final class CreateSourceHeadquartersChanged extends CreateSourceEvent {
5858
const CreateSourceHeadquartersChanged(this.headquarters);
59-
final Country? headquarters;
59+
final picker.Country? headquarters;
6060
@override
6161
List<Object?> get props => [headquarters];
6262
}

lib/content_management/bloc/create_source/create_source_state.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ final class CreateSourceState 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.createdSource,
@@ -42,7 +41,6 @@ final class CreateSourceState extends Equatable {
4241
final SourceType? sourceType;
4342
final String language;
4443
final Country? headquarters;
45-
final List<Country> countries;
4644
final ContentStatus contentStatus;
4745
final HttpException? exception;
4846
final Source? createdSource;
@@ -64,7 +62,6 @@ final class CreateSourceState extends Equatable {
6462
ValueGetter<SourceType?>? sourceType,
6563
String? language,
6664
ValueGetter<Country?>? headquarters,
67-
List<Country>? countries,
6865
ContentStatus? contentStatus,
6966
HttpException? exception,
7067
Source? createdSource,
@@ -77,7 +74,6 @@ final class CreateSourceState extends Equatable {
7774
sourceType: sourceType != null ? sourceType() : this.sourceType,
7875
language: language ?? this.language,
7976
headquarters: headquarters != null ? headquarters() : this.headquarters,
80-
countries: countries ?? this.countries,
8177
contentStatus: contentStatus ?? this.contentStatus,
8278
exception: exception,
8379
createdSource: createdSource ?? this.createdSource,
@@ -93,7 +89,6 @@ final class CreateSourceState extends Equatable {
9389
sourceType,
9490
language,
9591
headquarters,
96-
countries,
9792
contentStatus,
9893
exception,
9994
createdSource,

lib/content_management/view/create_source_page.dart

Lines changed: 14 additions & 21 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';
@@ -23,7 +24,6 @@ class CreateSourcePage extends StatelessWidget {
2324
return BlocProvider(
2425
create: (context) => CreateSourceBloc(
2526
sourcesRepository: context.read<DataRepository<Source>>(),
26-
countriesRepository: context.read<DataRepository<Country>>(),
2727
)..add(const CreateSourceDataLoaded()),
2828
child: const _CreateSourceView(),
2929
);
@@ -109,8 +109,7 @@ class _CreateSourceViewState extends State<_CreateSourceView> {
109109
);
110110
}
111111

112-
if (state.status == CreateSourceStatus.failure &&
113-
state.countries.isEmpty) {
112+
if (state.status == CreateSourceStatus.failure) {
114113
return FailureStateWidget(
115114
exception: state.exception!,
116115
onRetry: () => context.read<CreateSourceBloc>().add(
@@ -192,24 +191,18 @@ class _CreateSourceViewState extends State<_CreateSourceView> {
192191
.add(CreateSourceTypeChanged(value)),
193192
),
194193
const SizedBox(height: AppSpacing.lg),
195-
DropdownButtonFormField<Country?>(
196-
value: state.headquarters,
197-
decoration: InputDecoration(
198-
labelText: l10n.headquarters,
199-
border: const OutlineInputBorder(),
200-
),
201-
items: [
202-
DropdownMenuItem(value: null, child: Text(l10n.none)),
203-
...state.countries.map(
204-
(country) => DropdownMenuItem(
205-
value: country,
206-
child: Text(country.name),
207-
),
208-
),
209-
],
210-
onChanged: (value) => context
211-
.read<CreateSourceBloc>()
212-
.add(CreateSourceHeadquartersChanged(value)),
194+
CountryPickerFormField(
195+
labelText: l10n.headquarters,
196+
initialValue: state.headquarters != null
197+
? adaptCoreCountryToPackageCountry(
198+
state.headquarters!,
199+
)
200+
: null,
201+
onChanged: (picker.Country country) {
202+
context.read<CreateSourceBloc>().add(
203+
CreateSourceHeadquartersChanged(country),
204+
);
205+
},
213206
),
214207
const SizedBox(height: AppSpacing.lg),
215208
DropdownButtonFormField<ContentStatus>(

0 commit comments

Comments
 (0)