From 78e101314235dcd09c063d4bdc6de57e140e9969 Mon Sep 17 00:00:00 2001 From: Eragoneq Date: Fri, 7 Feb 2025 16:31:48 +0100 Subject: [PATCH] Fix deprecated routines --- README.md | 2 +- .../trustchain/demo/ui/DemoActivity.kt | 83 ++++++++++--------- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index dd875bf4..071d92b6 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ file needs to define these variables and dependencies, too. A working template w folder's `build.gradle` file. - Don't forget to `include ':ipv8'` into your own `settings.gradle`, as well as the module that you're going to use, presumably `ipv8-android` or `ipv8-jvm`. -- This repository currently uses Gradle version `8.8.0`. Ensure that your `gradle-wrapper.properties` uses the same version. +- This repository currently uses Gradle version `8.10.2`. Ensure that your `gradle-wrapper.properties` uses the same version. - This repository currently uses Java version `17`. Ensure that your Gradle builds with this, too. - By default, Gradle looks at the `JAVA_HOME` variable, which might not point to `17`. - This repository currently uses Kotlin version `2.1.10`. Ensure that your Gradle builds with this Kotlin version. diff --git a/demo-android/src/main/java/nl/tudelft/trustchain/demo/ui/DemoActivity.kt b/demo-android/src/main/java/nl/tudelft/trustchain/demo/ui/DemoActivity.kt index 6fa0644f..6d649ecf 100644 --- a/demo-android/src/main/java/nl/tudelft/trustchain/demo/ui/DemoActivity.kt +++ b/demo-android/src/main/java/nl/tudelft/trustchain/demo/ui/DemoActivity.kt @@ -8,9 +8,12 @@ import androidx.core.view.isVisible import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.repeatOnLifecycle import com.mattskala.itemadapter.ItemAdapter import kotlinx.coroutines.delay import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch import nl.tudelft.ipv8.android.IPv8Android import nl.tudelft.trustchain.demo.DemoCommunity import nl.tudelft.trustchain.demo.R @@ -45,46 +48,48 @@ class DemoActivity : AppCompatActivity() { } private fun loadNetworkInfo() { - lifecycleScope.launchWhenStarted { - while (isActive) { - val demoCommunity = IPv8Android.getInstance().getOverlay()!! - val peers = demoCommunity.getPeers() - - val discoveredAddresses = demoCommunity.network.getWalkableAddresses(demoCommunity.serviceId) - - val discoveredBluetoothAddresses = - demoCommunity.network.getNewBluetoothPeerCandidates().map { it.address } - - val peerItems = peers.map { - PeerItem( - it - ) - } - - val addressItems = discoveredAddresses.map { address -> - val contacted = demoCommunity.discoveredAddressesContacted[address] - AddressItem( - address, null, contacted - ) + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.STARTED) { + while (isActive) { + val demoCommunity = IPv8Android.getInstance().getOverlay()!! + val peers = demoCommunity.getPeers() + + val discoveredAddresses = demoCommunity.network.getWalkableAddresses(demoCommunity.serviceId) + + val discoveredBluetoothAddresses = + demoCommunity.network.getNewBluetoothPeerCandidates().map { it.address } + + val peerItems = peers.map { + PeerItem( + it + ) + } + + val addressItems = discoveredAddresses.map { address -> + val contacted = demoCommunity.discoveredAddressesContacted[address] + AddressItem( + address, null, contacted + ) + } + + val bluetoothAddressItems = discoveredBluetoothAddresses.map { address -> + AddressItem( + address, null, null + ) + } + + val items = peerItems + bluetoothAddressItems + addressItems + + adapter.updateItems(items) + binding.txtCommunityName.text = demoCommunity.javaClass.simpleName + binding.txtPeerCount.text = "${peers.size} peers" + val textColorResId = if (peers.isNotEmpty()) R.color.green else R.color.red + val textColor = ResourcesCompat.getColor(resources, textColorResId, null) + binding.txtPeerCount.setTextColor(textColor) + binding.imgEmpty.isVisible = items.isEmpty() + + delay(1000) } - - val bluetoothAddressItems = discoveredBluetoothAddresses.map { address -> - AddressItem( - address, null, null - ) - } - - val items = peerItems + bluetoothAddressItems + addressItems - - adapter.updateItems(items) - binding.txtCommunityName.text = demoCommunity.javaClass.simpleName - binding.txtPeerCount.text = "${peers.size} peers" - val textColorResId = if (peers.isNotEmpty()) R.color.green else R.color.red - val textColor = ResourcesCompat.getColor(resources, textColorResId, null) - binding.txtPeerCount.setTextColor(textColor) - binding.imgEmpty.isVisible = items.isEmpty() - - delay(1000) } } }