Skip to content

WeiTingWeng/RxAIDL

Repository files navigation

RxAIDL

Synopsis

An easy method for providing the reactive interface between client and service

Concept

How to use RxAIDL

1. Create Client which extends BaseRxClient

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)
    }
}

2. Create Service which extends BaseRxService

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())
    }
}

3. Define Request class

data class SampleRequest(var name: String = "SampleRequest",
                         var count: Int = 3)

4. Define Callback class

data class SampleCallback(var requestName: String = "TEMP",
                          var number: Int = 0)

5. Add Service in Manifest and exported it

<service
    android:name=".SampleService"
    android:exported="true" />

6. Request service by client

    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") })

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published