0

I'm using the getStream plugin to create a simple-ish android chat app using Jetpack Compose.I have some channels created through the dashboard, but they do not display.

From what I was following (https://getstream.io/chat/compose/tutorial/#displaying-the-channels-screen) there isn't even anything mentioned about creating channels.

Here is what I get when I load the app: Screenshot of channelsScreen

Here is my code.

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ForumChannelsScreen(
    navController: NavController,
    userData: UserData?,
    context: Context
) {
    // GetStream related code

    // Set-up for OfflinePlugin for offlineStorage
    val offlinePluginFactory = StreamOfflinePluginFactory(
        config = Config(
            backgroundSyncEnabled = true,
            userPresence = true,
            persistenceEnabled = true,
            uploadAttachmentsNetworkType = UploadAttachmentsNetworkType.NOT_ROAMING,
        ),
        appContext = context,
    )

    // Set-up client for API calls and with the plugin for offline storage
    val client = ChatClient.Builder("secret", context)
        .withPlugin(offlinePluginFactory)
        .logLevel(ChatLogLevel.ALL) // would change to nothing if app were to be in production :)
        .build()

    // Authenticate and connect user
    val user = User(
        id = userData!!.userId,
        name = userData.username.toString(),
        image = userData.profilePictureUrl.toString()
    )
    var token = ""
    token = if (userData.userId == "secret") {
        "secret"
    } else {
        "secret"
    }
    client.connectUser(
        user = user,
        token = token
    ).enqueue()

    // Creating a channel
    val channelClient = client.channel(channelType = "messaging", channelId = "general")

    val extraData = mutableMapOf<String, Any>(
        "name" to "Awesome channel about food"
    )
    channelClient.create(memberIds = emptyList(), extraData = extraData).enqueue() { result ->
        if (result.isSuccess) {
            val channel: Channel = result.data()
        } else {
            Log.d("ChannelError", "Something went wrong creating a channel")
        }
    }


    val selectedIndex = rememberSaveable { mutableStateOf(0)}

    Scaffold(
        modifier = Modifier.background(MaterialTheme.colorScheme.background),
        bottomBar = {
            NavigationBar(
            ) {
                NavigationBarItem(
                    icon = { Icon(
                        painter = painterResource(R.drawable.discover_temp),
                        contentDescription = "Discover"
                    ) },
                    label = { Text(text = "Discover") },
                    onClick = {
                        selectedIndex.value = 0
                        navController.navigate(Screens.DiscoverScreen.route)
                    },
                    selected = (selectedIndex.value == 0),
                    colors = NavigationBarItemDefaults
                        .colors(
                            unselectedIconColor = Orange,
                            indicatorColor = MaterialTheme.colorScheme.surfaceColorAtElevation(LocalAbsoluteTonalElevation.current)
                        )
                )
                NavigationBarItem(
                    icon = { Icon(
                        painter = painterResource(R.drawable.favorite_unfocused),
                        contentDescription = "Favourites"
                    ) },
                    label = { Text(text = "Favourite") },
                    onClick = {
                        selectedIndex.value = 0
                        navController.navigate(Screens.FavouriteScreen.route)
                    },
                    selected = (selectedIndex.value == 0),
                    colors = NavigationBarItemDefaults
                        .colors(
                            unselectedIconColor = Orange,
                            indicatorColor = MaterialTheme.colorScheme.surfaceColorAtElevation(LocalAbsoluteTonalElevation.current)
                        )
                )
                NavigationBarItem(
                    icon = { Icon(
                        painter = painterResource(R.drawable.forum_unfocused),
                        contentDescription = "Forum"
                    ) },
                    label = { Text(text = "Forum") },
                    onClick = {
                        selectedIndex.value = 1
                        navController.navigate(Screens.ForumChannelsScreen.route)
                    },
                    selected = (selectedIndex.value == 1),
                    colors = NavigationBarItemDefaults
                        .colors(
                            unselectedIconColor = Orange,
                            indicatorColor = MaterialTheme.colorScheme.surfaceColorAtElevation(LocalAbsoluteTonalElevation.current) // sets the indicator color to the same as the color of
                            // the navbar by making sure its the same tone. Tones change on elevation,
                            // so I had to set it to the same elevation as the navbar itself
                        )
                )
            }
        }
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(MaterialTheme.colorScheme.background),
            verticalArrangement = Arrangement.SpaceBetween
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(16.dp),
                horizontalArrangement = Arrangement.End
            ) {
                Text(
                    text = "Choose a channel",
                    style = MaterialTheme.typography.titleSmall,
                    modifier = Modifier.weight(1f),
                    textAlign = TextAlign.Center
                )
                if(userData?.profilePictureUrl != null) {
                    AsyncImage(
                        model = userData.profilePictureUrl,
                        contentDescription = "Profile Picture",
                        modifier = Modifier
                            .size(50.dp)
                            .clip(CircleShape)
                            .clickable { navController.navigate(Screens.ProfileScreen.route) },
                        contentScale = ContentScale.Crop,
                    )
                }
            }
            ChatTheme {
                ChannelsScreen(
                    title = "",
                    isShowingSearch = true,
                    onItemClick = { channel ->
                    },
                    onBackPressed = { navController.navigate(Screens.DiscoverScreen.route)}
                )
            }

        }
    }
}
Bartek
  • 3
  • 3

1 Answers1

0

The general way to create channels can be found in the documentation.

However, in the code snippet you shared you also have a portion that will create a channel:

// Creating a channel
val channelClient = client.channel(channelType = "messaging", channelId = "general")

val extraData = mutableMapOf<String, Any>(
        "name" to "Awesome channel about food"
    )
channelClient.create(memberIds = emptyList(), extraData = extraData).enqueue() { result ->
    if (result.isSuccess) {
        val channel: Channel = result.data()
    } else {
        Log.d("ChannelError", "Something went wrong creating a channel")
    }
}

It describes the way to create a channel without members (see the memberIds set to emptyList()) and the name of "Awesome channel about food".

Let me know if that makes things clear or if there are any errors happening for you. Always happy to help! :)

Stefan Blos
  • 172
  • 7