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

Dynamic Properties #47

Closed
jeffscaturro-aka opened this issue Apr 19, 2021 · 3 comments · Fixed by #49
Closed

Dynamic Properties #47

jeffscaturro-aka opened this issue Apr 19, 2021 · 3 comments · Fixed by #49

Comments

@jeffscaturro-aka
Copy link

It would be nice to have dynamic properties that become visible or hidden based on another property.

For instance, you can have a type property that drives subsequent properties that are visible, such as an additional label only present if that type were selected.

@erickzanardo
Copy link
Member

Hey, thanks for the suggestion.

I think I got the idea but I am not 100% sure.

Could elaborate more? Would be cool if you could put an example of what you would expect the API would look like.

@jeffscaturro-aka
Copy link
Author

jeffscaturro-aka commented Apr 20, 2021

Sure!

We add options to the DashbookContext and store them in a map of properties: https://github.com/erickzanardo/dashbook/blob/f2581ce298242a07302482b4eccb8045197ce2a4/lib/src/story.dart#L26-L38

It would be valuable to add some logic conditional on another property's key/value pair. Here is some high-level pseudocode that I'm sure would bring in some edge-cases or handling that needs to be accounted for somewhere else, but it provides the gist of the ask here.

class DashbookContext {
  /// Contain the current BoxConstraints of the area available for the Chapter
  BoxConstraints? constraints;
  Map<String, Property> properties = {};

  String textProperty(
    String name,
    String defaultValue, {
    Map<String, dynamic>? removeWhen,
  }) {
    // FIXME: Would have to check all the key/value pairs.
    if (removeWhen != null &&
        properties[removeWhen.keys.first]?.getValue() ==
            removeWhen.values.first) {
      properties.remove(name);
      // FIXME: Verify what we'd want to return.
      return '';
    }

    if (properties.containsKey(name)) {
      return properties[name]!.getValue();
    } else {
      final property = Property<String>(name, defaultValue);
      properties[name] = property;

      return property.getValue();
    }
  }
...

This does work, but currently it doesn't until you rebuild the Properties tab.

This would then enable consumers to do something like:

...
text: dashbookContext.textProperty(
    'Error Text',
    'Oops, something went wrong!',
    removeWhen: {
        'state': 'success',
    },
  ),
...

Where the user would only see the "Error Text" property (on the Properties tab) if the Property who's key "state" and current value is not success. This would hide properties that may confuse or pollute the list of properties when not applicable.

I'm not saying this approach is the best way to accomplish this as I haven't looked too in-depth into the source code, but hopefully it provides more context and an example of the intent behind this ask.

Let me know if not.

@erickzanardo
Copy link
Member

Got it @jeffscaturro-aka, thanks for the further explanation, I made some adaptations to your suggestion and just submitted #49

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

Successfully merging a pull request may close this issue.

2 participants