Changes

Jump to navigation Jump to search
no edit summary
Line 229: Line 229:     
Quit the activity using the "Quit" button then quit the emulator, for example using the shortcut "ALT+Q".
 
Quit the activity using the "Quit" button then quit the emulator, for example using the shortcut "ALT+Q".
 +
 +
 +
= Step 5: Retrieve the context =
 +
 +
As we saw at step 2, when the activity is launch from the Journal, it retrieve a new parameter called "objectid". This id is the record identifier in the Datastore.
 +
 +
Let's now write the method "LoadFile" that we need to retrieve the content of the context.
 +
 +
Go back to MonoDevelop.
 +
Add the method "LoadFile" in the file "MainWindow.cs".
 +
 +
string LoadFile()
 +
{
 +
// 1) Get the Datastore object
 +
if (objectId == null || objectId.Length == 0)
 +
return "";
 +
DSObject result = Datastore.get(objectId);
 +
if (result == null)
 +
return "";
 +
 +
// 2) Load the included text file
 +
StreamReader sr = new StreamReader(result.FilePath);
 +
string text = sr.ReadLine();
 +
sr.Close();
 +
 +
// 3) Return text content
 +
return text;
 +
}
 +
 +
The first step in the method is to retrieve the object in the Datastore. We just need to call the "Datastore.get()" method in the Sugar library with the "objectId" as parameter. A test condition is used because the "objectid" is not set when the activity is launch out of the Journal.
 +
 +
The second step is to retrieve the full path from the file using the instance variable "FilePath". Then, we just do a file read using a standard StreamReader object. Note that we could also access to other properties (title, mime_type, preview) using the "result.Metadata.getItem()" method.
 +
 +
Finally, the content of the file read is returned.
 +
 +
Now, we will update the constructor of the window to take into account the reading of the context. We just initialize the entry field with the content of the file (commented in the source code below):
 +
 +
public MainWindow(string activityId, string bundleId) : base("Lab",activityId, bundleId)
 +
{
 +
this.SetDefaultSize(400, 400);
 +
this.Maximize();
 +
this.DeleteEvent += new DeleteEventHandler(OnMainWindowDelete);
 +
 +
VBox vbox=new VBox();
 +
vbox.BorderWidth = 8;
 +
Label _text = new Label("Hello Lab Activity");
 +
vbox.Add(_text);
 +
_entry = new Entry();
 +
_entry.Text = LoadFile();        // Updated
 +
vbox.Add(_entry);
 +
Button _button = new Button();
 +
_button.Label = "Quit";
 +
_button.Clicked += new EventHandler(OnClick);
 +
vbox.Add(_button);
 +
 +
 +
this.Add(vbox);
 +
 +
ShowAll();
 +
}
 +
 +
 +
Launch the build using "Project/Build Solution"
 +
Then, let's deploy the activity using the "./deploy" script. If asked, type the password "user".
 +
We're now starting Sugar emulator from the desktop.
 +
 +
 +
[[Image:Lab1_4.png|150px]]
 +
 +
 +
Launch the Journal.
 +
 +
 +
[[Image:Lab2_3.png|700px]]
 +
 +
 +
Start the activity from the Journal (CAUTION: you need to click on the entry before the last one because the last run has left an empty field).
 +
 +
 +
[[Image:Lab2_7.png|700px]]
 +
 +
 +
Here what you could see:
 +
 +
 +
[[Image:Lab2_8.png|700px]]
 +
 +
 +
 +
Our message is now correctly displayed into the entry field.
 +
 +
So, we've got now an application able to store and retrieve its context in the Journal.

Navigation menu