stock_home.dart 8.93 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

typedef void ModeUpdater(StockMode mode);

9 10
enum StockHomeTab { market, portfolio }

Matt Perry's avatar
Matt Perry committed
11
class StockHome extends StatefulComponent {
12
  const StockHome(this.stocks, this.symbols, this.configuration, this.updater);
13

Hixie's avatar
Hixie committed
14 15
  final Map<String, Stock> stocks;
  final List<String> symbols;
16 17
  final StockConfiguration configuration;
  final ValueChanged<StockConfiguration> updater;
18

19 20 21 22
  StockHomeState createState() => new StockHomeState();
}

class StockHomeState extends State<StockHome> {
23

24
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
25 26
  bool _isSearching = false;
  String _searchQuery;
27

28
  void _handleSearchBegin() {
Hixie's avatar
Hixie committed
29 30
    ModalRoute.of(context).addLocalHistoryEntry(new LocalHistoryEntry(
      onRemove: () {
Adam Barth's avatar
Adam Barth committed
31 32 33 34 35 36
        setState(() {
          _isSearching = false;
          _searchQuery = null;
        });
      }
    ));
37 38 39 40 41 42
    setState(() {
      _isSearching = true;
    });
  }

  void _handleSearchEnd() {
Hixie's avatar
Hixie committed
43
    Navigator.pop(context);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  }

  void _handleSearchQueryChanged(String query) {
    setState(() {
      _searchQuery = query;
    });
  }

  bool _autorefresh = false;
  void _handleAutorefreshChanged(bool value) {
    setState(() {
      _autorefresh = value;
    });
  }

Adam Barth's avatar
Adam Barth committed
59
  void _handleStockModeChange(StockMode value) {
60 61
    if (config.updater != null)
      config.updater(config.configuration.copyWith(stockMode: value));
62 63
  }

64
  void _handleMenuShow() {
Adam Barth's avatar
Adam Barth committed
65 66
    showStockMenu(
      context: context,
67 68 69 70 71
      autorefresh: _autorefresh,
      onAutorefreshChanged: _handleAutorefreshChanged
    );
  }

72 73
  Widget _buildDrawer(BuildContext context) {
    return new Drawer(
74
      child: new Block(children: <Widget>[
75
        new DrawerHeader(child: new Text('Stocks')),
76 77 78
        new DrawerItem(
          icon: 'action/assessment',
          selected: true,
79 80
          child: new Text('Stock List')
        ),
81 82
        new DrawerItem(
          icon: 'action/account_balance',
83
          onPressed: () {
Adam Barth's avatar
Adam Barth committed
84 85 86
            showDialog(
              context: context,
              child: new Dialog(
87 88
                title: new Text('Not Implemented'),
                content: new Text('This feature has not yet been implemented.'),
Hixie's avatar
Hixie committed
89
                actions: <Widget>[
90 91
                  new FlatButton(
                    onPressed: () {
Hixie's avatar
Hixie committed
92
                      Navigator.pop(context, false);
Hixie's avatar
Hixie committed
93 94
                    },
                    child: new Text('USE IT')
95 96 97
                  ),
                  new FlatButton(
                    onPressed: () {
Hixie's avatar
Hixie committed
98
                      Navigator.pop(context, false);
Hixie's avatar
Hixie committed
99 100
                    },
                    child: new Text('OH WELL')
101 102
                  ),
                ]
Adam Barth's avatar
Adam Barth committed
103 104
              )
            );
105
          },
106 107
          child: new Text('Account Balance')
        ),
108 109
        new DrawerItem(
          icon: 'device/dvr',
Hixie's avatar
Hixie committed
110 111 112 113 114 115 116 117 118 119
          onPressed: () {
            try {
              debugDumpApp();
              debugDumpRenderTree();
              debugDumpLayerTree();
              debugDumpSemanticsTree();
            } catch (e, stack) {
              debugPrint('Exception while dumping app:\n$e\n$stack');
            }
          },
120 121
          child: new Text('Dump App to Console')
        ),
122 123 124 125
        new DrawerDivider(),
        new DrawerItem(
          icon: 'action/thumb_up',
          onPressed: () => _handleStockModeChange(StockMode.optimistic),
126 127 128 129 130 131
          child: new Row(
            children: <Widget>[
              new Flexible(child: new Text('Optimistic')),
              new Radio<StockMode>(value: StockMode.optimistic, groupValue: config.configuration.stockMode, onChanged: _handleStockModeChange)
            ]
          )
132
        ),
133 134 135
        new DrawerItem(
          icon: 'action/thumb_down',
          onPressed: () => _handleStockModeChange(StockMode.pessimistic),
136 137 138 139 140 141
          child: new Row(
            children: <Widget>[
              new Flexible(child: new Text('Pessimistic')),
              new Radio<StockMode>(value: StockMode.pessimistic, groupValue: config.configuration.stockMode, onChanged: _handleStockModeChange)
            ]
          )
142
        ),
143 144 145 146
        new DrawerDivider(),
        new DrawerItem(
          icon: 'action/settings',
          onPressed: _handleShowSettings,
147
          child: new Text('Settings')),
148 149
        new DrawerItem(
          icon: 'action/help',
150
          child: new Text('Help & Feedback'))
151
      ])
152 153 154
    );
  }

Adam Barth's avatar
Adam Barth committed
155
  void _handleShowSettings() {
Hixie's avatar
Hixie committed
156
    Navigator.popAndPushNamed(context, '/settings');
157 158 159 160
  }

