| 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 double _value; | 13 double _value; |
| 14 | 14 |
| 15 AnimatedValue(double initial) { | 15 AnimatedValue(double initial) { |
| 16 value = initial; | 16 _value = initial; |
| 17 } | 17 } |
| 18 | 18 |
| 19 // 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 // the |value| accessor. |
| 19 Stream<double> get onValueChanged => _controller.stream; | 22 Stream<double> get onValueChanged => _controller.stream; |
| 20 | 23 |
| 21 double get value => _value; | 24 double get value => _value; |
| 22 | 25 |
| 23 void set value(double value) { | 26 void set value(double value) { |
| 24 stop(); | 27 stop(); |
| 25 _setValue(value); | 28 _setValue(value); |
| 26 } | 29 } |
| 27 | 30 |
| 28 bool get isAnimating => _animation != null; | 31 bool get isAnimating => _animation != null; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 48 begin: _value, | 51 begin: _value, |
| 49 end: newValue, | 52 end: newValue, |
| 50 curve: curve, | 53 curve: curve, |
| 51 initialDelay: initialDelay); | 54 initialDelay: initialDelay); |
| 52 | 55 |
| 53 _animation.onTick.listen(_setValue, onDone: () { | 56 _animation.onTick.listen(_setValue, onDone: () { |
| 54 _animation = null; | 57 _animation = null; |
| 55 }); | 58 }); |
| 56 } | 59 } |
| 57 } | 60 } |
| 58 | |
| 59 class AnimatedValueListener { | |
| 60 final Component _component; | |
| 61 final AnimatedValue _value; | |
| 62 StreamSubscription<double> _subscription; | |
| 63 | |
| 64 AnimatedValueListener(this._component, this._value); | |
| 65 | |
| 66 double get value => _value == null ? null : _value.value; | |
| 67 | |
| 68 void ensureListening() { | |
| 69 if (_subscription != null || _value == null) | |
| 70 return; | |
| 71 _subscription = _value.onValueChanged.listen((_) { | |
| 72 _component.scheduleBuild(); | |
| 73 }); | |
| 74 } | |
| 75 | |
| 76 void stopListening() { | |
| 77 if (_subscription == null) | |
| 78 return; | |
| 79 _subscription.cancel(); | |
| 80 _subscription = null; | |
| 81 } | |
| 82 } | |
| OLD | NEW |