Reformat lambda expressions

This commit is contained in:
Niko Diamadis
2023-01-12 17:54:51 +01:00
parent 317ffa6d30
commit 8bf7b8e552
11 changed files with 110 additions and 102 deletions

View File

@@ -55,12 +55,8 @@ internal object MarkwonFactory {
StyleSpan(Typeface.BOLD) StyleSpan(Typeface.BOLD)
) )
} }
.setFactory(Emphasis::class.java) { _, _ -> .setFactory(Emphasis::class.java) { _, _ -> StyleSpan(Typeface.ITALIC) }
StyleSpan(Typeface.ITALIC) .setFactory(StrongEmphasis::class.java) { _, _ -> StyleSpan(Typeface.BOLD) }
}
.setFactory(StrongEmphasis::class.java) { _, _ ->
StyleSpan(Typeface.BOLD)
}
.setFactory(BlockQuote::class.java) { _, _ -> QuoteSpan() } .setFactory(BlockQuote::class.java) { _, _ -> QuoteSpan() }
.setFactory(Code::class.java) { _, _ -> .setFactory(Code::class.java) { _, _ ->
arrayOf<Any>( arrayOf<Any>(
@@ -68,9 +64,7 @@ internal object MarkwonFactory {
TypefaceSpan("monospace") TypefaceSpan("monospace")
) )
} }
.setFactory(ListItem::class.java) { _, _ -> .setFactory(ListItem::class.java) { _, _ -> BulletSpan(bulletGapWidth) }
BulletSpan(bulletGapWidth)
}
.setFactory(Link::class.java) { _, _ -> null } .setFactory(Link::class.java) { _, _ -> null }
} }
@@ -79,9 +73,7 @@ internal object MarkwonFactory {
} }
override fun configureVisitor(builder: MarkwonVisitor.Builder) { override fun configureVisitor(builder: MarkwonVisitor.Builder) {
builder.on( builder.on(TableCell::class.java) { visitor: MarkwonVisitor, node: TableCell? ->
TableCell::class.java
) { visitor: MarkwonVisitor, node: TableCell? ->
visitor.visitChildren(node!!) visitor.visitChildren(node!!)
visitor.builder().append(' ') visitor.builder().append(' ')
} }

View File

@@ -6,20 +6,21 @@ import com.github.gotify.api.Callback
import com.github.gotify.api.Callback.SuccessCallback 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.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(
if (messages.messages.size == 1) { onSuccess = { messages ->
successCallback.onSuccess(messages.messages[0].id) if (messages.messages.size == 1) {
} else { successCallback.onSuccess(messages.messages[0].id)
successCallback.onSuccess(NO_MESSAGES) } else {
} successCallback.onSuccess(NO_MESSAGES)
} }
) {} },
onError = {}
)
) )
} }

View File

@@ -40,20 +40,16 @@ 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) } },
context.runOnUiThread { onError = { exception -> context.runOnUiThread { onError.onError(exception) } }
onSuccess.onSuccess(it) )
}
},
{
context.runOnUiThread {
onError.onError(it)
}
})
} }
fun <T> call(): retrofit2.Callback<T> { fun <T> call(): retrofit2.Callback<T> {
return call({},{}) return call(
onSuccess = {},
onError = {}
)
} }
fun <T> call(onSuccess: SuccessCallback<T>, onError: ErrorCallback): retrofit2.Callback<T> { fun <T> call(onSuccess: SuccessCallback<T>, onError: ErrorCallback): retrofit2.Callback<T> {
@@ -68,18 +64,21 @@ internal class Callback<T> private constructor(
} }
private fun <T> errorCallback(): Callback<T> { private fun <T> errorCallback(): Callback<T> {
return Callback({}, { Log.e("Error while api call", it) }) return Callback(
onSuccess = {},
onError = { exception -> Log.e("Error while api call", exception) }
)
} }
private fun <T> merge(left: Callback<T>, right: Callback<T>): Callback<T> { private fun <T> merge(left: Callback<T>, right: Callback<T>): Callback<T> {
return Callback( return Callback(
{ onSuccess = { data ->
left.onSuccess.onSuccess(it) left.onSuccess.onSuccess(data)
right.onSuccess.onSuccess(it) right.onSuccess.onSuccess(data)
}, },
{ onError = { exception ->
left.onError.onError(it) left.onError.onError(exception)
right.onError.onError(it) right.onError.onError(exception)
} }
) )
} }

View File

@@ -61,9 +61,11 @@ internal class InitializationActivity : AppCompatActivity() {
ClientFactory.userApiWithToken(settings) ClientFactory.userApiWithToken(settings)
.currentUser() .currentUser()
.enqueue( .enqueue(
Callback.callInUI(this, { authenticated(it) }) { apiException -> Callback.callInUI(
failed(apiException) this,
} onSuccess = { user -> authenticated(user) },
onError = { exception -> failed(exception) }
)
) )
} }
@@ -110,13 +112,14 @@ internal class InitializationActivity : AppCompatActivity() {
} }
private fun requestVersion(runnable: Runnable) { private fun requestVersion(runnable: Runnable) {
requestVersion({ version: VersionInfo? -> requestVersion(
if (version != null) { callback = { 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() },
}) { runnable.run() } errorCallback = { runnable.run() }
)
} }
private fun requestVersion( private fun requestVersion(

View File

@@ -47,7 +47,7 @@ internal class LogsActivity : AppCompatActivity() {
} }
if (!isDestroyed) { if (!isDestroyed) {
handler.postDelayed(this::updateLogs, 5000) handler.postDelayed({ updateLogs() }, 5000)
} }
} }

