[story_shell] make example code use async bindings.

Change-Id: Icb075c8a988ad99d3bee45ed70b1f853cc920510
diff --git a/lib/story_shell/examples/example_manual_relationships/BUILD.gn b/lib/story_shell/examples/example_manual_relationships/BUILD.gn
index 2a8b9fb..0fc06cf 100644
--- a/lib/story_shell/examples/example_manual_relationships/BUILD.gn
+++ b/lib/story_shell/examples/example_manual_relationships/BUILD.gn
@@ -27,7 +27,8 @@
     "//third_party/dart-pkg/git/flutter/packages/flutter",
     "//topaz/public/dart/fidl",
     "//topaz/public/dart/fuchsia",
-    "//topaz/public/lib/app/dart",
-    "//topaz/public/lib/module_resolver/dart",
+    "//topaz/public/dart/fuchsia_logger",
+    "//topaz/public/dart/fuchsia_modular",
+    "//topaz/public/dart/fuchsia_services",
   ]
 }
diff --git a/lib/story_shell/examples/example_manual_relationships/lib/launch_copresent_button.dart b/lib/story_shell/examples/example_manual_relationships/lib/launch_copresent_button.dart
index 356b734..c6eba9c 100644
--- a/lib/story_shell/examples/example_manual_relationships/lib/launch_copresent_button.dart
+++ b/lib/story_shell/examples/example_manual_relationships/lib/launch_copresent_button.dart
@@ -1,36 +1,23 @@
 import 'dart:math' as math;
 
 import 'package:flutter/material.dart';
-import 'package:fidl_fuchsia_modular/fidl.dart';
+import 'package:fidl_fuchsia_modular/fidl_async.dart';
 
 import 'start_module_button.dart';
 
 /// Specify an emphasis and launch a copresented surface
 class CopresentLauncher extends StatefulWidget {
-  final ModuleContext _moduleContext;
-
   final GenerateChildId _generateChildId;
 
   /// CopresentLauncher
-  const CopresentLauncher(this._moduleContext, this._generateChildId, {Key key})
-      : super(key: key);
+  const CopresentLauncher(this._generateChildId, {Key key}) : super(key: key);
 
   @override
-  CopresentLauncherState createState() =>
-      CopresentLauncherState(_moduleContext, _generateChildId);
+  CopresentLauncherState createState() => CopresentLauncherState();
 }
 
 /// Copresent Launch State
 class CopresentLauncherState extends State<CopresentLauncher> {
-  final ModuleContext _moduleContext;
-
-  final GenerateChildId _generateChildId;
-
-  CopresentLauncherState(
-    this._moduleContext,
-    this._generateChildId,
-  ) : super();
-
   double _copresentEmphasisExp = 0.0;
 
   double get _emphasis =>
@@ -52,23 +39,21 @@
                   setState(() => _copresentEmphasisExp = value),
             ),
             StartModuleButton(
-              _moduleContext,
               SurfaceRelation(
                 emphasis: _emphasis,
                 arrangement: SurfaceArrangement.copresent,
               ),
               'Copresent',
-              _generateChildId,
+              widget._generateChildId,
             ),
             StartModuleButton(
-              _moduleContext,
               SurfaceRelation(
                 emphasis: _emphasis,
                 arrangement: SurfaceArrangement.copresent,
                 dependency: SurfaceDependency.dependent,
               ),
               'Dependent\nCopresent',
-              _generateChildId,
+              widget._generateChildId,
             ),
           ],
         ),
diff --git a/lib/story_shell/examples/example_manual_relationships/lib/main.dart b/lib/story_shell/examples/example_manual_relationships/lib/main.dart
index 82a8c17..186d80b 100644
--- a/lib/story_shell/examples/example_manual_relationships/lib/main.dart
+++ b/lib/story_shell/examples/example_manual_relationships/lib/main.dart
@@ -3,26 +3,18 @@
 // found in the LICENSE file.
 
 import 'dart:math' as math;
-import 'dart:typed_data';
 
-import 'package:fidl/fidl.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter/widgets.dart';
-import 'package:fidl_fuchsia_modular/fidl.dart';
-import 'package:fuchsia/fuchsia.dart';
-import 'package:lib.app.dart/app.dart';
-import 'package:lib.app.dart/logging.dart';
-import 'package:lib.module_resolver.dart/intent_builder.dart';
+import 'package:fidl_fuchsia_modular/fidl_async.dart';
+import 'package:fuchsia_logger/logger.dart';
+import 'package:fuchsia_modular/module.dart' as mod;
 
 import 'grouping.dart';
 import 'launch_copresent_button.dart';
 import 'start_module_button.dart';
 