  Widget buildToolBar() {
    return new ToolBar(
Hans Muller's avatar
Hans Muller committed
161
      elevation: 0,
162
      center: new Text(StockStrings.of(context).title()),
Hixie's avatar
Hixie committed
163
      right: <Widget>[
164 165
        new IconButton(
          icon: "action/search",
Hixie's avatar
Hixie committed
166 167
          onPressed: _handleSearchBegin,
          tooltip: 'Search'
168
        ),
169 170
        new IconButton(
          icon: "navigation/more_vert",
Hixie's avatar
Hixie committed
171 172
          onPressed: _handleMenuShow,
          tooltip: 'Show menu'
173
        )
174
      ],
175 176 177 178 179
      tabBar: new TabBar<StockHomeTab>(
        labels: <StockHomeTab, TabLabel>{
          StockHomeTab.market: new TabLabel(text: StockStrings.of(context).market()),
          StockHomeTab.portfolio: new TabLabel(text: StockStrings.of(context).portfolio())
        }
180
      )
181
    );
182 183
  }

Hixie's avatar
Hixie committed
184
  Iterable<Stock> _getStockList(Iterable<String> symbols) {
185 186
    return symbols.map((String symbol) => config.stocks[symbol])
        .where((Stock stock) => stock != null);
187 188 189 190 191 192
  }

