Skip to content

Commit

Permalink
emit network status on subscribe and error if calling getObservable b…
Browse files Browse the repository at this point in the history
…efore configure
  • Loading branch information
eeatonaws committed May 4, 2023
1 parent b470387 commit 62a7b15
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ public interface ReachabilityMonitor {

private class ReachabilityMonitorImpl constructor(val schedulerProvider: SchedulerProvider) : ReachabilityMonitor {
private val subject = BehaviorSubject.create<Boolean>()
private var connectivityProvider: ConnectivityProvider? = null

override fun configure(context: Context) {
return configure(context, DefaultConnectivityProvider())
}

override fun configure(context: Context, connectivityProvider: ConnectivityProvider) {
this.connectivityProvider = connectivityProvider
connectivityProvider.registerDefaultNetworkCallback(
context,
object : NetworkCallback() {
Expand All @@ -74,8 +77,16 @@ private class ReachabilityMonitorImpl constructor(val schedulerProvider: Schedul
}

override fun getObservable(): Observable<Boolean> {
return subject.subscribeOn(schedulerProvider.io())
.debounce(250, TimeUnit.MILLISECONDS, schedulerProvider.computation())
connectivityProvider?.let { connectivityProvider ->
return subject.subscribeOn(schedulerProvider.io())
.doOnSubscribe { subject.onNext(connectivityProvider.hasActiveNetwork) }
.debounce(250, TimeUnit.MILLISECONDS, schedulerProvider.computation())
} ?: run {
throw DataStoreException(
"ReachabilityMonitor has not been configured.",
"Call ReachabilityMonitor.configure() before calling ReachabilityMonitor.getObservable()"
)
}
}
}

Expand Down

0 comments on commit 62a7b15

Please sign in to comment.