@@ -5,12 +5,14 @@ import android.widget.LinearLayout
55import androidx.appcompat.app.AppCompatActivity
66import androidx.core.content.res.ResourcesCompat
77import androidx.core.view.isVisible
8+ import androidx.lifecycle.Lifecycle
89import androidx.lifecycle.lifecycleScope
10+ import androidx.lifecycle.repeatOnLifecycle
911import androidx.recyclerview.widget.DividerItemDecoration
1012import androidx.recyclerview.widget.LinearLayoutManager
1113import com.mattskala.itemadapter.ItemAdapter
1214import kotlinx.coroutines.delay
13- import kotlinx.coroutines.isActive
15+ import kotlinx.coroutines.launch
1416import nl.tudelft.ipv8.android.IPv8Android
1517import nl.tudelft.trustchain.demo.DemoCommunity
1618import nl.tudelft.trustchain.demo.R
@@ -41,51 +43,54 @@ class DemoActivity : AppCompatActivity() {
4143 binding.recyclerView.layoutManager = LinearLayoutManager (this )
4244 binding.recyclerView.addItemDecoration(DividerItemDecoration (this , LinearLayout .VERTICAL ))
4345
44- loadNetworkInfo()
46+ lifecycleScope.launch {
47+ lifecycle.repeatOnLifecycle(Lifecycle .State .STARTED ) {
48+ while (true ) {
49+ loadNetworkInfo()
50+ delay(1000 )
51+ }
52+ }
53+ }
4554 }
4655
4756 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()
5259
53- val discoveredAddresses = demoCommunity.network.getWalkableAddresses(demoCommunity.serviceId)
60+ val discoveredAddresses = demoCommunity.network.getWalkableAddresses(demoCommunity.serviceId)
5461
55- val discoveredBluetoothAddresses =
56- demoCommunity.network.getNewBluetoothPeerCandidates().map { it.address }
62+ val discoveredBluetoothAddresses =
63+ demoCommunity.network.getNewBluetoothPeerCandidates().map { it.address }
5764
58- val peerItems = peers.map {
59- PeerItem (
60- it
61- )
62- }
65+ val peerItems = peers.map {
66+ PeerItem (
67+ it
68+ )
69+ }
6370
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+ }
7077
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+ }
7683
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()
7893
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()
8694
87- delay(1000 )
88- }
89- }
9095 }
9196}
0 commit comments