Skip to content

Commit

Permalink
Update Breez SDK Flutter package to version v0.4.0-rc1
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Apr 11, 2024
1 parent 61d6a4a commit cfeec7b
Show file tree
Hide file tree
Showing 13 changed files with 2,021 additions and 363 deletions.
14 changes: 9 additions & 5 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
group 'com.breez.breez_sdk'
version '0.3.9'
version '0.4.0-rc1'

buildscript {
ext.kotlin_version = '1.7.10'
ext.kotlin_version = '1.8.20'
repositories {
google()
mavenCentral()
Expand All @@ -24,9 +24,10 @@ rootProject.allprojects {

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlinx-serialization'

android {
compileSdkVersion 31
compileSdkVersion 33

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand All @@ -42,11 +43,14 @@ android {
}

defaultConfig {
minSdkVersion 21
minSdkVersion 24
}
}

dependencies {
api "breez_sdk:bindings-android:$version"
implementation "net.java.dev.jna:jna:5.8.0@aar"
implementation "net.java.dev.jna:jna:5.14.0@aar"
/* JSON serialization */
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3'

}
65 changes: 42 additions & 23 deletions android/src/main/kotlin/com/breez/breez_sdk/BreezSDKPlugin.kt
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 android/src/main/kotlin/com/breez/breez_sdk/SdkLogInitializer.kt
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 android/src/main/kotlin/com/breez/breez_sdk/SdkLogListener.kt
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()
}
Loading

0 comments on commit cfeec7b

Please sign in to comment.