Fix crash on delete (app|message|client)

This commit is contained in:
Jannis Mattheis
2023-01-23 10:17:04 +01:00
parent 284428c7ad
commit 879248bf4f
8 changed files with 39 additions and 29 deletions

View File

@@ -3,20 +3,19 @@ package com.github.gotify
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.Callback import com.github.gotify.api.Callback
import com.github.gotify.api.Callback.SuccessCallback
import com.github.gotify.client.api.MessageApi import com.github.gotify.client.api.MessageApi
import com.github.gotify.client.model.Message import com.github.gotify.client.model.Message
import com.github.gotify.log.Log import com.github.gotify.log.Log
internal class MissedMessageUtil(private val api: MessageApi) { internal class MissedMessageUtil(private val api: MessageApi) {
fun lastReceivedMessage(successCallback: SuccessCallback<Long>) { fun lastReceivedMessage(acceptID: (Long) -> Unit) {
api.getMessages(1, 0L).enqueue( api.getMessages(1, 0L).enqueue(
Callback.call( Callback.call(
onSuccess = { messages -> onSuccess = Callback.SuccessBody { messages ->
if (messages.messages.size == 1) { if (messages.messages.size == 1) {
successCallback.onSuccess(messages.messages[0].id) acceptID(messages.messages[0].id)
} else { } else {
successCallback.onSuccess(NO_MESSAGES) acceptID(NO_MESSAGES)
} }
}, },
onError = {} onError = {}

View File

@@ -4,6 +4,19 @@ import retrofit2.Call
import java.io.IOException import java.io.IOException
internal object Api { internal object Api {
@Throws(ApiException::class)
fun execute(call: Call<Void>) {
try {
val response = call.execute()
if (!response.isSuccessful) {
throw ApiException(response)
}
} catch (e: IOException) {
throw ApiException(e)
}
}
@Throws(ApiException::class) @Throws(ApiException::class)
fun <T> execute(call: Call<T>): T { fun <T> execute(call: Call<T>): T {
try { try {

View File

@@ -10,7 +10,15 @@ internal class Callback<T> private constructor(
private val onError: ErrorCallback private val onError: ErrorCallback
) { ) {
fun interface SuccessCallback<T> { fun interface SuccessCallback<T> {
fun onSuccess(data: T) fun onSuccess(response: Response<T>)
}
fun interface SuccessBody<T> : SuccessCallback<T> {
override fun onSuccess(response: Response<T>) {
onResultSuccess(response.body() ?: throw ApiException("null response", response))
}
fun onResultSuccess(data: T)
} }
fun interface ErrorCallback { fun interface ErrorCallback {
@@ -20,9 +28,7 @@ internal class Callback<T> private constructor(
private class RetrofitCallback<T>(private val callback: Callback<T>) : retrofit2.Callback<T> { private class RetrofitCallback<T>(private val callback: Callback<T>) : retrofit2.Callback<T> {
override fun onResponse(call: Call<T>, response: Response<T>) { override fun onResponse(call: Call<T>, response: Response<T>) {
if (response.isSuccessful) { if (response.isSuccessful) {
callback.onSuccess.onSuccess( callback.onSuccess.onSuccess(response)
response.body() ?: throw ApiException("null response", response)
)
} else { } else {
callback.onError.onError(ApiException(response)) callback.onError.onError(ApiException(response))
} }
@@ -40,19 +46,12 @@ internal class Callback<T> private constructor(
onError: ErrorCallback onError: ErrorCallback
): retrofit2.Callback<T> { ): retrofit2.Callback<T> {
return call( return call(
onSuccess = { data -> context.runOnUiThread { onSuccess.onSuccess(data) } }, onSuccess = { response -> context.runOnUiThread { onSuccess.onSuccess(response) } },
onError = { exception -> context.runOnUiThread { onError.onError(exception) } } onError = { exception -> context.runOnUiThread { onError.onError(exception) } }
) )
} }
fun <T> call(): retrofit2.Callback<T> { fun <T> call(onSuccess: SuccessCallback<T> = SuccessCallback {}, onError: ErrorCallback = ErrorCallback {}): retrofit2.Callback<T> {
return call(
onSuccess = {},
onError = {}
)
}
fun <T> call(onSuccess: SuccessCallback<T>, onError: ErrorCallback): retrofit2.Callback<T> {
return RetrofitCallback(merge(of(onSuccess, onError), errorCallback())) return RetrofitCallback(merge(of(onSuccess, onError), errorCallback()))
} }

View File

@@ -63,7 +63,7 @@ internal class InitializationActivity : AppCompatActivity() {
.enqueue( .enqueue(
Callback.callInUI( Callback.callInUI(
this, this,
onSuccess = { user -> authenticated(user) }, onSuccess = Callback.SuccessBody { user -> authenticated(user) },
onError = { exception -> failed(exception) } onError = { exception -> failed(exception) }
) )
) )
@@ -113,7 +113,7 @@ internal class InitializationActivity : AppCompatActivity() {
private fun requestVersion(runnable: Runnable) { private fun requestVersion(runnable: Runnable) {
requestVersion( requestVersion(
callback = { version: VersionInfo -> callback = Callback.SuccessBody { version: VersionInfo ->
Log.i("Server version: ${version.version}@${version.buildDate}") Log.i("Server version: ${version.version}@${version.buildDate}")
settings.serverVersion = version.version settings.serverVersion = version.version
runnable.run() runnable.run()

View File

@@ -192,7 +192,7 @@ internal class LoginActivity : AppCompatActivity() {
} }
private fun onValidUrl(url: String): SuccessCallback<VersionInfo> { private fun onValidUrl(url: String): SuccessCallback<VersionInfo> {
return SuccessCallback { version -> return Callback.SuccessBody { version ->
settings.url = url settings.url = url
binding.checkurlProgress.visibility = View.GONE binding.checkurlProgress.visibility = View.GONE
binding.checkurl.visibility = View.VISIBLE binding.checkurl.visibility = View.VISIBLE
@@ -261,7 +261,7 @@ internal class LoginActivity : AppCompatActivity() {
.enqueue( .enqueue(
Callback.callInUI( Callback.callInUI(
this, this,
onSuccess = { client -> onCreatedClient(client) }, onSuccess = Callback.SuccessBody { client -> onCreatedClient(client) },
onError = { onFailedToCreateClient() } onError = { onFailedToCreateClient() }
) )
) )

View File

@@ -20,7 +20,7 @@ internal class ApplicationHolder(private val activity: Activity, private val cli
.enqueue( .enqueue(
Callback.callInUI( Callback.callInUI(
activity, activity,
onSuccess = { apps -> onReceiveApps(apps) }, onSuccess = Callback.SuccessBody { apps -> onReceiveApps(apps) },
onError = { onFailedApps() } onError = { onFailedApps() }
) )
) )

View File

@@ -68,7 +68,7 @@ internal class PicassoHandler(private val context: Context, private val settings
.apps .apps
.enqueue( .enqueue(
Callback.call( Callback.call(
onSuccess = { apps -> onSuccess = Callback.SuccessBody { apps ->
appIdToAppImage.clear() appIdToAppImage.clear()
appIdToAppImage.putAll(MessageImageCombiner.appIdToImage(apps)) appIdToAppImage.putAll(MessageImageCombiner.appIdToImage(apps))
}, },

View File

@@ -7,7 +7,6 @@ import android.os.Handler
import android.os.Looper import android.os.Looper
import com.github.gotify.SSLSettings import com.github.gotify.SSLSettings
import com.github.gotify.Utils import com.github.gotify.Utils
import com.github.gotify.api.Callback.SuccessCallback
import com.github.gotify.api.CertUtils import com.github.gotify.api.CertUtils
import com.github.gotify.client.model.Message import com.github.gotify.client.model.Message
import com.github.gotify.log.Log import com.github.gotify.log.Log
@@ -38,7 +37,7 @@ internal class WebSocketConnection(
private var errorCount = 0 private var errorCount = 0
private var webSocket: WebSocket? = null private var webSocket: WebSocket? = null
private lateinit var onMessage: SuccessCallback<Message> private lateinit var onMessageCallback: (Message) -> Unit
private lateinit var onClose: Runnable private lateinit var onClose: Runnable
private lateinit var onOpen: Runnable private lateinit var onOpen: Runnable
private lateinit var onBadRequest: BadRequestRunnable private lateinit var onBadRequest: BadRequestRunnable
@@ -56,8 +55,8 @@ internal class WebSocketConnection(
} }
@Synchronized @Synchronized
fun onMessage(onMessage: SuccessCallback<Message>): WebSocketConnection { fun onMessage(onMessage: (Message) -> Unit): WebSocketConnection {
this.onMessage = onMessage this.onMessageCallback = onMessage
return this return this
} }
@@ -168,7 +167,7 @@ internal class WebSocketConnection(
syncExec { syncExec {
Log.i("WebSocket($id): received message $text") Log.i("WebSocket($id): received message $text")
val message = Utils.JSON.fromJson(text, Message::class.java) val message = Utils.JSON.fromJson(text, Message::class.java)
onMessage.onSuccess(message) onMessageCallback(message)
} }
super.onMessage(webSocket, text) super.onMessage(webSocket, text)
} }