Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/content_management/bloc/create_source/create_source_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
on<CreateSourceNameChanged>(_onNameChanged);
on<CreateSourceDescriptionChanged>(_onDescriptionChanged);
on<CreateSourceUrlChanged>(_onUrlChanged);
on<CreateSourceLogoUrlChanged>(_onLogoUrlChanged);
on<CreateSourceTypeChanged>(_onSourceTypeChanged);
on<CreateSourceLanguageChanged>(_onLanguageChanged);
on<CreateSourceHeadquartersChanged>(_onHeadquartersChanged);
Expand Down Expand Up @@ -49,6 +50,14 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
emit(state.copyWith(url: event.url));
}

void _onLogoUrlChanged(
CreateSourceLogoUrlChanged event,
Emitter<CreateSourceState> emit,
) {
// Update state when the logo URL input changes.
emit(state.copyWith(logoUrl: event.logoUrl));
}

void _onSourceTypeChanged(
CreateSourceTypeChanged event,
Emitter<CreateSourceState> emit,
Expand All @@ -75,12 +84,14 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
CreateSourceSavedAsDraft event,
Emitter<CreateSourceState> emit,
) async {
// Log: Attempting to save source as draft with state: state
emit(state.copyWith(status: CreateSourceStatus.submitting));
try {
final now = DateTime.now();
final newSource = Source(
id: _uuid.v4(),
name: state.name,
logoUrl: state.logoUrl,
description: state.description,
url: state.url,
sourceType: state.sourceType!,
Expand All @@ -92,16 +103,19 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
);

await _sourcesRepository.create(item: newSource);
// Log: Successfully saved source as draft with id: newSource.id
emit(
state.copyWith(
status: CreateSourceStatus.success,
createdSource: newSource,
),
);
} on HttpException catch (e) {
// Log: Failed to save source as draft. Error: e
emit(state.copyWith(status: CreateSourceStatus.failure, exception: e));
} catch (e) {
emit(
// Log: An unexpected error occurred while saving source as draft. Error: e
state.copyWith(
status: CreateSourceStatus.failure,
exception: UnknownException('An unexpected error occurred: $e'),
Expand All @@ -115,12 +129,14 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
CreateSourcePublished event,
Emitter<CreateSourceState> emit,
) async {
// Log: Attempting to publish source with state: state
emit(state.copyWith(status: CreateSourceStatus.submitting));
try {
final now = DateTime.now();
final newSource = Source(
id: _uuid.v4(),
name: state.name,
logoUrl: state.logoUrl,
description: state.description,
url: state.url,
sourceType: state.sourceType!,
Expand All @@ -132,16 +148,19 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
);

await _sourcesRepository.create(item: newSource);
// Log: Successfully published source with id: newSource.id
emit(
state.copyWith(
status: CreateSourceStatus.success,
createdSource: newSource,
),
);
} on HttpException catch (e) {
// Log: Failed to publish source. Error: e
emit(state.copyWith(status: CreateSourceStatus.failure, exception: e));
} catch (e) {
emit(
// Log: An unexpected error occurred while publishing source. Error: e
state.copyWith(
status: CreateSourceStatus.failure,
exception: UnknownException('An unexpected error occurred: $e'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ final class CreateSourceUrlChanged extends CreateSourceEvent {
List<Object?> get props => [url];
}

/// Event for when the source's logo URL is changed.
final class CreateSourceLogoUrlChanged extends CreateSourceEvent {
const CreateSourceLogoUrlChanged(this.logoUrl);
final String logoUrl;
@override
List<Object?> get props => [logoUrl];
}

/// Event for when the source's type is changed.
final class CreateSourceTypeChanged extends CreateSourceEvent {
const CreateSourceTypeChanged(this.sourceType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ final class CreateSourceState extends Equatable {
this.name = '',
this.description = '',
this.url = '',
this.logoUrl = '',
this.sourceType,
this.language,
this.headquarters,
Expand All @@ -37,6 +38,7 @@ final class CreateSourceState extends Equatable {
final String name;
final String description;
final String url;
final String logoUrl;
final SourceType? sourceType;
final Language? language;
final Country? headquarters;
Expand All @@ -48,6 +50,7 @@ final class CreateSourceState extends Equatable {
name.isNotEmpty &&
description.isNotEmpty &&
url.isNotEmpty &&
logoUrl.isNotEmpty &&
sourceType != null &&
language != null &&
headquarters != null;
Expand All @@ -57,6 +60,7 @@ final class CreateSourceState extends Equatable {
String? name,
String? description,
String? url,
String? logoUrl,
ValueGetter<SourceType?>? sourceType,
ValueGetter<Language?>? language,
ValueGetter<Country?>? headquarters,
Expand All @@ -68,6 +72,7 @@ final class CreateSourceState extends Equatable {
name: name ?? this.name,
description: description ?? this.description,
url: url ?? this.url,
logoUrl: logoUrl ?? this.logoUrl,
sourceType: sourceType != null ? sourceType() : this.sourceType,
language: language != null ? language() : this.language,
headquarters: headquarters != null ? headquarters() : this.headquarters,
Expand All @@ -82,6 +87,7 @@ final class CreateSourceState extends Equatable {
name,
description,
url,
logoUrl,
sourceType,
language,
headquarters,
Expand Down
22 changes: 22 additions & 0 deletions lib/content_management/bloc/edit_source/edit_source_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
on<EditSourceNameChanged>(_onNameChanged);
on<EditSourceDescriptionChanged>(_onDescriptionChanged);
on<EditSourceUrlChanged>(_onUrlChanged);
on<EditSourceLogoUrlChanged>(_onLogoUrlChanged);
on<EditSourceTypeChanged>(_onSourceTypeChanged);
on<EditSourceLanguageChanged>(_onLanguageChanged);
on<EditSourceHeadquartersChanged>(_onHeadquartersChanged);
Expand All @@ -44,6 +45,7 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
name: source.name,
description: source.description,
url: source.url,
logoUrl: source.logoUrl,
sourceType: () => source.sourceType,
language: () => source.language,
headquarters: () => source.headquarters,
Expand Down Expand Up @@ -89,6 +91,16 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
emit(state.copyWith(url: event.url, status: EditSourceStatus.initial));
}

void _onLogoUrlChanged(
EditSourceLogoUrlChanged event,
Emitter<EditSourceState> emit,
) {
// Update state when the logo URL input changes.
emit(
state.copyWith(logoUrl: event.logoUrl, status: EditSourceStatus.initial),
);
}

void _onSourceTypeChanged(
EditSourceTypeChanged event,
Emitter<EditSourceState> emit,
Expand Down Expand Up @@ -130,11 +142,13 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
EditSourceSavedAsDraft event,
Emitter<EditSourceState> emit,
) async {
// Log: Attempting to save source as draft with state: state
emit(state.copyWith(status: EditSourceStatus.submitting));
try {
final originalSource = await _sourcesRepository.read(id: state.sourceId);
final updatedSource = originalSource.copyWith(
name: state.name,
logoUrl: state.logoUrl,
description: state.description,
url: state.url,
sourceType: state.sourceType,
Expand All @@ -148,16 +162,19 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
id: state.sourceId,
item: updatedSource,
);
// Log: Successfully saved source as draft with id: state.sourceId
emit(
state.copyWith(
status: EditSourceStatus.success,
updatedSource: updatedSource,
),
);
} on HttpException catch (e) {
// Log: Failed to save source as draft. Error: e
emit(state.copyWith(status: EditSourceStatus.failure, exception: e));
} catch (e) {
emit(
// Log: An unexpected error occurred while saving source as draft. Error: e
state.copyWith(
status: EditSourceStatus.failure,
exception: UnknownException('An unexpected error occurred: $e'),
Expand All @@ -171,11 +188,13 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
EditSourcePublished event,
Emitter<EditSourceState> emit,
) async {
// Log: Attempting to publish source with state: state
emit(state.copyWith(status: EditSourceStatus.submitting));
try {
final originalSource = await _sourcesRepository.read(id: state.sourceId);
final updatedSource = originalSource.copyWith(
name: state.name,
logoUrl: state.logoUrl,
description: state.description,
url: state.url,
sourceType: state.sourceType,
Expand All @@ -189,16 +208,19 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
id: state.sourceId,
item: updatedSource,
);
// Log: Successfully published source with id: state.sourceId
emit(
state.copyWith(
status: EditSourceStatus.success,
updatedSource: updatedSource,
),
);
} on HttpException catch (e) {
// Log: Failed to publish source. Error: e
emit(state.copyWith(status: EditSourceStatus.failure, exception: e));
} catch (e) {
emit(
// Log: An unexpected error occurred while publishing source. Error: e
state.copyWith(
status: EditSourceStatus.failure,
exception: UnknownException('An unexpected error occurred: $e'),
Expand Down
10 changes: 10 additions & 0 deletions lib/content_management/bloc/edit_source/edit_source_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ final class EditSourceUrlChanged extends EditSourceEvent {
List<Object?> get props => [url];
}

/// Event triggered when the source logo URL input changes.
final class EditSourceLogoUrlChanged extends EditSourceEvent {
const EditSourceLogoUrlChanged(this.logoUrl);

final String logoUrl;

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

/// Event triggered when the source type input changes.
final class EditSourceTypeChanged extends EditSourceEvent {
const EditSourceTypeChanged(this.sourceType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ final class EditSourceState extends Equatable {
this.name = '',
this.description = '',
this.url = '',
this.logoUrl = '',
this.sourceType,
this.language,
this.headquarters,
Expand All @@ -38,6 +39,7 @@ final class EditSourceState extends Equatable {
final String name;
final String description;
final String url;
final String logoUrl;
final SourceType? sourceType;
final Language? language;
final Country? headquarters;
Expand All @@ -50,6 +52,7 @@ final class EditSourceState extends Equatable {
name.isNotEmpty &&
description.isNotEmpty &&
url.isNotEmpty &&
logoUrl.isNotEmpty &&
sourceType != null &&
language != null &&
headquarters != null;
Expand All @@ -60,6 +63,7 @@ final class EditSourceState extends Equatable {
String? name,
String? description,
String? url,
String? logoUrl,
ValueGetter<SourceType?>? sourceType,
ValueGetter<Language?>? language,
ValueGetter<Country?>? headquarters,
Expand All @@ -72,6 +76,7 @@ final class EditSourceState extends Equatable {
name: name ?? this.name,
description: description ?? this.description,
url: url ?? this.url,
logoUrl: logoUrl ?? this.logoUrl,
sourceType: sourceType != null ? sourceType() : this.sourceType,
language: language != null ? language() : this.language,
headquarters: headquarters != null ? headquarters() : this.headquarters,
Expand All @@ -87,6 +92,7 @@ final class EditSourceState extends Equatable {
name,
description,
url,
logoUrl,
sourceType,
language,
headquarters,
Expand Down
14 changes: 14 additions & 0 deletions lib/content_management/view/create_source_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class _CreateSourceViewState extends State<_CreateSourceView> {
late final TextEditingController _nameController;
late final TextEditingController _descriptionController;
late final TextEditingController _urlController;
late final TextEditingController _logoUrlController;

@override
void initState() {
Expand All @@ -47,13 +48,15 @@ class _CreateSourceViewState extends State<_CreateSourceView> {
_nameController = TextEditingController(text: state.name);
_descriptionController = TextEditingController(text: state.description);
_urlController = TextEditingController(text: state.url);
_logoUrlController = TextEditingController(text: state.logoUrl);
}

@override
void dispose() {
_nameController.dispose();
_descriptionController.dispose();
_urlController.dispose();
_logoUrlController.dispose();
super.dispose();
}

Expand Down Expand Up @@ -211,6 +214,17 @@ class _CreateSourceViewState extends State<_CreateSourceView> {
.add(CreateSourceUrlChanged(value)),
),
const SizedBox(height: AppSpacing.lg),
TextFormField(
controller: _logoUrlController,
decoration: InputDecoration(
labelText: l10n.logoUrl,
border: const OutlineInputBorder(),
),
onChanged: (value) => context
.read<CreateSourceBloc>()
.add(CreateSourceLogoUrlChanged(value)),
),
const SizedBox(height: AppSpacing.lg),
SearchableSelectionInput<Language>(
label: l10n.language,
selectedItems: state.language != null
Expand Down
Loading
Loading