| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 | |
| 5 import 'dart:convert'; | |
| 6 import 'dart:math'; | |
| 7 | |
| 8 import 'package:sky/framework/net/fetch.dart'; | |
| 9 | |
| 10 // Snapshot from http://www.nasdaq.com/screening/company-list.aspx | |
| 11 // Fetched 2/23/2014. | |
| 12 // "Symbol","Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary
Quote", | |
| 13 // Data in stock_data.json | |
| 14 | |
| 15 final Random _rng = new Random(); | |
| 16 | |
| 17 class Stock { | |
| 18 String symbol; | |
| 19 String name; | |
| 20 double lastSale; | |
| 21 String marketCap; | |
| 22 double percentChange; | |
| 23 | |
| 24 Stock(this.symbol, this.name, this.lastSale, this.marketCap, this.percentChang
e); | |
| 25 | |
| 26 Stock.fromFields(List<String> fields) { | |
| 27 // FIXME: This class should only have static data, not lastSale, etc. | |
| 28 // "Symbol","Name","LastSale","MarketCap","IPOyear","Sector","industry","Sum
mary Quote", | |
| 29 lastSale = 0.0; | |
| 30 try{ | |
| 31 lastSale = double.parse(fields[2]); | |
| 32 } catch(_) {} | |
| 33 symbol = fields[0]; | |
| 34 name = fields[1]; | |
| 35 marketCap = fields[4]; | |
| 36 percentChange = (_rng.nextDouble() * 20) - 10; | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 class StockData { | |
| 41 List<List<String>> _data; | |
| 42 | |
| 43 StockData(this._data); | |
| 44 | |
| 45 void appendTo(List<Stock> stocks) { | |
| 46 for (List<String> fields in _data) | |
| 47 stocks.add(new Stock.fromFields(fields)); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 typedef void StockDataCallback(StockData data); | |
| 52 const _kChunkCount = 30; | |
| 53 | |
| 54 class StockDataFetcher { | |
| 55 int _currentChunk = 0; | |
| 56 final StockDataCallback callback; | |
| 57 | |
| 58 StockDataFetcher(this.callback) { | |
| 59 _fetchNextChunk(); | |
| 60 } | |
| 61 | |
| 62 void _fetchNextChunk() { | |
| 63 fetchBody('../data/stock_data_${_currentChunk++}.json').then((Response respo
nse) { | |
| 64 String json = response.bodyAsString(); | |
| 65 JsonDecoder decoder = new JsonDecoder(); | |
| 66 | |
| 67 callback(new StockData(decoder.convert(json))); | |
| 68 | |
| 69 if (_currentChunk < _kChunkCount) | |
| 70 _fetchNextChunk(); | |
| 71 }); | |
| 72 } | |
| 73 } | |
| OLD | NEW |