Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: sky/sdk/example/stocks/lib/stock_data.dart

Issue 1208053008: Teach Stocks how to load data when run from a package (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 import 'dart:convert'; 5 import 'dart:convert';
6 import 'dart:math'; 6 import 'dart:math';
7 7
8 import 'package:sky/mojo/net/fetch.dart'; 8 import 'package:sky/mojo/net/fetch.dart';
9 import 'package:sky/mojo/asset_bundle.dart';
9 10
10 // Snapshot from http://www.nasdaq.com/screening/company-list.aspx 11 // Snapshot from http://www.nasdaq.com/screening/company-list.aspx
11 // Fetched 2/23/2014. 12 // Fetched 2/23/2014.
12 // "Symbol","Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary Quote", 13 // "Symbol","Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary Quote",
13 // Data in stock_data.json 14 // Data in stock_data.json
14 15
15 final Random _rng = new Random(); 16 final Random _rng = new Random();
16 17
17 class Stock { 18 class Stock {
18 String symbol; 19 String symbol;
(...skipping 25 matching lines...) Expand all
44 45
45 void appendTo(List<Stock> stocks) { 46 void appendTo(List<Stock> stocks) {
46 for (List<String> fields in _data) 47 for (List<String> fields in _data)
47 stocks.add(new Stock.fromFields(fields)); 48 stocks.add(new Stock.fromFields(fields));
48 } 49 }
49 } 50 }
50 51
51 typedef void StockDataCallback(StockData data); 52 typedef void StockDataCallback(StockData data);
52 const _kChunkCount = 30; 53 const _kChunkCount = 30;
53 54
55 String _urlToFetch(int chunk) {
56 if (rootBundle == null)
57 return '../data/stock_data_${chunk}.json';
58 return 'https://domokit.github.io/example/stocks/data/stock_data_${chunk}.json ';
59 }
60
54 class StockDataFetcher { 61 class StockDataFetcher {
55 int _currentChunk = 0; 62 int _currentChunk = 0;
56 final StockDataCallback callback; 63 final StockDataCallback callback;
57 64
58 StockDataFetcher(this.callback) { 65 StockDataFetcher(this.callback) {
59 _fetchNextChunk(); 66 _fetchNextChunk();
60 } 67 }
61 68
62 void _fetchNextChunk() { 69 void _fetchNextChunk() {
63 fetchBody('../data/stock_data_${_currentChunk++}.json').then((Response respo nse) { 70 fetchBody(_urlToFetch(_currentChunk++)).then((Response response) {
64 String json = response.bodyAsString(); 71 String json = response.bodyAsString();
65 JsonDecoder decoder = new JsonDecoder(); 72 JsonDecoder decoder = new JsonDecoder();
66 73
67 callback(new StockData(decoder.convert(json))); 74 callback(new StockData(decoder.convert(json)));
68 75
69 if (_currentChunk < _kChunkCount) 76 if (_currentChunk < _kChunkCount)
70 _fetchNextChunk(); 77 _fetchNextChunk();
71 }); 78 });
72 } 79 }
73 } 80 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698