<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vidit Jha on Medium]]></title>
        <description><![CDATA[Stories by Vidit Jha on Medium]]></description>
        <link>https://medium.com/@jhavidit?source=rss-e2db5a99b076------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gfwmzvExcYu5q-r_zhfrtw.jpeg</url>
            <title>Stories by Vidit Jha on Medium</title>
            <link>https://medium.com/@jhavidit?source=rss-e2db5a99b076------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 01:56:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jhavidit/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Implementing Type-Safe Multi-Module Nested Navigation in Bottom Navigation using Jetpack Compose]]></title>
            <link>https://medium.com/@jhavidit/implementing-type-safe-multi-module-nested-navigation-in-bottom-navigation-using-jetpack-compose-890576c0bebf?source=rss-e2db5a99b076------2</link>
            <guid isPermaLink="false">https://medium.com/p/890576c0bebf</guid>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[multi-module-project]]></category>
            <category><![CDATA[bottom-navigation]]></category>
            <category><![CDATA[navigation]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Vidit Jha]]></dc:creator>
            <pubDate>Wed, 02 Oct 2024 17:32:11 GMT</pubDate>
            <atom:updated>2024-10-02T17:32:11.823Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*pWeVIQBYkZJRb8W-dwCzxA.png" /></figure><p>Navigation in Jetpack Compose has been a frustration for many of us developers. Traditionally, we had to define routes and pass arguments as strings, which was highly error-prone and lacked compile-time safety.</p><p>Finally, Android has recently introduced its <strong>type-safe navigation library</strong>, which has made navigation simpler and provides compile-time safety. Now, instead of relying on fragile string-based routes, we can use <strong>Serializable objects</strong> to pass data between screens. While Android still internally uses strings to match routes, the new approach offers significant relief by reducing errors and making navigation more robust and developer-friendly.</p><p>In this blog, we will explore how to implement <strong>bottom navigation</strong> using <strong>type-safe navigation</strong> in Jetpack Compose. We’ll be using a <strong>multi-module architecture</strong>, where each tab in the bottom navigation will have its own module and navigation graph. This approach makes our solution more manageable, scalable, and easier to maintain as the app grows in complexity.</p><p>The <strong>multi-module architecture</strong> we will use includes three primary modules: <strong>app</strong>, <strong>core</strong>, and <strong>feature</strong> modules for each tab. In the example project, we will implement three tabs: <strong>Home</strong>, <strong>Search</strong>, and <strong>Profile</strong>.</p><h3>So let the coding begins🚀</h3><p>First add the required dependencies. Don’t forget to implement these dependencies in app level and project level <em>build.gradle.</em></p><pre>[versions]<br>composeNavigation = &quot;2.8.1&quot;<br>serialization = &quot;1.6.3&quot;<br>kotlinGradlePlugin = &quot;1.9.0&quot;<br><br>[libraries]<br># Jetpack Compose Navigation<br>androidx-compose-navigation = { group = &quot;androidx.navigation&quot;, name = &quot;navigation-compose&quot;, version.ref = &quot;composeNavigation&quot; }<br><br># Kotlin Serialization<br>kotlinx-serialization-json = { module = &quot;org.jetbrains.kotlinx:kotlinx-serialization-json&quot;, version.ref = &quot;serialization&quot; }<br><br>[plugins]<br>kotlin-serialization = { id = &quot;org.jetbrains.kotlin.plugin.serialization&quot;, version.ref = &quot;kotlinGradlePlugin&quot; }</pre><p>Now, in each <strong>feature module</strong>, we will add the <strong>Navigation Graph</strong>, define the <strong>Navigation Objects</strong>, and implement the starting <strong>Composable screen</strong>. This ensures that each tab — whether it’s Home, Search, or Profile — manages its own navigation independently.</p><p>Each NavGraph is an independent navigation graph where we can add destinations and routes. For each screen, we define objects, and if we need to pass data through arguments, we define a corresponding data class. These objects or data classes can be used instead of strings, providing type safety. The composable screens themselves are simple layouts with just the screen name at center.</p><pre>fun NavGraphBuilder.homeNavGraph(navController: NavController) {<br>    navigation&lt;HomeGraph&gt;(startDestination = HomeScreen) {<br>        composable&lt;HomeScreen&gt; { HomeScreen() }<br>    }<br>}</pre><pre>@Serializable<br>object HomeGraph<br><br>@Serializable<br>object HomeScreen</pre><pre><br>@Composable<br>fun HomeScreen() {<br>    Scaffold(<br>        topBar = {},<br>        content = { paddingValues -&gt;<br>            Column(<br>                modifier = Modifier<br>                    .fillMaxSize()<br>                    .padding(paddingValues),<br>                verticalArrangement = Arrangement.Center,<br>                horizontalAlignment = Alignment.CenterHorizontally<br>            ) {<br>                Text(<br>                    text = &quot;Home Screen&quot;,<br>                    fontSize = 40.sp<br>                )<br>            }<br>        }<br>    )<br>}</pre><p>We will repeat this setup for all of our tabs, ensuring each feature module contains its <strong>Navigation Graph</strong>, <strong>Navigation Objects</strong>, and a starting <strong>Composable screen</strong>.</p><p>Once each <strong>feature </strong>module is set up, we move on to the <strong>App Module</strong>, where we create a <strong>sealed class</strong> that holds all the details for our bottom navigation tabs. In the <strong>route</strong> property, instead of using string-based routes like we did earlier, we now leverage <strong>Serializable</strong> objects that are defined in each feature module.</p><pre>@Serializable<br>sealed class BottomScreens&lt;T&gt;(<br>    val name: String, // the name of the tab<br>    val selectedIcon: Int, // filled icon when selected<br>    val unselectedIcon: Int, // unfilled icon when not selected<br>    val route: T //graphs defined in feature module for each tab<br>                <br>) {<br><br>    @Serializable<br>    data object Home : BottomScreens&lt;HomeGraph&gt;(<br>        name = &quot;Home&quot;,<br>        unselectedIcon = R.drawable.home_unfilled,<br>        selectedIcon = R.drawable.home_filled,<br>        route = HomeGraph<br>    )<br><br>    @Serializable<br>    data object Search : BottomScreens&lt;SearchGraph&gt;(<br>        name = &quot;Search&quot;,<br>        unselectedIcon = R.drawable.search_unfilled,<br>        selectedIcon = R.drawable.search_filled,<br>        route = SearchGraph<br>    )<br><br>    @Serializable<br>    data object Profile : BottomScreens&lt;ProfileGraph&gt;(<br>        name = &quot;Profile&quot;,<br>        unselectedIcon = R.drawable.profile_unfilled,<br>        selectedIcon = R.drawable.profile_filled,<br>        route = ProfileGraph<br>    )<br><br>}</pre><p>We are now creating our BottomNavigation composable using Material 3&#39;s bottom navigation items. We&#39;ll determine the current destination and, on tab selection, check if any navigation graph hierarchy matches before navigating to the corresponding nav graph.</p><p>We’ll use popUpTo to remove all destinations from the back stack. The saveState and restoreState options ensure that when switching between tabs, their state is preserved. The launchSingleTop option prevents adding the destination to the back stack again if it&#39;s already at the top.</p><pre>@Composable<br>fun AppBottomNavigation(navController: NavController) {<br>    val bottomScreens = remember {<br>        listOf(<br>            BottomScreens.Home,<br>            BottomScreens.Search,<br>            BottomScreens.Profile,<br>        )<br>    }<br><br>    NavigationBar {<br>        val currentDestination = navController.currentBackStackEntryAsState().value?.destination<br><br>        bottomScreens.forEach { screen -&gt;<br>            val isSelected =<br>                currentDestination?.hierarchy?.any { it.hasRoute(screen.route::class) } == true<br>            NavigationBarItem(<br>                icon = {<br>                    if (isSelected) Icon(<br>                        modifier = Modifier.size(24.dp),<br>                        painter = painterResource(id = screen.selectedIcon),<br>                        contentDescription = screen.name<br>                    )<br>                    else Icon(<br>                        modifier = Modifier.size(24.dp),<br>                        painter = painterResource(id = screen.unselectedIcon),<br>                        contentDescription = screen.name<br>                    )<br>                },<br>                label = { Text(screen.name) },<br>                selected = isSelected,<br>                onClick = {<br>                    navController.navigate(screen.route) {<br>                        popUpTo(navController.graph.findStartDestination().id) {<br>                            saveState = true<br>                        }<br>                        launchSingleTop = true<br>                        restoreState = true<br>                    }<br>                }<br>            )<br>        }<br>    }<br>}</pre><p>Now, we’ll create a central navigation component, which will serve as our main nav graph containing all the individual navigation graphs used in the bottom navigation. We’ll integrate this into the composable screen where we want to add the bottom navigation, along with a composable bottom sheet we created.</p><pre>@Composable<br>fun CentralNavigation(navController: NavHostController) {<br>    NavHost(navController = navController, startDestination = BottomScreens.Home.route){<br>        homeNavGraph(navController)<br>        searchNavGraph(navController)<br>        profileNavGraph(navController)<br>    }<br><br>}</pre><pre>@Composable<br>fun MainScreen() {<br>    Surface(modifier = Modifier.fillMaxSize()) {<br>        val navController = rememberNavController()<br>        CentralNavigation(navController = navController)<br>        Box(<br>            modifier = Modifier<br>                .fillMaxSize()<br>                .systemBarsPadding(),<br>            contentAlignment = Alignment.BottomCenter<br>        ) {<br>            AppBottomNavigation(navController = navController)<br>        }<br>    }<br>}</pre><p>And that’s it — your Bottom Navigation is ready! You can also check out the source code on my <a href="https://github.com/jhavidit/compose-multi-module-clean-architecture">GitHub repository</a>. If you have any questions, feel free to leave a comment below or connect with me on <a href="https://www.linkedin.com/in/jhavidit/">LinkedIn </a>or <a href="https://x.com/viditjha28">Twitter</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=890576c0bebf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Integrating Sockets in Kotlin]]></title>
            <link>https://medium.com/dsckiet/integrating-sockets-in-kotlin-d5666a4bc5ab?source=rss-e2db5a99b076------2</link>
            <guid isPermaLink="false">https://medium.com/p/d5666a4bc5ab</guid>
            <category><![CDATA[chatting-application]]></category>
            <category><![CDATA[socketio]]></category>
            <category><![CDATA[sockets]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Vidit Jha]]></dc:creator>
            <pubDate>Fri, 16 Oct 2020 06:47:45 GMT</pubDate>
            <atom:updated>2020-10-16T07:18:06.787Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U-UrmRmKU0yb5l_jOpuvEA.png" /><figcaption>A blog by Vidit Jha</figcaption></figure><p>Ever wondered how popular apps like Cricbuzz, Whatsapp, Trello, Google Docs work, or thought of building an app that gets automatic live data like a chatting app, a multiplayer game, a live quiz, etc? Then Socket.IO is the answer for you. In this article, I will illustrate how you can implement Socket.IO in your android app.</p><blockquote>Socket.IO is a library that enables real-time, bidirectional, and event-based communication between the client and the server.</blockquote><p>Socket.IO is built on top of the WebSockets API (Client-side) and NodeJs. But it is not an implementation of web sockets. It is used only for transport. Here both servers and clients can emit and listen to their calls. The server can also trigger the event on a specific route which is listened by their subscribers, which means the client has put their listener for particular events.</p><blockquote>Kotlin doesn’t have its official documentation for Socket.IO so we will be studying its operation from Java documentation and will translate it to Kotlin as both languages are interoperable.</blockquote><p>There are two methods by which you can implement Socket.IO in your native android app.</p><ol><li><a href="https://github.com/socketio/socket.io-client-java"><strong>Socket.IO Android-Java</strong></a></li><li><a href="https://socket.io/blog/native-socket-io-and-android/"><strong>Naoyuki Kanezawa Android Socket.IO</strong></a></li></ol><blockquote>The library provided by Naoyuki Kanezawa has also published its <a href="https://socket.io/blog/native-socket-io-and-android/">blog</a> for Socket.IO using java in android. I would recommend you to use this library as it is easy to implement, provides socket (connect, disconnect, reconnect) detection, auto-reconnect feature, and is also promoted in official Socket.IO documentation</blockquote><p>Now let’s move to the coding part and in the next few steps, you can set up your socket.io connection. ✌️</p><h3>Startup‍🚀</h3><p>• First and foremost add the dependency to app-level build.gradle file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50ca987ea595ada20e3f1e1adcbf1393/href">https://medium.com/media/50ca987ea595ada20e3f1e1adcbf1393/href</a></iframe><p>• Don’t forget to add internet permission in the manifest file. 😅</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/749331eeb02ecdffed7a9049adbe1b18/href">https://medium.com/media/749331eeb02ecdffed7a9049adbe1b18/href</a></iframe><h3>Socket Initialization😎</h3><p>• Create a Kotlin class for socket initialization which can be used anywhere in the project. Here <strong><em>IO.socket()</em></strong> method returns a socket for <a href="https://yoursocketurl.com/"><strong><em>https://yourSocketURL.com</em></strong></a><strong> </strong>with the default options. Notice that the method <strong><em>mSocket</em></strong> caches the result, so you can always get the same <strong><em>Socket</em></strong> instance for an URL from any Activity or Fragment.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/08d3de6f10cd28ecb10417c23fb06cfe/href">https://medium.com/media/08d3de6f10cd28ecb10417c23fb06cfe/href</a></iframe><p>• Don’t forget to add your socket initialization application class name in the manifest file. 😅</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c6f27e5f469686a8dc31d53236ea6da/href">https://medium.com/media/4c6f27e5f469686a8dc31d53236ea6da/href</a></iframe><h3>Socket Connection to the Server😃</h3><p>• Now we will create the object of the Socket Instance class which we created earlier and connect the socket using <strong><em>.connect()</em></strong> method. We will use <strong><em>IO.options()</em></strong> method so that socket will be reconnected by itself if ever It gets disconnected due to some network problem.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca650780c5774bca35c4acee0b9f3bb4/href">https://medium.com/media/ca650780c5774bca35c4acee0b9f3bb4/href</a></iframe><p>• To check if the socket is connected or not we can use <strong><em>.connected()</em></strong> method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c241ac7cc6d0350c01db89d1fd6ea25/href">https://medium.com/media/5c241ac7cc6d0350c01db89d1fd6ea25/href</a></iframe><h3>Emit and Listen Events😊</h3><p>• For receiving instant real-time data we need to use <strong><em>on()</em></strong> method. We can get data in a JSON data block or any other data form. To show anything on the screen all the UI bind logic should be coded on the UI Thread part as the socket works on the background thread.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3337b31614a4b73cc85df4e7f4d41492/href">https://medium.com/media/3337b31614a4b73cc85df4e7f4d41492/href</a></iframe><p>• Checking when the socket is disconnected or reconnected.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/441f677a6a538815153bbac9474ae513/href">https://medium.com/media/441f677a6a538815153bbac9474ae513/href</a></iframe><p>• For sending data to the server in the form of JSON data block or any other data form we have to use the<strong><em> emit()</em></strong> method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6dd8894920a39e14908e7f734e6b3573/href">https://medium.com/media/6dd8894920a39e14908e7f734e6b3573/href</a></iframe><blockquote>If you are going to receive data based on what you emit to the server then first you should start listening to the event using the <strong>on()</strong> method and then <strong>emit</strong> your data.</blockquote><p>• And last but not the least, don’t forget to close the socket connection using <strong><em>.disconnect()</em></strong> method and remove the listener using <strong><em>.off()</em></strong> method when you are done.😇</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a01e94a17df5a5b679576889cbde699a/href">https://medium.com/media/a01e94a17df5a5b679576889cbde699a/href</a></iframe><p>Let me know in the comments if you have any issues and topics you would be interested in learning more about. You can also reach out to me on <a href="https://twitter.com/ViditJha28">Twitter</a> and <a href="https://www.instagram.com/viditjha/">Instagram</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d5666a4bc5ab" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dsckiet/integrating-sockets-in-kotlin-d5666a4bc5ab">Integrating Sockets in Kotlin</a> was originally published in <a href="https://medium.com/dsckiet">DSC KIET</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>