createAndJoinGroup method

Future<void> createAndJoinGroup(
  1. String groupName
)

Creates a new group and immediately subscribes to it.

This is the flow when a user creates a new group from the UI:

  1. Saves the group to the local database with isSubscribed = true.
  2. Recalculates the endpoint name with the new group included.
  3. Validates the byte limit.
  4. 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
}