An easy method for providing the reactive interface between client and service
class SampleClient : BaseRxClient {
constructor(context: Context) : super(context)
companion object {
const val SERVICE_PACKAGE_NAME: String = "com.timweng.lib.rxaidl.sample"
const val SERVICE_CLASSNAME: String = "com.timweng.lib.rxaidl.sample.SampleService"
}
override fun getVersion(): Long {
return 1L
}
override fun getPackageName(): String {
return SERVICE_PACKAGE_NAME
}
override fun getClassName(): String {
return SERVICE_CLASSNAME
}
fun requestSample(request: SampleRequest): Observable<SampleCallback> {
return requestObservable(request, SampleRequest::class.java, SampleCallback::class.java,
minServiceVersion = 1, maxServiceVersion = 10)
}
}
class SampleService : BaseRxService() {
override fun getVersion(): Long {
return 1L
}
@Keep
@RequestRequirement(minClientVersion = 0, maxClientVersion = 10)
fun requestTestObservable(request: SampleRequest): Observable<SampleCallback> {
val callback = SampleCallback()
callback.requestName = request.name
return Observable.create<SampleCallback> { e ->
while (callback.number < request.count) {
if (!e.isDisposed) {
Timber.d("onNext: $callback")
e.onNext(callback.copy())
} else {
break
}
callback.number++
}
e.onComplete()
}.subscribeOn(Schedulers.newThread())
}
}
data class SampleRequest(var name: String = "SampleRequest",
var count: Int = 3)
data class SampleCallback(var requestName: String = "TEMP",
var number: Int = 0)
<service
android:name=".SampleService"
android:exported="true" />
val client = SampleClient(this)
val request = SampleRequest("Observer0", 10)
val observable = client.requestSample(request)
observable.observeOn(AndroidSchedulers.mainThread()).subscribe(
{ n -> updateLog("onNext:\n$n") },
{ e -> updateLog("onError:\n$e") },
{ updateLog("onComplete") })