View File

@@ -220,9 +220,13 @@ internal class LoginActivity : AppCompatActivity() {
val client = ClientFactory.basicAuth(settings.url, tempSslSettings(), username, password) val client = ClientFactory.basicAuth(settings.url, tempSslSettings(), username, password)
client.createService(UserApi::class.java) client.createService(UserApi::class.java)
.currentUser() .currentUser()
.enqueue(Callback.callInUI(this, { newClientDialog(client) }) { .enqueue(
onInvalidLogin() Callback.callInUI(
}) this,
onSuccess = { newClientDialog(client) },
onError = { onInvalidLogin() }
)
)
} }
private fun onInvalidLogin() { private fun onInvalidLogin() {
@@ -240,9 +244,7 @@ internal class LoginActivity : AppCompatActivity() {
.setMessage(R.string.create_client_message) .setMessage(R.string.create_client_message)
.setView(clientName) .setView(clientName)
.setPositiveButton(R.string.create, doCreateClient(client, clientName)) .setPositiveButton(R.string.create, doCreateClient(client, clientName))
.setNegativeButton(R.string.cancel) { _, _ -> .setNegativeButton(R.string.cancel) { _, _ -> onCancelClientDialog() }
onCancelClientDialog()
}
.show() .show()
} }
@@ -254,9 +256,13 @@ 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, { onCreatedClient(it) }) { .enqueue(
onFailedToCreateClient() Callback.callInUI(
}) this,
onSuccess = { client -> onCreatedClient(client) },
onError = { onFailedToCreateClient() }
)
)
} }
} }

View File

@@ -189,14 +189,12 @@ internal class MessagesActivity :
viewModel.targetReferences.clear() viewModel.targetReferences.clear()
updateMessagesAndStopLoading(viewModel.messages[viewModel.appId]) updateMessagesAndStopLoading(viewModel.messages[viewModel.appId])
var selectedItem = menu.findItem(R.id.nav_all_messages) var selectedItem = menu.findItem(R.id.nav_all_messages)
applications.indices.forEach { applications.indices.forEach { index ->
val app = applications[it] val app = applications[index]
val item = menu.add(R.id.apps, it, APPLICATION_ORDER, app.name) val item = menu.add(R.id.apps, index, APPLICATION_ORDER, app.name)
item.isCheckable = true item.isCheckable = true
if (app.id == viewModel.appId) selectedItem = item if (app.id == viewModel.appId) selectedItem = item
val t = Utils.toDrawable(resources) { icon: Drawable? -> val t = Utils.toDrawable(resources) { icon -> item.icon = icon }
item.icon = icon
}
viewModel.targetReferences.add(t) viewModel.targetReferences.add(t)
viewModel.picassoHandler viewModel.picassoHandler
.get() .get()
@@ -238,9 +236,7 @@ internal class MessagesActivity :
getString(R.string.versions, BuildConfig.VERSION_NAME, settings.serverVersion) getString(R.string.versions, BuildConfig.VERSION_NAME, settings.serverVersion)
val refreshAll = headerView.findViewById<ImageButton>(R.id.refresh_all) val refreshAll = headerView.findViewById<ImageButton>(R.id.refresh_all)
refreshAll.setOnClickListener { refreshAll.setOnClickListener { refreshAll() }
refreshAll()
}
} }
override fun onBackPressed() { override fun onBackPressed() {
@@ -267,9 +263,7 @@ internal class MessagesActivity :
AlertDialog.Builder(ContextThemeWrapper(this, R.style.AppTheme_Dialog)) AlertDialog.Builder(ContextThemeWrapper(this, R.style.AppTheme_Dialog))
.setTitle(R.string.logout) .setTitle(R.string.logout)
.setMessage(getString(R.string.logout_confirm)) .setMessage(getString(R.string.logout_confirm))
.setPositiveButton(R.string.yes) { _, _ -> .setPositiveButton(R.string.yes) { _, _ -> doLogout() }
doLogout()
}
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.show() .show()
} else if (id == R.id.nav_logs) { } else if (id == R.id.nav_logs) {
@@ -315,9 +309,9 @@ internal class MessagesActivity :
val appId = viewModel.appId val appId = viewModel.appId
if (appId != MessageState.ALL_MESSAGES) { if (appId != MessageState.ALL_MESSAGES) {
val apps = viewModel.appsHolder.get() val apps = viewModel.appsHolder.get()
apps.indices.forEach { apps.indices.forEach { index ->
if (apps[it].id == appId) { if (apps[index].id == appId) {
selectedIndex = it selectedIndex = index
} }
} }
} }
@@ -532,10 +526,8 @@ internal class MessagesActivity :
val alert = android.app.AlertDialog.Builder(this) val alert = android.app.AlertDialog.Builder(this)
alert.setTitle(R.string.delete_app) alert.setTitle(R.string.delete_app)
alert.setMessage(R.string.ack) alert.setMessage(R.string.ack)
alert.setPositiveButton( alert.setPositiveButton(R.string.yes) { _, _ -> deleteApp(viewModel.appId) }
R.string.yes alert.setNegativeButton(R.string.no, null)
) { _, _ -> deleteApp(viewModel.appId) }
alert.setNegativeButton(R.string.no) { dialog, _ -> dialog.dismiss() }
alert.show() alert.show()
} }
return super.onContextItemSelected(item) return super.onContextItemSelected(item)
@@ -548,9 +540,12 @@ internal class MessagesActivity :
client.createService(ApplicationApi::class.java) client.createService(ApplicationApi::class.java)
.deleteApp(appId) .deleteApp(appId)
.enqueue( .enqueue(
Callback.callInUI(this, { refreshAll() }) { Callback.callInUI(
Utils.showSnackBar(this, getString(R.string.error_delete_app)) this,
}) onSuccess = { refreshAll() },
onError = { Utils.showSnackBar(this, getString(R.string.error_delete_app)) }
)
)
} }
private suspend fun loadMore(appId: Long) { private suspend fun loadMore(appId: Long) {

View File

@@ -2,7 +2,6 @@ package com.github.gotify.messages.provider
import android.app.Activity import android.app.Activity
import com.github.gotify.Utils import com.github.gotify.Utils
import com.github.gotify.api.ApiException
import com.github.gotify.api.Callback import com.github.gotify.api.Callback
import com.github.gotify.client.ApiClient import com.github.gotify.client.ApiClient
import com.github.gotify.client.api.ApplicationApi import com.github.gotify.client.api.ApplicationApi
@@ -19,9 +18,11 @@ 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(activity, { onReceiveApps(it) }) { e: ApiException -> Callback.callInUI(
onFailedApps(e) activity,
} onSuccess = { apps -> onReceiveApps(apps) },
onError = { onFailedApps() }
)
) )
} }
@@ -30,7 +31,7 @@ internal class ApplicationHolder(private val activity: Activity, private val cli
if (onUpdate != null) onUpdate!!.run() if (onUpdate != null) onUpdate!!.run()
} }
private fun onFailedApps(e: ApiException) { private fun onFailedApps() {
Utils.showSnackBar(activity, "Could not request applications, see logs.") Utils.showSnackBar(activity, "Could not request applications, see logs.")
if (onUpdateFailed != null) onUpdateFailed!!.run() if (onUpdateFailed != null) onUpdateFailed!!.run()
} }

