Skip to content

Commit

Permalink
Fix updating view on non-UI thread
Browse files Browse the repository at this point in the history
  • Loading branch information
layou233 committed Nov 11, 2023
1 parent a34b898 commit 12f4e86
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ android {
applicationId = "com.launium.mcping"
minSdk = 21
targetSdk = 34
versionCode = 4
versionName = "0.2.2"
versionCode = 5
versionName = "0.2.3"
setProperty("archivesBaseName", "MCPing-$versionName")

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down
20 changes: 14 additions & 6 deletions app/src/main/java/com/launium/mcping/server/MinecraftResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.ktor.utils.io.core.Input
import io.ktor.utils.io.core.readShort
import io.ktor.utils.io.core.readTextExactBytes
import io.ktor.utils.io.core.readUShort
import io.ktor.utils.io.errors.IOException
import io.ktor.utils.io.streams.asInput
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
Expand Down Expand Up @@ -101,6 +102,7 @@ class MinecraftResolver(uri: String) {
return results
}

@OptIn(ExperimentalStdlibApi::class)
@RequiresApi(Build.VERSION_CODES.Q)
private suspend fun querySRVPlatform(serviceName: String) = withContext(Dispatchers.IO) {
return@withContext suspendCoroutine { continuation ->
Expand All @@ -115,13 +117,19 @@ class MinecraftResolver(uri: String) {

override fun onAnswer(answer: ByteArray, rCode: Int) {
when (rCode) {
0 -> continuation.resume(
parseSRVResponse(
ByteArrayInputStream(answer).asInput(),
serviceName,
verifyTransactionID = false
0 -> try {
continuation.resume(
parseSRVResponse(
ByteArrayInputStream(answer).asInput(),
serviceName,
verifyTransactionID = false
)
)
) // 0 stands for NO_ERROR
} catch (e: Exception) {
continuation.resumeWithException(IOException("Fail to parse SRV response: [00000000 ${
answer.joinToString { it.toHexString() + " " }
}]", e))
} // 0 stands for NO_ERROR

3 -> continuation.resume(null) // NX_DOMAIN

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.withContext
import java.net.URL
import java.util.Locale

private const val SERVER_LIST_URL = "https://www.jsip.club/api/ajax.php?request=get_line_list"

Expand Down Expand Up @@ -104,10 +104,11 @@ class DiscoveryFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener {
adapter.servers = (resultObject["data"] as JSONArray).map {
val serverObject = it as JSONObject
return@map MinecraftServer(
serverObject["name"] as String, serverObject["address"] as String
serverObject["name"] as String,
(serverObject["address"] as String).lowercase(Locale.getDefault())
)
}
withContext(Dispatchers.Main) {
activity?.runOnUiThread {
adapter.notifyDataSetChanged()
}
val semaphore = Semaphore(Preferences.maxConcurrentPings)
Expand All @@ -125,7 +126,9 @@ class DiscoveryFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener {
}
semaphore.release()
if (changed) {
adapter.notifyItemChanged(i)
activity?.runOnUiThread {
adapter.notifyItemChanged(i)
}
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/com/launium/mcping/ui/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ class HomeFragment : Fragment() {
}
lifecycleScope.launch(Dispatchers.IO) {
adapter.updateServerList()
binding.container.adapter = adapter
activity?.runOnUiThread {
binding.container.adapter = adapter
}
}

binding.swipeRefreshLayout.setOnRefreshListener {
Expand All @@ -75,7 +77,9 @@ class HomeFragment : Fragment() {
}
semaphore.release()
if (changed) {
adapter.notifyItemChanged(i)
activity?.runOnUiThread {
adapter.notifyItemChanged(i)
}
ServerManager.serverDao.update(server)
}
}
Expand Down

0 comments on commit 12f4e86

Please sign in to comment.