Custom Condition
Conditions, as seen in an earlier article help in switching between two or more possibilities for an Action, Layout, Route or a Content Block. They help in managing more complex scenarios by introducing branch-points at the right places.
Conditions can be defined on the CMS, via a schema, and have its counterpart on the Flutter side. Note that the CMS side is purely declarative and helps in configuring the condition. The Flutter side is where we do the actual implementation for the condition.
CMS is for configuration, Flutter is for implementation
Vyuh allows you to create custom conditions and have them exported from a
feature, inside the FeatureDescriptor
, both for the CMS side and for the
Flutter side.
A Condition for “Part of a Day”
In this guide, let’s go about creating a custom condition that returns the current part of a day. As per general understanding, we will adopt the following parts in our custom condition:
morning
: 5 am to 12 pmafternoon
: 12 pm to 5 pmevening
: 5 pm to 9 pmnight
: 9 pm to 5 am.
Arguably, the time ranges for some of these parts can be tweaked a bit but we will go with these for now.
1. Define the schema for the condition
This condition is dependent on the current time and not something we can
statically define. Hence, the schema that we expose on the CMS is super minimal
and only contains a title
for easy identification. Many of the custom
conditions you may define in the future could have such a simple schema.
Below is the Sanity schema we will use.
Note that we have deliberately set the title
field to readOnly
along
with an initialValue
.
2. Export the condition schema
The schema on its own will not show up inside the CMS until it is exported in
our feature. We do this with the FeatureDescriptor
inside the index.ts
file
for the feature.
At the top level where the Sanity Studio is defined, we have already included the misc feature in our list of features. Thus the part-of-day condition will now be visible when you try associating this condition for, say, a conditional-block.
3. Create the Flutter counterpart
The condition equivalent on the Flutter side is where we bring it to life. This
is done by extending the ConditionConfiguration
and implementing its
execute()
method.
The logic which we described earlier in the article is now being used to
determine the part of the day. The typeDescriptor
field helps in defining
the registry entry for the content deserializer, which we will use in when
exporting the feature.
4. Export inside the feature
Just like we did for the schema, we include the condition with the
FeatureDescriptor
for the Flutter side as well. This is where the
typeDescriptor
comes in handy.
5. Apply the condition to a Content Block
Finally, we get to use our condition!
Let’s create a Conditional Block in our CMS and define the content items to show for the different conditions. We have created simple Card items with the title mentioning the part of the day.
And here is the App in action, showing the appropriate greeting. We are just shy
of 12pm by 18 minutes, which is why its still morning
.
Summary
Custom conditions allow extending the framework to add App specific conditional
logic. This can be done by creating a schema for the CMS and then its
counterpart in Flutter. In both places, we export the custom condition inside
the FeatureDescriptor
.
In this guide, we saw an example of using it for a simple Conditional block showing different Cards depending on the “part of the day”. This can also be applied to more complex scenarios such as:
- changing the theme of the App as the day progresses
- switching Layouts of content items, say between morning and evening
- invoking different Actions
- switching between different pages based on the part of the day