@@ -5,7 +5,6 @@ import android.support.v7.app.AppCompatActivity
5
5
import android.util.Log
6
6
import kotlinx.android.synthetic.main.activity_main.*
7
7
import kotlinx.coroutines.*
8
- import org.jetbrains.anko.sdk25.coroutines.onClick
9
8
import org.jetbrains.anko.toast
10
9
import java.net.InetAddress
11
10
import java.net.UnknownHostException
@@ -30,34 +29,38 @@ class MainActivity : AppCompatActivity(), CoroutineScope {
30
29
setContentView(R .layout.activity_main)
31
30
32
31
launch(Dispatchers .IO ) {
33
- try {
34
- controller = MiLightController ( InetAddress .getByName(bridge) )
32
+ val ip = try {
33
+ InetAddress .getByName(bridge)
35
34
} catch (ex: UnknownHostException ) {
36
35
withContext(Dispatchers .Main ) {
37
36
toast(" Error while looking for bridge:\n ${ex.message} " )
38
37
}
38
+ null
39
+ }
40
+ ip?.let {
41
+ controller = MiLightController (ip)
39
42
}
40
43
}
41
44
42
- on_button.onClick {
45
+ on_button.setOnClickListener {
43
46
Log .i(TAG , " Turning lights on" )
44
47
launch(Dispatchers .IO ) {
45
48
try {
46
49
controller.turnOn()
47
- } catch (ex: Exception ) {
50
+ } catch (ex: Exception ) {
48
51
withContext(Dispatchers .Main ) {
49
52
toast(" Error turning on lights:\n ${ex.message} " )
50
53
}
51
54
}
52
55
}
53
56
}
54
57
55
- off_button.onClick {
58
+ off_button.setOnClickListener {
56
59
Log .i(TAG , " Turning lights off" )
57
60
launch(Dispatchers .IO ) {
58
61
try {
59
62
controller.turnOff()
60
- } catch (ex: Exception ) {
63
+ } catch (ex: Exception ) {
61
64
withContext(Dispatchers .Main ) {
62
65
toast(" Error turning off lights:\n ${ex.message} " )
63
66
}
@@ -66,6 +69,7 @@ class MainActivity : AppCompatActivity(), CoroutineScope {
66
69
}
67
70
}
68
71
72
+
69
73
override fun onDestroy () {
70
74
super .onDestroy()
71
75
job.cancel() // Cancel job on activity destroy. After destroy all children jobs will be cancelled automatically
0 commit comments