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)
)
}
.setFactory(Emphasis::class.java) { _, _ ->
StyleSpan(Typeface.ITALIC)
}
.setFactory(StrongEmphasis::class.java) { _, _ ->
StyleSpan(Typeface.BOLD)
}
.setFactory(Emphasis::class.java) { _, _ -> StyleSpan(Typeface.ITALIC) }
.setFactory(StrongEmphasis::class.java) { _, _ -> StyleSpan(Typeface.BOLD) }
.setFactory(BlockQuote::class.java) { _, _ -> QuoteSpan() }
.setFactory(Code::class.java) { _, _ ->
arrayOf<Any>(
@@ -68,9 +64,7 @@ internal object MarkwonFactory {
TypefaceSpan("monospace")
)
}
.setFactory(ListItem::class.java) { _, _ ->
BulletSpan(bulletGapWidth)
}
.setFactory(ListItem::class.java) { _, _ -> BulletSpan(bulletGapWidth) }
.setFactory(Link::class.java) { _, _ -> null }
}
@@ -79,9 +73,7 @@ internal object MarkwonFactory {
}
override fun configureVisitor(builder: MarkwonVisitor.Builder) {
builder.on(
TableCell::class.java
) { visitor: MarkwonVisitor, node: TableCell? ->
builder.on(TableCell::class.java) { visitor: MarkwonVisitor, node: TableCell? ->
visitor.visitChildren(node!!)
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.client.api.MessageApi
import com.github.gotify.client.model.Message
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>) {
api.getMessages(1, 0L).enqueue(
Callback.call({ messages: PagedMessages ->
if (messages.messages.size == 1) {
successCallback.onSuccess(messages.messages[0].id)
} else {
successCallback.onSuccess(NO_MESSAGES)
}
}
) {}
Callback.call(
onSuccess = { messages ->
if (messages.messages.size == 1) {
successCallback.onSuccess(messages.messages[0].id)
} else {
successCallback.onSuccess(NO_MESSAGES)
}
},
onError = {}
)
)
}

View File

@@ -40,20 +40,16 @@ internal class Callback<T> private constructor(
onError: ErrorCallback
): retrofit2.Callback<T> {
return call(
{
context.runOnUiThread {
onSuccess.onSuccess(it)
}
},
{
context.runOnUiThread {
onError.onError(it)
}
})
onSuccess = { data -> context.runOnUiThread { onSuccess.onSuccess(data) } },
onError = { exception -> context.runOnUiThread { onError.onError(exception) } }
)
}
fun <T> call(): retrofit2.Callback<T> {
return call({},{})
return call(
onSuccess = {},
onError = {}
)
}
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> {
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> {
return Callback(
{
left.onSuccess.onSuccess(it)
right.onSuccess.onSuccess(it)
onSuccess = { data ->
left.onSuccess.onSuccess(data)
right.onSuccess.onSuccess(data)
},
{
left.onError.onError(it)
right.onError.onError(it)
onError = { exception ->
left.onError.onError(exception)
right.onError.onError(exception)
}
)
}

View File

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

View File

@@ -47,7 +47,7 @@ internal class LogsActivity : AppCompatActivity() {
}
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)
client.createService(UserApi::class.java)
.currentUser()
.enqueue(Callback.callInUI(this, { newClientDialog(client) }) {
onInvalidLogin()
})
.enqueue(
Callback.callInUI(
this,
onSuccess = { newClientDialog(client) },
onError = { onInvalidLogin() }
)
)
}
private fun onInvalidLogin() {
@@ -240,9 +244,7 @@ internal class LoginActivity : AppCompatActivity() {
.setMessage(R.string.create_client_message)
.setView(clientName)
.setPositiveButton(R.string.create, doCreateClient(client, clientName))
.setNegativeButton(R.string.cancel) { _, _ ->
onCancelClientDialog()
}
.setNegativeButton(R.string.cancel) { _, _ -> onCancelClientDialog() }
.show()
}
@@ -254,9 +256,13 @@ internal class LoginActivity : AppCompatActivity() {
val newClient = Client().name(nameProvider.text.toString())
client.createService(ClientApi::class.java)
.createClient(newClient)
.enqueue(Callback.callInUI(this, { onCreatedClient(it) }) {
onFailedToCreateClient()
})
.enqueue(
Callback.callInUI(
this,
onSuccess = { client -> onCreatedClient(client) },
onError = { onFailedToCreateClient() }
)
)
}
}

View File

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

View File

@@ -2,7 +2,6 @@ package com.github.gotify.messages.provider
import android.app.Activity
import com.github.gotify.Utils
import com.github.gotify.api.ApiException
import com.github.gotify.api.Callback
import com.github.gotify.client.ApiClient
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)
.apps
.enqueue(
Callback.callInUI(activity, { onReceiveApps(it) }) { e: ApiException ->
onFailedApps(e)
}
Callback.callInUI(
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()
}
private fun onFailedApps(e: ApiException) {
private fun onFailedApps() {
Utils.showSnackBar(activity, "Could not request applications, see logs.")
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)
.createService(ApplicationApi::class.java)
.apps
.enqueue(Callback.call({ apps ->
appIdToAppImage.clear()
appIdToAppImage.putAll(MessageImageCombiner.appIdToImage(apps ?: emptyList()))
}) { appIdToAppImage.clear() })
.enqueue(
Callback.call(
onSuccess = { apps ->
appIdToAppImage.clear()
appIdToAppImage.putAll(MessageImageCombiner.appIdToImage(apps))
},
onError = { appIdToAppImage.clear() }
)
)
}
fun get() = picasso

View File

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

View File

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