goToChat function

void goToChat(
  1. dynamic localContext,
  2. String roomId
)

helper to figure out how to route to the specific chat room

Implementation

void goToChat(BuildContext localContext, String roomId) {
  final context = rootNavKey.currentContext!;
  final currentUri = context.read(currentRoutingLocation);
  if (!currentUri.startsWith(chatRoomUriMatcher)) {
    // we are not in a chat room. just a regular push routing
    // will do
    navigateOnRightBranch(
      localContext,
      ShellBranch.chatsShell,
      (navContext) => navContext
          .pushNamed(Routes.chatroom.name, pathParameters: {'roomId': roomId}),
    );
    return;
  }

  // we are in a chat page
  if (roomId == rootNavKey.currentContext!.read(selectedChatIdProvider)) {
    // we are on the same page, nothing to be done
    return;
  }

  // we are on a different chat page. Push replace the current screen
  context.pushReplacementNamed(
    Routes.chatroom.name,
    pathParameters: {'roomId': roomId},
  );
}