  Iterable<Stock> _filterBySearchQuery(Iterable<Stock> stocks) {
    if (_searchQuery == null)
      return stocks;
    RegExp regexp = new RegExp(_searchQuery, caseSensitive: false);
Hixie's avatar
Hixie committed
193
    return stocks.where((Stock stock) => stock.symbol.contains(regexp));
194 195
  }

Hixie's avatar
Hixie committed
196 197 198 199 200
  void _buyStock(Stock stock, Key arrowKey) {
    setState(() {
      stock.percentChange = 100.0 * (1.0 / stock.lastSale);
      stock.lastSale += 1.0;
    });
201
    _scaffoldKey.currentState.showSnackBar(new SnackBar(
Hixie's avatar
Hixie committed
202 203 204 205 206 207 208
      content: new Text("Purchased ${stock.symbol} for ${stock.lastSale}"),
      actions: <SnackBarAction>[
        new SnackBarAction(label: "BUY MORE", onPressed: () { _buyStock(stock, arrowKey); })
      ]
    ));
  }

209
  Widget _buildStockList(BuildContext context, Iterable<Stock> stocks, StockHomeTab tab) {
210
    return new StockList(
211
      keySalt: tab,
212
      stocks: stocks.toList(),
Hixie's avatar
Hixie committed
213
      onAction: _buyStock,
Hixie's avatar
Hixie committed
214
      onOpen: (Stock stock, Key arrowKey) {
215
        Set<Key> mostValuableKeys = new HashSet<Key>();
Hixie's avatar
Hixie committed
216
        mostValuableKeys.add(arrowKey);
Hixie's avatar
Hixie committed
217
        Navigator.pushNamed(context, '/stock/${stock.symbol}', mostValuableKeys: mostValuableKeys);
218 219
      },
      onShow: (Stock stock, Key arrowKey) {
220
        _scaffoldKey.currentState.showBottomSheet((BuildContext context) => new StockSymbolBottomSheet(stock: stock));
221 222
      }
    );
223 224
  }

225 226 227 228 229 230 231
  Widget _buildStockTab(BuildContext context, StockHomeTab tab, List<String> stockSymbols) {
    return new Container(
      key: new ValueKey<StockHomeTab>(tab),
      child: _buildStockList(context, _filterBySearchQuery(_getStockList(stockSymbols)).toList(), tab)
    );
  }

Hixie's avatar
Hixie committed
232 233
  static const List<String> portfolioSymbols = const <String>["AAPL","FIZZ", "FIVE", "FLAT", "ZINC", "ZNGA"];

Eric Seidel's avatar
Eric Seidel committed
234
  static GlobalKey searchFieldKey = new GlobalKey();
Hixie's avatar
Hixie committed
235
  static GlobalKey companyNameKey = new GlobalKey();
Eric Seidel's avatar
Eric Seidel committed
236

237 238 239 240
  // TODO(abarth): Should we factor this into a SearchBar in the framework?
  Widget buildSearchBar() {
    return new ToolBar(
      left: new IconButton(
241
        icon: 'navigation/arrow_back',
Adam Barth's avatar
Adam Barth committed
242
        color: Theme.of(context).accentColor,
Hixie's avatar
Hixie committed
243 244
        onPressed: _handleSearchEnd,
        tooltip: 'Back'
245 246
      ),
      center: new Input(
Eric Seidel's avatar
Eric Seidel committed
247
        key: searchFieldKey,
248
        autofocus: true,
249
        hintText: 'Search stocks',
250 251
        onChanged: _handleSearchQueryChanged
      ),
252
      backgroundColor: Theme.of(context).canvasColor
253 254 255
    );
  }

Hixie's avatar
Hixie committed
256 257 258
  void _handleCreateCompany() {
    showModalBottomSheet(
      // TODO(ianh): Fill this out.
Adam Barth's avatar
Adam Barth committed
259
      context: context,
260
      builder: (BuildContext context) {
261 262 263 264
        return new Column(
          children: <Widget>[
            new Input(
              key: companyNameKey,
265
              autofocus: true,
266
              hintText: 'Company Name'
267 268 269
            ),
          ]
        );
270
      }
Matt Perry's avatar
Matt Perry committed
271
    );
272 273 274
  }

  Widget buildFloatingActionButton() {
275
    return new FloatingActionButton(
276
      child: new Icon(icon: 'content/add'),
277
      backgroundColor: Colors.redAccent[200],
Hixie's avatar
Hixie committed
278
      onPressed: _handleCreateCompany
279
    );
280 281
  }

282
  Widget build(BuildContext context) {
283 284
    return new TabBarSelection<StockHomeTab>(
      values: <StockHomeTab>[StockHomeTab.market, StockHomeTab.portfolio],
285 286 287 288 289
      child: new Scaffold(
        key: _scaffoldKey,
        toolBar: _isSearching ? buildSearchBar() : buildToolBar(),
        floatingActionButton: buildFloatingActionButton(),
        drawer: _buildDrawer(context),
Adam Barth's avatar
Adam Barth committed
290 291 292 293 294
        body: new TabBarView(
          children: <Widget>[
            _buildStockTab(context, StockHomeTab.market, config.symbols),
            _buildStockTab(context, StockHomeTab.portfolio, portfolioSymbols),
          ]
295
        )
296
      )
297
    );
298 299
  }
}