-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9020988
Showing
42 changed files
with
1,340 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
[![Release](https://jitpack.io/v/jitpack/android-example.svg)](https://jitpack.io/#rommansabbir/NetworkX) | ||
# NetworkX | ||
An easy & handy library to monitor device internet connection status | ||
|
||
## Features | ||
* The real-time update on internet connection status changed | ||
* Lightweight | ||
* Provide both regular data or Live Data to observe from a fragment or an activity | ||
* Thread Safe | ||
* Cancel monitoring Internet connection status any time | ||
|
||
## How does it work? | ||
Once you start observing internet connection status by calling from your ``Application`` class `onCreate()` | ||
method, ``NetworkX`` will start monitor internet connection status immediately under a `CoroutineScope` in a `IO Thread` | ||
for the thread safe purpose. ``NetworkX`` provide both traditional data which is `Boolean` or `MutableLiveData` | ||
so that you can observe the status in a fragment or an activity. | ||
|
||
--- | ||
|
||
### Library Preview | ||
|
||
https://www.youtube.com/watch?v=dHlhsvPF7RY | ||
--- | ||
|
||
## Documentation | ||
|
||
#### Installation | ||
Step 1. Add the JitPack repository to your build file . | ||
|
||
```gradle | ||
allprojects { | ||
repositories { | ||
maven { url 'https://jitpack.io' } | ||
} | ||
} | ||
``` | ||
|
||
Step 2. Add the dependency. | ||
|
||
```gradle | ||
dependencies { | ||
implementation 'com.github.rommansabbir:NetworkX:Tag' | ||
} | ||
``` | ||
|
||
--- | ||
|
||
### Version available | ||
|
||
| Releases | ||
| ------------- | | ||
| 1.0 | | ||
|
||
# Usages | ||
|
||
##### Start observing internet connection status from your `Application` class. | ||
|
||
To Start observing network status using ``NetworkX`` call `NetworkX.startObserving` from application `onCreate` method. | ||
Otherwise, when try to check if device is connected to the internet or not it will throw an `Exception` | ||
Provide `Application` reference & `NetworkXObservingStrategy` to `NetworkX` . | ||
|
||
`NetworkXObservingStrategy` represent delay time in internet connection status checking | ||
Sates are: | ||
|
||
|
||
* NetworkXObservingStrategy.LOW | ||
* NetworkXObservingStrategy.MEDIUM | ||
* NetworkXObservingStrategy.HIGH | ||
* NetworkXObservingStrategy.REALTIME | ||
|
||
|
||
```` | ||
override fun onCreate() { | ||
super.onCreate() | ||
NetworkX.startObserving(this, NetworkXObservingStrategy.HIGH) | ||
} | ||
```` | ||
--- | ||
|
||
##### Also, you can provide custom time interval in millis. | ||
Custom State: | ||
|
||
|
||
* NetworkXObservingStrategy.CUSTOM | ||
|
||
```` | ||
override fun onCreate() { | ||
super.onCreate() | ||
NetworkX.startObserving(this, NetworkXObservingStrategy.CUSTOM(15 * 1000)) | ||
} | ||
```` | ||
|
||
--- | ||
|
||
##### How to check the connection status in a `Fragment` or an `Activity` or `Anywhere of the application` . | ||
|
||
* To get status of internet connection for single time, use `NetworkX.isConnected` method to retrieve connection status | ||
|
||
```` | ||
NetworkX.isConnected().let { | ||
when (it) { | ||
true -> { | ||
/** | ||
* Do your stuff here when internet is connected | ||
*/ | ||
Log.d("NetworkX", "Is internet connected: $it") | ||
} | ||
else -> { | ||
/** | ||
* Do your stuff here when internet is not connected | ||
*/ | ||
Log.d("NetworkX", "Is internet connected: $it") | ||
} | ||
} | ||
} | ||
```` | ||
|
||
or you can use extension function | ||
|
||
```` | ||
isInternetConnected().let { | ||
when (it) { | ||
true -> { | ||
/** | ||
* Do your stuff here when internet is connected | ||
*/ | ||
Log.d("NetworkX", "Is internet connected: $it") | ||
} | ||
else -> { | ||
/** | ||
* Do your stuff here when internet is not connected | ||
*/ | ||
Log.d("NetworkX", "Is internet connected: $it") | ||
} | ||
} | ||
} | ||
```` | ||
|
||
--- | ||
|
||
* To get status of internet connection in realtime, use `NetworkX.isConnectedLiveData` method to retrieve connection status. | ||
|
||
```` | ||
NetworkX.isConnectedLiveData().observe( | ||
fragment/activity), | ||
Observer { | ||
when (it) { | ||
true -> { | ||
/** | ||
* Do your stuff here when internet is connected | ||
*/ | ||
Log.d("NetworkX", "Is internet connected: $it") | ||
} | ||
else -> { | ||
/** | ||
* Do your stuff here when internet is not connected | ||
*/ | ||
Log.d("NetworkX", "Is internet connected: $it") | ||
} | ||
} | ||
} | ||
) | ||
```` | ||
|
||
or you can use extension function | ||
|
||
```` | ||
isInternetConnectedLiveData().observe(this, Observer { | ||
when (it) { | ||
true -> { | ||
/** | ||
* Do your stuff here when internet is connected | ||
*/ | ||
Log.d("NetworkX", "Is internet connected: $it") | ||
} | ||
else -> { | ||
/** | ||
* Do your stuff here when internet is not connected | ||
*/ | ||
Log.d("NetworkX", "Is internet connected: $it") | ||
} | ||
} | ||
}) | ||
```` | ||
|
||
--- | ||
|
||
* If you want to cancel the observing if internet is connected status, use `NetworkX.cancelObserving` method | ||
|
||
```` | ||
NetworkX.cancelObserving() | ||
```` | ||
|
||
or you can use extension function | ||
|
||
```` | ||
cancelObservingIsInternetConnected() | ||
```` | ||
|
||
--- | ||
|
||
### Contact me | ||
[Portfolio](https://www.rommansabbir.com/) | [LinkedIn](https://www.linkedin.com/in/rommansabbir/) | [Twitter](https://www.twitter.com/itzrommansabbir/) | [Facebook](https://www.facebook.com/itzrommansabbir/) | ||
|
||
--- | ||
|
||
### License | ||
[Apache Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html) | ||
|
||
```` | ||
Copyright (C) 2020 Romman Sabbir | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
```` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
apply plugin: 'com.android.library' | ||
apply plugin: 'kotlin-android' | ||
apply plugin: 'kotlin-android-extensions' | ||
apply plugin: 'com.github.dcendents.android-maven' | ||
group = 'rommansabbir' | ||
version '1.0' | ||
|
||
android { | ||
compileSdkVersion 30 | ||
buildToolsVersion "29.0.3" | ||
|
||
defaultConfig { | ||
minSdkVersion 16 | ||
targetSdkVersion 30 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
consumerProguardFiles 'consumer-rules.pro' | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility = 1.8 | ||
targetCompatibility = 1.8 | ||
} | ||
kotlinOptions { | ||
jvmTarget = "1.8" | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: "libs", include: ["*.jar"]) | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | ||
implementation 'androidx.core:core-ktx:1.3.1' | ||
implementation 'androidx.appcompat:appcompat:1.1.0' | ||
testImplementation 'junit:junit:4.13' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.1' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' | ||
def lifecycle_version = "2.2.0" | ||
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" | ||
|
||
def coroutine_version = '1.3.6' | ||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:${coroutine_version}" | ||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:${coroutine_version}" | ||
|
||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.rommansabbir.networkx"> | ||
|
||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
<uses-permission android:name="android.permission.INTERNET" /> | ||
</manifest> |
Oops, something went wrong.