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