stock_settings.dart 3.72 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
part of stocks;
6 7 8 9 10 11 12

typedef void SettingsUpdater({
  StockMode optimism,
  BackupMode backup
});

class StockSettings extends StatefulComponent {
13
  const StockSettings(this.navigator, this.optimism, this.backup, this.updater);
14

15 16 17 18
  final NavigatorState navigator;
  final StockMode optimism;
  final BackupMode backup;
  final SettingsUpdater updater;
19

20 21
  StockSettingsState createState() => new StockSettingsState();
}
22

23
class StockSettingsState extends State<StockSettings> {
24
  void _handleOptimismChanged(bool value) {
25
    sendUpdates(value ? StockMode.optimistic : StockMode.pessimistic, config.backup);
26 27 28
  }

  void _handleBackupChanged(bool value) {
29
    sendUpdates(config.optimism, value ? BackupMode.enabled : BackupMode.disabled);
30 31
  }

Adam Barth's avatar
Adam Barth committed
32
  void _confirmOptimismChange() {
33
    switch (config.optimism) {
34 35 36 37
      case StockMode.optimistic:
        _handleOptimismChanged(false);
        break;
      case StockMode.pessimistic:
Adam Barth's avatar
Adam Barth committed
38 39 40
        showDialog(
          context: context,
          child: new Dialog(
41 42
            title: new Text("Change mode?"),
            content: new Text("Optimistic mode means everything is awesome. Are you sure you can handle that?"),
43
            onDismiss: () {
Adam Barth's avatar
Adam Barth committed
44
              config.navigator.pop(false);
45
            },
Hixie's avatar
Hixie committed
46
            actions: <Widget>[
47 48
              new FlatButton(
                child: new Text('NO THANKS'),
49
                onPressed: () {
Adam Barth's avatar
Adam Barth committed
50
                  config.navigator.pop(false);
51
                }
52 53 54 55
              ),
              new FlatButton(
                child: new Text('AGREE'),
                onPressed: () {
Adam Barth's avatar
Adam Barth committed
56
                  config.navigator.pop(true);
57 58 59
                }
              ),
            ]
Adam Barth's avatar
Adam Barth committed
60 61
          )
        ).then(_handleOptimismChanged);
62 63 64 65
        break;
    }
  }

66 67 68
  void sendUpdates(StockMode optimism, BackupMode backup) {
    if (config.updater != null)
      config.updater(
69 70 71 72 73
        optimism: optimism,
        backup: backup
      );
  }

74
  Widget buildToolBar(BuildContext context) {
75 76 77
    return new ToolBar(
      left: new IconButton(
        icon: 'navigation/arrow_back',
78 79
        onPressed: config.navigator.pop
      ),
80 81 82 83
      center: new Text('Settings')
    );
  }

84
  Widget buildSettingsPane(BuildContext context) {
85 86
    // TODO(ianh): Once we have the gesture API hooked up, fix https://github.com/domokit/mojo/issues/281
    // (whereby tapping the widgets below causes both the widget and the menu item to fire their callbacks)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    return new ScrollableViewport(
      child: new Container(
        padding: const EdgeDims.symmetric(vertical: 20.0),
        child: new BlockBody(<Widget>[
          new DrawerItem(
            icon: 'action/thumb_up',
            onPressed: () => _confirmOptimismChange(),
            child: new Row(<Widget>[
              new Flexible(child: new Text('Everything is awesome')),
              new Checkbox(
                value: config.optimism == StockMode.optimistic,
                onChanged: (bool value) => _confirmOptimismChange()
              ),
            ])
          ),
          new DrawerItem(
            icon: 'action/backup',
            onPressed: () { _handleBackupChanged(!(config.backup == BackupMode.enabled)); },
            child: new Row(<Widget>[
              new Flexible(child: new Text('Back up stock list to the cloud')),
              new Switch(
                value: config.backup == BackupMode.enabled,
                onChanged: _handleBackupChanged
              ),
            ])
          ),
        ])
114 115 116 117
      )
    );
  }

118
  Widget build(BuildContext context) {
119
    return new Scaffold(
Adam Barth's avatar
Adam Barth committed
120
      toolBar: buildToolBar(context),
121
      body: buildSettingsPane(context)
122
    );
123 124
  }
}