Ashera provides formatting framework which helps to format the objects on native side onto to the use interface without a round trip to the webview.
The objects are stored in particular scope on the native side. Let us take a example, where a list is stored on the native side. If we want to display the sum of an attribute in a list, we need to request the entire list into the webview and loop over it do the sum and update the total on the textView. Instead simple expression in modelPojoToUi will allow you to format the result of the expression to a string.
<TextView
style="@style/h2_bold_black"
modelPojoToUi="text = total(.) from allocatedItems->view"
modelPojoToUiParams="path:memPrice;numberFormat:##.00"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="10.23" />
The above example shows a scenario where textView is displaying the total of allocatedItems list which is stored in a view. The total is formatted to 2 decimal format. total(.) specifies that when a modelPojoToUi is done, it needs to send the result to total method with params, path as memPrice and numberFormat as ##.00.
The formatting framework handles the total call, loops over the object and sums the memPrice and finally formats it and returns the result. The result is set on the TextView. This helps to achieve native performance rather than being handled by the webview.
The list of methods available in formatting framework are give below:
Name | Params | Description |
---|---|---|
toString | - | Converts object attribute to string |
not | - | Boolean object is negated |
listToString | separator | list of strings to separator separated string. Default is comma |
concat | fields, separator | concat list of fields using separator specified. Default separator is whitespace |
size | - | Count of list object. |
visible | - | If the target object is list, size > 0. If the object is string, string is checked if it is not empty. If the object is boolean, the value must be true. In all these cases, the widget is visible else it is hidden |
gone | - | Opposite of visible. |
getDescFromModel | scope, id, value | Displays the description for the id passed in. The scope contains the list though which you loop to match the id and return value in the model. |
formatString | format, fields | Display the string using String.format method with the values retirved by path specified in the comma separated field list. |
getDescFromRes | separator, entries, values | Multi selected list is converted to string with separator. The values array is used to match the values in list so that the entries array description are displayed. |
multiply | op1, op2 | Multiples model retrieved by op1 and op2. |
total | path | Sum of all list retrieved by path specified. |
baseElapsedTimeInMillis | defaultValue, allowNegativeValues | Used in chronometer to calculate the baseElapsedTimeInMillis. If the object retrieved is null, return defaultValues. If allowNegativeValues is false, the value if negative is zero. |
getFileAsset | - | Get the file asset as a string. |
Let us take an example of displaying the size of list and sum of an attribute in the list.