| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 'curves.dart'; | |
| 6 import 'dart:async'; | |
| 7 import 'generators.dart'; | |
| 8 | |
| 9 typedef void Callback (); | |
| 10 | |
| 11 class AnimatedValue { | |
| 12 StreamController<double> _controller = new StreamController<double>.broadcast(
sync: true); | |
| 13 AnimationGenerator _animation; | |
| 14 Completer _completer; | |
| 15 double _value; | |
| 16 | |
| 17 AnimatedValue(double initial, { Callback onChange }) { | |
| 18 _value = initial; | |
| 19 _onChange = onChange; | |
| 20 } | |
| 21 Callback _onChange; | |
| 22 | |
| 23 // A stream of change in value from |initial|. The stream does not | |
| 24 // contain the initial value. Consumers should check the initial value via | |
| 25 // the |value| accessor. | |
| 26 // TODO(ianh): Rename this to valueStream once we've landed the fn2 fork | |
| 27 Stream<double> get onValueChanged => _controller.stream; | |
| 28 | |
| 29 double get value => _value; | |
| 30 | |
| 31 void set value(double value) { | |
| 32 stop(); | |
| 33 _setValue(value); | |
| 34 } | |
| 35 | |
| 36 bool get isAnimating => _animation != null; | |
| 37 | |
| 38 void _setValue(double value) { | |
| 39 _value = value; | |
| 40 _controller.add(_value); | |
| 41 if (_onChange != null) | |
| 42 _onChange(); | |
| 43 } | |
| 44 | |
| 45 void _done() { | |
| 46 _animation = null; | |
| 47 if (_completer == null) | |
| 48 return; | |
| 49 Completer completer = _completer; | |
| 50 _completer = null; | |
| 51 completer.complete(_value); | |
| 52 } | |
| 53 | |
| 54 void stop() { | |
| 55 if (_animation != null) { | |
| 56 _animation.cancel(); // will call _done() if it isn't already finished | |
| 57 _done(); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 Future<double> animateTo(double newValue, double duration, | |
| 62 { Curve curve: linear, double initialDelay: 0.0 }) { | |
| 63 stop(); | |
| 64 | |
| 65 _animation = new AnimationGenerator( | |
| 66 duration: duration, | |
| 67 begin: _value, | |
| 68 end: newValue, | |
| 69 curve: curve, | |
| 70 initialDelay: initialDelay) | |
| 71 ..onTick.listen(_setValue, onDone: _done); | |
| 72 | |
| 73 _completer = new Completer(); | |
| 74 return _completer.future; | |
| 75 } | |
| 76 | |
| 77 double get remainingTime { | |
| 78 if (_animation == null) | |
| 79 return 0.0; | |
| 80 return _animation.remainingTime; | |
| 81 } | |
| 82 | |
| 83 } | |
| OLD | NEW |