@@ -5,12 +5,14 @@ import android.widget.LinearLayout
5
5
import androidx.appcompat.app.AppCompatActivity
6
6
import androidx.core.content.res.ResourcesCompat
7
7
import androidx.core.view.isVisible
8
+ import androidx.lifecycle.Lifecycle
8
9
import androidx.lifecycle.lifecycleScope
10
+ import androidx.lifecycle.repeatOnLifecycle
9
11
import androidx.recyclerview.widget.DividerItemDecoration
10
12
import androidx.recyclerview.widget.LinearLayoutManager
11
13
import com.mattskala.itemadapter.ItemAdapter
12
14
import kotlinx.coroutines.delay
13
- import kotlinx.coroutines.isActive
15
+ import kotlinx.coroutines.launch
14
16
import nl.tudelft.ipv8.android.IPv8Android
15
17
import nl.tudelft.trustchain.demo.DemoCommunity
16
18
import nl.tudelft.trustchain.demo.R
@@ -41,51 +43,54 @@ class DemoActivity : AppCompatActivity() {
41
43
binding.recyclerView.layoutManager = LinearLayoutManager (this )
42
44
binding.recyclerView.addItemDecoration(DividerItemDecoration (this , LinearLayout .VERTICAL ))
43
45
44
- loadNetworkInfo()
46
+ lifecycleScope.launch {
47
+ lifecycle.repeatOnLifecycle(Lifecycle .State .STARTED ) {
48
+ while (true ) {
49
+ loadNetworkInfo()
50
+ delay(1000 )
51
+ }
52
+ }
53
+ }
45
54
}
46
55
47
56
private fun loadNetworkInfo () {
48
- lifecycleScope.launchWhenStarted {
49
- while (isActive) {
50
- val demoCommunity = IPv8Android .getInstance().getOverlay<DemoCommunity >()!!
51
- val peers = demoCommunity.getPeers()
57
+ val demoCommunity = IPv8Android .getInstance().getOverlay<DemoCommunity >()!!
58
+ val peers = demoCommunity.getPeers()
52
59
53
- val discoveredAddresses = demoCommunity.network.getWalkableAddresses(demoCommunity.serviceId)
60
+ val discoveredAddresses = demoCommunity.network.getWalkableAddresses(demoCommunity.serviceId)
54
61
55
- val discoveredBluetoothAddresses =
56
- demoCommunity.network.getNewBluetoothPeerCandidates().map { it.address }
62
+ val discoveredBluetoothAddresses =
63
+ demoCommunity.network.getNewBluetoothPeerCandidates().map { it.address }
57
64
58
- val peerItems = peers.map {
59
- PeerItem (
60
- it
61
- )
62
- }
65
+ val peerItems = peers.map {
66
+ PeerItem (
67
+ it
68
+ )
69
+ }
63
70
64
- val addressItems = discoveredAddresses.map { address ->
65
- val contacted = demoCommunity.discoveredAddressesContacted[address]
66
- AddressItem (
67
- address, null , contacted
68
- )
69
- }
71
+ val addressItems = discoveredAddresses.map { address ->
72
+ val contacted = demoCommunity.discoveredAddressesContacted[address]
73
+ AddressItem (
74
+ address, null , contacted
75
+ )
76
+ }
70
77
71
- val bluetoothAddressItems = discoveredBluetoothAddresses.map { address ->
72
- AddressItem (
73
- address, null , null
74
- )
75
- }
78
+ val bluetoothAddressItems = discoveredBluetoothAddresses.map { address ->
79
+ AddressItem (
80
+ address, null , null
81
+ )
82
+ }
76
83
77
- val items = peerItems + bluetoothAddressItems + addressItems
84
+ val items = peerItems + bluetoothAddressItems + addressItems
85
+
86
+ adapter.updateItems(items)
87
+ binding.txtCommunityName.text = demoCommunity.javaClass.simpleName
88
+ binding.txtPeerCount.text = " ${peers.size} peers"
89
+ val textColorResId = if (peers.isNotEmpty()) R .color.green else R .color.red
90
+ val textColor = ResourcesCompat .getColor(resources, textColorResId, null )
91
+ binding.txtPeerCount.setTextColor(textColor)
92
+ binding.imgEmpty.isVisible = items.isEmpty()
78
93
79
- adapter.updateItems(items)
80
- binding.txtCommunityName.text = demoCommunity.javaClass.simpleName
81
- binding.txtPeerCount.text = " ${peers.size} peers"
82
- val textColorResId = if (peers.isNotEmpty()) R .color.green else R .color.red
83
- val textColor = ResourcesCompat .getColor(resources, textColorResId, null )
84
- binding.txtPeerCount.setTextColor(textColor)
85
- binding.imgEmpty.isVisible = items.isEmpty()
86
94
87
- delay(1000 )
88
- }
89
- }
90
95
}
91
96
}
0 commit comments