Skip to content

Commit a484da3

Browse files
committed
refactor(app): extract device registration logic to private method
- Extracts device registration logic to a new private method _registerDeviceForPushNotifications. - Reduces code duplication in AppBloc class. - Improves maintainability and testability of the device registration process. - Updates all relevant method calls to use the new private method.
1 parent 92cbca5 commit a484da3

File tree

1 file changed

+30
-44
lines changed

1 file changed

+30
-44
lines changed

lib/app/bloc/app_bloc.dart

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -121,24 +121,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
121121
// If a user is already logged in when the app starts, register their
122122
// device for push notifications.
123123
if (state.user != null) {
124-
_logger.info(
125-
'[AppBloc] App started with user ${state.user!.id}. '
126-
'Attempting to register device for push notifications.',
127-
);
128-
// The PushNotificationService will handle getting the token and
129-
// calling the repository's create method. This call is now wrapped in a
130-
// try-catch to prevent unhandled exceptions from crashing the app if
131-
// registration fails.
132-
//
133-
// The `registerDevice` method now implements a "delete-then-create"
134-
// pattern to ensure idempotency and align with backend expectations.
135-
// It uses a composite ID of userId and provider name.
136-
try {
137-
await _pushNotificationService.registerDevice(userId: state.user!.id);
138-
add(const AppPushNotificationDeviceRegistered());
139-
} catch (e, s) {
140-
_logger.severe('[AppBloc] Failed to register device on start.', e, s);
141-
}
124+
await _registerDeviceForPushNotifications(state.user!.id);
142125
}
143126
}
144127

@@ -252,31 +235,8 @@ class AppBloc extends Bloc<AppEvent, AppState> {
252235

253236
// After any successful user transition (including guest), attempt to
254237
// register their device for push notifications. This ensures that
255-
// guests can receive notifications for their saved filters.
256-
_logger.info(
257-
'[AppBloc] User ${user.id} detected. Attempting to register device '
258-
'for push notifications.',
259-
);
260-
try {
261-
// The PushNotificationService will handle getting the token and calling
262-
// the repository's create method. This call is now wrapped in a
263-
// try-catch to prevent unhandled exceptions from crashing the app if
264-
// registration fails.
265-
//
266-
// The `registerDevice` method now implements a "delete-then-create"
267-
// pattern to ensure idempotency and align with backend expectations.
268-
// It uses a composite ID of userId and provider name.
269-
await _pushNotificationService.registerDevice(userId: user.id);
270-
add(const AppPushNotificationDeviceRegistered());
271-
} catch (e, s) {
272-
// Log the error but allow the app to continue functioning.
273-
// The user can still use the app even if push registration fails.
274-
_logger.severe(
275-
'[AppBloc] Failed to register push notification device for user ${user.id}.',
276-
e,
277-
s,
278-
);
279-
}
238+
// guests can also receive notifications.
239+
await _registerDeviceForPushNotifications(user.id);
280240
// If the transition fails (e.g., due to a network error while
281241
// fetching user data), emit a critical error state.
282242
case InitializationFailure(:final status, :final error):
@@ -682,6 +642,32 @@ class AppBloc extends Bloc<AppEvent, AppState> {
682642
_logger.info(
683643
'[AppBloc] Push notification token refreshed. Re-registering device.',
684644
);
685-
await _pushNotificationService.registerDevice(userId: state.user!.id);
645+
await _registerDeviceForPushNotifications(state.user!.id);
646+
}
647+
648+
/// A private helper method to encapsulate the logic for registering a
649+
/// device for push notifications.
650+
///
651+
/// This method is called from multiple places (`_onAppStarted`,
652+
/// `_onAppUserChanged`, `_onAppPushNotificationTokenRefreshed`) to avoid
653+
/// code duplication. It includes robust error handling to prevent unhandled
654+
/// exceptions from crashing the BLoC.
655+
Future<void> _registerDeviceForPushNotifications(String userId) async {
656+
_logger.info(
657+
'[AppBloc] Attempting to register device for push notifications for user $userId.',
658+
);
659+
try {
660+
// The PushNotificationService handles getting the token and calling the
661+
// repository's create method. The `registerDevice` method implements a
662+
// "delete-then-create" pattern for idempotency.
663+
await _pushNotificationService.registerDevice(userId: userId);
664+
add(const AppPushNotificationDeviceRegistered());
665+
} catch (e, s) {
666+
_logger.severe(
667+
'[AppBloc] Failed to register push notification device for user $userId.',
668+
e,
669+
s,
670+
);
671+
}
686672
}
687673
}

0 commit comments

Comments
 (0)