Skip to content

Commit fee419e

Browse files
Add kotlin linter plugin to and update all the code
This code introduces a koltin linter, in particular a plugin called kotlinter, and it is available on https://github.com/jeremymailen/kotlinter-gradle This commit adds the support in all the app and also in the Github cli, so now all the commit need to pass also the kotlin format check. To make easy the support of the guideline a .editorconfig is added but an additional configuration of Android studio is needed. The Android Studio configuration that is needed is Settings -> Editor -> CodeStyle -> Kotlin -> import -> enable Use single name import. Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 6c4b09d commit fee419e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+409
-299
lines changed

.editorconfig

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Note that in this case 'import-ordering' rule will be active and 'indent' will be disabled
2+
[api/*.{kt,kts}]
3+
root = true
4+
disabled_rules=indent
5+
6+
# Comma-separated list of rules to disable (Since 0.34.0)
7+
# Note that rules in any ruleset other than the standard ruleset will need to be prefixed
8+
# by the ruleset identifier.
9+
disabled_rules=no-wildcard-imports,experimental:annotation,my-custom-ruleset
10+
11+
# Defines the imports layout. The layout can be composed by the following symbols:
12+
# "*" - wildcard. There must be at least one entry of a single wildcard to match all other imports. Matches anything after a specified symbol/import as well.
13+
# "|" - blank line. Supports only single blank lines between imports. No blank line is allowed in the beginning or end of the layout.
14+
# "^" - alias import, e.g. "^android.*" will match all android alias imports, "^" will match all other alias imports.
15+
# import paths - these can be full paths, e.g. "java.util.List.*" as well as wildcard paths, e.g. "kotlin.**"
16+
# Examples (we use ij_kotlin_imports_layout to set an imports layout for both ktlint and IDEA via a single property):
17+
# ij_kotlin_imports_layout=* # alphabetical with capital letters before lower case letters (e.g. Z before a), no blank lines
18+
# ij_kotlin_imports_layout=*,java.**,javax.**,kotlin.**,^ # default IntelliJ IDEA style, same as alphabetical, but with "java", "javax", "kotlin" and alias imports in the end of the imports list
19+
# ij_kotlin_imports_layout=android.**,|,^org.junit.**,kotlin.io.Closeable.*,|,*,^ # custom imports layout
20+
21+
# According to https://kotlinlang.org/docs/reference/coding-conventions.html#names-for-test-methods it is acceptable to write method names
22+
# in natural language. When using natural language, the description tends to be longer. Allow lines containing an identifier between
23+
# backticks to be longer than the maximum line length. (Since 0.41.0)
24+
[**/test/**.kt]
25+
ktlint_ignore_back_ticked_identifier=true

.github/workflows/android-master.yml

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ jobs:
1919
uses: actions/setup-java@v1
2020
with:
2121
java-version: 1.8
22+
- name: Check koltin formatting
23+
run: bash ./gradlew lintKotlin
2224
- name: Build debug APK
2325
run: bash ./gradlew assembleDebug --stacktrace
2426
- name: Upload APK

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ Read the following instructions at [Tor on clightning](https://lightning.readthe
5757

5858
* [in Linux using cmdline tools](doc/cmdline-tools-setup.md)
5959

60+
## Code Style
61+
[![ktlint](https://img.shields.io/badge/code%20style-%E2%9D%A4-FF4081.svg)](https://ktlint.github.io/)
62+
63+
> We live in a world where robots can drive a car, so we shouldn't just write code, we should write elegant code.
64+
65+
This repository use [ktlint](https://github.com/pinterest/ktlint) to maintains the code of the repository elegant, so
66+
before submit the code check the Kotlin format with the following command on the root of the directory
67+
68+
```bash
69+
./gradlew formatKotlin
70+
```
71+
6072
## References
6173

6274
- [ABCore](https://github.com/greenaddress/abcore) Android Bitcoin Core wallet

app/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
apply plugin: 'com.android.application'
22
apply plugin: 'kotlin-android'
33
apply plugin: 'kotlin-android-extensions'
4+
apply plugin: "org.jmailen.kotlinter"
45

56
android {
67
compileSdkVersion 28

app/src/androidTest/java/com/lvaccaro/lamp/ExampleInstrumentedTest.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package com.lvaccaro.lamp
22

33
import androidx.test.platform.app.InstrumentationRegistry
44
import androidx.test.runner.AndroidJUnit4
5-
5+
import org.junit.Assert.assertEquals
66
import org.junit.Test
77
import org.junit.runner.RunWith
88

9-
import org.junit.Assert.*
10-
119
/**
1210
* Instrumented test, which will execute on an Android device.
1311
*

app/src/main/java/com/lvaccaro/lamp/LightningCli.kt

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ class LightningCli {
2525
String.format("%s/cli/%s", binaryDir.canonicalPath, command),
2626
String.format("--network=%s", network),
2727
String.format("--lightning-dir=%s", lightningDir.path),
28-
String.format("--%s", if (json == true) "json" else "raw" ))
28+
String.format("--%s", if (json == true) "json" else "raw")
29+
)
2930

3031
val pb = ProcessBuilder((args + options).asList())
3132
pb.directory(binaryDir)
32-
//pb.redirectErrorStream(true)
33+
// pb.redirectErrorStream(true)
3334

3435
val process = pb.start()
3536
val code = process.waitFor()
@@ -38,15 +39,15 @@ class LightningCli {
3839
val input = process.inputStream.toText()
3940
log.info(error)
4041
log.info(input)
41-
throw Exception(if(!error.isEmpty()) error else input)
42+
throw Exception(if (!error.isEmpty()) error else input)
4243
}
4344
return process.inputStream
4445
}
4546
}
4647

4748
// extension to convert inputStream in text
4849
fun InputStream.toText(): String {
49-
val reader = bufferedReader()
50+
val reader = bufferedReader()
5051
val builder = StringBuilder()
5152
var line = reader.readLine()
5253
while (line != null) {
@@ -71,4 +72,4 @@ fun Context.rootDir(): File {
7172
return noBackupFilesDir
7273
}
7374
return filesDir
74-
}
75+
}

app/src/main/java/com/lvaccaro/lamp/MainActivity.kt

+45-20
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import android.Manifest
44
import android.app.Activity
55
import android.app.ActivityManager
66
import android.app.DownloadManager
7-
import android.content.*
7+
import android.content.BroadcastReceiver
8+
import android.content.ClipboardManager
9+
import android.content.Context
10+
import android.content.Intent
11+
import android.content.IntentFilter
812
import android.content.pm.PackageManager
913
import android.net.Uri
1014
import android.os.Build
@@ -25,28 +29,48 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager
2529
import androidx.preference.PreferenceManager
2630
import androidx.recyclerview.widget.DividerItemDecoration
2731
import androidx.recyclerview.widget.LinearLayoutManager
28-
import com.lvaccaro.lamp.activities.*
32+
import com.lvaccaro.lamp.activities.BuildInvoiceActivity
33+
import com.lvaccaro.lamp.activities.ChannelsActivity
34+
import com.lvaccaro.lamp.activities.ConsoleActivity
35+
import com.lvaccaro.lamp.activities.LogActivity
36+
import com.lvaccaro.lamp.activities.ScanActivity
37+
import com.lvaccaro.lamp.activities.SendActivity
38+
import com.lvaccaro.lamp.activities.SettingsActivity
39+
import com.lvaccaro.lamp.activities.UriResultActivity
2940
import com.lvaccaro.lamp.adapters.Balance
3041
import com.lvaccaro.lamp.adapters.BalanceAdapter
3142
import com.lvaccaro.lamp.fragments.PeerInfoFragment
3243
import com.lvaccaro.lamp.fragments.WithdrawFragment
33-
import com.lvaccaro.lamp.handlers.*
44+
import com.lvaccaro.lamp.handlers.BrokenStatus
45+
import com.lvaccaro.lamp.handlers.NewBlockHandler
46+
import com.lvaccaro.lamp.handlers.NewChannelPayment
47+
import com.lvaccaro.lamp.handlers.NewTransaction
48+
import com.lvaccaro.lamp.handlers.NodeUpHandler
49+
import com.lvaccaro.lamp.handlers.PaidInvoice
50+
import com.lvaccaro.lamp.handlers.ShutdownNode
3451
import com.lvaccaro.lamp.services.LightningService
3552
import com.lvaccaro.lamp.services.TorService
3653
import com.lvaccaro.lamp.utils.Archive
3754
import com.lvaccaro.lamp.utils.SimulatorPlugin
3855
import com.lvaccaro.lamp.utils.UI
3956
import com.lvaccaro.lamp.views.HistoryBottomSheet
40-
import kotlinx.android.synthetic.main.activity_main.*
41-
import kotlinx.android.synthetic.main.content_main_off.*
42-
import kotlinx.android.synthetic.main.content_main_on.*
43-
import kotlinx.android.synthetic.main.fragment_history.*
57+
import kotlinx.android.synthetic.main.activity_main.contentMainOff
58+
import kotlinx.android.synthetic.main.activity_main.contentMainOn
59+
import kotlinx.android.synthetic.main.content_main_off.powerImageView
60+
import kotlinx.android.synthetic.main.content_main_off.statusText
61+
import kotlinx.android.synthetic.main.content_main_off.versionText
62+
import kotlinx.android.synthetic.main.content_main_on.balanceText
63+
import kotlinx.android.synthetic.main.content_main_on.floatingActionButton
64+
import kotlinx.android.synthetic.main.content_main_on.receiveButton
65+
import kotlinx.android.synthetic.main.content_main_on.recyclerView
66+
import kotlinx.android.synthetic.main.content_main_on.sendButton
67+
import kotlinx.android.synthetic.main.content_main_on.syncText
68+
import kotlinx.android.synthetic.main.fragment_history.bottomSheet
4469
import org.jetbrains.anko.doAsync
4570
import org.json.JSONArray
4671
import java.io.File
4772
import java.util.logging.Logger
4873

49-
5074
class MainActivity : UriResultActivity() {
5175

5276
private val REQUEST_SCAN = 102
@@ -178,7 +202,8 @@ class MainActivity : UriResultActivity() {
178202

179203
override fun onRequestPermissionsResult(
180204
requestCode: Int,
181-
permissions: Array<String>, grantResults: IntArray
205+
permissions: Array<String>,
206+
grantResults: IntArray
182207
) {
183208
when (requestCode) {
184209
WRITE_REQUEST_CODE -> {
@@ -280,13 +305,14 @@ class MainActivity : UriResultActivity() {
280305
val peers: JSONArray = listPeers["peers"] as JSONArray
281306

282307
runOnUiThread {
283-
balanceText.text = "${(SimulatorPlugin.funds(listPeers).toDouble()/1000)} sat"
308+
balanceText.text = "${(SimulatorPlugin.funds(listPeers).toDouble() / 1000)} sat"
284309
recyclerView.adapter = BalanceAdapter(
285310
arrayListOf(
286-
Balance("Spendable in channels", "${peers.length()} Peers", "${SimulatorPlugin.funds(listPeers).toDouble()/1000} sat"),
287-
Balance("Locked in channels", "${channels.length()} Channels", "${SimulatorPlugin.offchain(listFunds).toDouble()/1000} sat"),
311+
Balance("Spendable in channels", "${peers.length()} Peers", "${SimulatorPlugin.funds(listPeers).toDouble() / 1000} sat"),
312+
Balance("Locked in channels", "${channels.length()} Channels", "${SimulatorPlugin.offchain(listFunds).toDouble() / 1000} sat"),
288313
Balance("Bitcoin on chain", "${outputs.length()} Transactions", "${SimulatorPlugin.onchain(listFunds)} sat")
289-
), null
314+
),
315+
null
290316
)
291317
}
292318
}
@@ -335,17 +361,16 @@ class MainActivity : UriResultActivity() {
335361
doAsync {
336362
try {
337363
Archive.uncompressXZ(tarFile, rootDir())
338-
} catch(ex: Exception) {
364+
} catch (ex: Exception) {
339365
Log.e(TAG, "Error during uncompressXZ operation %s".format(ex.localizedMessage))
340366
runOnUiThread {
341367
UI.snackBar(this@MainActivity, "Error During download lightning node")
342368
}
343-
}finally {
369+
} finally {
344370
runOnUiThread {
345371
powerOff()
346372
}
347373
}
348-
349374
}
350375
} else {
351376
statusText.text =
@@ -388,7 +413,7 @@ class MainActivity : UriResultActivity() {
388413
title = alias
389414
powerImageView.on()
390415
val delta = blockcount - blockheight
391-
syncText.text = if (delta > 0) "Syncing blocks -${delta}" else ""
416+
syncText.text = if (delta > 0) "Syncing blocks -$delta" else ""
392417
}
393418
} catch (e: Exception) {
394419
log.info("---" + e.localizedMessage + "---")
@@ -636,7 +661,7 @@ class MainActivity : UriResultActivity() {
636661

637662
private val notificationReceiver = object : BroadcastReceiver() {
638663
// I can create a mediator that I can use to call all method inside the
639-
//lightning-cli and return a json if the answer i ok or I throw an execeptions
664+
// lightning-cli and return a json if the answer i ok or I throw an execeptions
640665

641666
override fun onReceive(context: Context?, intent: Intent?) {
642667
Log.d(TAG, "onReceive action ${intent?.action}")
@@ -650,7 +675,7 @@ class MainActivity : UriResultActivity() {
650675
NewBlockHandler.NOTIFICATION -> runOnUiThread {
651676
val blockheight = intent.getIntExtra("height", 0)
652677
val delta = blockcount - blockheight
653-
statusText.text = if (delta > 0) "Syncing blocks -${delta}" else ""
678+
statusText.text = if (delta > 0) "Syncing blocks -$delta" else ""
654679
}
655680
BrokenStatus.NOTIFICATION -> runOnUiThread {
656681
val message = intent.getStringExtra("message")
@@ -662,7 +687,7 @@ class MainActivity : UriResultActivity() {
662687
NewTransaction.NOTIFICATION, NewChannelPayment.NOTIFICATION, PaidInvoice.NOTIFICATION -> doAsync {
663688
updateBalanceView(context)
664689
}
665-
NodeUpHandler.NOTIFICATION -> {
690+
NodeUpHandler.NOTIFICATION -> {
666691
isRunning = true
667692
}
668693
}

app/src/main/java/com/lvaccaro/lamp/activities/BuildInvoiceActivity.kt

+17-7
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,24 @@ import com.lvaccaro.lamp.LightningCli
1010
import com.lvaccaro.lamp.R
1111
import com.lvaccaro.lamp.toJSONObject
1212
import com.lvaccaro.lamp.utils.UI
13-
import kotlinx.android.synthetic.main.activity_build_invoice.*
14-
import kotlinx.android.synthetic.main.list_balance.view.*
15-
import kotlinx.android.synthetic.main.list_tx.*
13+
import kotlinx.android.synthetic.main.activity_build_invoice.balanceText
14+
import kotlinx.android.synthetic.main.activity_build_invoice.btcButton
15+
import kotlinx.android.synthetic.main.activity_build_invoice.btclnLayout
16+
import kotlinx.android.synthetic.main.activity_build_invoice.copyButton
17+
import kotlinx.android.synthetic.main.activity_build_invoice.copyShareLayout
18+
import kotlinx.android.synthetic.main.activity_build_invoice.descriptionText
19+
import kotlinx.android.synthetic.main.activity_build_invoice.expiredText
20+
import kotlinx.android.synthetic.main.activity_build_invoice.expiredTitle
21+
import kotlinx.android.synthetic.main.activity_build_invoice.labelText
22+
import kotlinx.android.synthetic.main.activity_build_invoice.lightningButton
23+
import kotlinx.android.synthetic.main.activity_build_invoice.qrImage
24+
import kotlinx.android.synthetic.main.activity_build_invoice.shareButton
1625
import org.jetbrains.anko.contentView
1726
import org.jetbrains.anko.doAsync
1827
import org.json.JSONObject
1928
import java.lang.Exception
2029
import java.text.SimpleDateFormat
21-
import java.util.*
22-
30+
import java.util.Date
2331

2432
class BuildInvoiceActivity : AppCompatActivity() {
2533

@@ -59,13 +67,15 @@ class BuildInvoiceActivity : AppCompatActivity() {
5967

6068
fun invoice(amount: String, label: String, description: String) {
6169
try {
62-
val res = cli.exec(this,
70+
val res = cli.exec(
71+
this,
6372
arrayOf(
6473
"invoice",
6574
amount,
6675
label,
6776
description
68-
), true
77+
),
78+
true
6979
).toJSONObject()
7080
runOnUiThread { showInvoice(res["bolt11"] as String) }
7181
} catch (e: Exception) {

0 commit comments

Comments
 (0)