Prev: Accessing SMS
Editing Data
At this point we have a working SMS service. However, to modify replies or add new keywords, we must modify our script. This is not really all that user friendly. It would be much better to have a graphical user interface allowing us to edit the database. We would like to be able to modify replies for existing keywords, and to add new keywords and replies.
In m, it is fairly easy to construct such a user interface using the functions from module module ui:
First, the user should be able to choose one of the existing keywords to modify it, or pick an item <New> if she wants to add a new keyword/reply pair:
use ui, array
list=keys(db);
array.sort(list);
array.insert(list, 0, "<New>");
i=ui.list(list);
|
This code fragment, if executed on our database variable db, shows the following dialog:
 | |  |
| Series 60 sample screen | | UIQ sample screen |
Comments:
- We need two other modules, module ui and module array.
- The builtin keys function is called to obtain an array with the keys from db. We assign it to variable list.
- The array.sort() function sorts the list alphabetically.
- The array.insert() function inserts the string <New> at the beginning of the list.
- Eventually, the ui.list() function is called to display the dialog. This function returns when the user chooses an item, or cancels the dialog. We assign the result to variable i.
Once the user has made her choice, we can add a new key-value pair, or edit an existing one.
i=i[0];
if i=0 then // <New> was selected: add
f=ui.form(["Key":"","Text":"\n"]);
if f#null then
k=f["Key"];
if db[k]=null then db[k]=f["Text"]
else ui.error(k + " already exists") end
end
else // an existing keyword was selected: edit
k=list[i];
f=ui.form([k,"Text":db[k]+"\n"]);
if f#null then db[k]=f["Text"] end
end
|
A few explanations:
- If the user did not cancel the dialog, ui.list returns an array with the indices of the selected items. Since the call to ui.list did not specify multiple items to be selected, the index array i will always have a single index i[0].
- If the selected index is zero (i=0), the user has chosen <New> (since list[0]="<New>"), and we display a dialog to add a keyword and a reply.
Otherwise, the reply of the keyword at list[i] is to be edited, and we display the corresponding dialog.
- The ui.form() function takes an associative array of values to be edited, and displays a corresponding dialog. The keys of the array become labels in the dialog:
 | |  |
| Series 60 sample screen | | UIQ sample screen |
Inside the strings for the Text fields, you will note a \n. This is the code for a line break (the n stands for "newline"). Having a new line in the contents for a ui.form() field marks this field as multi-line, so the field can contain several lines, and can also scroll vertically.
- ui.form returns null if the dialog has been canceled. If this happens, we do nothing. Otherwise, f is an associative array containing the edited values: for instance, f["Text"] contains the edited reply text.
- Before adding a new pair with keyword k, we check whether it already exists. If it does, we display an error message with ui.error():
 | |  |
| Series 60 sample screen | | UIQ sample screen |
Next: Making it a Function© 2004-2011 airbit AG, CH-8008 Zürich
Document AB-M-TUT-887