Skip to content
This repository was archived by the owner on Oct 5, 2020. It is now read-only.

Commit 6446cda

Browse files
committed
throw errors when sdk returns an error code
1 parent 15a7128 commit 6446cda

File tree

5 files changed

+35
-40
lines changed

5 files changed

+35
-40
lines changed

android/src/main/kotlin/io/chirp/chirpsdk/ChirpsdkPlugin.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,30 +129,42 @@ class ChirpsdkPlugin(private val activity: Activity) : MethodCallHandler {
129129
if (!isInitialised(call, result)) return
130130
val config: String = call.arguments as String
131131
val error: ChirpError = chirpSDK.setConfig(config)
132-
133-
//TODO: brainstorm if this is the best approach, maybe we should just report error here?
134-
result.success(error.code)
135-
if (error.code == 0) {
136-
setCallbacks()
132+
if (error.code > 0) {
133+
result.error(error.code.toString(), error.message, null)
134+
return
137135
}
136+
result.success(error.code)
137+
setCallbacks()
138138
}
139139

140140
private fun start(call: MethodCall, result: Result) {
141141
if (!isInitialised(call, result)) return
142142
val error: ChirpError = chirpSDK.start()
143+
if (error.code > 0) {
144+
result.error(error.code.toString(), error.message, null)
145+
return
146+
}
143147
result.success(error.code)
144148
}
145149

146150
private fun stop(call: MethodCall, result: Result) {
147151
if (!isInitialised(call, result)) return
148152
val error: ChirpError = chirpSDK.stop()
153+
if (error.code > 0) {
154+
result.error(error.code.toString(), error.message, null)
155+
return
156+
}
149157
result.success(error.code)
150158
}
151159

152160
private fun send(call: MethodCall, result: Result) {
153161
if (!isInitialised(call, result)) return
154162
val payload = call.arguments as ByteArray
155163
val error: ChirpError = chirpSDK.send(payload)
164+
if (error.code > 0) {
165+
result.error(error.code.toString(), error.message, null)
166+
return
167+
}
156168
result.success(error.code)
157169
}
158170

example/android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
buildscript {
2-
ext.kotlin_version = '1.2.71'
2+
ext.kotlin_version = '1.3.10'
33
repositories {
44
google()
55
jcenter()
66
}
77

88
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.2.1'
9+
classpath 'com.android.tools.build:gradle:3.5.1'
1010
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1111
}
1212
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Fri Jun 23 08:50:38 CEST 2017
1+
#Thu Oct 10 16:14:48 BST 2019
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip

example/lib/main.dart

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ class _ChirpAppState extends State<ChirpApp> with WidgetsBindingObserver {
4646
Future<void> _initChirp() async {
4747
try {
4848
//Init ChirpSDK
49-
var errorCode = await ChirpSDK.init(_appKey, _appSecret);
50-
if (errorCode > 0) {
51-
setErrorCode(errorCode);
52-
return;
53-
}
49+
await ChirpSDK.init(_appKey, _appSecret);
5450

5551
//Get and print SDK version
5652
final String chirpVersion = await ChirpSDK.version;
@@ -59,12 +55,7 @@ class _ChirpAppState extends State<ChirpApp> with WidgetsBindingObserver {
5955
});
6056

6157
//Set SDK config
62-
errorCode = await ChirpSDK.setConfig(_appConfig);
63-
if (errorCode > 0) {
64-
setErrorCode(errorCode);
65-
return;
66-
}
67-
58+
await ChirpSDK.setConfig(_appConfig);
6859
_setChirpCallbacks();
6960

7061
} catch (err) {
@@ -87,11 +78,7 @@ class _ChirpAppState extends State<ChirpApp> with WidgetsBindingObserver {
8778

8879
void _startSDK() async {
8980
try {
90-
int errorCode = await ChirpSDK.start();
91-
if (errorCode > 0) {
92-
setErrorCode(errorCode);
93-
return;
94-
}
81+
await ChirpSDK.start();
9582
setState(() {
9683
_startStopBtnText = "STOP";
9784
});
@@ -102,11 +89,7 @@ class _ChirpAppState extends State<ChirpApp> with WidgetsBindingObserver {
10289

10390
void _stopSDK() async {
10491
try {
105-
int errorCode = await ChirpSDK.stop();
106-
if (errorCode > 0) {
107-
setErrorCode(errorCode);
108-
return;
109-
}
92+
await ChirpSDK.stop();
11093
setState(() {
11194
_startStopBtnText = "START";
11295
});

lib/chirpsdk.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class ChirpSDK {
3232
/// up for Chirp at the Developer Hub[1].
3333
///
3434
/// [1]: https://developers.chirp.io
35-
static Future<int> init(String key, String secret) async {
35+
static Future init(String key, String secret) async {
3636
var parameters = {'key': key, 'secret': secret};
37-
return _methods.invokeMethod('init', new Map.from(parameters));
37+
_methods.invokeMethod('init', new Map.from(parameters));
3838
}
3939

4040
/// Get the Chirp SDK version info as a string
@@ -48,24 +48,24 @@ class ChirpSDK {
4848
/// A config string can be retrieved from the Developer Hub[1].
4949
///
5050
/// [1]: https://developers.chirp.io
51-
static Future<int> setConfig(String config) async {
52-
return await _methods.invokeMethod('setConfig', config);
51+
static Future setConfig(String config) async {
52+
await _methods.invokeMethod('setConfig', config);
5353
}
5454

5555
/// Start audio processing
5656
///
5757
/// This should be called after `setConfig`, and when
5858
/// resuming the app from the background. See example.
59-
static Future<int> start() async {
60-
return await _methods.invokeMethod('start');
59+
static Future start() async {
60+
await _methods.invokeMethod('start');
6161
}
6262

6363
/// Stop audio processing
6464
///
6565
/// This should be called when the app enters the background
6666
/// or when shutting down the app for exit. See example.
67-
static Future<int> stop() async {
68-
return await _methods.invokeMethod('stop');
67+
static Future stop() async {
68+
await _methods.invokeMethod('stop');
6969
}
7070

7171
/// Send a payload to the speakers
@@ -79,8 +79,8 @@ class ChirpSDK {
7979
/// data[2] = 3;
8080
/// data[3] = 4;
8181
/// sdk.send(data);
82-
static Future<int> send(Uint8List payload) async {
83-
return await _methods.invokeMethod('send', payload);
82+
static Future send(Uint8List payload) async {
83+
await _methods.invokeMethod('send', payload);
8484
}
8585

8686
/// Returns a randomly generated payload

0 commit comments

Comments
 (0)