Skip to content

Commit 46e222d

Browse files
committed
refactor: encapsulate parse and execute in command
1 parent c17a2ea commit 46e222d

1 file changed

Lines changed: 17 additions & 27 deletions

File tree

app/src/debug/java/to/bitkit/dev/DevToolsProvider.kt

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,17 @@ class DevToolsProvider : ContentProvider() {
2222
fun lightningRepo(): LightningRepo
2323
}
2424

25-
private val lightningRepo: LightningRepo by lazy {
26-
EntryPointAccessors.fromApplication(context!!, Dependencies::class.java).lightningRepo()
25+
private val deps: Dependencies by lazy {
26+
EntryPointAccessors.fromApplication(context!!, Dependencies::class.java)
2727
}
2828

29-
override fun call(method: String, arg: String?, extras: Bundle?): Bundle {
30-
val command = DevCommand.parse(method, arg)
31-
?: return DevResult.Error("Unknown method: '$method'").toBundle()
32-
return execute { handle(command) }
33-
}
34-
35-
private fun execute(block: suspend () -> DevResult): Bundle = runCatching {
36-
ServiceQueue.LDK.blocking { block() }
29+
override fun call(method: String, arg: String?, extras: Bundle?): Bundle = runCatching {
30+
val command = requireNotNull(DevCommand.parse(method, arg))
31+
ServiceQueue.LDK.blocking { command.execute(deps) }
3732
}.getOrElse {
3833
DevResult.Error(it.message)
3934
}.toBundle()
4035

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(
46-
onSuccess = { DevResult.Invoice(it) },
47-
onFailure = { DevResult.Error(it.message) },
48-
)
49-
}
50-
5136
override fun onCreate() = true
5237
override fun getType(uri: Uri): String? = null
5338
override fun insert(uri: Uri, values: ContentValues?): Uri? = null
@@ -58,22 +43,27 @@ class DevToolsProvider : ContentProvider() {
5843

5944
private sealed interface DevCommand {
6045

61-
val method: String
46+
suspend fun execute(deps: DevToolsProvider.Dependencies): DevResult
6247

6348
data class CreateInvoice(val args: Args) : DevCommand {
49+
@Serializable
50+
data class Args(val amount: ULong? = null, val description: String = "dev-invoice")
51+
52+
override suspend fun execute(deps: DevToolsProvider.Dependencies) =
53+
deps.lightningRepo().createInvoice(args.amount, args.description).fold(
54+
onSuccess = { DevResult.Invoice(it) },
55+
onFailure = { DevResult.Error(it.message) },
56+
)
57+
6458
companion object {
6559
const val METHOD = "createInvoice"
60+
fun parse(arg: String?) = CreateInvoice(arg.deserialize<Args>())
6661
}
67-
68-
override val method get() = METHOD
69-
70-
@Serializable
71-
data class Args(val amount: ULong? = null, val description: String = "dev-invoice")
7262
}
7363

7464
companion object {
7565
fun parse(method: String, arg: String?): DevCommand? = when (method) {
76-
CreateInvoice.METHOD -> CreateInvoice(arg.deserialize<CreateInvoice.Args>())
66+
CreateInvoice.METHOD -> CreateInvoice.parse(arg)
7767
else -> null
7868
}
7969
}

0 commit comments

Comments
 (0)