Disallow null response of SuccessCallback

This commit is contained in:
Niko Diamadis
2023-01-05 22:51:59 +01:00
parent 005aea4e5f
commit aeddf50875
6 changed files with 21 additions and 21 deletions

View File

@@ -10,10 +10,10 @@ import com.github.gotify.client.model.PagedMessages
import com.github.gotify.log.Log
internal class MissedMessageUtil(private val api: MessageApi) {
fun lastReceivedMessage(successCallback: SuccessCallback<Long?>) {
fun lastReceivedMessage(successCallback: SuccessCallback<Long>) {
api.getMessages(1, 0L).enqueue(
Callback.call({ messages: PagedMessages? ->
if (messages!!.messages.size == 1) {
Callback.call({ messages: PagedMessages ->
if (messages.messages.size == 1) {
successCallback.onSuccess(messages.messages[0].id)
} else {
successCallback.onSuccess(NO_MESSAGES)

View File

@@ -10,7 +10,7 @@ internal class Callback<T> private constructor(
private val onError: ErrorCallback
) {
fun interface SuccessCallback<T> {
fun onSuccess(data: T?)
fun onSuccess(data: T)
}
fun interface ErrorCallback {
@@ -20,7 +20,9 @@ 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())
callback.onSuccess.onSuccess(
response.body() ?: throw ApiException("null response", response)
)
} else {
callback.onError.onError(ApiException(response))
}

View File

@@ -61,7 +61,7 @@ internal class InitializationActivity : AppCompatActivity() {
ClientFactory.userApiWithToken(settings)
.currentUser()
.enqueue(
Callback.callInUI(this, { if (it != null) authenticated(it) }) { apiException ->
Callback.callInUI(this, { authenticated(it) }) { apiException ->
failed(apiException)
}
)

View File

@@ -194,7 +194,7 @@ internal class LoginActivity : AppCompatActivity() {
settings.url = url
binding.checkurlProgress.visibility = View.GONE
binding.checkurl.visibility = View.VISIBLE
binding.checkurl.text = getString(R.string.found_gotify_version, version?.version)
binding.checkurl.text = getString(R.string.found_gotify_version, version.version)
binding.username.visibility = View.VISIBLE
binding.username.requestFocus()
binding.password.visibility = View.VISIBLE
@@ -254,7 +254,7 @@ internal class LoginActivity : AppCompatActivity() {
val newClient = Client().name(nameProvider.text.toString())
client.createService(ClientApi::class.java)
.createClient(newClient)
.enqueue(Callback.callInUI(this, { if (it != null) onCreatedClient(it) }) {
.enqueue(Callback.callInUI(this, { onCreatedClient(it) }) {
onFailedToCreateClient()
})
}

View File

@@ -19,12 +19,10 @@ internal class ApplicationHolder(private val activity: Activity, private val cli
client.createService(ApplicationApi::class.java)
.apps
.enqueue(
Callback.callInUI(
activity,
{ apps: List<Application>? ->
if (apps != null) onReceiveApps(apps)
Callback.callInUI(activity, { onReceiveApps(it) }) { e: ApiException ->
onFailedApps(e)
}
) { e: ApiException -> onFailedApps(e) })
)
}
private fun onReceiveApps(apps: List<Application>) {

View File

@@ -80,7 +80,7 @@ internal class WebSocketService : Service() {
showForegroundNotification(getString(R.string.websocket_init))
if (lastReceivedMessage.get() == NOT_LOADED) {
missingMessageUtil.lastReceivedMessage { lastReceivedMessage.set(it ?: 0L) }
missingMessageUtil.lastReceivedMessage { lastReceivedMessage.set(it) }
}
val cm = getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
@@ -97,7 +97,7 @@ internal class WebSocketService : Service() {
.onClose { onClose() }
.onBadRequest { message -> onBadRequest(message) }
.onNetworkFailure { minutes -> onNetworkFailure(minutes) }
.onMessage { if (it != null) onMessage(it) }
.onMessage { onMessage(it) }
.onReconnected { notifyMissedNotifications() }
.start()