@@ -13,15 +13,9 @@ import kotlinx.serialization.Serializable
1313import kotlinx.serialization.json.Json
1414import to.bitkit.async.ServiceQueue
1515import to.bitkit.repositories.LightningRepo
16- import to.bitkit.utils.Logger
1716
1817class DevToolsProvider : ContentProvider () {
1918
20- companion object {
21- private const val TAG = " DevToolsProvider"
22- private const val KEY_DESCRIPTION = " description"
23- }
24-
2519 @EntryPoint
2620 @InstallIn(SingletonComponent ::class )
2721 interface Dependencies {
@@ -33,28 +27,24 @@ class DevToolsProvider : ContentProvider() {
3327 }
3428
3529 override fun call (method : String , arg : String? , extras : Bundle ? ): Bundle {
36- val command = DevCommand .from (method)
30+ val command = DevCommand .parse (method, arg )
3731 ? : return DevResult .Error (" Unknown method: '$method '" ).toBundle()
38- return execute(command) { handle(command, arg, extras ) }
32+ return execute { handle(command) }
3933 }
4034
41- private fun execute (command : DevCommand , block : suspend () -> DevResult ): Bundle = runCatching {
35+ private fun execute (block : suspend () -> DevResult ): Bundle = runCatching {
4236 ServiceQueue .LDK .blocking { block() }
4337 }.getOrElse {
44- Logger .error(" ${command.method} failed" , it, context = TAG )
45- DevResult .Error (it.message ? : " Failed command: '$command .method'" )
38+ DevResult .Error (it.message)
4639 }.toBundle()
4740
48- private suspend fun handle (command : DevCommand , arg : String? , extras : Bundle ? ): DevResult = when (command) {
49- DevCommand .CREATE_INVOICE -> createInvoice(arg, extras)
50- }
51-
52- private suspend fun createInvoice (arg : String? , extras : Bundle ? ): DevResult {
53- val amountSats = arg?.toULongOrNull()
54- val description = extras?.getString(KEY_DESCRIPTION ) ? : " dev-invoice"
55- return lightningRepo.createInvoice(amountSats, description).fold(
41+ private suspend fun handle (command : DevCommand ): DevResult = when (command) {
42+ is DevCommand .CreateInvoice -> lightningRepo.createInvoice(
43+ command.args.amount,
44+ command.args.description,
45+ ).fold(
5646 onSuccess = { DevResult .Invoice (it) },
57- onFailure = { DevResult .Error (it.message ? : " Failed command: 'createInvoice'. " ) },
47+ onFailure = { DevResult .Error (it.message) },
5848 )
5949 }
6050
@@ -66,25 +56,43 @@ class DevToolsProvider : ContentProvider() {
6656 override fun query (uri : Uri , proj : Array <String >? , sel : String? , args : Array <String >? , sort : String? ) = null
6757}
6858
69- enum class DevCommand (val method : String ) {
70- CREATE_INVOICE (" createInvoice" );
59+ private sealed interface DevCommand {
60+
61+ val method: String
62+
63+ data class CreateInvoice (val args : Args ) : DevCommand {
64+ companion object {
65+ const val METHOD = " createInvoice"
66+ }
67+
68+ override val method get() = METHOD
69+
70+ @Serializable
71+ data class Args (val amount : ULong? = null , val description : String = " dev-invoice" )
72+ }
7173
7274 companion object {
73- fun from (method : String ) = entries.find { it.method == method }
75+ fun parse (method : String , arg : String? ): DevCommand ? = when (method) {
76+ CreateInvoice .METHOD -> CreateInvoice (arg.deserialize<CreateInvoice .Args >())
77+ else -> null
78+ }
7479 }
7580}
7681
7782@Serializable
78- sealed interface DevResult {
83+ private sealed interface DevResult {
7984 @Serializable
8085 data class Invoice (val bolt11 : String ) : DevResult
8186
8287 @Serializable
83- data class Error (val message : String ) : DevResult
88+ data class Error (val message : String? = null ) : DevResult
8489
8590 fun toBundle () = bundleOf(KEY_RESULT to Json .encodeToString(this ))
8691
8792 companion object {
8893 private const val KEY_RESULT = " result"
8994 }
9095}
96+
97+ private inline fun <reified T > String?.deserialize (): T =
98+ if (isNullOrBlank()) Json .decodeFromString(" {}" ) else Json .decodeFromString(this )
0 commit comments