Zhuinden / simple-stack

[ACTIVE] Simple Stack, a backstack library / navigation framework for simpler navigation and state management (for fragments, views, or whatevers).
Apache License 2.0
1.36k stars 76 forks source link

Consider adding Backstack.services() method as in GlobalServices.services() #284

Closed ElianFabian closed 2 months ago

ElianFabian commented 2 months ago

I think it would be helpful to have a way of getting a list of all the currently available services with the Backstack, similar to GlobalServices.services().

A use case for this could be implementing custom callbacks for services, similar to ScopedServices.Registered.

For example, let's say I want a service to be notified when Activity.onNewIntent() is called, It would look like this:

override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)

    backstack.services().forEach { (key, service) ->
       if (service is OnNewIntentListener) {
           service.onNewIntent(intent)
       }
    }
}

I'm not sure if there would be a better way to achieve a similar result with ease.

Zhuinden commented 2 months ago

Good idea. It could be similar to findScopesForKey()

https://github.com/Zhuinden/simple-stack/blob/528fb852a7db94f54984bd6946ef7a0296679d09/simple-stack/src/main/java/com/zhuinden/simplestack/Backstack.java#L834

However, it needs to consider that setParentServices() was added, allowing chaining Backstack instances hierarchically, in which case the lookup should be either scoped to be local (only include results from this backstack) or global (also include results of the parents).

Zhuinden commented 2 months ago

Haven't really had time to work on this but I hope to get it done by the end of this week at the latest.

Zhuinden commented 2 months ago

Released in 2.9.0 (it's called findServices()).

Zhuinden commented 2 months ago

https://github.com/Zhuinden/simple-stack/issues/277 vaguely also applies.