-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Breez SDK Flutter package to version v0.4.0-rc1
- Loading branch information
github-actions
committed
Apr 11, 2024
1 parent
61d6a4a
commit cfeec7b
Showing
13 changed files
with
2,021 additions
and
363 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
65 changes: 42 additions & 23 deletions
65
android/src/main/kotlin/com/breez/breez_sdk/BreezSDKPlugin.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 |
---|---|---|
@@ -1,35 +1,54 @@ | ||
package com.breez.breez_sdk | ||
|
||
import androidx.annotation.NonNull | ||
|
||
import breez_sdk.LogEntry | ||
import io.flutter.embedding.engine.plugins.FlutterPlugin | ||
import io.flutter.plugin.common.EventChannel | ||
import io.flutter.plugin.common.MethodCall | ||
import io.flutter.plugin.common.MethodChannel | ||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler | ||
import io.flutter.plugin.common.MethodChannel.Result | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
|
||
/** BreezSDKPlugin */ | ||
class BreezSDKPlugin: FlutterPlugin, MethodCallHandler { | ||
/// The MethodChannel that will the communication between Flutter and native Android | ||
/// | ||
/// This local reference serves to register the plugin with the Flutter Engine and unregister it | ||
/// when the Flutter Engine is detached from the Activity | ||
private lateinit var channel : MethodChannel | ||
|
||
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { | ||
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "breez_sdk") | ||
channel.setMethodCallHandler(this) | ||
} | ||
|
||
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { | ||
if (call.method == "getPlatformVersion") { | ||
result.success("Android ${android.os.Build.VERSION.RELEASE}") | ||
} else { | ||
result.notImplemented() | ||
class BreezSDKPlugin : FlutterPlugin, MethodCallHandler, EventChannel.StreamHandler { | ||
private lateinit var channel: MethodChannel | ||
private var eventChannel: EventChannel? = null | ||
private var eventSink: EventChannel.EventSink? = null | ||
private var scope = CoroutineScope(Dispatchers.Main) | ||
|
||
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { | ||
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "breez_sdk") | ||
channel.setMethodCallHandler(this) | ||
|
||
eventChannel = EventChannel(flutterPluginBinding.binaryMessenger, "breez_sdk_node_logs") | ||
val nodeLogStream = SdkLogInitializer.initializeNodeLogStream() | ||
nodeLogStream.subscribe(scope) { l: LogEntry -> | ||
val data = mapOf("level" to l.level, "line" to l.line) | ||
eventSink?.success(data) | ||
} | ||
eventChannel?.setStreamHandler(this) | ||
} | ||
|
||
override fun onMethodCall(call: MethodCall, result: Result) { | ||
if (call.method == "getPlatformVersion") { | ||
result.success("Android ${android.os.Build.VERSION.RELEASE}") | ||
} else { | ||
result.notImplemented() | ||
} | ||
} | ||
} | ||
|
||
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { | ||
channel.setMethodCallHandler(null) | ||
} | ||
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { | ||
SdkLogInitializer.unsubscribeNodeLogStream(scope) | ||
channel.setMethodCallHandler(null) | ||
} | ||
|
||
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) { | ||
eventSink = events | ||
} | ||
|
||
override fun onCancel(arguments: Any?) { | ||
eventSink = null | ||
eventChannel = null | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
android/src/main/kotlin/com/breez/breez_sdk/SdkLogInitializer.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,27 @@ | ||
package com.breez.breez_sdk | ||
|
||
import breez_sdk.setLogStream | ||
import kotlinx.coroutines.CoroutineScope | ||
|
||
object SdkLogInitializer { | ||
private var nodeLogStream: SdkLogListener? = null | ||
|
||
fun initializeNodeLogStream(): SdkLogListener { | ||
if (nodeLogStream == null) { | ||
try { | ||
nodeLogStream = SdkLogListener() | ||
setLogStream(nodeLogStream!!) | ||
} catch (e: Throwable) { | ||
// Reset nodeLogStream if setting log stream fails | ||
e.printStackTrace() | ||
nodeLogStream = null | ||
throw e | ||
} | ||
} | ||
return nodeLogStream!! | ||
} | ||
|
||
fun unsubscribeNodeLogStream(scope: CoroutineScope) { | ||
nodeLogStream?.unsubscribe(scope) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
android/src/main/kotlin/com/breez/breez_sdk/SdkLogListener.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,31 @@ | ||
package com.breez.breez_sdk | ||
|
||
import breez_sdk.LogEntry | ||
import breez_sdk.LogStream | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.launch | ||
|
||
class SdkLogListener : LogStream { | ||
private val scope = CoroutineScope(SupervisorJob()) | ||
|
||
private val _logEvents = MutableSharedFlow<LogEntry>() | ||
private val logEvents: SharedFlow<LogEntry> = _logEvents.asSharedFlow() | ||
|
||
override fun log(l: LogEntry) { | ||
scope.launch { | ||
_logEvents.emit(l) | ||
} | ||
} | ||
|
||
fun subscribe(scope: CoroutineScope, block: suspend (LogEntry) -> Unit) = | ||
logEvents.onEach(block).launchIn(scope) | ||
|
||
fun unsubscribe(scope: CoroutineScope) = scope.cancel() | ||
} |
Oops, something went wrong.