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