agoda-com / Kakao

This repo is no longer supported. Please visit a https://github.com/KakaoCup/Kakao
Apache License 2.0
1.11k stars 102 forks source link

Intercepting screen state? #193

Closed plnice closed 3 years ago

plnice commented 4 years ago

Hi, we want to set up a screen interceptor which would be fired when screen becomes active - only once, before running the function passed to onScreen method. In our case the interceptor is heavy and time-consuming and we don't need to invoke it on each check/perform. Would there be a possibility to add such a functionality/would you accept a pull request with such functionality? The other option which probably would work for us is to make the screen's invoke operator open so that we can override it and perform our code before running the function.

Unlimity commented 4 years ago

Hi there! Thanks for the question. Generally, you can easily make your own extension function with your interceptor inside. Something like this:

fun <T: Screen> onInterceptedScreen(receiver: T.() -> Unit) {
    myInterceptor()
    onScreen<T>(receiver)
}
plnice commented 4 years ago

Yes, but this solution has two drawbacks:

  1. Requires using different method than onScreen provided by the library - if somebody uses onScreen by mistake, she won't get any warning or compilation error and our interceptor won't be called
  2. The method is separated from the implementation of the screen. In our case we need to add different interceptor per screen, which would result in many onInterceptedScreen methods added or a need to add an abstract screen implementation with method providing the interceptor

What we also thought about was extending the intercept with onFirst functionality which would fire the interceptor only once:

intercept {
  onViewInteraction {
    onFirst {
      ...
    }
  }
}      
Unlimity commented 4 years ago

In that case you should just define another interceptor holder:

intercept {
    onFirstViewInteraction {
        ...
    }
}

You are welcome to submit a PR with such implementation 👍

Unlimity commented 3 years ago

Closing this issue due to no activity. You can reopen issue if it is not solved yet.