Replace ShareActivity's AsyncTask with Coroutine
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package com.github.gotify.sharing
|
package com.github.gotify.sharing
|
||||||
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.AsyncTask
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import android.view.View
|
import android.view.View
|
||||||
@@ -11,7 +10,7 @@ import android.widget.Toast
|
|||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import com.github.gotify.R
|
import com.github.gotify.R
|
||||||
import com.github.gotify.Settings
|
import com.github.gotify.Settings
|
||||||
import com.github.gotify.Utils
|
import com.github.gotify.Utils.launchCoroutine
|
||||||
import com.github.gotify.api.Api
|
import com.github.gotify.api.Api
|
||||||
import com.github.gotify.api.ApiException
|
import com.github.gotify.api.ApiException
|
||||||
import com.github.gotify.api.ClientFactory
|
import com.github.gotify.api.ClientFactory
|
||||||
@@ -21,6 +20,8 @@ import com.github.gotify.client.model.Message
|
|||||||
import com.github.gotify.databinding.ActivityShareBinding
|
import com.github.gotify.databinding.ActivityShareBinding
|
||||||
import com.github.gotify.log.Log
|
import com.github.gotify.log.Log
|
||||||
import com.github.gotify.messages.provider.ApplicationHolder
|
import com.github.gotify.messages.provider.ApplicationHolder
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
internal class ShareActivity : AppCompatActivity() {
|
internal class ShareActivity : AppCompatActivity() {
|
||||||
private lateinit var binding: ActivityShareBinding
|
private lateinit var binding: ActivityShareBinding
|
||||||
@@ -114,24 +115,37 @@ internal class ShareActivity : AppCompatActivity() {
|
|||||||
message.message = contentText
|
message.message = contentText
|
||||||
message.priority = priority.toLong()
|
message.priority = priority.toLong()
|
||||||
|
|
||||||
PushMessage({
|
launchCoroutine {
|
||||||
val pushClient = ClientFactory.clientToken(
|
val response = executeMessageCall(appIndex, message)
|
||||||
settings.url,
|
withContext(Dispatchers.Main) {
|
||||||
settings.sslSettings(),
|
if (response) {
|
||||||
appsHolder.get()[appIndex].token
|
Toast.makeText(this@ShareActivity, "Pushed!", Toast.LENGTH_LONG).show()
|
||||||
)
|
finish()
|
||||||
try {
|
} else {
|
||||||
val messageApi = pushClient.createService(MessageApi::class.java)
|
Toast.makeText(
|
||||||
Api.execute(messageApi.createMessage(it))
|
this@ShareActivity,
|
||||||
"Pushed!"
|
"Oops! Something went wrong...",
|
||||||
} catch (apiException: ApiException) {
|
Toast.LENGTH_LONG
|
||||||
Log.e("Failed sending message", apiException)
|
).show()
|
||||||
"Oops! Something went wrong..."
|
}
|
||||||
}
|
}
|
||||||
}, {
|
}
|
||||||
Toast.makeText(this, it, Toast.LENGTH_LONG).show()
|
}
|
||||||
finish()
|
|
||||||
}).execute(message)
|
private fun executeMessageCall(appIndex: Int, message: Message): Boolean {
|
||||||
|
val pushClient = ClientFactory.clientToken(
|
||||||
|
settings.url,
|
||||||
|
settings.sslSettings(),
|
||||||
|
appsHolder.get()[appIndex].token
|
||||||
|
)
|
||||||
|
return try {
|
||||||
|
val messageApi = pushClient.createService(MessageApi::class.java)
|
||||||
|
Api.execute(messageApi.createMessage(message))
|
||||||
|
true
|
||||||
|
} catch (apiException: ApiException) {
|
||||||
|
Log.e("Failed sending message", apiException)
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun populateSpinner(apps: List<Application>) {
|
private fun populateSpinner(apps: List<Application>) {
|
||||||
@@ -143,19 +157,4 @@ internal class ShareActivity : AppCompatActivity() {
|
|||||||
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, appNameList)
|
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, appNameList)
|
||||||
binding.appSpinner.adapter = adapter
|
binding.appSpinner.adapter = adapter
|
||||||
}
|
}
|
||||||
|
|
||||||
class PushMessage(
|
|
||||||
private val backgroundAction: (message: Message?) -> String,
|
|
||||||
private val postAction: (message: String) -> Unit
|
|
||||||
) : AsyncTask<Message?, String?, String>() {
|
|
||||||
@Deprecated("Deprecated in Java")
|
|
||||||
override fun doInBackground(vararg messages: Message?): String {
|
|
||||||
return backgroundAction(messages.first())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated("Deprecated in Java")
|
|
||||||
override fun onPostExecute(message: String) {
|
|
||||||
postAction(message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user