Make already selected peers have toggled checkbox in peer list

This commit is contained in:
ChronosX88 2022-01-15 19:24:05 +03:00
parent 992e8058d9
commit 9a2d534620
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A

View File

@ -37,6 +37,8 @@ import java.net.URI
import java.net.URL import java.net.URL
import java.net.UnknownHostException import java.net.UnknownHostException
import java.nio.charset.Charset import java.nio.charset.Charset
import java.util.*
import kotlin.collections.ArrayList
class PeerListActivity : AppCompatActivity() { class PeerListActivity : AppCompatActivity() {
@ -83,7 +85,9 @@ class PeerListActivity : AppCompatActivity() {
} }
var extras = intent.extras var extras = intent.extras
var peerList = findViewById<ListView>(R.id.peerList) var peerList = findViewById<ListView>(R.id.peerList)
var adapter = SelectPeerInfoListAdapter(this, arrayListOf(), mutableSetOf())
val alreadySelectedPeers = deserializeStringList2PeerInfoSet(extras!!.getStringArrayList(MainActivity.PEER_LIST)!!)
var adapter = SelectPeerInfoListAdapter(this, arrayListOf(), alreadySelectedPeers)
peerList.adapter = adapter peerList.adapter = adapter
var peerInfoListCache = Builder<List<PeerInfo>>(CACHE_NAME, TEST_APP_VERSION) var peerInfoListCache = Builder<List<PeerInfo>>(CACHE_NAME, TEST_APP_VERSION)
.enableLog() .enableLog()
@ -95,10 +99,7 @@ class PeerListActivity : AppCompatActivity() {
GlobalScope.launch() { GlobalScope.launch() {
try { try {
var cp = deserializeStringList2PeerInfoSet( for (pi in alreadySelectedPeers) {
extras!!.getStringArrayList(MainActivity.PEER_LIST)!!
)
for (pi in cp) {
var ping = ping(pi.hostName, pi.port) var ping = ping(pi.hostName, pi.port)
pi.ping = ping pi.ping = ping
} }
@ -108,7 +109,7 @@ class PeerListActivity : AppCompatActivity() {
for (peerInfo in peerInfoCache) { for (peerInfo in peerInfoCache) {
var ping = ping(peerInfo.hostName, peerInfo.port) var ping = ping(peerInfo.hostName, peerInfo.port)
peerInfo.ping = ping peerInfo.ping = ping
if (cp.contains(peerInfo)) { if (alreadySelectedPeers.contains(peerInfo)) {
continue continue
} }
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
@ -129,7 +130,7 @@ class PeerListActivity : AppCompatActivity() {
for ((peer, status) in peers) { for ((peer, status) in peers) {
if (status.up) { if (status.up) {
for (ccp in countries) { for (ccp in countries) {
if (ccp.name.toLowerCase() if (ccp.name.lowercase(Locale.getDefault())
.contains(country.replace(".md", "").replace("-", " ")) .contains(country.replace(".md", "").replace("-", " "))
) { ) {
if(!peerListPing){ if(!peerListPing){
@ -147,7 +148,7 @@ class PeerListActivity : AppCompatActivity() {
) )
var ping = ping(url.host, url.port) var ping = ping(url.host, url.port)
peerInfo.ping = ping peerInfo.ping = ping
if (cp.contains(peerInfo)) { if (alreadySelectedPeers.contains(peerInfo)) {
continue continue
} }
if (peerInfo.ping < Int.MAX_VALUE) { if (peerInfo.ping < Int.MAX_VALUE) {
@ -181,7 +182,7 @@ class PeerListActivity : AppCompatActivity() {
for (peerInfo in onlinePeerInfoList) { for (peerInfo in onlinePeerInfoList) {
var ping = ping(peerInfo.hostName, peerInfo.port) var ping = ping(peerInfo.hostName, peerInfo.port)
peerInfo.ping = ping peerInfo.ping = ping
if (cp.contains(peerInfo)) { if (alreadySelectedPeers.contains(peerInfo)) {
continue continue
} }
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
@ -197,7 +198,7 @@ class PeerListActivity : AppCompatActivity() {
else -> e.printStackTrace() else -> e.printStackTrace()
} }
} }
var currentPeers = ArrayList(cp.sortedWith(compareBy { it.ping })) var currentPeers = ArrayList(alreadySelectedPeers.sortedWith(compareBy { it.ping }))
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
adapter.addAll(0, currentPeers) adapter.addAll(0, currentPeers)
} }