Skip to content

Commit dc7c5ca

Browse files
committed
feat(local_ads_management): add handlers for saving and publishing local interstitial ads
- Remove _onSubmitted handler - Add _onSavedAsDraft handler for saving ad as a draft - Add _onPublished handler for publishing ad - Update event names in CreateLocalInterstitialAdBloc class
1 parent 0c4f1a8 commit dc7c5ca

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_interstitial_ad_bloc.dart

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class CreateLocalInterstitialAdBloc
1818
super(const CreateLocalInterstitialAdState()) {
1919
on<CreateLocalInterstitialAdImageUrlChanged>(_onImageUrlChanged);
2020
on<CreateLocalInterstitialAdTargetUrlChanged>(_onTargetUrlChanged);
21-
on<CreateLocalInterstitialAdSubmitted>(_onSubmitted);
21+
on<CreateLocalInterstitialAdSavedAsDraft>(_onSavedAsDraft);
22+
on<CreateLocalInterstitialAdPublished>(_onPublished);
2223
}
2324

2425
final DataRepository<LocalAd> _localAdsRepository;
@@ -38,8 +39,52 @@ class CreateLocalInterstitialAdBloc
3839
emit(state.copyWith(targetUrl: event.targetUrl));
3940
}
4041

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

0 commit comments

Comments
 (0)