Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Jim Simon | 3089686 | 2015-07-24 10:53:34 -0700 | [diff] [blame] | 5 | library stocks; |
| 6 | |
Hixie | a3ae46b | 2015-09-14 13:35:05 -0700 | [diff] [blame] | 7 | import 'dart:async'; |
Ian Hickson | 5494323 | 2016-01-19 09:31:57 -0800 | [diff] [blame] | 8 | import 'dart:collection'; |
Jim Simon | 3089686 | 2015-07-24 10:53:34 -0700 | [diff] [blame] | 9 | import 'dart:math' as math; |
Adam Barth | ecce1eb | 2015-10-09 20:55:54 -0700 | [diff] [blame] | 10 | import 'dart:ui' as ui; |
Jim Simon | 3089686 | 2015-07-24 10:53:34 -0700 | [diff] [blame] | 11 | |
Adam Barth | 65eba90 | 2015-10-09 20:44:52 -0700 | [diff] [blame] | 12 | import 'package:flutter/gestures.dart'; |
| 13 | import 'package:flutter/material.dart'; |
| 14 | import 'package:flutter/painting.dart'; |
Hixie | 1f40d96 | 2015-10-15 11:07:46 -0700 | [diff] [blame] | 15 | import 'package:flutter/rendering.dart'; |
Hixie | e249c0a | 2015-12-03 14:28:27 -0800 | [diff] [blame] | 16 | import 'package:flutter/scheduler.dart'; |
Jason Simmons | e08d446 | 2015-12-03 15:03:19 -0800 | [diff] [blame] | 17 | import 'package:intl/intl.dart'; |
Jim Simon | 3089686 | 2015-07-24 10:53:34 -0700 | [diff] [blame] | 18 | |
Hixie | db63850 | 2015-07-24 14:42:36 -0700 | [diff] [blame] | 19 | import 'stock_data.dart'; |
Jason Simmons | e08d446 | 2015-12-03 15:03:19 -0800 | [diff] [blame] | 20 | import 'i18n/stock_messages_all.dart'; |
Hixie | db63850 | 2015-07-24 14:42:36 -0700 | [diff] [blame] | 21 | |
Jim Simon | 3089686 | 2015-07-24 10:53:34 -0700 | [diff] [blame] | 22 | part 'stock_arrow.dart'; |
Jim Simon | 3089686 | 2015-07-24 10:53:34 -0700 | [diff] [blame] | 23 | part 'stock_home.dart'; |
| 24 | part 'stock_list.dart'; |
| 25 | part 'stock_menu.dart'; |
| 26 | part 'stock_row.dart'; |
| 27 | part 'stock_settings.dart'; |
Jason Simmons | e08d446 | 2015-12-03 15:03:19 -0800 | [diff] [blame] | 28 | part 'stock_strings.dart'; |
Hixie | f2b7dd6 | 2015-09-29 09:37:53 -0700 | [diff] [blame] | 29 | part 'stock_symbol_viewer.dart'; |
Jim Simon | 3089686 | 2015-07-24 10:53:34 -0700 | [diff] [blame] | 30 | part 'stock_types.dart'; |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 31 | |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 32 | class StocksApp extends StatefulComponent { |
| 33 | StocksAppState createState() => new StocksAppState(); |
| 34 | } |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 35 | |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 36 | class StocksAppState extends State<StocksApp> { |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 37 | |
Hixie | f2b7dd6 | 2015-09-29 09:37:53 -0700 | [diff] [blame] | 38 | final Map<String, Stock> _stocks = <String, Stock>{}; |
| 39 | final List<String> _symbols = <String>[]; |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 40 | |
Ian Hickson | 34cfb60 | 2016-01-10 00:36:01 -0800 | [diff] [blame] | 41 | StockConfiguration _configuration = new StockConfiguration( |
| 42 | stockMode: StockMode.optimistic, |
| 43 | backupMode: BackupMode.enabled, |
Ian Hickson | 48e1350 | 2016-01-10 15:43:23 -0800 | [diff] [blame] | 44 | debugShowGrid: false, |
| 45 | debugShowSizes: false, |
Hixie | 28a1788 | 2015-12-16 11:11:18 -0800 | [diff] [blame] | 46 | showPerformanceOverlay: false, |
| 47 | showSemanticsDebugger: false |
Ian Hickson | 34cfb60 | 2016-01-10 00:36:01 -0800 | [diff] [blame] | 48 | ); |
| 49 | |
Adam Barth | 347bd25 | 2015-09-30 13:15:46 -0700 | [diff] [blame] | 50 | void initState() { |
| 51 | super.initState(); |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 52 | new StockDataFetcher((StockData data) { |
| 53 | setState(() { |
Hixie | f2b7dd6 | 2015-09-29 09:37:53 -0700 | [diff] [blame] | 54 | data.appendTo(_stocks, _symbols); |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 55 | }); |
| 56 | }); |
| 57 | } |
| 58 | |
Ian Hickson | 34cfb60 | 2016-01-10 00:36:01 -0800 | [diff] [blame] | 59 | void configurationUpdater(StockConfiguration value) { |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 60 | setState(() { |
Ian Hickson | 34cfb60 | 2016-01-10 00:36:01 -0800 | [diff] [blame] | 61 | _configuration = value; |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 62 | }); |
| 63 | } |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 64 | |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 65 | ThemeData get theme { |
Ian Hickson | 34cfb60 | 2016-01-10 00:36:01 -0800 | [diff] [blame] | 66 | switch (_configuration.stockMode) { |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 67 | case StockMode.optimistic: |
| 68 | return new ThemeData( |
| 69 | brightness: ThemeBrightness.light, |
| 70 | primarySwatch: Colors.purple |
| 71 | ); |
| 72 | case StockMode.pessimistic: |
| 73 | return new ThemeData( |
| 74 | brightness: ThemeBrightness.dark, |
| 75 | accentColor: Colors.redAccent[200] |
| 76 | ); |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 77 | } |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 78 | } |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 79 | |
Collin Jackson | d05c564 | 2015-12-07 15:39:16 -0800 | [diff] [blame] | 80 | Route _getRoute(RouteSettings settings) { |
Hixie | df07a69 | 2015-12-03 12:41:48 -0800 | [diff] [blame] | 81 | List<String> path = settings.name.split('/'); |
Hixie | f2b7dd6 | 2015-09-29 09:37:53 -0700 | [diff] [blame] | 82 | if (path[0] != '') |
| 83 | return null; |
| 84 | if (path[1] == 'stock') { |
| 85 | if (path.length != 3) |
| 86 | return null; |
Hixie | df07a69 | 2015-12-03 12:41:48 -0800 | [diff] [blame] | 87 | if (_stocks.containsKey(path[2])) { |
| 88 | return new MaterialPageRoute( |
| 89 | settings: settings, |
| 90 | builder: (BuildContext context) => new StockSymbolPage(stock: _stocks[path[2]]) |
| 91 | ); |
| 92 | } |
Hixie | f2b7dd6 | 2015-09-29 09:37:53 -0700 | [diff] [blame] | 93 | } |
| 94 | return null; |
| 95 | } |
| 96 | |
Seth Ladd | 2666628 | 2015-12-08 14:16:14 -0800 | [diff] [blame] | 97 | Future<LocaleQueryData> _onLocaleChanged(ui.Locale locale) async { |
Jason Simmons | 9693cd5 | 2015-12-07 11:19:15 -0800 | [diff] [blame] | 98 | String localeString = locale.toString(); |
Seth Ladd | 2666628 | 2015-12-08 14:16:14 -0800 | [diff] [blame] | 99 | await initializeMessages(localeString); |
| 100 | Intl.defaultLocale = localeString; |
| 101 | return StockStrings.instance; |
Jason Simmons | 9693cd5 | 2015-12-07 11:19:15 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 104 | Widget build(BuildContext context) { |
Ian Hickson | 48e1350 | 2016-01-10 15:43:23 -0800 | [diff] [blame] | 105 | assert(() { |
| 106 | debugPaintSizeEnabled = _configuration.debugShowSizes; |
| 107 | return true; |
| 108 | }); |
Adam Barth | db3b9e8 | 2015-10-09 10:19:35 -0700 | [diff] [blame] | 109 | return new MaterialApp( |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 110 | title: 'Stocks', |
| 111 | theme: theme, |
Ian Hickson | 48e1350 | 2016-01-10 15:43:23 -0800 | [diff] [blame] | 112 | debugShowMaterialGrid: _configuration.debugShowGrid, |
Ian Hickson | 88c43c3 | 2016-01-10 23:39:20 -0800 | [diff] [blame] | 113 | showPerformanceOverlay: _configuration.showPerformanceOverlay, |
Hixie | 28a1788 | 2015-12-16 11:11:18 -0800 | [diff] [blame] | 114 | showSemanticsDebugger: _configuration.showSemanticsDebugger, |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 115 | routes: <String, RouteBuilder>{ |
Ian Hickson | 34cfb60 | 2016-01-10 00:36:01 -0800 | [diff] [blame] | 116 | '/': (RouteArguments args) => new StockHome(_stocks, _symbols, _configuration, configurationUpdater), |
| 117 | '/settings': (RouteArguments args) => new StockSettings(_configuration, configurationUpdater) |
Hixie | f2b7dd6 | 2015-09-29 09:37:53 -0700 | [diff] [blame] | 118 | }, |
Jason Simmons | 9693cd5 | 2015-12-07 11:19:15 -0800 | [diff] [blame] | 119 | onGenerateRoute: _getRoute, |
| 120 | onLocaleChanged: _onLocaleChanged |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 121 | ); |
| 122 | } |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void main() { |
Jason Simmons | 9693cd5 | 2015-12-07 11:19:15 -0800 | [diff] [blame] | 126 | runApp(new StocksApp()); |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 127 | } |