stock_data.dart 2.62 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Snapshot from http://www.nasdaq.com/screening/company-list.aspx
// Fetched 2/23/2014.
// "Symbol","Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary Quote",
// Data in stock_data.json

10 11 12
import 'dart:convert';
import 'dart:math' as math;

13
import 'package:flutter/foundation.dart';
14
import 'package:http/http.dart' as http;
15 16

final math.Random _rng = new math.Random();
17 18 19 20 21 22 23 24 25 26 27 28 29 30

class Stock {
  String symbol;
  String name;
  double lastSale;
  String marketCap;
  double percentChange;

  Stock(this.symbol, this.name, this.lastSale, this.marketCap, this.percentChange);

  Stock.fromFields(List<String> fields) {
    // FIXME: This class should only have static data, not lastSale, etc.
    // "Symbol","Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary Quote",
    lastSale = 0.0;
31
    try {
32
      lastSale = double.parse(fields[2]);
33
    } catch (_) {}
34 35 36 37 38 39 40
    symbol = fields[0];
    name = fields[1];
    marketCap = fields[4];
    percentChange = (_rng.nextDouble() * 20) - 10;
  }
}

41 42 43
class StockData extends ChangeNotifier {
  StockData() {
    if (actuallyFetchData) {
44
      _httpClient = new http.Client();
45 46 47 48 49 50 51 52
      _fetchNextChunk();
    }
  }

  final List<String> _symbols = <String>[];
  final Map<String, Stock> _stocks = <String, Stock>{};

  Iterable<String> get allSymbols => _symbols;
53

54
  Stock operator [](String symbol) => _stocks[symbol];
55

56 57
  bool get loading => _httpClient != null;

58 59 60
  void add(List<dynamic> data) {
    for (List<dynamic> fields in data) {
      final Stock stock = new Stock.fromFields(fields.cast<String>());
61 62
      _symbols.add(stock.symbol);
      _stocks[stock.symbol] = stock;
Hixie's avatar
Hixie committed
63
    }
64 65
    _symbols.sort();
    notifyListeners();
66 67
  }

68
  static const int _chunkCount = 30;
69
  int _nextChunk = 0;
70

71 72
  String _urlToFetch(int chunk) {
    return 'https://domokit.github.io/examples/stocks/data/stock_data_$chunk.json';
73 74 75
  }

  http.Client _httpClient;
76

77 78
  static bool actuallyFetchData = true;

79
  void _fetchNextChunk() {
80
    _httpClient.get(_urlToFetch(_nextChunk++)).then<Null>((http.Response response) {
81
      final String json = response.body;
82
      if (json == null) {
83 84 85
        debugPrint('Failed to load stock data chunk ${_nextChunk - 1}');
        _end();
        return;
86
      }
87
      const JsonDecoder decoder = const JsonDecoder();
88
      add(decoder.convert(json));
89
      if (_nextChunk < _chunkCount) {
90
        _fetchNextChunk();
91 92 93
      } else {
        _end();
      }
94 95
    });
  }
96 97 98 99 100

  void _end() {
    _httpClient?.close();
    _httpClient = null;
  }
101
}