-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
149 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
flutter-kmp/src/commonMain/kotlin/de/voize/flutterkmp/utils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package de.voize.flutterkmp | ||
|
||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.buildClassSerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonArray | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.JsonEncoder | ||
import kotlinx.serialization.json.JsonNull | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.JsonPrimitive | ||
import kotlinx.serialization.modules.serializersModuleOf | ||
|
||
internal class DynamicLookupSerializer : KSerializer<Any> { | ||
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("Any") | ||
|
||
override fun serialize(encoder: Encoder, value: Any) { | ||
val jsonEncoder = encoder as JsonEncoder | ||
val jsonElement = serializeAny(value) | ||
jsonEncoder.encodeJsonElement(jsonElement) | ||
} | ||
|
||
private fun serializeAny(value: Any?): JsonElement = when (value) { | ||
null -> JsonNull | ||
is Map<*, *> -> { | ||
val mapContents = value.entries.associate { mapEntry -> | ||
mapEntry.key.toString() to serializeAny(mapEntry.value) | ||
} | ||
JsonObject(mapContents) | ||
} | ||
|
||
is List<*> -> { | ||
val arrayContents = value.map { listEntry -> serializeAny(listEntry) } | ||
JsonArray(arrayContents) | ||
} | ||
|
||
is Number -> JsonPrimitive(value) | ||
is Boolean -> JsonPrimitive(value) | ||
is String -> JsonPrimitive(value) | ||
else -> error("Unsupported type ${value::class}") | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): Any { | ||
error("Unsupported") | ||
} | ||
} | ||
|
||
internal val flowToFlutterJson = Json { | ||
serializersModule = serializersModuleOf(DynamicLookupSerializer()) | ||
encodeDefaults = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package de.voize.flutterkmp | ||
|
||
import cocoapods.Flutter.FlutterError | ||
import cocoapods.Flutter.FlutterEventSink | ||
import kotlinx.coroutines.flow.Flow | ||
import cocoapods.Flutter.FlutterStreamHandlerProtocol | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.serialization.SerializationStrategy | ||
import kotlinx.serialization.serializer | ||
import platform.darwin.NSObject | ||
|
||
|
||
inline fun <reified T> Flow<T>.toEventStreamHandler(): NSObject = | ||
toEventStreamHandler(serializer<T>()) | ||
|
||
fun <T> Flow<T>.toEventStreamHandler(serializer: SerializationStrategy<T>): NSObject { | ||
return object : FlutterStreamHandlerProtocol, NSObject() { | ||
var job: Job? = null | ||
|
||
override fun onListenWithArguments( | ||
arguments: Any?, | ||
eventSink: FlutterEventSink, | ||
): FlutterError? { | ||
if (eventSink == null) { | ||
return FlutterError.errorWithCode("no_event_sink", "No event sink available", null) | ||
} | ||
|
||
job = CoroutineScope(Dispatchers.Default).launch { | ||
this@toEventStreamHandler.collect { | ||
withContext(Dispatchers.Main) { | ||
eventSink(flowToFlutterJson.encodeToString(serializer, it)) | ||
} | ||
} | ||
} | ||
return null | ||
} | ||
|
||
override fun onCancelWithArguments(arguments: Any?): FlutterError? { | ||
job?.cancel() | ||
job = null | ||
return null | ||
} | ||
} | ||
} |