Skip to content

Commit 1055811

Browse files
authored
Merge pull request #16 from rommansabbir/dev
Version 4.1.0
2 parents d833b26 + 85ae5ee commit 1055811

File tree

17 files changed

+362
-129
lines changed

17 files changed

+362
-129
lines changed

NetworkX/src/main/java/com/rommansabbir/networkx/NetworkXConfig.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.rommansabbir.networkx.exceptions.NetworkXNotInitializedException
1212
* @param application, [Application] reference to initialize NetworkX properly
1313
* @param enableSpeedMeter, determine if to enable monitoring network speed
1414
*/
15+
@Deprecated("Use new SmartConfig.")
1516
class NetworkXConfig private constructor(
1617
val application: Application,
1718
val enableSpeedMeter: Boolean
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.rommansabbir.networkx
2+
3+
/**
4+
* Define the lifecycle for NetworkX. Either activity or application.
5+
*/
6+
sealed class NetworkXLifecycle {
7+
object Application : NetworkXLifecycle()
8+
object Activity : NetworkXLifecycle()
9+
}

NetworkX/src/main/java/com/rommansabbir/networkx/NetworkXManager.kt

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,20 @@ import android.util.Log
1414
import java.net.InetAddress
1515

1616
internal class NetworkXManager constructor(
17-
private val application: Application,
18-
private val isSpeedMeterEnabled: Boolean
17+
private val smartConfig: SmartConfig
1918
) {
2019
// Callback for activity lifecycle for this specific application
2120
private val activityCallback = object : Application.ActivityLifecycleCallbacks {
2221
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
23-
try {
24-
getConnectivityManager(application).apply {
25-
registerNetworkCallback(
26-
getNetworkRequest(),
27-
getNetworkCallBack
28-
)
29-
if (isSpeedMeterEnabled) {
30-
enabledSpeedMeter()
31-
logThis("onActivityCreated: speed meter enabled")
22+
when (smartConfig.lifecycle) {
23+
NetworkXLifecycle.Activity -> {
24+
initObservation()
25+
}
26+
NetworkXLifecycle.Application -> {
27+
if (!isObservationRunning) {
28+
initObservation()
3229
}
33-
logThis("onActivityCreated: listener registered")
3430
}
35-
} catch (e: Exception) {
36-
e.printStackTrace()
37-
logThis(e.message)
3831
}
3932
}
4033

@@ -49,19 +42,58 @@ internal class NetworkXManager constructor(
4942
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
5043

5144
override fun onActivityDestroyed(activity: Activity) {
52-
try {
53-
getConnectivityManager(application).unregisterNetworkCallback(getNetworkCallBack)
54-
logThis("onActivityDestroyed: listener unregistered")
55-
if (isSpeedMeterEnabled) {
56-
disableSpeedMeter()
57-
logThis("onActivityDestroyed: speed meter disabled")
45+
when (smartConfig.lifecycle) {
46+
NetworkXLifecycle.Activity -> {
47+
stopObservation()
5848
}
59-
} catch (e: Exception) {
60-
e.printStackTrace()
61-
logThis(e.message)
49+
NetworkXLifecycle.Application -> {
50+
logThis("stopObservation(): can't stop observation since the lifecycle is set to Application")
51+
}
52+
}
53+
}
54+
55+
}
56+
57+
private fun stopObservation() {
58+
isObservationRunning = try {
59+
getConnectivityManager(smartConfig.application).unregisterNetworkCallback(
60+
getNetworkCallBack
61+
)
62+
logThis("stopObservation(): listener unregistered")
63+
if (smartConfig.enableSpeedMeter) {
64+
disableSpeedMeter()
65+
logThis("stopObservation(): speed meter disabled")
6266
}
67+
false
68+
} catch (e: Exception) {
69+
e.printStackTrace()
70+
logThis(e.message)
71+
false
6372
}
73+
}
74+
75+
@Volatile
76+
private var isObservationRunning: Boolean = false
6477

78+
private fun initObservation() {
79+
try {
80+
getConnectivityManager(smartConfig.application).apply {
81+
registerNetworkCallback(
82+
getNetworkRequest(),
83+
getNetworkCallBack
84+
)
85+
if (smartConfig.enableSpeedMeter) {
86+
enabledSpeedMeter()
87+
logThis("initObservation(): speed meter enabled")
88+
}
89+
logThis("initObservation(): listener registered")
90+
isObservationRunning = true
91+
}
92+
} catch (e: Exception) {
93+
e.printStackTrace()
94+
logThis(e.message)
95+
isObservationRunning = false
96+
}
6597
}
6698

6799
@Volatile
@@ -70,8 +102,8 @@ internal class NetworkXManager constructor(
70102
// Initialize the Manager
71103
init {
72104
try {
73-
getConnectivityManager(application)
74-
application.registerActivityLifecycleCallbacks(activityCallback)
105+
getConnectivityManager(smartConfig.application)
106+
smartConfig.application.registerActivityLifecycleCallbacks(activityCallback)
75107
trafficUtils = TrafficUtils()
76108
} catch (e: Exception) {
77109
e.printStackTrace()

NetworkX/src/main/java/com/rommansabbir/networkx/NetworkXProvider.kt

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import androidx.lifecycle.LiveData
77
import androidx.lifecycle.MutableLiveData
88
import com.rommansabbir.networkx.extension.getDefaultIOScope
99
import com.rommansabbir.networkx.extension.getDefaultLastKnownSpeed
10-
import kotlinx.coroutines.CoroutineScope
1110
import kotlinx.coroutines.flow.MutableStateFlow
1211
import kotlinx.coroutines.launch
1312

@@ -106,7 +105,13 @@ object NetworkXProvider {
106105
if (manager != null) {
107106
return
108107
}
109-
manager = NetworkXManager(application, enableSpeedMeter)
108+
manager = NetworkXManager(
109+
SmartConfig(
110+
application,
111+
enableSpeedMeter,
112+
NetworkXLifecycle.Application
113+
)
114+
)
110115
} catch (e: Exception) {
111116
throw e
112117
}
@@ -119,12 +124,46 @@ object NetworkXProvider {
119124
*
120125
* @param config [NetworkXConfig] reference.
121126
*/
127+
@Deprecated(
128+
"Use new SmartConfig.",
129+
replaceWith = ReplaceWith(
130+
"NetworkXProvider.enable(SmartConfig(application, true, NetworkXLifecycle.Application))",
131+
imports = arrayOf(
132+
"com.rommansabbir.networkx.SmartConfig",
133+
"com.rommansabbir.networkx.NetworkXLifecycle"
134+
)
135+
)
136+
)
122137
fun enable(config: NetworkXConfig) {
123138
try {
124139
if (manager != null) {
125140
return
126141
}
127-
manager = NetworkXManager(config.application, config.enableSpeedMeter)
142+
manager = NetworkXManager(
143+
SmartConfig(
144+
config.application,
145+
config.enableSpeedMeter,
146+
NetworkXLifecycle.Application
147+
)
148+
)
149+
} catch (e: Exception) {
150+
throw e
151+
}
152+
}
153+
154+
/**
155+
* Main entry point for the client.
156+
* First check for [NetworkXManager] instance, if the status is null then initialize it properly
157+
* else ignore the initialization.
158+
*
159+
* @param config [SmartConfig] reference.
160+
*/
161+
fun enable(config: SmartConfig) {
162+
try {
163+
if (manager != null) {
164+
return
165+
}
166+
manager = NetworkXManager(config)
128167
} catch (e: Exception) {
129168
throw e
130169
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.rommansabbir.networkx
2+
3+
import android.app.Application
4+
5+
data class SmartConfig(
6+
val application: Application,
7+
val enableSpeedMeter: Boolean,
8+
val lifecycle: NetworkXLifecycle
9+
)

NetworkX/src/main/java/com/rommansabbir/networkx/dialog/NoInternetDialogV2.kt

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.rommansabbir.networkx.dialog
22

33
import android.app.Activity
44
import android.app.Dialog
5+
import androidx.annotation.DrawableRes
6+
import androidx.core.content.ContextCompat
57
import com.rommansabbir.networkx.R
68
import com.rommansabbir.networkx.databinding.ContentDialogNoInternetBinding
79
import com.rommansabbir.networkx.extension.getDialogInstance
@@ -13,31 +15,49 @@ class NoInternetDialogV2 constructor(
1315
private val message: String,
1416
private val buttonTitle: String,
1517
private val isCancelable: Boolean,
18+
@DrawableRes private val drawable: Int? = null,
1619
private val callback: () -> Unit
1720
) {
1821
init {
19-
activity.get()?.let {
20-
getDialogInstance<ContentDialogNoInternetBinding>(
21-
it,
22+
activity.get()?.let { activity ->
23+
dialog = getDialogInstance<ContentDialogNoInternetBinding>(
24+
activity,
2225
R.layout.content_dialog_no_internet,
2326
R.style.my_dialog,
2427
isCancelable
25-
) { dialog, binding ->
26-
NoInternetDialogV2.dialog = dialog
28+
) { binding ->
2729
binding.cdniBtnRetry.text = buttonTitle
2830
binding.cdniTvTitle.text = title
2931
binding.cdniTvMessage.text = message
30-
NoInternetDialogV2.dialog?.setOnDismissListener {
31-
NoInternetDialogV2.dialog = null
32+
drawable?.let {
33+
binding.imageView.setImageDrawable(
34+
ContextCompat.getDrawable(
35+
activity,
36+
drawable
37+
)
38+
)
3239
}
33-
binding.cdniBtnRetry.setOnClickListener { callback.invoke();NoInternetDialogV2.dialog?.cancel() }
34-
NoInternetDialogV2.dialog?.show()
40+
41+
binding.cdniBtnRetry.setOnClickListener { callback.invoke(); forceClose() }
42+
}
43+
dialog?.setOnDismissListener {
44+
dialog = null
3545
}
46+
dialog?.show()
3647
} ?: kotlin.run { dialog = null }
3748
}
3849

3950
companion object {
51+
@Volatile
4052
private var dialog: Dialog? = null
53+
4154
val isVisible: Boolean = dialog != null && dialog!!.isShowing
55+
fun forceClose() {
56+
try {
57+
dialog?.cancel()
58+
} catch (e: Exception) {
59+
e.printStackTrace()
60+
}
61+
}
4262
}
4363
}

NetworkX/src/main/java/com/rommansabbir/networkx/exceptions/NoInternetDialogException.kt

Lines changed: 0 additions & 5 deletions
This file was deleted.

NetworkX/src/main/java/com/rommansabbir/networkx/extension/NetworkXExtensions.kt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.rommansabbir.networkx.extension
22

33
import androidx.lifecycle.LiveData
4-
import com.rommansabbir.networkx.LastKnownSpeed
5-
import com.rommansabbir.networkx.NetworkSpeedType
6-
import com.rommansabbir.networkx.NetworkXConfig
7-
import com.rommansabbir.networkx.NetworkXProvider
4+
import com.rommansabbir.networkx.*
85
import kotlinx.coroutines.CoroutineScope
96
import kotlinx.coroutines.Dispatchers
107
import kotlinx.coroutines.flow.MutableStateFlow
@@ -56,4 +53,22 @@ internal val getDefaultIOScope by lazy { CoroutineScope(Dispatchers.IO) }
5653
*
5754
* @param config [NetworkXConfig].
5855
*/
56+
57+
@Deprecated(
58+
"Use new extension API", replaceWith = ReplaceWith(
59+
"smartEnableNetworkX(SmartConfig(application, true, NetworkXLifecycle.Application))",
60+
imports = arrayOf(
61+
"com.rommansabbir.networkx.extension.smartEnableNetworkX",
62+
"com.rommansabbir.networkx.SmartConfig",
63+
"com.rommansabbir.networkx.NetworkXLifecycle"
64+
)
65+
)
66+
)
5967
fun enableNetworkX(config: NetworkXConfig) = NetworkXProvider.enable(config)
68+
69+
/**
70+
* Initialize [NetworkXProvider].
71+
*
72+
* @param config [SmartConfig].
73+
*/
74+
fun smartEnableNetworkX(config: SmartConfig) = NetworkXProvider.enable(config)

NetworkX/src/main/java/com/rommansabbir/networkx/extension/ViewExtension.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ inline fun <V : ViewDataBinding> getDialogInstance(
2323
layoutId: Int,
2424
@StyleRes customStyle: Int? = null,
2525
setCancelable: Boolean = false,
26-
crossinline onSuccess: (Dialog, V) -> Unit
27-
) {
26+
crossinline onSuccess: (V) -> Unit
27+
): Dialog {
2828
val layout = DataBindingUtil.inflate<V>(LayoutInflater.from(activity), layoutId, null, false)
2929
val dialog = if (customStyle == null) Dialog(activity) else Dialog(activity, customStyle)
3030
dialog.setContentView(layout.root)
3131
dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
3232
dialog.setCanceledOnTouchOutside(setCancelable)
3333
dialog.setCancelable(setCancelable)
34-
onSuccess.invoke(dialog, layout)
34+
onSuccess.invoke(layout)
35+
return dialog
3536
}

NetworkX/src/main/res/layout/content_dialog_no_internet.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
<androidx.constraintlayout.widget.ConstraintLayout
1111
android:layout_width="match_parent"
12-
android:layout_height="wrap_content"
13-
android:orientation="vertical">
12+
android:layout_height="wrap_content">
1413

1514
<androidx.cardview.widget.CardView
1615
android:layout_width="0dp"

0 commit comments

Comments
 (0)