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

View File

@@ -4,6 +4,19 @@ import retrofit2.Call
import java.io.IOException
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)
fun <T> execute(call: Call<T>): T {
try {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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