Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callbacks for AppLifecycleState's to StoreConnector #100

Closed
jimsimon opened this issue Jan 25, 2019 · 3 comments
Closed

Add callbacks for AppLifecycleState's to StoreConnector #100

jimsimon opened this issue Jan 25, 2019 · 3 comments

Comments

@jimsimon
Copy link

So far tying into application lifecycle events has been the only thing I've needed to create StatefulWidgets for. It'd be great if the StoreConnector allowed specifying suspend and resume functions that are called with the viewmodel passed to them (similar to onInitialBuild).

Something along the lines of...

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      widget.onResumed(latestValue);
    } else if (state == AppLifecycleState.inactive) {
      widget.onInactive(latestValue);
    } else if (state == AppLifecycleState.paused) {
      widget.onPaused(latestValue);
    } else if (state == AppLifecycleState.suspending) {
      widget.onSuspending(latestValue);
    } 
  }

Another option would be to just add didChangeAppLifecycleState and have it be passed the viewmodel:

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    widget.didChangeAppLifecycleState(state, latestValue);
  }

The main use case I have for using these events is for activating a lock screen when the user navigates away from the app. I suspect another use case might include refreshing data when the app is resumed. Right now I'm forced to pass the store down to any widget that needs to use it in didChangeAppLifecycleState it via constructor parameter or use StoreProvider.of(context). Ideally I'd be able to use my viewmodel instead.

@jimsimon
Copy link
Author

jimsimon commented Jan 26, 2019

If you're open to adding this feature I can open a PR for it. Feel free to close the issue if not and I'll just create a separate utility for it.

@brianegan
Copy link
Owner

brianegan commented Feb 9, 2019

Hey hey -- sorry, somehow missed this message chain :/

Hm, I can see how this is helpful, but I think we might want to be precise about the implementation.

For example, if every StoreConnector suddenly becomes a WidgetsBindingObserver, will that cause any perf issues? That might require registering a LOT more Widgets as WidgetsBindingObservers than need be.

I could see two options:

  1. Provide a specific storeconnector with this extra functionality
  2. Create a specific State class depending on the situation (if they pass through ANY of those callbacks, then create a State class that uses WidgetsBindingObserver, otherwise use the current implementation). The only tricky thing with that: What if someone goes from NOT providing a callback to providing a callback! The createState method would have already run and produced a State class that does not include the WidgetBindingsObserver stuff.

Thanks for the suggestion! What are your thoughts?

@brianegan
Copy link
Owner

Hey all, Gonna close this one out for now, but happy to reopen if more folks would like this functionality!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants