Skip to content

Commit 701c409

Browse files
committed
Documentation updated
1 parent 1a78e60 commit 701c409

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

README.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,65 @@ Step 2. Add the dependency.
3434

3535
| Releases
3636
| ------------- |
37-
| 3.1.0 |
37+
| 3.3.0 |
3838

3939

4040
# Usages
4141

4242
## Step 1:
43-
Initialize `NetworkX` from your `Application.onCreate()` method by calling `NetworkXProvider.init()` which take `Application` reference as the param.
43+
Initialize `NetworkX` from your `Application.onCreate()`
44+
````
45+
val builder = NetworkXConfig.Builder()
46+
.withApplication(this)
47+
// You can disable speed meter if not required
48+
.withEnableSpeedMeter(true)
49+
.build()
50+
NetworkXProvider.enable(builder)
51+
````
4452

4553
## Step 2:
4654
- To check internet connection status, simply call `NetworkXProvider.isInternetConnected` which return a `Boolean` value.
55+
````
56+
val status = NetworkXProvider.isInternetConnected
57+
textView.text = "Internet connection status: $status"
58+
````
4759

4860
- To observe the internet connection status, start observing `NetworkXProvider.isInternetConnectedLiveData`
4961

50-
## Note:
62+
````
63+
NetworkXProvider.isInternetConnectedLiveData.observe(this) { status ->
64+
status?.let {
65+
textView.text = "Internet connection status: $it"
66+
}
67+
}
68+
````
69+
70+
- To get connected network speed/last known speed call `NetworkXProvider.lastKnownSpeed` which return an `LastKnownSpeed` object
71+
72+
````
73+
NetworkXProvider.lastKnownSpeed?.let {
74+
textView2.text =
75+
"Last Known Speed: Speed - ${it.speed} | Type - ${it.networkTypeNetwork} | Simplified Speed - ${it.simplifiedSpeed}"
76+
}
77+
````
78+
79+
- To obsever current network speed/last known speed start observing `NetworkXProvider.lastKnownSpeedLiveData`
80+
81+
````
82+
NetworkXProvider.lastKnownSpeedLiveData.observe(this) {
83+
it?.let {
84+
textView2.text =
85+
"Last Known Speed: Speed - ${it.speed} | Type - ${it.networkTypeNetwork} | Simplified Speed - ${it.simplifiedSpeed}"
86+
}
87+
}
88+
````
89+
90+
## Notes:
91+
- NetworkX (including Speed Meter) only works when the app is in the foreground
5192
- The default value for `NetworkXProvider.isInternetConnected` is `false`
5293
- The default value for `NetworkXProvider.isInternetConnectedLiveData` is `null`
94+
- The default value for `NetworkXProvider.lastKnownSpeed` is `null`
95+
- The default value for `NetworkXProvider.lastKnownSpeedLiveData` is `null`
5396

5497
---
5598

app/src/main/java/com/rommansabbir/networkobserverexample/MainActivity.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@ class MainActivity : AppCompatActivity() {
1212
super.onCreate(savedInstanceState)
1313
setContentView(R.layout.activity_main)
1414

15+
val status = NetworkXProvider.isInternetConnected
16+
textView.text = "Internet connection status: $status"
17+
1518
//
16-
NetworkXProvider.isInternetConnectedLiveData.observe(this) {
17-
it?.let {
19+
NetworkXProvider.isInternetConnectedLiveData.observe(this) { status ->
20+
status?.let {
1821
textView.text = "Internet connection status: $it"
1922
}
2023
}
2124

25+
NetworkXProvider.lastKnownSpeed?.let {
26+
textView2.text =
27+
"Last Known Speed: Speed - ${it.speed} | Type - ${it.networkTypeNetwork} | Simplified Speed - ${it.simplifiedSpeed}"
28+
}
29+
2230
NetworkXProvider.lastKnownSpeedLiveData.observe(this) {
2331
it?.let {
2432
textView2.text =

app/src/main/java/com/rommansabbir/networkobserverexample/MyApplication.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ class MyApplication : Application() {
1212
super.onCreate()
1313

1414
// NetworkXProvider.init(this, true)
15-
NetworkXProvider.enable(
16-
NetworkXConfig.Builder().withApplication(this).withEnableSpeedMeter(true).build()
17-
)
15+
/**
16+
* Initialize NetworkX
17+
*/
18+
val builder = NetworkXConfig.Builder()
19+
.withApplication(this)
20+
// You can disable speed meter if not required
21+
.withEnableSpeedMeter(true)
22+
.build()
23+
NetworkXProvider.enable(builder)
1824

1925
/**
2026
* To Start observing network status using [NetworkX] call [NetworkXCore.init] from application [onCreate] method

0 commit comments

Comments
 (0)