blob: 38fe4981ae0bac436a5c1047782329ea35e5f392 [file] [log] [blame]
Collin Jackson633b6502015-07-16 11:54:25 -07001// 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 Simon30896862015-07-24 10:53:34 -07005library stocks;
6
Hixiea3ae46b2015-09-14 13:35:05 -07007import 'dart:async';
Ian Hickson54943232016-01-19 09:31:57 -08008import 'dart:collection';
Jim Simon30896862015-07-24 10:53:34 -07009import 'dart:math' as math;
Adam Barthecce1eb2015-10-09 20:55:54 -070010import 'dart:ui' as ui;
Jim Simon30896862015-07-24 10:53:34 -070011
Adam Barth65eba902015-10-09 20:44:52 -070012import 'package:flutter/gestures.dart';
13import 'package:flutter/material.dart';
14import 'package:flutter/painting.dart';
Hixie1f40d962015-10-15 11:07:46 -070015import 'package:flutter/rendering.dart';
Hixiee249c0a2015-12-03 14:28:27 -080016import 'package:flutter/scheduler.dart';
Jason Simmonse08d4462015-12-03 15:03:19 -080017import 'package:intl/intl.dart';
Jim Simon30896862015-07-24 10:53:34 -070018
Hixiedb638502015-07-24 14:42:36 -070019import 'stock_data.dart';
Jason Simmonse08d4462015-12-03 15:03:19 -080020import 'i18n/stock_messages_all.dart';
Hixiedb638502015-07-24 14:42:36 -070021
Jim Simon30896862015-07-24 10:53:34 -070022part 'stock_arrow.dart';
Jim Simon30896862015-07-24 10:53:34 -070023part 'stock_home.dart';
24part 'stock_list.dart';
25part 'stock_menu.dart';
26part 'stock_row.dart';
27part 'stock_settings.dart';
Jason Simmonse08d4462015-12-03 15:03:19 -080028part 'stock_strings.dart';
Hixief2b7dd62015-09-29 09:37:53 -070029part 'stock_symbol_viewer.dart';
Jim Simon30896862015-07-24 10:53:34 -070030part 'stock_types.dart';
Collin Jackson633b6502015-07-16 11:54:25 -070031
Hixie0a0a92e2015-09-25 16:00:56 -070032class StocksApp extends StatefulComponent {
33 StocksAppState createState() => new StocksAppState();
34}
Collin Jackson633b6502015-07-16 11:54:25 -070035
Hixie0a0a92e2015-09-25 16:00:56 -070036class StocksAppState extends State<StocksApp> {
Collin Jackson633b6502015-07-16 11:54:25 -070037
Hixief2b7dd62015-09-29 09:37:53 -070038 final Map<String, Stock> _stocks = <String, Stock>{};
39 final List<String> _symbols = <String>[];
Hixie0a0a92e2015-09-25 16:00:56 -070040
Ian Hickson34cfb602016-01-10 00:36:01 -080041 StockConfiguration _configuration = new StockConfiguration(
42 stockMode: StockMode.optimistic,
43 backupMode: BackupMode.enabled,
Ian Hickson48e13502016-01-10 15:43:23 -080044 debugShowGrid: false,
45 debugShowSizes: false,
Hixie28a17882015-12-16 11:11:18 -080046 showPerformanceOverlay: false,
47 showSemanticsDebugger: false
Ian Hickson34cfb602016-01-10 00:36:01 -080048 );
49
Adam Barth347bd252015-09-30 13:15:46 -070050 void initState() {
51 super.initState();
Collin Jackson633b6502015-07-16 11:54:25 -070052 new StockDataFetcher((StockData data) {
53 setState(() {
Hixief2b7dd62015-09-29 09:37:53 -070054 data.appendTo(_stocks, _symbols);
Collin Jackson633b6502015-07-16 11:54:25 -070055 });
56 });
57 }
58
Ian Hickson34cfb602016-01-10 00:36:01 -080059 void configurationUpdater(StockConfiguration value) {
Hixie0a0a92e2015-09-25 16:00:56 -070060 setState(() {
Ian Hickson34cfb602016-01-10 00:36:01 -080061 _configuration = value;
Hixie0a0a92e2015-09-25 16:00:56 -070062 });
63 }
Collin Jackson633b6502015-07-16 11:54:25 -070064
Hixie0a0a92e2015-09-25 16:00:56 -070065 ThemeData get theme {
Ian Hickson34cfb602016-01-10 00:36:01 -080066 switch (_configuration.stockMode) {
Hixie0a0a92e2015-09-25 16:00:56 -070067 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 Jackson633b6502015-07-16 11:54:25 -070077 }
Hixie0a0a92e2015-09-25 16:00:56 -070078 }
Collin Jackson633b6502015-07-16 11:54:25 -070079
Collin Jacksond05c5642015-12-07 15:39:16 -080080 Route _getRoute(RouteSettings settings) {
Hixiedf07a692015-12-03 12:41:48 -080081 List<String> path = settings.name.split('/');
Hixief2b7dd62015-09-29 09:37:53 -070082 if (path[0] != '')
83 return null;
84 if (path[1] == 'stock') {
85 if (path.length != 3)
86 return null;
Hixiedf07a692015-12-03 12:41:48 -080087 if (_stocks.containsKey(path[2])) {
88 return new MaterialPageRoute(
89 settings: settings,
90 builder: (BuildContext context) => new StockSymbolPage(stock: _stocks[path[2]])
91 );
92 }
Hixief2b7dd62015-09-29 09:37:53 -070093 }
94 return null;
95 }
96
Seth Ladd26666282015-12-08 14:16:14 -080097 Future<LocaleQueryData> _onLocaleChanged(ui.Locale locale) async {
Jason Simmons9693cd52015-12-07 11:19:15 -080098 String localeString = locale.toString();
Seth Ladd26666282015-12-08 14:16:14 -080099 await initializeMessages(localeString);
100 Intl.defaultLocale = localeString;
101 return StockStrings.instance;
Jason Simmons9693cd52015-12-07 11:19:15 -0800102 }
103
Hixie0a0a92e2015-09-25 16:00:56 -0700104 Widget build(BuildContext context) {
Ian Hickson48e13502016-01-10 15:43:23 -0800105 assert(() {
106 debugPaintSizeEnabled = _configuration.debugShowSizes;
107 return true;
108 });
Adam Barthdb3b9e82015-10-09 10:19:35 -0700109 return new MaterialApp(
Hixie0a0a92e2015-09-25 16:00:56 -0700110 title: 'Stocks',
111 theme: theme,
Ian Hickson48e13502016-01-10 15:43:23 -0800112 debugShowMaterialGrid: _configuration.debugShowGrid,
Ian Hickson88c43c32016-01-10 23:39:20 -0800113 showPerformanceOverlay: _configuration.showPerformanceOverlay,
Hixie28a17882015-12-16 11:11:18 -0800114 showSemanticsDebugger: _configuration.showSemanticsDebugger,
Hixie0a0a92e2015-09-25 16:00:56 -0700115 routes: <String, RouteBuilder>{
Ian Hickson34cfb602016-01-10 00:36:01 -0800116 '/': (RouteArguments args) => new StockHome(_stocks, _symbols, _configuration, configurationUpdater),
117 '/settings': (RouteArguments args) => new StockSettings(_configuration, configurationUpdater)
Hixief2b7dd62015-09-29 09:37:53 -0700118 },
Jason Simmons9693cd52015-12-07 11:19:15 -0800119 onGenerateRoute: _getRoute,
120 onLocaleChanged: _onLocaleChanged
Hixie0a0a92e2015-09-25 16:00:56 -0700121 );
122 }
Collin Jackson633b6502015-07-16 11:54:25 -0700123}
124
125void main() {
Jason Simmons9693cd52015-12-07 11:19:15 -0800126 runApp(new StocksApp());
Collin Jackson633b6502015-07-16 11:54:25 -0700127}