Skip to content

Commit 3abdd73

Browse files
committed
feat(local_ads_management): add handlers for saving and publishing local video ads
- Replace CreateLocalVideoAdSubmitted event with CreateLocalVideoAdSavedAsDraft and CreateLocalVideoAdPublished events - Implement _onSavedAsDraft method to handle saving local video ads as drafts - Implement _onPublished method to handle publishing local video ads - Update event handlers to set appropriate statuses and handle exceptions
1 parent da20e18 commit 3abdd73

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

lib/local_ads_management/bloc/create_local_ads/create_local_video_ad_bloc.dart

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class CreateLocalVideoAdBloc
1717
super(const CreateLocalVideoAdState()) {
1818
on<CreateLocalVideoAdVideoUrlChanged>(_onVideoUrlChanged);
1919
on<CreateLocalVideoAdTargetUrlChanged>(_onTargetUrlChanged);
20-
on<CreateLocalVideoAdSubmitted>(_onSubmitted);
20+
on<CreateLocalVideoAdSavedAsDraft>(_onSavedAsDraft);
21+
on<CreateLocalVideoAdPublished>(_onPublished);
2122
}
2223

2324
final DataRepository<LocalAd> _localAdsRepository;
@@ -37,8 +38,52 @@ class CreateLocalVideoAdBloc
3738
emit(state.copyWith(targetUrl: event.targetUrl));
3839
}
3940

40-
Future<void> _onSubmitted(
41-
CreateLocalVideoAdSubmitted event,
41+
/// Handles saving the local video ad as a draft.
42+
Future<void> _onSavedAsDraft(
43+
CreateLocalVideoAdSavedAsDraft event,
44+
Emitter<CreateLocalVideoAdState> emit,
45+
) async {
46+
if (!state.isFormValid) return;
47+
48+
emit(state.copyWith(status: CreateLocalVideoAdStatus.submitting));
49+
try {
50+
final now = DateTime.now();
51+
final newLocalVideoAd = LocalVideoAd(
52+
id: _uuid.v4(),
53+
videoUrl: state.videoUrl,
54+
targetUrl: state.targetUrl,
55+
createdAt: now,
56+
updatedAt: now,
57+
status: ContentStatus.draft,
58+
);
59+
60+
await _localAdsRepository.create(item: newLocalVideoAd);
61+
emit(
62+
state.copyWith(
63+
status: CreateLocalVideoAdStatus.success,
64+
createdLocalVideoAd: newLocalVideoAd,
65+
),
66+
);
67+
} on HttpException catch (e) {
68+
emit(
69+
state.copyWith(
70+
status: CreateLocalVideoAdStatus.failure,
71+
exception: e,
72+
),
73+
);
74+
} catch (e) {
75+
emit(
76+
state.copyWith(
77+
status: CreateLocalVideoAdStatus.failure,
78+
exception: UnknownException('An unexpected error occurred: $e'),
79+
),
80+
);
81+
}
82+
}
83+
84+
/// Handles publishing the local video ad.
85+
Future<void> _onPublished(
86+
CreateLocalVideoAdPublished event,
4287
Emitter<CreateLocalVideoAdState> emit,
4388
) async {
4489
if (!state.isFormValid) return;

0 commit comments

Comments
 (0)