{"id":13606906,"url":"https://github.com/RhymezxCode/NetworkStateObserver","last_synced_at":"2025-04-12T11:31:07.627Z","repository":{"id":63885196,"uuid":"571095676","full_name":"RhymezxCode/NetworkStateObserver","owner":"RhymezxCode","description":"An Android library that helps you check the state of your network, if it is either available, lost, unavailable and also check the reach-ability of your network when your server is either down or your ISP is connected but no data subscription.","archived":false,"fork":false,"pushed_at":"2025-01-25T10:21:28.000Z","size":6927,"stargazers_count":52,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-01-25T11:19:52.276Z","etag":null,"topics":["android","android-library","kotlin","kotlin-android","kotlin-library"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RhymezxCode.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2022-11-27T06:14:38.000Z","updated_at":"2025-01-25T10:21:32.000Z","dependencies_parsed_at":"2024-01-16T23:30:39.737Z","dependency_job_id":"0c5e9ab4-12a9-4553-be39-dde5a07901b8","html_url":"https://github.com/RhymezxCode/NetworkStateObserver","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RhymezxCode%2FNetworkStateObserver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RhymezxCode%2FNetworkStateObserver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RhymezxCode%2FNetworkStateObserver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RhymezxCode%2FNetworkStateObserver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RhymezxCode","download_url":"https://codeload.github.com/RhymezxCode/NetworkStateObserver/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248560065,"owners_count":21124588,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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","android-library","kotlin","kotlin-android","kotlin-library"],"created_at":"2024-08-01T19:01:13.674Z","updated_at":"2025-04-12T11:31:07.612Z","avatar_url":"https://github.com/RhymezxCode.png","language":"Kotlin","funding_links":[],"categories":["Kotlin"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\u003ch1\u003eNetworkStateObserver Android Library\u003c/h1\u003e\n\n\u003ca href=\"https://android-arsenal.com/api?level=21\" target=\"blank\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/API-21%2B-brightgreen.svg?style=flat\" alt=\"NetworkStateObserver Android Library least API level\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://android-arsenal.com/details/1/8485\" target=\"blank\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/Android%20Arsenal-NetworkStateObserver-yellow.svg?style=flat\" alt=\"NetworkStateObserver Android Library on Android-Arsenal\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://jitpack.io/#RhymezxCode/NetworkStateObserver\" target=\"blank\"\u003e\n    \u003cimg src=\"https://jitpack.io/v/RhymezxCode/NetworkStateObserver.svg\" alt=\"NetworkStateObserver Android Library on jitpack.io\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://github.com/RhymezxCode/NetworkStateObserver/blob/main/LICENSE\" target=\"blank\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/license/RhymezxCode/NetworkStateObserver\" alt=\"NetworkStateObserver Android Library License.\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://github.com/RhymezxCode/NetworkStateObserver/stargazers\" target=\"blank\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/stars/RhymezxCode/NetworkStateObserver\" alt=\"NetworkStateObserver Android Library Stars\"/\u003e\n\u003c/a\u003e\n\u003ca href=\"https://github.com/RhymezxCode/NetworkStateObserver/fork\" target=\"blank\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/forks/RhymezxCode/NetworkStateObserver\" alt=\"NetworkStateObserver Android Library Forks\"/\u003e\n\u003c/a\u003e\n\u003ca href=\"https://github.com/RhymezxCode/NetworkStateObserver/issues\" target=\"blank\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/issues/RhymezxCode/NetworkStateObserver\" alt=\"NetworkStateObserver Android Library Issues\"/\u003e\n\u003c/a\u003e\n\u003ca href=\"https://github.com/RhymezxCode/NetworkStateObserver/commits?author=RhymezxCode\" target=\"blank\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/last-commit/RhymezxCode/NetworkStateObserver\" alt=\"NetworkStateObserver Android Library Issues\"/\u003e\n\u003c/a\u003e\n\u003c/div\u003e\n\u003cbr /\u003e\n\n## NetworkStateObserver Android Library\nA library that helps you check the state of your network, if it is either available, lost, unavailable and also check the reach-ability of your network when your server is either down or your ISP is connected but no data subscription. \n## Guide:  \n\n**Dev.to:**  \n[Using NetworkStateObserver in Large Projects: A Comprehensive Guide](https://dev.to/rhymezxcode/using-networkstateobserver-in-large-projects-a-comprehensive-guide-3bo9)  \n\u003cbr /\u003e\n**Medium:**  \n[Using NetworkStateObserver in Large Projects: A Comprehensive Guide](https://rhymezxcode.medium.com/using-networkstateobserver-in-large-projects-a-comprehensive-guide-b41e61ab64dc)\n\nDemo:\n\n\n![](Demo.gif)\n\n### 1. Adding NetworkStateObserver to your project\n\n* Include jitpack in your root `settings.gradle` file.\n\n```gradle\npluginManagement {\n    repositories {\n        ...\n        maven { url 'https://jitpack.io' }\n    }\n}\n\ndependencyResolutionManagement {\n    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)\n    repositories {\n        ...\n        maven { url 'https://jitpack.io' }\n    }\n}\n```\n\n* And add it's dependency to your app level `build.gradle` file:\n\n```gradle\ndependencies {\n    implementation 'com.github.RhymezxCode:NetworkStateObserver:1.1.3'\n\n    //Livedata\n    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0'\n    implementation 'androidx.lifecycle:lifecycle-livedata-core-ktx:2.7.0'\n    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.7.0'\n    implementation 'android.arch.lifecycle:extensions:1.1.1'\n}\n```\n\n#### Sync your project, and :scream: boom :fire: you have added NetworkStateObserver successfully. :exclamation:\n\n### 2. Usage\n\n* First initialize the builder class:\n\n```kt\n        val network = NetworkStateObserver.Builder()\n            .activity(activity = this@NetworkStateObserverExample)\n            .build()\n```\n\n* If you just want to check for connectivity, before performing a task or job():\n\n```kt\n        if(CheckConnectivity.isNetworkAvailable(requireContext())){\n                         showToast(\n                                this@NetworkStateObserverExample,\n                                \"Network restored\"\n                            )\n        }\n```\n\n* Use the live-data method to determine your network state, and replace the code in the lifecycleScope.launchWhenStarted { ....your code here } to do what you want:\n\n```kt\n        network.callNetworkConnection().observe(this) { isConnected -\u003e\n            lifecycleScope.launch(Dispatchers.IO) {\n                if (isConnected) {\n                    when {\n                        Reachability.hasServerConnected(\n                            context = this@NetworkStateObserverExample,\n                            serverUrl = \"https://www.your-server-url.com\"\n                        ) -\u003e lifecycleScope.launch{\n                            showToast(\n                                \"Server url works\"\n                            )\n                        }\n\n                        Reachability.hasInternetConnected(\n                            context = this@NetworkStateObserverExample\n                        ) -\u003e lifecycleScope.launch{\n                            showToast(\n                                \"Network restored\"\n                            )\n                        }\n\n                        else -\u003e lifecycleScope.launch{\n                            showToast(\n                                \"Network is lost or issues with server\"\n                            )\n                        }\n                    }\n                } else {\n                    //check for lost connection\n                    lifecycleScope.launch{\n                        showToast(\n                            \"No Network connection\"\n                        )\n                    }\n                }\n\n            }\n\n        }\n    }\n ```\n \n * Use the flow method to determine your network state, and also retry when an exception is thrown:\n\n```kt\n        lifecycleScope.launch {\n            network.callNetworkConnectionFlow()\n                .observe()\n                .collect {\n                    when (it) {\n                        NetworkObserver.Status.Available -\u003e {\n                            lifecycleScope.launch {\n                                when {\n                                    Reachability.hasServerConnectedFlow(\n                                        context = this@NetworkStateObserverExample,\n                                        serverUrl = \"https://www.github.com\"\n                                    ).retryWhen { cause, attempt -\u003e\n                                        if (cause is IOException \u0026\u0026 attempt \u003c 3) {\n                                            delay(2000)\n                                            return@retryWhen true\n                                        } else {\n                                            return@retryWhen false\n                                        }\n                                    }.buffer().first() -\u003e lifecycleScope.launch {\n                                        showToast(\n                                            this@NetworkStateObserverExample,\n                                            \"Server url works\"\n                                        )\n                                    }\n\n                                    Reachability.hasInternetConnectedFlow(\n                                        context = this@NetworkStateObserverExample\n                                    ).retryWhen { cause, attempt -\u003e\n                                        if (cause is IOException \u0026\u0026 attempt \u003c 3) {\n                                            delay(2000)\n                                            return@retryWhen true\n                                        } else {\n                                            return@retryWhen false\n                                        }\n                                    }.buffer().first() -\u003e lifecycleScope.launch {\n                                        showToast(\n                                            this@NetworkStateObserverExample,\n                                            \"Network restored\"\n                                        )\n                                    }\n\n                                    else -\u003e lifecycleScope.launch {\n                                        showToast(\n                                            this@NetworkStateObserverExample,\n                                            \"Network is lost or issues with server\"\n                                        )\n                                    }\n                                }\n                            }\n                        }\n\n                        NetworkObserver.Status.Unavailable -\u003e {\n                            showToast(\n                                \"Network is unavailable!\"\n                            )\n                        }\n\n                        NetworkObserver.Status.Losing -\u003e {\n                            showToast(\n                                \"You are losing your network!\"\n                            )\n                        }\n\n                        NetworkObserver.Status.Lost -\u003e {\n                            showToast(\n                                \"Network is lost!\"\n                            )\n                        }\n                    }\n                }\n        }\n ```\n\n* You can check if your internet connection is stable only, if you don't have a server url: \n\n```kt\n        network.callNetworkConnection().observe(this) { isConnected -\u003e\n            lifecycleScope.launch(Dispatchers.IO) {\n                if (isConnected) {\n                    when {\n\n                        Reachability.hasInternetConnected(\n                            context = this@NetworkStateObserverExample\n                        ) -\u003e lifecycleScope.launchW{\n                            showToast(\n                                \"Network restored\"\n                            )\n                        }\n\n                        else -\u003e lifecycleScope.launch{\n                            showToast(\n                                \"Network is lost or issues with server\"\n                            )\n                        }\n                    }\n                } else {\n                    //check for lost connection\n                    lifecycleScope.launch{\n                        showToast(\n                            \"No Network connection\"\n                        )\n                    }\n                }\n\n            }\n\n        }\n    }\n```\n    \n### 3. You can also inject NetworkStateObserver, and use it everywhere in your app with Hilt :syringe: :\n\n* Create an object for the NetworkStateModule in your di package:\n\n```kt\n@Module\n@InstallIn(ActivityComponent::class)\nobject NetworkStateModule {\n    @Provides\n    fun provideNetworkStateObserver(\n        activity: Activity\n    ): NetworkStateObserver {\n        return NetworkStateObserver.Builder()\n            .activity(activity = activity)\n            .build()\n    }\n}\n```\n\n* Declare the variable in your class either a fragment or activity, it works in both:\n\n```kt\n@AndroidEntryPoint\nclass myFragment : Fragment(){\n     @Inject\n     lateinit var network: NetworkStateObserver\n\n     private fun callNetworkConnection() {\n        network.callNetworkConnection().observe(this) { isConnected -\u003e\n            lifecycleScope.launch(Dispatchers.IO) {\n                if (isConnected) {\n                    when {\n                        Reachability.hasInternetConnected(\n                            context = this@NetworkStateObserverExample\n                        ) -\u003e lifecycleScope.launch{\n                            showToast(\n                                \"Network restored\"\n                            )\n                        }\n\n                        else -\u003e lifecycleScope.launch{\n                            showToast(\n                                \"Network is lost or issues with server\"\n                            )\n                        }\n                    }\n                } else {\n                    //check for lost connection\n                    lifecycleScope.launch{\n                        showToast(\n                            \"No Network connection\"\n                        )\n                    }\n                }\n\n            }\n\n        }\n    }\n    }\n\n override fun onResume() {\n        super.onResume()\n        callNetworkConnection()\n    }\n    \n }\n ```\n\n* Add the method in onResume() of your fragment or activity to have a great experience:\n\n```kt\n    override fun onResume() {\n        super.onResume()\n        callNetworkConnection()\n    }\n```\n\n:pushpin: Please, feel free to give me a star :star2:, I also love sparkles :sparkles: :relaxed:\n\u003cdiv align=\"center\"\u003e\n    \u003csub\u003eDeveloped with :sparkling_heart: by\n        \u003ca href=\"https://github.com/RhymezxCode\"\u003eAwodire Babajide Samuel\u003c/a\u003e\n    \u003c/sub\u003e\n\u003c/div\u003e\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRhymezxCode%2FNetworkStateObserver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FRhymezxCode%2FNetworkStateObserver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRhymezxCode%2FNetworkStateObserver/lists"}