Preparing for casting

Change-Id: Iff0e024e799aef7b783a2988badffa33b5f59ee4
diff --git a/examples/distro/app/src/flutter/lib/main.dart b/examples/distro/app/src/flutter/lib/main.dart
index 6772b09..c7fcffa 100644
--- a/examples/distro/app/src/flutter/lib/main.dart
+++ b/examples/distro/app/src/flutter/lib/main.dart
@@ -33,15 +33,44 @@
 
 class _BakuDistroState extends State<BakuDistro> {
   final Map<String, String> devices = {};
-  InputValue data = InputValue.empty;
+  InputValue _data = InputValue.empty;
+  String _castTargetName;
+
+  InputValue get data => _data;
+
+  void set data(final InputValue value) {
+    _data = value;
+    if (castTargetName != null) {
+      HostMessages.sendToHost('updateCast', getCastData());
+    }
+  }
+
+  String get castTargetName => _castTargetName;
+
+  void set castTargetName(final String value) {
+    if (_castTargetName != value) {
+      if (_castTargetName != null) {
+        HostMessages.sendToHost('terminateCast', castTargetName);
+      }
+
+      _castTargetName = value;
+
+      if (_castTargetName != null) {
+        HostMessages.sendToHost('initiateCast', getCastData());
+      }
+    }
+  }
 
   _BakuDistroState() {
     HostMessages.addMessageHandler('deviceOnline', _onDeviceOnline);
     HostMessages.addMessageHandler('deviceOffline', _onDeviceOffline);
   }
 
-  void castTo(final String name) {
-
+  String getCastData() {
+    return JSON.encode({
+      'name': castTargetName,
+      'data': data.toString()
+    });
   }
 
   @override
@@ -50,41 +79,50 @@
         new _Device(name, devices[name])).toList(growable: false);
     sortedDevices.sort((a, b) => a.description.compareTo(b.description));
 
+    final List<Widget> layout = [];
+
+    if (castTargetName == null) {
+      layout.add(new Padding(
+        padding: new EdgeInsets.all(8.0),
+        child: data.text.isEmpty?
+          new Text('No content', style:
+            new TextStyle(color: Theme.of(context).disabledColor)) :
+          new Text(data.text)
+      ));
+    }
+
+    layout.add(new Input(
+      value: data,
+      labelText: 'Text content',
+      onChanged: (value) => setState(() => data = value)
+    ));
+
+    if (castTargetName == null) {
+      layout.add(new MaterialList(
+        type: MaterialListType.oneLine,
+        children: sortedDevices.map((d) => new ListItem(
+          title: new Text(d.description),
+          onTap: () => castTargetName = d.name
+        ))
+      ));
+    } else {
+      layout.add(new RaisedButton(
+        child: new Text('Recall'),
+        onPressed: () => castTargetName = null
+      ));
+    }
+
     return new Scaffold(
       appBar: new AppBar(
         title: new Text('Baku Distro Example')
       ),
-      body: new Column(
-        children: <Widget>[
-          new Padding(
-            padding: new EdgeInsets.all(8.0),
-            child: data.text.isEmpty?
-              new Text('No content', style:
-                new TextStyle(color: Theme.of(context).disabledColor)) :
-              new Text(data.text)
-          ),
-          new Input(
-            value: data,
-            labelText: 'Text content',
-            onChanged: (value) => setState(() => data = value)
-          ),
-          new MaterialList(
-            type: MaterialListType.oneLine,
-            children: sortedDevices.map((d) => new ListItem(
-              title: new Text(d.description),
-              onTap: () => castTo(d.name)
-            ))
-          )
-        ]
-      )
+      body: new Column(children: layout)
     );
   }
 
   Future<String> _onDeviceOnline(final String json) async {
     final Map<String, dynamic> message = JSON.decode(json);
-    setState(() {
-      devices[message['name']] = message['description'];
-    });
+    setState(() => devices[message['name']] = message['description']);
 
     return null;
   }
@@ -92,6 +130,9 @@
   Future<String> _onDeviceOffline(final String name) async {
     setState(() {
       devices.remove(name);
+      if (castTargetName == name) {
+        castTargetName = null;
+      }
     });
     return null;
   }