Kotlin multi platform observable pattern

Viewed 51

I have 2 apps on android and ios side. Both of apps using apollo graphql library. So i decided to use it on kotlin multi library project. Everything works well with this type of usage

suspend fun addFeedback(request: AddFeedBackRequest): BaseResponseModel<Any> {

        val feedBackType = if (request.type == AddFeedBackType.Bug) {
            FeedbackType.BUG
        } else {
            FeedbackType.FEEDBACK
        }

        val input = AddFeedbackInput(request.note, Optional.presentIfNotNull(feedBackType))

        val mutation = AddFeedbackMutation(input)

        val response = executeMutation(mutation)

        if (response.isFailed()) return onError()

        return onSuccess()

    }

and when i want to use this method on android application i can use it like this

 lifecycleScope.launch {

            HelpRepository().addFeedback(AddFeedBackRequest(AddFeedBackType.Bug, ""))

        }

I've been searching about observable pattern i found [https://github.com/badoo/Reaktive1

library for observable rx kotlin usages

For example i want to use addFeedBack method on android side like this

HelpRepository()
  .addFeedback()
    .onSuccess { response ->
    }
    .onLoading { state ->
    }
    .onError { err ->
    }

Last two days i searched about this structure but i couldn't much more anything. Any re-search subjects or example projects that you can help please ?

Thanks for that

1 Answers

If you want to use Reaktive, you can do the following:

  1. Add the following dependencies: com.badoo.reaktive:reaktive:<version>, com.badoo.reaktive:coroutines-interop:<version>
  2. Change you method signature to the following: fun addFeedback(request: AddFeedBackRequest): Single<T>
  3. Inside this method use singleFromCoroutine {} function to wrap the suspend call executeMutation into Single<T>
  4. Use it as follows:
val disposable = HelpRepository()
  .addFeedback()
  .doOnBeforeSubscribe { /* Loading started */ }
  .subscribe(
    onSuccess = { response -> },
    onError = { e -> }
  )

Since you are also sharing the code with iOS, please read the following docs: for the old memory model, for the new memory model.

Related