Disallow null response of SuccessCallback
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -59,12 +59,12 @@ internal class InitializationActivity : AppCompatActivity() {
|
||||
|
||||
private fun tryAuthenticate() {
|
||||
ClientFactory.userApiWithToken(settings)
|
||||
.currentUser()
|
||||
.enqueue(
|
||||
Callback.callInUI(this, { if (it != null) authenticated(it) }) { apiException ->
|
||||
failed(apiException)
|
||||
}
|
||||
)
|
||||
.currentUser()
|
||||
.enqueue(
|
||||
Callback.callInUI(this, { authenticated(it) }) { apiException ->
|
||||
failed(apiException)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private fun failed(exception: ApiException) {
|
||||
|
||||
@@ -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()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
) { e: ApiException -> onFailedApps(e) })
|
||||
Callback.callInUI(activity, { onReceiveApps(it) }) { e: ApiException ->
|
||||
onFailedApps(e)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private fun onReceiveApps(apps: List<Application>) {
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user