Replace LogsActivity's AsyncTask with Coroutine
This commit is contained in:
@@ -3,7 +3,6 @@ package com.github.gotify.log
|
|||||||
import android.content.ClipData
|
import android.content.ClipData
|
||||||
import android.content.ClipboardManager
|
import android.content.ClipboardManager
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.os.AsyncTask
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
@@ -12,7 +11,10 @@ import android.view.MenuItem
|
|||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import com.github.gotify.R
|
import com.github.gotify.R
|
||||||
import com.github.gotify.Utils
|
import com.github.gotify.Utils
|
||||||
|
import com.github.gotify.Utils.launchCoroutine
|
||||||
import com.github.gotify.databinding.ActivityLogsBinding
|
import com.github.gotify.databinding.ActivityLogsBinding
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
internal class LogsActivity : AppCompatActivity() {
|
internal class LogsActivity : AppCompatActivity() {
|
||||||
|
|
||||||
@@ -34,12 +36,15 @@ internal class LogsActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun updateLogs() {
|
private fun updateLogs() {
|
||||||
RefreshLogs { result ->
|
launchCoroutine {
|
||||||
val content = binding.logContent
|
val log = Log.get()
|
||||||
if (content.selectionStart == content.selectionEnd) {
|
withContext(Dispatchers.Main) {
|
||||||
content.text = result
|
val content = binding.logContent
|
||||||
|
if (content.selectionStart == content.selectionEnd) {
|
||||||
|
content.text = log
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}.execute()
|
}
|
||||||
|
|
||||||
if (!isDestroyed) {
|
if (!isDestroyed) {
|
||||||
handler.postDelayed(this::updateLogs, 5000)
|
handler.postDelayed(this::updateLogs, 5000)
|
||||||
@@ -74,19 +79,4 @@ internal class LogsActivity : AppCompatActivity() {
|
|||||||
else -> false
|
else -> false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class RefreshLogs(private val action: (result: String) -> Unit)
|
|
||||||
: AsyncTask<Unit, Unit, String>() {
|
|
||||||
|
|
||||||
@Deprecated("Deprecated in Java")
|
|
||||||
override fun doInBackground(vararg params: Unit?): String {
|
|
||||||
return Log.get()
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated("Deprecated in Java")
|
|
||||||
override fun onPostExecute(result: String?) {
|
|
||||||
action(result ?: "")
|
|
||||||
super.onPostExecute(result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user