OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 '../fn.dart'; | 5 import '../fn.dart'; |
6 import 'curves.dart'; | 6 import 'curves.dart'; |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'generators.dart'; | 8 import 'generators.dart'; |
9 | 9 |
10 class AnimatedValue { | 10 class AnimatedValue { |
11 StreamController _controller = new StreamController(sync: true); | 11 StreamController _controller = new StreamController(sync: true); |
12 AnimationGenerator _animation; | 12 AnimationGenerator _animation; |
| 13 Completer _completer; |
13 double _value; | 14 double _value; |
14 | 15 |
15 AnimatedValue(double initial) { | 16 AnimatedValue(double initial) { |
16 _value = initial; | 17 _value = initial; |
17 } | 18 } |
18 | 19 |
19 // A stream of change in value from |initial|. The stream does not | 20 // A stream of change in value from |initial|. The stream does not |
20 // contain the initial value. Consumers should check the initial value via | 21 // contain the initial value. Consumers should check the initial value via |
21 // the |value| accessor. | 22 // the |value| accessor. |
22 Stream<double> get onValueChanged => _controller.stream; | 23 Stream<double> get onValueChanged => _controller.stream; |
23 | 24 |
24 double get value => _value; | 25 double get value => _value; |
25 | 26 |
26 void set value(double value) { | 27 void set value(double value) { |
27 stop(); | 28 stop(); |
28 _setValue(value); | 29 _setValue(value); |
29 } | 30 } |
30 | 31 |
31 bool get isAnimating => _animation != null; | 32 bool get isAnimating => _animation != null; |
32 | 33 |
33 void _setValue(double value) { | 34 void _setValue(double value) { |
34 _value = value; | 35 _value = value; |
35 _controller.add(_value); | 36 _controller.add(_value); |
36 } | 37 } |
37 | 38 |
| 39 void _done() { |
| 40 _animation = null; |
| 41 if (_completer == null) |
| 42 return; |
| 43 Completer completer = _completer; |
| 44 _completer = null; |
| 45 completer.complete(_value); |
| 46 } |
| 47 |
38 void stop() { | 48 void stop() { |
39 if (_animation != null) { | 49 if (_animation != null) { |
40 _animation.cancel(); | 50 _animation.cancel(); |
41 _animation = null; | 51 _done(); |
42 } | 52 } |
43 } | 53 } |
44 | 54 |
45 void animateTo(double newValue, double duration, | 55 Future<double> animateTo(double newValue, double duration, |
46 { Curve curve: linear, double initialDelay: 0.0 }) { | 56 { Curve curve: linear, double initialDelay: 0.0 }) { |
47 stop(); | 57 stop(); |
48 | 58 |
49 _animation = new AnimationGenerator( | 59 _animation = new AnimationGenerator( |
50 duration: duration, | 60 duration: duration, |
51 begin: _value, | 61 begin: _value, |
52 end: newValue, | 62 end: newValue, |
53 curve: curve, | 63 curve: curve, |
54 initialDelay: initialDelay); | 64 initialDelay: initialDelay) |
| 65 ..onTick.listen(_setValue, onDone: _done); |
55 | 66 |
56 _animation.onTick.listen(_setValue, onDone: () { | 67 _completer = new Completer(); |
57 _animation = null; | 68 return _completer.future; |
58 }); | |
59 } | 69 } |
60 } | 70 } |
OLD | NEW |