-final StartupContext _context = StartupContext.fromStartupInfo();
-
 /// This is used for keeping the reference around.
-ModuleImpl _module = ModuleImpl();
-ModuleContextProxy _moduleContext = ModuleContextProxy();
 int _childId = 0;
 
 String _generateChildId() {
@@ -51,55 +43,7 @@
 
 /// Starts a predefined test container
 void startContainerInShell() {
-  IntentBuilder intentBuilder = IntentBuilder.handler(
-      'fuchsia-pkg://fuchsia.com/example_manual_relationships#meta/example_manual_relationships.cmx');
-
-  const List<double> leftRect = <double>[0.0, 0.0, 0.5, 1.0];
-  const List<double> trRect = <double>[0.5, 0.0, 0.5, 0.5];
-  const List<double> brRect = <double>[0.5, 0.5, 0.5, 0.5];
-  LayoutEntry left = LayoutEntry(
-      nodeName: 'left', rectangle: Float32List.fromList(leftRect));
-  LayoutEntry tr = LayoutEntry(
-      nodeName: 'top_right', rectangle: Float32List.fromList(trRect));
-  LayoutEntry br = LayoutEntry(
-      nodeName: 'bottom_right', rectangle: Float32List.fromList(brRect));
-  ContainerLayout main =
-      ContainerLayout(surfaces: <LayoutEntry>[left, tr, br]);
-  List<ContainerLayout> layouts = <ContainerLayout>[main];
-  ContainerRelationEntry rootLeft = ContainerRelationEntry(
-      nodeName: 'left',
-      parentNodeName: 'test',
-      relationship: SurfaceRelation());
-  ContainerRelationEntry rootTr = ContainerRelationEntry(
-      nodeName: 'top_right',
-      parentNodeName: 'test',
-      relationship: SurfaceRelation());
-  ContainerRelationEntry rootBr = ContainerRelationEntry(
-      nodeName: 'bottom_right',
-      parentNodeName: 'test',
-      relationship:
-          const SurfaceRelation(dependency: SurfaceDependency.dependent));
-  List<ContainerRelationEntry> relations = <ContainerRelationEntry>[
-    rootLeft,
-    rootTr,
-    rootBr
-  ];
-  ContainerNode leftNode =
-      ContainerNode(nodeName: 'left', intent: intentBuilder.intent);
-  ContainerNode trNode =
-      ContainerNode(nodeName: 'top_right', intent: intentBuilder.intent);
-  ContainerNode brNode =
-      ContainerNode(nodeName: 'bottom_right', intent: intentBuilder.intent);
-  List<ContainerNode> nodes = <ContainerNode>[leftNode, trNode, brNode];
-
-  _moduleContext.startContainerInShell(
-      'test',
-      const SurfaceRelation(
-          arrangement: SurfaceArrangement.sequential,
-          dependency: SurfaceDependency.none),
-      layouts,
-      relations,
-      nodes);
+  log.info('startContainerInShell is no longer supported.');
 }
 
 /// Launch a (prebaked) Container
@@ -187,29 +131,22 @@
                     padding: EdgeInsets.all(8.0),
                     child: RaisedButton(
                       child: Text('Close'),
-                      onPressed: () {
-                        // NOTE(mesch): There used to be code here that calls
-                        // ModuleContext.Done(), but that method no longer
-                        // exists.
-                        log.warning('Module done is no longer supported.');
-                      },
+                      onPressed: mod.Module().removeSelfFromStory,
                     ),
                   ),
                 ],
               ),
               Grouping(
                 children: <Widget>[
-                  CopresentLauncher(_moduleContext, _generateChildId),
+                  CopresentLauncher(_generateChildId),
                   const Divider(),
                   StartModuleButton(
-                    _moduleContext,
                     const SurfaceRelation(
                         arrangement: SurfaceArrangement.sequential),
                     'Sequential',
                     _generateChildId,
                   ),
                   StartModuleButton(
-                    _moduleContext,
                     const SurfaceRelation(
                         arrangement: SurfaceArrangement.ontop),
                     'On Top',
@@ -226,43 +163,14 @@
   }
 }
 
-/// Module related Services: Lifecycle and ModuleContext
-class ModuleImpl implements Lifecycle {
-  final LifecycleBinding _lifecycleBinding = LifecycleBinding();
-
-  ModuleImpl() {
-    log.info('ModuleImpl::initialize call');
-    connectToService(_context.environmentServices, _moduleContext.ctrl);
-  }
-
-  /// Bind an [InterfaceRequest] for a [Lifecycle] interface to this object.
-  void bindLifecycle(InterfaceRequest<Lifecycle> request) {
-    _lifecycleBinding.bind(this, request);
-  }
-
-  @override
-  void terminate() {
-    log.info('ModuleImpl::terminate call');
-    _moduleContext.ctrl.close();
-    _lifecycleBinding.close();
-    exit(0);
-  }
-}
-
 /// Entry point for this module.
 void main() {
   setupLogger(name: 'exampleManualRelationships');
 
-  /// Add [ModuleImpl] to this application's outgoing ServiceProvider.
-  _context.outgoingServices.addServiceForName(
-    (InterfaceRequest<Lifecycle> request) {
-      _module.bindLifecycle(request);
-    },
-    Lifecycle.$serviceName,
-  );
+  // Opt out of intent handling for this module
+  mod.Module().registerIntentHandler(mod.NoopIntentHandler());
 
-  Color randomColor =
-      Color(0xFF000000 + math.Random().nextInt(0xFFFFFF));
+  Color randomColor = Color(0xFF000000 + math.Random().nextInt(0xFFFFFF));
 
   runApp(MaterialApp(
     title: 'Manual Module',
diff --git a/lib/story_shell/examples/example_manual_relationships/lib/start_module_button.dart b/lib/story_shell/examples/example_manual_relationships/lib/start_module_button.dart
index c0c67b7..bc43b40 100644
--- a/lib/story_shell/examples/example_manual_relationships/lib/start_module_button.dart
+++ b/lib/story_shell/examples/example_manual_relationships/lib/start_module_button.dart
@@ -1,6 +1,9 @@
+import 'dart:async';
+
 import 'package:flutter/material.dart';
-import 'package:fidl_fuchsia_modular/fidl.dart';
-import 'package:lib.module_resolver.dart/intent_builder.dart';
+import 'package:fuchsia_modular/module.dart';
+import 'package:fidl_fuchsia_modular/fidl_async.dart' as fidl_mod;
+import 'package:fuchsia_logger/logger.dart';
 
 const String _kModuleUrl =
     'fuchsia-pkg://fuchsia.com/example_manual_relationships#meta/example_manual_relationships.cmx';
@@ -9,10 +12,8 @@
 
 /// Button widget to start a module
 class StartModuleButton extends StatelessWidget {
-  final ModuleContext _moduleContext;
-
   /// The relationship to introduce a new surface with
-  final SurfaceRelation _relation;
+  final fidl_mod.SurfaceRelation _relation;
 
   /// The display text for the relationship
   final String _display;
@@ -21,8 +22,7 @@
   final GenerateChildId _generateChildId;
 
   /// Construct a button [Widget] to add new surface with given relationship
-  const StartModuleButton(this._moduleContext, this._relation, this._display,
-      this._generateChildId);
+  const StartModuleButton(this._relation, this._display, this._generateChildId);
 
   @override
   Widget build(BuildContext context) {
@@ -33,28 +33,25 @@
           child: Text(_display),
         ),
         onPressed: () {
-          print(
-              'starting module with relation $_relation moduleContext $_moduleContext');
-          startChildModule(_moduleContext, _relation);
+          log.fine('starting module with relation $_relation');
+          _startChildModule(_relation).catchError((e) {
+            log.warning('Failed to start child module', e);
+          });
         },
       ),
     );
   }
 
   /// Starts a new module and returns its controller
-  ModuleController startChildModule(
-      ModuleContext moduleContext, SurfaceRelation relation) {
-    ModuleControllerProxy moduleController = ModuleControllerProxy();
-
+  Future<fidl_mod.ModuleControllerProxy> _startChildModule(
+      fidl_mod.SurfaceRelation relation) {
     String name = _generateChildId();
 
-    IntentBuilder intentBuilder = IntentBuilder.handler(_kModuleUrl);
-    moduleContext.addModuleToStory(
-        name,
-        intentBuilder.intent,
-        moduleController.ctrl.request(),
-        relation,
-        (StartModuleStatus status) {});
-    return moduleController;
+    final intent = Intent(action: '', handler: _kModuleUrl);
+    return Module().addModuleToStory(
+      name: name,
+      intent: intent,
+      surfaceRelation: relation,
+    );
   }
 }