@@ -11,8 +11,12 @@ import android.graphics.Paint
11
11
import android.graphics.RectF
12
12
import android.util.AttributeSet
13
13
import android.view.View
14
+ import kotlinx.coroutines.experimental.CoroutineScope
15
+ import kotlinx.coroutines.experimental.Dispatchers
14
16
import kotlinx.coroutines.experimental.Job
15
- import kotlinx.coroutines.experimental.android.UI
17
+ import kotlinx.coroutines.experimental.android.Main
18
+ import kotlinx.coroutines.experimental.android.awaitFrame
19
+ import kotlinx.coroutines.experimental.cancelChildren
16
20
import kotlinx.coroutines.experimental.launch
17
21
import java.util.*
18
22
@@ -58,9 +62,11 @@ class AnimationView(
58
62
59
63
private val rnd = Random ()
60
64
61
- class AnimationModel : ViewModel () {
65
+ class AnimationModel : ViewModel (), CoroutineScope {
66
+
67
+ override val coroutineContext = Job () + Dispatchers .Main
68
+
62
69
private val shapes = MutableLiveData <Set <AnimatedShape >>()
63
- private val jobs = arrayListOf<Job >()
64
70
65
71
fun observe (owner : LifecycleOwner , observer : Observer <Set <AnimatedShape >>) =
66
72
shapes.observe(owner, observer)
@@ -71,13 +77,13 @@ class AnimationModel : ViewModel() {
71
77
}
72
78
73
79
fun addAnimation () {
74
- jobs + = launch( UI ) {
80
+ launch {
75
81
animateShape(if (rnd.nextBoolean()) AnimatedCircle () else AnimatedSquare ())
76
82
}
77
83
}
78
84
79
85
fun clearAnimations () {
80
- jobs.forEach { it.cancel() }
86
+ coroutineContext.cancelChildren()
81
87
shapes.value = NO_SHAPES
82
88
}
83
89
}
@@ -101,7 +107,7 @@ suspend fun AnimationModel.animateShape(shape: AnimatedShape) {
101
107
var time = System .nanoTime() // nanos
102
108
var checkTime = time
103
109
while (true ) {
104
- val dt = time.let { old -> UI . awaitFrame().also { time = it } - old }
110
+ val dt = time.let { old -> awaitFrame().also { time = it } - old }
105
111
if (dt > 0 .5e9) continue // don't animate through over a half second lapses
106
112
val dx = shape.x - 0.5f
107
113
val dy = shape.y - 0.5f
@@ -121,7 +127,7 @@ suspend fun AnimationModel.animateShape(shape: AnimatedShape) {
121
127
when (rnd.nextInt(20 )) { // roll d20
122
128
0 -> {
123
129
animateColor(shape) // wait a second & animate color
124
- time = UI . awaitFrame() // and sync with next frame
130
+ time = awaitFrame() // and sync with next frame
125
131
}
126
132
1 -> { // random speed change
127
133
sx = rnd.nextSpeed()
@@ -138,7 +144,7 @@ suspend fun AnimationModel.animateColor(shape: AnimatedShape) {
138
144
val aColor = shape.color
139
145
val bColor = rnd.nextColor()
140
146
while (true ) {
141
- val time = UI . awaitFrame()
147
+ val time = awaitFrame()
142
148
val b = (time - startTime) / duration
143
149
if (b >= 1.0f ) break
144
150
val a = 1 - b
0 commit comments