Unfortunately, I can't understand how to check Observable.
Depending on connection - I want to get my data from network or DB.
I have a method that checks network connection:
companion object {
fun isConnected() : Observable<Boolean> {
val connectivityManager = MyApplication.applicationContext().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork = connectivityManager.activeNetworkInfo
val isConnectedException = activeNetwork != null && activeNetwork.isConnectedOrConnecting
return Observable.just(isConnectedException)
}
}
So if it's true I want to call my network method:
override fun searchGroups(q: String): Observable<List<Group>> {
return groupApi.searchGroups(GroupSearchRequest(q).toMap())
.flatMap { groupResponse -> Observable.just(groupResponse.response.items) }
.doOnNext{ groupList -> groupRepository.insertGroups(groupList)}
}
and in the other case I want to call DB method:
override fun getGroupsFromDB(q: String): Observable<List<Group>> {
return groupRepository.findByName(q)
}
Here is my try to do this, but I think there is problem because of nullable interactor, but still don't know what to do.
compositeDisposable.add(
NetworkManager.isConnected()
.flatMap {
if (it) {
interactor?.searchGroups(q)
} else {
interactor?.getGroupsFromDB(q)
}
}
}
)
Could anybody please help me with that ?
UPDATE
So the problem was in nullable object interactor
.
Could anybody please suggest the better way to not using !!
for interactor
object?