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'; |
Jim Simon | 3089686 | 2015-07-24 10:53:34 -0700 | [diff] [blame] | 8 | |
Adam Barth | 65eba90 | 2015-10-09 20:44:52 -0700 | [diff] [blame] | 9 | import 'package:flutter/material.dart'; |
Hixie | a95c866 | 2016-02-12 11:41:05 -0800 | [diff] [blame] | 10 | import 'package:flutter/rendering.dart' show |
| 11 | debugPaintSizeEnabled, |
| 12 | debugPaintBaselinesEnabled, |
| 13 | debugPaintLayerBordersEnabled, |
| 14 | debugPaintPointersEnabled, |
| 15 | debugRepaintRainbowEnabled; |
Hans Muller | c3d56b1 | 2017-10-11 16:01:13 -0700 | [diff] [blame] | 16 | import 'package:flutter_localizations/flutter_localizations.dart'; |
Jim Simon | 3089686 | 2015-07-24 10:53:34 -0700 | [diff] [blame] | 17 | |
Ian Hickson | a94999b | 2016-02-10 18:11:03 -0800 | [diff] [blame] | 18 | import 'stock_data.dart'; |
| 19 | import 'stock_home.dart'; |
| 20 | import 'stock_settings.dart'; |
| 21 | import 'stock_strings.dart'; |
| 22 | import 'stock_symbol_viewer.dart'; |
| 23 | import 'stock_types.dart'; |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 24 | |
Hans Muller | 8c2c502 | 2017-08-23 17:07:59 -0700 | [diff] [blame] | 25 | class _StocksLocalizationsDelegate extends LocalizationsDelegate<StockStrings> { |
| 26 | @override |
| 27 | Future<StockStrings> load(Locale locale) => StockStrings.load(locale); |
| 28 | |
| 29 | @override |
Hans Muller | 796c543 | 2017-10-20 10:45:28 -0700 | [diff] [blame] | 30 | bool isSupported(Locale locale) => locale.languageCode == 'es' || locale.languageCode == 'en'; |
| 31 | |
| 32 | @override |
Hans Muller | 8c2c502 | 2017-08-23 17:07:59 -0700 | [diff] [blame] | 33 | bool shouldReload(_StocksLocalizationsDelegate old) => false; |
| 34 | } |
| 35 | |
Adam Barth | 95fc5ae | 2016-03-12 12:13:16 -0800 | [diff] [blame] | 36 | class StocksApp extends StatefulWidget { |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 37 | @override |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 38 | StocksAppState createState() => new StocksAppState(); |
| 39 | } |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 40 | |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 41 | class StocksAppState extends State<StocksApp> { |
Ian Hickson | 9adb4a7 | 2017-06-23 14:58:29 -0700 | [diff] [blame] | 42 | StockData stocks; |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 43 | |
Ian Hickson | 34cfb60 | 2016-01-10 00:36:01 -0800 | [diff] [blame] | 44 | StockConfiguration _configuration = new StockConfiguration( |
| 45 | stockMode: StockMode.optimistic, |
| 46 | backupMode: BackupMode.enabled, |
Ian Hickson | 48e1350 | 2016-01-10 15:43:23 -0800 | [diff] [blame] | 47 | debugShowGrid: false, |
| 48 | debugShowSizes: false, |
Hixie | a95c866 | 2016-02-12 11:41:05 -0800 | [diff] [blame] | 49 | debugShowBaselines: false, |
| 50 | debugShowLayers: false, |
| 51 | debugShowPointers: false, |
| 52 | debugShowRainbow: false, |
Hixie | 28a1788 | 2015-12-16 11:11:18 -0800 | [diff] [blame] | 53 | showPerformanceOverlay: false, |
| 54 | showSemanticsDebugger: false |
Ian Hickson | 34cfb60 | 2016-01-10 00:36:01 -0800 | [diff] [blame] | 55 | ); |
| 56 | |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 57 | @override |
Adam Barth | 347bd25 | 2015-09-30 13:15:46 -0700 | [diff] [blame] | 58 | void initState() { |
| 59 | super.initState(); |
Ian Hickson | 9adb4a7 | 2017-06-23 14:58:29 -0700 | [diff] [blame] | 60 | stocks = new StockData(); |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Ian Hickson | 34cfb60 | 2016-01-10 00:36:01 -0800 | [diff] [blame] | 63 | void configurationUpdater(StockConfiguration value) { |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 64 | setState(() { |
Ian Hickson | 34cfb60 | 2016-01-10 00:36:01 -0800 | [diff] [blame] | 65 | _configuration = value; |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 66 | }); |
| 67 | } |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 68 | |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 69 | ThemeData get theme { |
Ian Hickson | 34cfb60 | 2016-01-10 00:36:01 -0800 | [diff] [blame] | 70 | switch (_configuration.stockMode) { |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 71 | case StockMode.optimistic: |
| 72 | return new ThemeData( |
Todd Volkert | 7ac0ce7 | 2016-06-07 14:39:15 -0700 | [diff] [blame] | 73 | brightness: Brightness.light, |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 74 | primarySwatch: Colors.purple |
| 75 | ); |
| 76 | case StockMode.pessimistic: |
| 77 | return new ThemeData( |
Todd Volkert | 7ac0ce7 | 2016-06-07 14:39:15 -0700 | [diff] [blame] | 78 | brightness: Brightness.dark, |
Alexandre Ardhuin | 578ca0a | 2017-03-21 23:14:55 +0100 | [diff] [blame] | 79 | accentColor: Colors.redAccent |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 80 | ); |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 81 | } |
pq | f5a4e63 | 2016-06-14 15:19:14 -0700 | [diff] [blame] | 82 | assert(_configuration.stockMode != null); |
pq | 7a95548 | 2016-06-14 12:13:56 -0700 | [diff] [blame] | 83 | return null; |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 84 | } |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 85 | |
Ian Hickson | aba0379 | 2018-03-22 13:21:07 -0700 | [diff] [blame] | 86 | Route<dynamic> _getRoute(RouteSettings settings) { |
Ian Hickson | 9adb4a7 | 2017-06-23 14:58:29 -0700 | [diff] [blame] | 87 | // Routes, by convention, are split on slashes, like filesystem paths. |
Chris Bracken | 7a52fb6 | 2017-03-03 18:05:27 -0800 | [diff] [blame] | 88 | final List<String> path = settings.name.split('/'); |
Ian Hickson | 9adb4a7 | 2017-06-23 14:58:29 -0700 | [diff] [blame] | 89 | // We only support paths that start with a slash, so bail if |
| 90 | // the first component is not empty: |
Hixie | f2b7dd6 | 2015-09-29 09:37:53 -0700 | [diff] [blame] | 91 | if (path[0] != '') |
| 92 | return null; |
Ian Hickson | 9adb4a7 | 2017-06-23 14:58:29 -0700 | [diff] [blame] | 93 | // If the path is "/stock:..." then show a stock page for the |
| 94 | // specified stock symbol. |
| 95 | if (path[1].startsWith('stock:')) { |
| 96 | // We don't yet support subpages of a stock, so bail if there's |
| 97 | // any more path components. |
| 98 | if (path.length != 2) |
Hixie | f2b7dd6 | 2015-09-29 09:37:53 -0700 | [diff] [blame] | 99 | return null; |
Ian Hickson | 9adb4a7 | 2017-06-23 14:58:29 -0700 | [diff] [blame] | 100 | // Extract the symbol part of "stock:..." and return a route |
| 101 | // for that symbol. |
| 102 | final String symbol = path[1].substring(6); |
Ian Hickson | aba0379 | 2018-03-22 13:21:07 -0700 | [diff] [blame] | 103 | return new MaterialPageRoute<void>( |
Ian Hickson | 9adb4a7 | 2017-06-23 14:58:29 -0700 | [diff] [blame] | 104 | settings: settings, |
| 105 | builder: (BuildContext context) => new StockSymbolPage(symbol: symbol, stocks: stocks), |
| 106 | ); |
Hixie | f2b7dd6 | 2015-09-29 09:37:53 -0700 | [diff] [blame] | 107 | } |
Ian Hickson | 9adb4a7 | 2017-06-23 14:58:29 -0700 | [diff] [blame] | 108 | // The other paths we support are in the routes table. |
Hixie | f2b7dd6 | 2015-09-29 09:37:53 -0700 | [diff] [blame] | 109 | return null; |
| 110 | } |
| 111 | |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 112 | @override |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 113 | Widget build(BuildContext context) { |
Ian Hickson | 48e1350 | 2016-01-10 15:43:23 -0800 | [diff] [blame] | 114 | assert(() { |
| 115 | debugPaintSizeEnabled = _configuration.debugShowSizes; |
Hixie | a95c866 | 2016-02-12 11:41:05 -0800 | [diff] [blame] | 116 | debugPaintBaselinesEnabled = _configuration.debugShowBaselines; |
| 117 | debugPaintLayerBordersEnabled = _configuration.debugShowLayers; |
| 118 | debugPaintPointersEnabled = _configuration.debugShowPointers; |
| 119 | debugRepaintRainbowEnabled = _configuration.debugShowRainbow; |
Ian Hickson | 48e1350 | 2016-01-10 15:43:23 -0800 | [diff] [blame] | 120 | return true; |
Alexandre Ardhuin | 2958d7d | 2017-09-21 09:33:01 +0200 | [diff] [blame] | 121 | }()); |
Adam Barth | db3b9e8 | 2015-10-09 10:19:35 -0700 | [diff] [blame] | 122 | return new MaterialApp( |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 123 | title: 'Stocks', |
| 124 | theme: theme, |
Hans Muller | c3d56b1 | 2017-10-11 16:01:13 -0700 | [diff] [blame] | 125 | localizationsDelegates: <LocalizationsDelegate<dynamic>>[ |
Hans Muller | 8c2c502 | 2017-08-23 17:07:59 -0700 | [diff] [blame] | 126 | new _StocksLocalizationsDelegate(), |
Hans Muller | c3d56b1 | 2017-10-11 16:01:13 -0700 | [diff] [blame] | 127 | GlobalMaterialLocalizations.delegate, |
| 128 | GlobalWidgetsLocalizations.delegate, |
Hans Muller | 8c2c502 | 2017-08-23 17:07:59 -0700 | [diff] [blame] | 129 | ], |
Hans Muller | 4262c1e | 2017-09-08 16:47:40 -0700 | [diff] [blame] | 130 | supportedLocales: const <Locale>[ |
Alexandre Ardhuin | eda03e2 | 2018-08-02 12:02:32 +0200 | [diff] [blame^] | 131 | Locale('en', 'US'), |
| 132 | Locale('es', 'ES'), |
Hans Muller | 4262c1e | 2017-09-08 16:47:40 -0700 | [diff] [blame] | 133 | ], |
Ian Hickson | 48e1350 | 2016-01-10 15:43:23 -0800 | [diff] [blame] | 134 | debugShowMaterialGrid: _configuration.debugShowGrid, |
Ian Hickson | 88c43c3 | 2016-01-10 23:39:20 -0800 | [diff] [blame] | 135 | showPerformanceOverlay: _configuration.showPerformanceOverlay, |
Hixie | 28a1788 | 2015-12-16 11:11:18 -0800 | [diff] [blame] | 136 | showSemanticsDebugger: _configuration.showSemanticsDebugger, |
Adam Barth | 9b9ad3d | 2016-03-12 17:01:45 -0800 | [diff] [blame] | 137 | routes: <String, WidgetBuilder>{ |
Ian Hickson | 9adb4a7 | 2017-06-23 14:58:29 -0700 | [diff] [blame] | 138 | '/': (BuildContext context) => new StockHome(stocks, _configuration, configurationUpdater), |
Adam Barth | 9b9ad3d | 2016-03-12 17:01:45 -0800 | [diff] [blame] | 139 | '/settings': (BuildContext context) => new StockSettings(_configuration, configurationUpdater) |
Hixie | f2b7dd6 | 2015-09-29 09:37:53 -0700 | [diff] [blame] | 140 | }, |
Jason Simmons | 9693cd5 | 2015-12-07 11:19:15 -0800 | [diff] [blame] | 141 | onGenerateRoute: _getRoute, |
Hixie | 0a0a92e | 2015-09-25 16:00:56 -0700 | [diff] [blame] | 142 | ); |
| 143 | } |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void main() { |
Jason Simmons | 9693cd5 | 2015-12-07 11:19:15 -0800 | [diff] [blame] | 147 | runApp(new StocksApp()); |
Collin Jackson | 633b650 | 2015-07-16 11:54:25 -0700 | [diff] [blame] | 148 | } |