@@ -54,11 +54,14 @@ public interface ReachabilityMonitor {
54
54
55
55
private class ReachabilityMonitorImpl constructor(val schedulerProvider : SchedulerProvider ) : ReachabilityMonitor {
56
56
private val subject = BehaviorSubject .create<Boolean >()
57
+ private var connectivityProvider: ConnectivityProvider ? = null
58
+
57
59
override fun configure (context : Context ) {
58
60
return configure(context, DefaultConnectivityProvider ())
59
61
}
60
62
61
63
override fun configure (context : Context , connectivityProvider : ConnectivityProvider ) {
64
+ this .connectivityProvider = connectivityProvider
62
65
connectivityProvider.registerDefaultNetworkCallback(
63
66
context,
64
67
object : NetworkCallback () {
@@ -74,8 +77,16 @@ private class ReachabilityMonitorImpl constructor(val schedulerProvider: Schedul
74
77
}
75
78
76
79
override fun getObservable (): Observable <Boolean > {
77
- return subject.subscribeOn(schedulerProvider.io())
78
- .debounce(250 , TimeUnit .MILLISECONDS , schedulerProvider.computation())
80
+ connectivityProvider?.let { connectivityProvider ->
81
+ return subject.subscribeOn(schedulerProvider.io())
82
+ .doOnSubscribe { subject.onNext(connectivityProvider.hasActiveNetwork) }
83
+ .debounce(250 , TimeUnit .MILLISECONDS , schedulerProvider.computation())
84
+ } ? : run {
85
+ throw DataStoreException (
86
+ " ReachabilityMonitor has not been configured." ,
87
+ " Call ReachabilityMonitor.configure() before calling ReachabilityMonitor.getObservable()"
88
+ )
89
+ }
79
90
}
80
91
}
81
92
0 commit comments