@@ -128,114 +128,6 @@ class AndroidModuleGenerator {
128
128
fileSpec.writeTo(codeGenerator, false )
129
129
}
130
130
131
- private fun CodeBlock.Builder.addMethodCodeBlock (
132
- method : KSFunctionDeclaration ,
133
- wrappedModuleVarName : String ,
134
- ) {
135
- beginControlFlow(" %S ->" , method.simpleName.asString())
136
-
137
- if (method.parameters.isNotEmpty()) {
138
- //
139
- // val arguments = call.arguments as List<*>
140
- // val arg0 = arguments[0] as Arg0Type
141
- // ...
142
- //
143
- addStatement(" val arguments = call.arguments as List<*>" )
144
- method.parameters.forEachIndexed { index, parameter ->
145
- getKotlinDeserialization(
146
- parameter.type.resolve(),
147
- " arguments[$index ]" ,
148
- " arg$index " ,
149
- )
150
- }
151
- }
152
-
153
- fun withCoroutineScopeControlFlow (block : CodeBlock .Builder .() -> Unit ) {
154
- beginControlFlow(
155
- " %T(%T.Default).%M" ,
156
- CoroutineScope ,
157
- Dispatchers ,
158
- launch,
159
- )
160
- block()
161
- endControlFlow()
162
- }
163
-
164
- fun invokeUnitMethodStatement () {
165
- addStatement(
166
- " %N.%L(%L)" ,
167
- wrappedModuleVarName,
168
- method.simpleName.asString(),
169
- method.parameters.indices.joinToString(" , " ) { " arg$it " }
170
- )
171
- }
172
-
173
- fun invokeMethodStatement () {
174
- addStatement(
175
- " val resultData = %N.%L(%L)" ,
176
- wrappedModuleVarName,
177
- method.simpleName.asString(),
178
- method.parameters.indices.joinToString(" , " ) { " arg$it " }
179
- )
180
-
181
- val returnTypeDeclaration = (method.returnType ? : error(" return type is null" )).resolve().declaration
182
-
183
- addStatement(" val json = %T { encodeDefaults = true }" , JsonClassName )
184
- getKotlinSerialization(returnTypeDeclaration, " resultData" , " json" )
185
- }
186
-
187
- fun unitResultSuccessStatement () {
188
- addStatement(" result.success(null)" )
189
- }
190
-
191
- if (method.returnsUnit()) {
192
- if (method.modifiers.contains(Modifier .SUSPEND )) {
193
- //
194
- // CoroutineScope(Dispatchers.Default).launch {
195
- // wrappedModule.method(arg0, arg1, ...)
196
- // result.success(null)
197
- // }
198
- //
199
- withCoroutineScopeControlFlow {
200
- invokeUnitMethodStatement()
201
- unitResultSuccessStatement()
202
- }
203
-
204
- } else {
205
- //
206
- // wrappedModule.method(arg0, arg1, ...)
207
- // result.success(null)
208
- //
209
- invokeUnitMethodStatement()
210
- unitResultSuccessStatement()
211
- }
212
- } else {
213
- if (method.modifiers.contains(Modifier .SUSPEND )) {
214
- //
215
- // CoroutineScope(Dispatchers.Default).launch {
216
- // result.success(wrappedModule.method())
217
- // }
218
- //
219
- withCoroutineScopeControlFlow {
220
- invokeMethodStatement()
221
- }
222
- } else {
223
- //
224
- // result.success(wrappedModule.method())
225
- //
226
- invokeMethodStatement()
227
- }
228
- }
229
- endControlFlow()
230
- }
231
-
232
- fun KSValueParameter.toParameterSpec (): ParameterSpec {
233
- return ParameterSpec .builder(
234
- this .name?.asString() ? : error(" Parameter must have a name" ),
235
- this .type.toTypeName()
236
- )
237
- .build()
238
- }
239
131
240
132
241
133
/* *
@@ -256,6 +148,7 @@ class AndroidModuleGenerator {
256
148
private fun CodeBlock.Builder.addStateFlowCodeBlock (
257
149
stateFlow : KSDeclaration ,
258
150
wrappedModuleVarName : String ,
151
+ resultStatement : (resultParameter: String ) -> String = { "result.success($it)" },
259
152
) {
260
153
val flowTypeArgument = stateFlow.getStateFlowDeclarationFlowTypeArgument()
261
154
val parameters = when (stateFlow) {
@@ -339,112 +232,23 @@ class AndroidModuleGenerator {
339
232
}
340
233
endControlFlow()
341
234
342
- getKotlinSerialization(flowTypeArgument.declaration, " next" , " json" )
235
+ getKotlinSerialization(
236
+ flowTypeArgument.declaration,
237
+ " next" ,
238
+ " serializedNext" ,
239
+ " json"
240
+ )
241
+ addStatement(resultStatement(" serializedNext" ))
343
242
344
243
endControlFlow()
345
244
endControlFlow()
346
245
}
347
246
}
348
247
349
- private fun CodeBlock.Builder.getKotlinSerialization (
350
- declaration : KSDeclaration ,
351
- varName : String ,
352
- jsonInstanceVarName : String? = null,
353
- ) {
354
- if (declaration.requiresSerialization()) {
355
- require(jsonInstanceVarName != null )
356
- addStatement(
357
- " result.success(%L.%M(%L))" ,
358
- jsonInstanceVarName,
359
- encodeToString,
360
- varName,
361
- )
362
- } else when (declaration.qualifiedName?.asString()) {
363
- " kotlinx.datetime.Instant" ,
364
- " kotlinx.datetime.LocalDateTime" ,
365
- " kotlinx.datetime.LocalTime" ,
366
- " kotlinx.datetime.LocalDate" -> addStatement(" result.success(%L.toString())" , varName)
367
- " kotlin.time.Duration" -> addStatement(" result.success(%L.toIsoString())" , varName)
368
- else -> addStatement(" result.success(%L)" , varName)
369
- }
370
- }
371
-
372
- private fun CodeBlock.Builder.getKotlinDeserialization (type : KSType , varName : String , assignTo : String ) {
373
- if (type.declaration.requiresSerialization()) {
374
- addStatement(
375
- " val %L = %T.decodeFromString<%T>(%L as String)" ,
376
- assignTo,
377
- JsonClassName ,
378
- type.toTypeName(),
379
- varName,
380
- )
381
- } else when (type.declaration.qualifiedName?.asString()) {
382
- " kotlinx.datetime.Instant" -> {
383
- addStatement(
384
- " val %L = %T.parse(%L as String)" ,
385
- assignTo,
386
- Instant ,
387
- varName,
388
- )
389
- }
390
- " kotlinx.datetime.LocalDateTime" -> {
391
- addStatement(
392
- " val %L = %T.parse(%L as String)" ,
393
- assignTo,
394
- LocalDateTime ,
395
- varName,
396
- )
397
- }
398
- " kotlinx.datetime.LocalDate" -> {
399
- addStatement(
400
- " val %L = %T.parse(%L as String)" ,
401
- assignTo,
402
- LocalDate ,
403
- varName,
404
- )
405
- }
406
- " kotlinx.datetime.LocalTime" -> {
407
- addStatement(
408
- " val %L = %T.parse(%L as String)" ,
409
- assignTo,
410
- LocalTime ,
411
- varName,
412
- )
413
- }
414
- " kotlin.time.Duration" -> {
415
- addStatement(
416
- " val %L = %T.parse(%L as String)" ,
417
- assignTo,
418
- Duration ,
419
- varName,
420
- )
421
- }
422
- else -> addStatement(
423
- " val %L = %L as %T" ,
424
- assignTo,
425
- varName,
426
- type.toTypeName(),
427
- )
428
- }
429
- }
430
-
431
248
private val MethodCallHandler = ClassName (" io.flutter.plugin.common.MethodChannel" , " MethodCallHandler" )
432
249
private val BinaryMessenger = ClassName (" io.flutter.plugin.common" , " BinaryMessenger" )
433
250
private val MessageChannel = ClassName (" io.flutter.plugin.common" , " MethodChannel" )
434
251
private val EventChannel = ClassName (" io.flutter.plugin.common" , " EventChannel" )
435
252
private val MethodCall = ClassName (" io.flutter.plugin.common" , " MethodCall" )
436
253
private val MethodChannelResult = ClassName (" io.flutter.plugin.common.MethodChannel" , " Result" )
437
254
private val toEventStreamHandler = MemberName (flutterKmpPackageName, " toEventStreamHandler" )
438
-
439
- private val Duration = ClassName (" kotlin.time" , " Duration" )
440
- private val Instant = ClassName (" kotlinx.datetime" , " Instant" )
441
- private val LocalDate = ClassName (" kotlinx.datetime" , " LocalDate" )
442
- private val LocalTime = ClassName (" kotlinx.datetime" , " LocalTime" )
443
- private val LocalDateTime = ClassName (" kotlinx.datetime" , " LocalDateTime" )
444
- private val Dispatchers = ClassName (" kotlinx.coroutines" , " Dispatchers" )
445
- private val launch = MemberName (" kotlinx.coroutines" , " launch" )
446
- private val CoroutineScope = ClassName (" kotlinx.coroutines" , " CoroutineScope" )
447
- private val ListOfMember = MemberName (" kotlin.collections" , " listOf" )
448
- private val JsonClassName = ClassName (" kotlinx.serialization.json" , " Json" )
449
- private val encodeToString = MemberName (" kotlinx.serialization" , " encodeToString" )
450
- private val first = MemberName (" kotlinx.coroutines.flow" , " first" )
0 commit comments