createAndJoinGroup method
Creates a new group and immediately subscribes to it.
This is the flow when a user creates a new group from the UI:
- Saves the group to the local database with
isSubscribed = true. - Recalculates the endpoint name with the new group included.
- Validates the byte limit.
- Tells the background service to re-advertise.
Throws GroupSubscriptionLimitException if adding this group would exceed the 131-byte endpoint name limit.
Implementation
Future<void> createAndJoinGroup(String groupName) async {
_log('Creating and joining group: "$groupName"');
// Get current subscriptions
final currentGroupIds = await _getSubscribedGroupIds();
// Validate byte limit BEFORE committing
_validateEndpointLength([...currentGroupIds, groupName]);
// Save the group as discovered + subscribed
await _db.upsertDiscoveredGroup(groupId: groupName, displayName: groupName);
await _db.subscribeToGroup(groupName, exclusive: false);
// Tell the background service to re-advertise with updated groups
final updatedGroupIds = await _getSubscribedGroupIds();
updateAdvertising(
groupIds: updatedGroupIds,
); // Fire and forget to avoid UI lag
}