{"id":20881453,"url":"https://github.com/js-bhavyansh/network_connectivity","last_synced_at":"2026-05-26T09:31:05.451Z","repository":{"id":251019540,"uuid":"836120350","full_name":"js-bhavyansh/Network_Connectivity","owner":"js-bhavyansh","description":"A simple Jetpack Compose app to monitor and display network connectivity status.","archived":false,"fork":false,"pushed_at":"2024-07-31T12:19:33.000Z","size":5543,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-26T20:57:39.143Z","etag":null,"topics":["android-studio","jetpack-compose","kotlin","network-connectivity","network-status"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/js-bhavyansh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-31T07:34:52.000Z","updated_at":"2024-08-01T13:15:19.000Z","dependencies_parsed_at":null,"dependency_job_id":"e8ae7104-c7f0-419b-9c23-3dd682c7a81d","html_url":"https://github.com/js-bhavyansh/Network_Connectivity","commit_stats":null,"previous_names":["bhavyansh03-tech/network_connectivity","js-bhavyansh/network_connectivity"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/js-bhavyansh/Network_Connectivity","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js-bhavyansh%2FNetwork_Connectivity","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js-bhavyansh%2FNetwork_Connectivity/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js-bhavyansh%2FNetwork_Connectivity/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js-bhavyansh%2FNetwork_Connectivity/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/js-bhavyansh","download_url":"https://codeload.github.com/js-bhavyansh/Network_Connectivity/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js-bhavyansh%2FNetwork_Connectivity/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33513839,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T03:12:49.672Z","status":"ssl_error","status_checked_at":"2026-05-26T03:12:47.976Z","response_time":63,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android-studio","jetpack-compose","kotlin","network-connectivity","network-status"],"created_at":"2024-11-18T07:24:56.249Z","updated_at":"2026-05-26T09:31:05.425Z","avatar_url":"https://github.com/js-bhavyansh.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Network Connectivity Example\n\nA simple Android application demonstrating how to detect network connectivity status using Jetpack Compose and Kotlin.\n\n## Features\n- Displays network connectivity status (\"Connected\" or \"Unavailable\") in real-time.\n- Utilizes Jetpack Compose for the UI.\n- Implements a ConnectivityManager to observe network state changes.\n\n## Screenshot\n\u003cdiv style=\"display: flex; justify-content: center; align-items: center;\"\u003e\n    \u003cimg src=\"https://github.com/user-attachments/assets/88e0de7c-5fe3-4465-a10c-bbdd8ebf6a32\" alt=\"First Screenshot\" style=\"width: 200px; height: auto; margin-right: 10px;\"\u003e\n    \u003cimg src=\"https://github.com/user-attachments/assets/52aa43f1-c9ef-4210-b636-c2106ea4226d\" alt=\"Second Screenshot\" style=\"width: 200px; height: auto;\"\u003e\n\u003c/div\u003e\n\n## Code Overview\nThe application consists of a `MainActivity` that sets the content view with a `NetworkScreen` composable. The NetworkScreen displays the network status text based on the current connectivity state.\n\n**MainActivity**\nThe `MainActivity` sets up the content view and applies the theme.\n\n```kotlin\nclass MainActivity : ComponentActivity() {\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        enableEdgeToEdge()\n        setContent {\n            NetworkConnectivityTheme {\n                NetworkScreen()\n            }\n        }\n    }\n}\n```\n\n**NetworkScreen**\nThe `NetworkScreen` composable displays \"Connected\" or \"Unavailable\" based on the network connectivity state.\n\n```kotlin\n@Composable\nfun NetworkScreen() {\n    val connectionState by rememberConnectivityState()\n\n    val isConnected by remember(connectionState) {\n        derivedStateOf {\n            connectionState === NetworkConnectionState.Available\n        }\n    }\n\n    Box(\n        modifier = Modifier.fillMaxSize(),\n        contentAlignment = Alignment.Center\n    ) {\n        Text(\n            text = if (isConnected) \"Connected\" else \"Unavailable\",\n            fontSize = 48.sp,\n            fontWeight = FontWeight.Bold\n        )\n    }\n}\n```\n\n**Network Connectivity State**\nThe application uses a sealed interface to represent the network connection states:\n\n```kotlin\nsealed interface NetworkConnectionState {\n    data object Available : NetworkConnectionState\n    data object Unavailable : NetworkConnectionState\n}\n```\n\n**Connectivity Manager**\nThe `ConnectivityManager` is used to observe the network connectivity state as a Flow:\n\n```kotlin\nprivate fun networkCallback(callback: (NetworkConnectionState) -\u003e Unit): ConnectivityManager.NetworkCallback =\n    object : ConnectivityManager.NetworkCallback() {\n        override fun onAvailable(network: Network) {\n            callback(NetworkConnectionState.Available)\n        }\n\n        override fun onLost(network: Network) {\n            callback(NetworkConnectionState.Unavailable)\n        }\n    }\n\nfun getCurrentConnectivityState(connectivityManager: ConnectivityManager): NetworkConnectionState {\n    val network = connectivityManager.activeNetwork\n\n    val isConnected = connectivityManager.getNetworkCapabilities(network)\n        ?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) ?: false\n\n    return if (isConnected) NetworkConnectionState.Available else NetworkConnectionState.Unavailable\n}\n\nfun Context.observeConnectivityAsFlow(): Flow\u003cNetworkConnectionState\u003e = callbackFlow {\n    val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager\n\n    val callback = networkCallback { connectionState -\u003e trySend(connectionState) }\n\n    val networkRequest = NetworkRequest.Builder()\n        .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)\n        .build()\n\n    connectivityManager.registerNetworkCallback(networkRequest, callback)\n\n    val currentState = getCurrentConnectivityState(connectivityManager)\n    trySend(currentState)\n\n    awaitClose {\n        connectivityManager.unregisterNetworkCallback(callback)\n    }\n}\n\nval Context.currentConnectivityState: NetworkConnectionState\n    get() {\n        val connectivityManager =\n            getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager\n        return getCurrentConnectivityState(connectivityManager)\n    }\n```\n\n**Composable for Remembering Connectivity State**\nThe `rememberConnectivityState` composable provides a state holder for the network connection state:\n\n```kotlin\n@Composable\nfun rememberConnectivityState(): State\u003cNetworkConnectionState\u003e {\n    val context = LocalContext.current\n\n    return produceState(initialValue = context.currentConnectivityState) {\n        context.observeConnectivityAsFlow().collect {\n            value = it\n        }\n    }\n}\n```\n\n## Contributing\n\nContributions are welcome! Please fork the repository and submit a pull request for any improvements or bug fixes.\n\n1. Fork the repository.\n2. Create your feature branch (`git checkout -b feature/your-feature`).\n3. Commit your changes (`git commit -am 'Add some feature'`).\n4. Push to the branch (`git push origin feature/your-feature`).\n5. Create a new Pull Request.\n\n## Contact\n\nFor questions or feedback, please contact [@Bhavyansh03-tech](https://github.com/Bhavyansh03-tech).\n\n---\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjs-bhavyansh%2Fnetwork_connectivity","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjs-bhavyansh%2Fnetwork_connectivity","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjs-bhavyansh%2Fnetwork_connectivity/lists"}