From 0ca330e66f1e1da270415db574f7ea49ef7541b1 Mon Sep 17 00:00:00 2001 From: vadym Date: Mon, 22 Jun 2020 02:00:23 -0700 Subject: [PATCH] 1. added dynamic peer list loading --- app/build.gradle | 9 --- .../chronosx88/yggdrasil/PeerListActivity.kt | 79 +++---------------- .../config/SelectPeerInfoListAdapter.kt | 26 +++++- 3 files changed, 37 insertions(+), 77 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index cf1cf5a..c9c4b05 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -38,15 +38,6 @@ android { } } -task ndkBuild(type: Exec) { - def rootDir = project.rootDir - workingDir = new File(rootDir,"yggdrasil") - commandLine 'make' -} - -gradle.projectsEvaluated { - tasks.compileDebugKotlin.dependsOn(ndkBuild) -} dependencies { diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/PeerListActivity.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/PeerListActivity.kt index 90dea4e..371d39c 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/PeerListActivity.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/PeerListActivity.kt @@ -32,57 +32,6 @@ class PeerListActivity : AppCompatActivity() { companion object { const val PEER_LIST_URL = "https://publicpeers.neilalexander.dev/publicnodes.json" - - var allPeers = arrayListOf( - PeerInfo( - "tcp", - Inet4Address.getByName("194.177.21.156"), - 5066, - "RU" - ), - PeerInfo( - "tcp", - Inet4Address.getByName("46.151.26.194"), - 60575, - "RU" - ), - PeerInfo( - "tcp", - Inet4Address.getByName("188.226.125.64"), - 54321, - "RU" - ), - PeerInfo( - "tcp", - Inet4Address.getByName("88.201.129.205"), - 8777, - "RU" - ), - PeerInfo( - "tcp", - Inet4Address.getByName("45.11.19.26"), - 5001, - "DE" - ), - PeerInfo( - "tcp", - Inet4Address.getByName("82.165.69.111"), - 61216, - "DE" - ), - PeerInfo( - "tcp", - Inet4Address.getByName("104.248.15.125"), - 31337, - "US" - ), - PeerInfo( - "tcp", - Inet4Address.getByName("108.175.10.127"), - 61216, - "US" - ) - ) } fun downloadJson(link: String): String { @@ -118,7 +67,10 @@ class PeerListActivity : AppCompatActivity() { } var extras = intent.extras var peerList = findViewById(R.id.peerList) - var instance = this + var allPeers = arrayListOf() + var adapter = SelectPeerInfoListAdapter(this, allPeers, mutableSetOf()) + peerList.adapter = adapter + GlobalScope.launch { try { var json = downloadJson(PEER_LIST_URL) @@ -126,7 +78,6 @@ class PeerListActivity : AppCompatActivity() { val mapType: Type = object : TypeToken>>() {}.type val peersMap: Map> = Gson().fromJson(json, mapType) - val allOnlinePeers = arrayListOf() for ((country, peers) in peersMap.entries) { println("$country:") for ((peer, status) in peers) { @@ -142,7 +93,12 @@ class PeerListActivity : AppCompatActivity() { var peerInfo = PeerInfo(url.scheme, address, url.port, ccp.nameCode) peerInfo.ping = ping - allOnlinePeers.add(peerInfo) + adapter.addItem(peerInfo) + if(peerList.adapter.count % 5 == 0) { + withContext(Dispatchers.Main) { + adapter.sort() + } + } } catch (e: Throwable){ e.printStackTrace() } @@ -151,31 +107,20 @@ class PeerListActivity : AppCompatActivity() { } } } - if (allOnlinePeers.size > 0) { - allPeers = ArrayList(allOnlinePeers.sortedWith(compareBy { it.ping })) - } - if (extras != null) { var cp = MainActivity.deserializeStringList2PeerInfoSet( extras.getStringArrayList(MainActivity.PEER_LIST)!! ) var currentPeers = ArrayList(cp.sortedWith(compareBy { it.ping })) - allPeers.removeAll(currentPeers) - allPeers.addAll(0, currentPeers) - var adapter = SelectPeerInfoListAdapter(instance, allPeers, cp) withContext(Dispatchers.Main) { - peerList.adapter = adapter - } - } else { - var adapter = SelectPeerInfoListAdapter(instance, allPeers, mutableSetOf()) - withContext(Dispatchers.Main) { - peerList.adapter = adapter + adapter.addAll(0, currentPeers) } } } catch (e: Throwable){ e.printStackTrace() } } + (peerList.adapter as SelectPeerInfoListAdapter).sort() } override fun onCreateOptionsMenu(menu: Menu): Boolean { diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/SelectPeerInfoListAdapter.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/SelectPeerInfoListAdapter.kt index 65fa0a7..baff802 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/SelectPeerInfoListAdapter.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/SelectPeerInfoListAdapter.kt @@ -19,9 +19,17 @@ class SelectPeerInfoListAdapter( ) : ArrayAdapter (context, 0, allPeers) { private val mContext: Context = context - private var allPeers: List = allPeers + private var allPeers: MutableList = allPeers as MutableList private var currentPeers: MutableSet = currentPeers + override fun getItem(position: Int): PeerInfo? { + return allPeers.get(position) + } + + override fun getCount(): Int { + return allPeers.size + } + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { var peerInfoHolder = PeerInfoHolder() var listItem: View? = convertView @@ -66,6 +74,22 @@ class SelectPeerInfoListAdapter( return currentPeers } + fun addItem(peerInfo: PeerInfo){ + allPeers.add(peerInfo) + } + + fun addAll(index: Int, peerInfo: ArrayList){ + currentPeers.addAll(peerInfo) + allPeers.removeAll(peerInfo) + allPeers.addAll(index, peerInfo) + this.notifyDataSetChanged() + } + + fun sort(){ + allPeers = ArrayList(allPeers.sortedWith(compareBy { it.ping })) + this.notifyDataSetChanged() + } + class PeerInfoHolder { lateinit var checkbox: CheckBox lateinit var countryFlag: ImageView