stock_data.dart 2.67 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9
// 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 = math.Random();
17 18 19 20 21

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

  Stock.fromFields(List<String> fields) {
Kate Lovett's avatar
Kate Lovett committed
22
    // TODO(jackson): This class should only have static data, not lastSale, etc.
23 24
    // "Symbol","Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary Quote",
    lastSale = 0.0;
25
    try {
26
      lastSale = double.parse(fields[2]);
27
    } catch (_) { }
28 29 30 31 32
    symbol = fields[0];
    name = fields[1];
    marketCap = fields[4];
    percentChange = (_rng.nextDouble() * 20) - 10;
  }
33

34 35 36 37 38
  late String symbol;
  late String name;
  late double lastSale;
  late String marketCap;
  late double percentChange;
39 40
}

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

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

52
  List<String> get allSymbols => _symbols;
53

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

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

58
  void add(List<dynamic> data) {
59
    for (final List<dynamic> fields in data.cast<List<dynamic>>()) {
60
      final Stock stock = 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
  Uri _urlToFetch(int chunk) => Uri.https(
      'domokit.github.io', 'examples/stocks/data/stock_data_$chunk.json');
73

74
  http.Client? _httpClient;
75

76 77
  static bool actuallyFetchData = true;

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

  void _end() {
97
    _httpClient!.close();
98 99
    _httpClient = null;
  }
100
}