Skip to content

Commit a75a151

Browse files
committed
fix test cancel
1 parent 6c3eea7 commit a75a151

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

app/src/main/java/io/nekohasekai/sagernet/ui/ConfigurationFragment.kt

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ import okhttp3.internal.closeQuietly
110110
import java.net.InetSocketAddress
111111
import java.net.Socket
112112
import java.net.UnknownHostException
113-
import java.util.Collections
113+
import java.util.concurrent.ConcurrentHashMap
114114
import java.util.concurrent.ConcurrentLinkedQueue
115115
import java.util.concurrent.atomic.AtomicInteger
116116
import java.util.zip.ZipInputStream
@@ -608,22 +608,30 @@ class ConfigurationFragment @JvmOverloads constructor(
608608
lateinit var cancel: () -> Unit
609609
lateinit var minimize: () -> Unit
610610

611-
var dialogHidden = false
611+
val dialogStatus = AtomicInteger(0) // 1: hidden 2: cancelled
612612
var notification: ConnectionTestNotification? = null
613613

614-
val results = Collections.synchronizedSet(mutableSetOf<ProxyEntity>())
614+
val results: MutableSet<ProxyEntity> = ConcurrentHashMap.newKeySet()
615615
var proxyN = 0
616616
val finishedN = AtomicInteger(0)
617617

618618
fun update(profile: ProxyEntity) {
619-
results.add(profile)
619+
if (dialogStatus.get() != 2) {
620+
results.add(profile)
621+
}
620622
runOnMainDispatcher {
621623
val context = context ?: return@runOnMainDispatcher
624+
val progress = finishedN.addAndGet(1)
625+
val status = dialogStatus.get()
626+
notification?.updateNotification(
627+
progress,
628+
proxyN,
629+
progress >= proxyN || status == 2
630+
)
631+
if (status >= 1) return@runOnMainDispatcher
622632
if (!isAdded) return@runOnMainDispatcher
623633

624-
val progress = finishedN.addAndGet(1)
625-
notification?.updateNotification(progress, proxyN, progress >= proxyN)
626-
if (dialogHidden) return@runOnMainDispatcher
634+
// refresh dialog
627635

628636
var profileStatusText: String? = null
629637
var profileStatusColor = 0
@@ -797,8 +805,11 @@ class ConfigurationFragment @JvmOverloads constructor(
797805
}
798806
}
799807
test.cancel = {
808+
test.dialogStatus.set(2)
800809
dialog.dismiss()
801810
runOnDefaultDispatcher {
811+
mainJob.cancel()
812+
testJobs.forEach { it.cancel() }
802813
test.results.forEach {
803814
try {
804815
ProfileManager.updateProfile(it)
@@ -807,13 +818,11 @@ class ConfigurationFragment @JvmOverloads constructor(
807818
}
808819
}
809820
GroupManager.postReload(DataStore.currentGroupId())
810-
mainJob.cancel()
811-
testJobs.forEach { it.cancel() }
812821
DataStore.runningTest = false
813822
}
814823
}
815824
test.minimize = {
816-
test.dialogHidden = true
825+
test.dialogStatus.set(1)
817826
test.notification = ConnectionTestNotification(
818827
dialog.context,
819828
"[${group.displayName()}] ${getString(R.string.connection_test)}"
@@ -865,8 +874,11 @@ class ConfigurationFragment @JvmOverloads constructor(
865874
}
866875
}
867876
test.cancel = {
877+
test.dialogStatus.set(2)
868878
dialog.dismiss()
869879
runOnDefaultDispatcher {
880+
mainJob.cancel()
881+
testJobs.forEach { it.cancel() }
870882
test.results.forEach {
871883
try {
872884
ProfileManager.updateProfile(it)
@@ -875,13 +887,11 @@ class ConfigurationFragment @JvmOverloads constructor(
875887
}
876888
}
877889
GroupManager.postReload(DataStore.currentGroupId())
878-
mainJob.cancel()
879-
testJobs.forEach { it.cancel() }
880890
DataStore.runningTest = false
881891
}
882892
}
883893
test.minimize = {
884-
test.dialogHidden = true
894+
test.dialogStatus.set(1)
885895
test.notification = ConnectionTestNotification(
886896
dialog.context,
887897
"[${group.displayName()}] ${getString(R.string.connection_test)}"

nb4a.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
PACKAGE_NAME=moe.nb4a
22
VERSION_NAME=1.3.9
3-
PRE_VERSION_NAME=pre-1.4.0-20250905-1
3+
PRE_VERSION_NAME=pre-1.4.0-20250906-1
44
VERSION_CODE=43

0 commit comments

Comments
 (0)