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 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(successCallback: SuccessCallback<Long>) {
api.getMessages(1, 0L).enqueue( api.getMessages(1, 0L).enqueue(
Callback.call({ messages: PagedMessages? -> Callback.call({ messages: PagedMessages ->
if (messages!!.messages.size == 1) { if (messages.messages.size == 1) {
successCallback.onSuccess(messages.messages[0].id) successCallback.onSuccess(messages.messages[0].id)
} else { } else {
successCallback.onSuccess(NO_MESSAGES) successCallback.onSuccess(NO_MESSAGES)

View File

@@ -10,7 +10,7 @@ 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(data: T)
} }
fun interface ErrorCallback { 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> { 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(response.body()) callback.onSuccess.onSuccess(
response.body() ?: throw ApiException("null response", response)
)
} else { } else {
callback.onError.onError(ApiException(response)) callback.onError.onError(ApiException(response))
} }

View File

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

View File

@@ -194,7 +194,7 @@ internal class LoginActivity : AppCompatActivity() {
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
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.visibility = View.VISIBLE
binding.username.requestFocus() binding.username.requestFocus()
binding.password.visibility = View.VISIBLE binding.password.visibility = View.VISIBLE
@@ -254,7 +254,7 @@ internal class LoginActivity : AppCompatActivity() {
val newClient = Client().name(nameProvider.text.toString()) val newClient = Client().name(nameProvider.text.toString())
client.createService(ClientApi::class.java) client.createService(ClientApi::class.java)
.createClient(newClient) .createClient(newClient)
.enqueue(Callback.callInUI(this, { if (it != null) onCreatedClient(it) }) { .enqueue(Callback.callInUI(this, { onCreatedClient(it) }) {
onFailedToCreateClient() onFailedToCreateClient()
}) })
} }

View File

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

View File

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