Skip to content

Commit d7d3d24

Browse files
authored
Merge pull request #13 from algolia/feature/update-coroutines-and-dependencies
Feature/update coroutines and dependencies
2 parents 33f3e8d + dedf70a commit d7d3d24

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ buildscript {
77
mavenCentral()
88
}
99
dependencies {
10-
classpath 'com.android.tools.build:gradle:3.3.0-alpha08'
10+
classpath 'com.android.tools.build:gradle:3.3.0-alpha11'
1111
classpath "com.github.ben-manes:gradle-versions-plugin:$benManes"
1212
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlins"
1313
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$bintray"

dependency.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ext.with {
22
sdkCompile = 28
3-
sdkMin = 23
3+
sdkMin = 14
44
sdkTarget = 28
55

66
versioningName = '1.0'
@@ -10,15 +10,15 @@ ext.with {
1010
jvm = '1.8'
1111

1212
appcompat = '28.0.0-rc02'
13-
design = '28.0.0-rc01'
13+
design = '28.0.0-rc02'
1414
constraintLayout = '1.1.2'
1515
espresso = '3.0.2'
1616

1717
archRuntime = '1.1.1'
1818
archCompiler = '1.1.1'
1919

20-
kotlins = '1.2.61'
21-
coroutines = '0.25.0'
20+
kotlins = '1.2.70'
21+
coroutines = '0.26.1'
2222

2323
bintray = '1.8.4'
2424
maven = '2.1'

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip

ui/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,9 @@ dependencies {
4545
testImplementation dependency_jvm.kotlin_test
4646
testImplementation dependency_jvm.kotlin_test_junit
4747
}
48+
49+
kotlin {
50+
experimental {
51+
coroutines "enable"
52+
}
53+
}

ui/src/main/kotlin/com/algolia/instantsearch/voice/ui/RippleView.kt

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package com.algolia.instantsearch.voice.ui
33
import android.animation.AnimatorSet
44
import android.content.Context
55
import android.graphics.Canvas
6+
import android.os.Looper
67
import android.util.AttributeSet
78
import android.view.View
8-
import kotlinx.coroutines.experimental.Job
9+
import kotlinx.coroutines.experimental.*
10+
import kotlinx.coroutines.experimental.android.Main
911
import kotlinx.coroutines.experimental.android.UI
10-
import kotlinx.coroutines.experimental.delay
11-
import kotlinx.coroutines.experimental.launch
12+
import kotlinx.coroutines.experimental.android.awaitFrame
1213

1314
/** A View displaying a ripple effect. */
1415
class RippleView : View {
@@ -39,7 +40,8 @@ class RippleView : View {
3940
private var radius: Float = 0f
4041
private var size: Int = 0
4142

42-
private lateinit var jobAnimation: Job
43+
private var jobAnimation: Job? = null
44+
private var jobFps : Job? = null
4345

4446
private var state = State.None
4547
set(value) {
@@ -51,14 +53,33 @@ class RippleView : View {
5153
jobAnimation = animation()
5254
}
5355
State.Stopped -> try {
54-
jobAnimation.cancel()
56+
jobAnimation?.cancel()
57+
jobAnimation = null
5558
} catch (e: UninitializedPropertyAccessException) {
5659
// cancel() was called before start()
5760
}
5861
State.None -> Unit
5962
}
6063
}
6164

65+
override fun onAttachedToWindow() {
66+
super.onAttachedToWindow()
67+
jobFps = GlobalScope.launch(Dispatchers.Main) {
68+
while (isActive) {
69+
awaitFrame()
70+
invalidate()
71+
}
72+
}
73+
}
74+
75+
override fun onDetachedFromWindow() {
76+
super.onDetachedFromWindow()
77+
jobFps?.cancel()
78+
jobFps = null
79+
jobAnimation?.cancel()
80+
jobAnimation = null
81+
}
82+
6283
private fun init(attrs: AttributeSet) {
6384
context.obtainStyledAttributes(attrs, R.styleable.RippleView, 0, 0).also {
6485
val drawable = it.getDrawable(R.styleable.RippleView_drawable)
@@ -70,18 +91,10 @@ class RippleView : View {
7091

7192
circles = (0 until circleCount).map { DrawableSprite(drawable, size, Opacity.p0) }
7293
}.recycle()
73-
launch(UI) {
74-
// This coroutine refreshes the view at 60 fps
75-
while (isActive) {
76-
UI.awaitFrame()
77-
invalidate()
78-
}
79-
}
8094
}
8195

8296
/** This coroutine generates an animation at each [interval][circleCount]. */
83-
private fun animation(): Job = launch(UI) {
84-
97+
private fun animation(): Job = GlobalScope.launch(Dispatchers.Main) {
8598
var index = 0
8699
while (isActive) {
87100
animations[index] = circles[index].circleAnimation().also { it.start() }

0 commit comments

Comments
 (0)