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

Extending PreferenceBuilder to multiple values #6

Open
degloff opened this issue Mar 15, 2020 · 2 comments
Open

Extending PreferenceBuilder to multiple values #6

degloff opened this issue Mar 15, 2020 · 2 comments
Assignees

Comments

@degloff
Copy link

degloff commented Mar 15, 2020

I like the PreferenceBuilder a lot. It would be even better if there would be a MultiPreferenceBuilder that allows to listen on multiple changing values, probably with Rx.combineLatest. It is a bit tedious to implement, as we would need to have it for 2, 3, 4, etc. Would it be possible to add such a feature?

@roughike
Copy link
Owner

Great idea! I'm working on it.

I'll likely have support for listening to 3 preferences. If you need more than that, you should maybe rethink your code. That said, I'll make it easy to extend to more than 3 preferences.

@roughike roughike self-assigned this Jan 17, 2021
@roughike
Copy link
Owner

roughike commented Jan 19, 2021

@TheBestMoshe & @degloff this is the current plan.

Let's say we have these three preferences:

PreferenceBuilder3<String, int, bool>(
  preferences.getString('name'),
  preferences.getInt('age'),
  preferences.getBool('isAwesome'),
  builder: (BuildContext context, String name, int age, bool isAwesome) {
    // Do something with "name", "age", and "isAwesome" typed values.
  },
)

It works a bit like combineLatest() from rxdart/rxjava/rx*, but without the rxdart dependency.

The order of the arguments in the builder method is the same order as they were passed as arguments for the PreferenceBuilder3.
The generic type arguments (=<String, int, bool>), which can be inferred, must also be in the same order.

There's also a PreferenceBuilder2 for watching changes and rebuilding a widget based on two preference values.

I'm not making PreferenceBuilder4, PreferenceBuilder5, etc - I think at that point you probably want to rethink your code.
If you're sure about it, it's possible to make your own PreferenceBuilder4 (or 5, 6, 7, etc.) by extending PreferenceBuilderBase:

typedef PreferenceWidgetBuilder4<A, B, C, D> = Widget Function(
  BuildContext context,
  A a,
  B b,
  C c,
  D d,
);

class PreferenceBuilder4<A, B, C, D> extends PreferenceBuilderBase<dynamic> {
  PreferenceBuilder4(
    this.a,
    this.b,
    this.c, 
    this.d, {
    Key key,
    @required this.builder,
  }) : super(preferences: [a, b, c, d], key: key);

  final Preference<A> a;
  final Preference<B> b;
  final Preference<C> c;
  final Preference<D> d;
  final PreferenceWidgetBuilder4<A, B, C, D> builder;

  @override
  Widget build(BuildContext context, List<dynamic> values) {
    return builder(
      context,
      values[0] as A,
      values[1] as B,
      values[2] as C,
      values[3] as D,
    );
  }
}

I should have this on the master branch this week, then I'll do some testing and make a new release.

Let me know what you think!

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