View File

@@ -66,10 +66,15 @@ internal class PicassoHandler(private val context: Context, private val settings
ClientFactory.clientToken(settings.url, settings.sslSettings(), settings.token) ClientFactory.clientToken(settings.url, settings.sslSettings(), settings.token)
.createService(ApplicationApi::class.java) .createService(ApplicationApi::class.java)
.apps .apps
.enqueue(Callback.call({ apps -> .enqueue(
appIdToAppImage.clear() Callback.call(
appIdToAppImage.putAll(MessageImageCombiner.appIdToImage(apps ?: emptyList())) onSuccess = { apps ->
}) { appIdToAppImage.clear() }) appIdToAppImage.clear()
appIdToAppImage.putAll(MessageImageCombiner.appIdToImage(apps))
},
onError = { appIdToAppImage.clear() }
)
)
} }
fun get() = picasso fun get() = picasso

View File

@@ -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 { onMessage(it) } .onMessage { message -> onMessage(message) }
.onReconnected { notifyMissedNotifications() } .onReconnected { notifyMissedNotifications() }
.start() .start()
@@ -113,17 +113,23 @@ internal class WebSocketService : Service() {
) )
ClientFactory.userApiWithToken(settings) ClientFactory.userApiWithToken(settings)
.currentUser() .currentUser()
.enqueue(Callback.call({ doReconnect() }) { exception -> .enqueue(
if (exception.code == 401) { Callback.call(
showForegroundNotification( onSuccess = { doReconnect() },
getString(R.string.user_action), onError = { exception ->
getString(R.string.websocket_closed_logout) if (exception.code == 401) {
) showForegroundNotification(
} else { getString(R.string.user_action),
Log.i("WebSocket closed but the user still authenticated, trying to reconnect") getString(R.string.websocket_closed_logout)
doReconnect() )
} } else {
}) Log.i("WebSocket closed but the user still authenticated, " +
"trying to reconnect")
doReconnect()
}
}
)
)
} }
private fun doReconnect() { private fun doReconnect() {

View File

@@ -153,8 +153,8 @@ internal class ShareActivity : AppCompatActivity() {
private fun populateSpinner(apps: List<Application>) { private fun populateSpinner(apps: List<Application>) {
val appNameList = mutableListOf<String>() val appNameList = mutableListOf<String>()
apps.forEach { app -> apps.forEach {
appNameList.add(app.name) appNameList.add(it.name)
} }
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, appNameList) val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, appNameList)