@@ -11,14 +11,15 @@ import io.flutter.plugin.common.MethodChannel.MethodCallHandler
1111import io.flutter.plugin.common.MethodChannel.Result
1212import io.flutter.plugin.common.PluginRegistry.Registrar
1313
14- import io.chirp.connect.ChirpConnect;
15- import io.chirp.connect.interfaces.ConnectEventListener;
16- import io.chirp.connect.models.ChirpConnectState;
17- import io.chirp.connect.models.ChirpError
14+ import io.chirp.chirpsdk.interfaces.ChirpEventListener
15+ import io.chirp.chirpsdk.models.ChirpSDKState
16+ import io.chirp.chirpsdk.models.ChirpError
1817
1918
2019class ChirpsdkPlugin (val activity : Activity ) : MethodCallHandler {
2120
21+ // TODO: Don't see the reason why we need multiple StreamHandlers for the same ChirpEventListener events.
22+ // I don't think that StreamHandler is limited to one single event type.
2223 val stateStreamHandler = StateStreamHandler ()
2324 val sendingStreamHandler = SendingStreamHandler ()
2425 val sentStreamHandler = SentStreamHandler ()
@@ -27,7 +28,7 @@ class ChirpsdkPlugin(val activity: Activity) : MethodCallHandler {
2728 val errorStreamHandler = ErrorStreamHandler ()
2829
2930 companion object {
30- lateinit var chirpConnect : ChirpConnect
31+ lateinit var chirpSDK : ChirpSDK
3132
3233 @JvmStatic
3334 fun registerWith (registrar : Registrar ) {
@@ -51,22 +52,26 @@ class ChirpsdkPlugin(val activity: Activity) : MethodCallHandler {
5152
5253 override fun onMethodCall (call : MethodCall , result : Result ) {
5354
55+ // TODO: I think a switch->case is a better option here instead of multiple if/else
5456 if (call.method == " init" ) {
55- val arguments = call.arguments as java.util.HashMap <String , String >
57+ val arguments = call.arguments as java.util.HashMap <* , * >
5658 val appKey = arguments[" key" ] as String
5759 val appSecret = arguments[" secret" ] as String
58- chirpConnect = ChirpConnect (activity, appKey, appSecret)
60+ chirpSDK = ChirpSDK (activity, appKey, appSecret)
5961 }
6062 else if (call.method == " version" ) {
61- result.success(chirpConnect .version)
63+ result.success(chirpSDK .version)
6264 }
6365 else if (call.method == " setConfig" ) {
64- var config: String = call.arguments as String
65- val error: ChirpError = chirpConnect .setConfig(config)
66+ val config: String = call.arguments as String
67+ val error: ChirpError = chirpSDK .setConfig(config)
6668 if (error.code > 0 ) {
69+ // TODO: How do you know that errorHandler comes from this specific method call?
70+ // Why not returning here instead? We are awaiting for the operation to complete anyway.
71+ // Same for all other interface method calls.
6772 errorStreamHandler.send(error.code, error.message)
6873 } else {
69- chirpConnect .onSending { payload: ByteArray , channel: Int ->
74+ chirpSDK .onSending { payload: ByteArray , channel: Int ->
7075 /* *
7176 * onSending is called when a send event begins.
7277 * The data argument contains the payload being sent.
@@ -75,7 +80,7 @@ class ChirpsdkPlugin(val activity: Activity) : MethodCallHandler {
7580 sendingStreamHandler.send(payload, channel)
7681 }
7782 }
78- chirpConnect .onSent { payload: ByteArray , channel: Int ->
83+ chirpSDK .onSent { payload: ByteArray , channel: Int ->
7984 /* *
8085 * onSent is called when a send event has completed.
8186 * The payload argument contains the payload data that was sent.
@@ -84,7 +89,7 @@ class ChirpsdkPlugin(val activity: Activity) : MethodCallHandler {
8489 sentStreamHandler.send(payload, channel)
8590 }
8691 }
87- chirpConnect .onReceiving { channel: Int ->
92+ chirpSDK .onReceiving { channel: Int ->
8893 /* *
8994 * onReceiving is called when a receive event begins.
9095 * No data has yet been received.
@@ -93,7 +98,7 @@ class ChirpsdkPlugin(val activity: Activity) : MethodCallHandler {
9398 receivingStreamHandler.send(channel)
9499 }
95100 }
96- chirpConnect .onReceived { payload: ByteArray? , channel: Int ->
101+ chirpSDK .onReceived { payload: ByteArray? , channel: Int ->
97102 /* *
98103 * onReceived is called when a receive event has completed.
99104 * If the payload was decoded successfully, it is passed in payload.
@@ -103,69 +108,67 @@ class ChirpsdkPlugin(val activity: Activity) : MethodCallHandler {
103108 if (payload != null ) {
104109 receivedStreamHandler.send(payload, channel)
105110 } else {
111+ // TODO: Decode failure is not an error and should not processed as an error
106112 errorStreamHandler.send(0 , " Chirp: Decode failed." )
107113 }
108114 }
109115 }
110- chirpConnect .onStateChanged { oldState: ChirpConnectState , newState: ChirpConnectState ->
116+ chirpSDK .onStateChanged { oldState: ChirpSDKState , newState: ChirpSDKState ->
111117 /* *
112118 * onStateChanged is called when the SDK changes state.
113119 */
114120 activity.runOnUiThread {
115121 stateStreamHandler.send(oldState.code, newState.code)
116122 }
117123 }
118- chirpConnect .onSystemVolumeChanged { oldVolume: Float , newVolume: Float ->
124+ chirpSDK .onSystemVolumeChanged { oldVolume: Float , newVolume: Float ->
119125 /* *
120126 * onSystemVolumeChanged is called when the system volume is changed.
121127 */
122128 }
123129 }
124130 }
125131 else if (call.method == " start" ) {
126- val error: ChirpError = chirpConnect .start();
132+ val error: ChirpError = chirpSDK .start()
127133 if (error.code > 0 ) {
128134 errorStreamHandler.send(error.code, error.message)
129135 }
130136 }
131137 else if (call.method == " stop" ) {
132- val error: ChirpError = chirpConnect .stop()
138+ val error: ChirpError = chirpSDK .stop()
133139 if (error.code > 0 ) {
134140 errorStreamHandler.send(error.code, error.message)
135141 }
136142 }
137143 else if (call.method == " send" ) {
138- val payload: ByteArray = call.arguments as ByteArray
139- val error: ChirpError = chirpConnect .send(payload)
144+ val payload = call.arguments as ByteArray
145+ val error: ChirpError = chirpSDK .send(payload)
140146 if (error.code > 0 ) {
141147 errorStreamHandler.send(error.code, error.message)
142148 }
143149 }
144150 else if (call.method == " sendRandom" ) {
145- val payload: ByteArray = chirpConnect .randomPayload(0 )
146- val error: ChirpError = chirpConnect .send(payload)
151+ val payload = chirpSDK .randomPayload(0 )
152+ val error: ChirpError = chirpSDK .send(payload)
147153 if (error.code > 0 ) {
148154 errorStreamHandler.send(error.code, error.message)
149155 }
150156 }
151- // else if (call.method == "isValidPayload") {
152- // val payload: ByteArray = call.arguments as ByteArray
153- // val error: ChirpError = chirpConnect.send(payload)
154- // if (error.code > 0) {
155- // errorStreamHandler.send(error.code, error.message)
156- // }
157- // }
157+ else if (call.method == " isValidPayload" ) {
158+ val payload = call.arguments as ByteArray
159+ result.success(payload.size <= chirpSDK.maxPayloadLength())
160+ }
158161 else if (call.method == " getState" ) {
159- result.success(chirpConnect .getState().code)
162+ result.success(chirpSDK .getState().code)
160163 }
161164 else if (call.method == " maxPayloadLength" ) {
162- result.success(chirpConnect .maxPayloadLength())
165+ result.success(chirpSDK .maxPayloadLength())
163166 }
164167 else if (call.method == " channelCount" ) {
165- result.success(chirpConnect .getChannelCount())
168+ result.success(chirpSDK .getChannelCount())
166169 }
167170 else if (call.method == " transmissionChannel" ) {
168- result.success(chirpConnect .getTransmissionChannel())
171+ result.success(chirpSDK .getTransmissionChannel())
169172 }
170173 else {
171174 result.notImplemented()
0 commit comments